Download hf_agent_course_final_assignment/python_agent.py from agents-course/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/agents-course/Final_Assignment_Template/resolve/refs%2Fpr%2F592/hf_agent_course_final_assignment/python_agent.py
- Command line
-
hf download hf://spaces/agents-course/Final_Assignment_Template@refs/pr/592/hf_agent_course_final_assignment/python_agent.py
-
curl -L -o python_agent.py https://huggingface.co/spaces/agents-course/Final_Assignment_Template/resolve/refs%2Fpr%2F592/hf_agent_course_final_assignment/python_agent.py
1.49 kB
| from __future__ import annotations | |
| import os | |
| import base64 | |
| import mimetypes | |
| import argparse | |
| from pathlib import Path | |
| from typing import List, Dict, Any, Optional | |
| from dotenv import load_dotenv | |
| from huggingface_hub import InferenceClient | |
| import io | |
| from contextlib import redirect_stdout | |
| load_dotenv() | |
| class PythonAgent: | |
| def __init__(self): | |
| # print("Python code execution agent initialized.") | |
| pass | |
| def __call__(self, file_path: str) -> str: | |
| return self.execute_python_file(file_path) | |
| def execute_python_file(self, file_path: str) -> str: | |
| import io | |
| from contextlib import redirect_stdout | |
| with open(file_path, "r") as f_in: | |
| code_to_exec = f_in.read() | |
| # important: real module-like globals with __name__="__main__" | |
| sandbox_globals = { | |
| "__name__": "__main__", | |
| "__file__": file_path, | |
| } | |
| buf = io.StringIO() | |
| with redirect_stdout(buf): | |
| try: | |
| exec(code_to_exec, sandbox_globals) | |
| except Exception as e: | |
| print(f"An error occurred: {e}") | |
| raw_response = buf.getvalue() | |
| # the response is the last line | |
| response = raw_response.strip().splitlines()[-1] | |
| return response | |
| # path_python_file = r"downloaded\f918266a-b3e0-4914-865d-4faa564f1aef.py" | |
| # response = execute_python_file(path_python_file) | |
| # print(response) | |