Spaces:
Runtime error
Runtime error
Download agent.py from NeuroAbundance/Final_Assignment: direct link, hf CLI and curl.
- Browser
- Download file 2.06 kB
-
https://huggingface.co/spaces/NeuroAbundance/Final_Assignment/resolve/main/agent.py
- Command line
-
hf download hf://spaces/NeuroAbundance/Final_Assignment/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/NeuroAbundance/Final_Assignment/resolve/main/agent.py
2.06 kB
| # --- Imports --- | |
| import os | |
| import random | |
| import gradio as gr | |
| from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, WikipediaSearchTool | |
| from tools import DownloadTaskAttachmentTool, VisitWebpageTool | |
| class MyAgent: | |
| def __init__(self): | |
| self.evaluation_agent = CodeAgent( | |
| model=LiteLLMModel(model_id="openrouter/qwen/qwen3-235b-a22b:free", api_key=os.getenv("OPENROUTER_KEY")), | |
| tools=[DuckDuckGoSearchTool(), DownloadTaskAttachmentTool(), VisitWebpageTool(), WikipediaSearchTool()], | |
| add_base_tools=True, | |
| additional_authorized_imports=['pandas', 'numpy', 'csv', 'subprocess', 'exec'] | |
| ) | |
| print("Evaluation agent instance created.") | |
| def __call__(self, query: str) -> str: | |
| print(f"Processing query (initial segment): {query[:50]}...") | |
| response = self.evaluation_agent.run(query) | |
| print(f"Generated response: {response}") | |
| return response | |
| def download_file(self, task_id: str) -> str: | |
| """ | |
| Retrieves task-specific file from remote storage. | |
| Returns local storage path for the downloaded file. | |
| """ | |
| download_url = f"{DEFAULT_API_URL}/files/{task_id}" | |
| save_path = f"downloads/{task_id}.file" | |
| print(f"Downloading file for task ID {task_id} from {download_url}...") | |
| try: | |
| response = requests.get(download_url, stream=True, timeout=15) | |
| response.raise_for_status() | |
| os.makedirs("downloads", exist_ok=True) | |
| with open(save_path, "wb") as file: | |
| for chunk in response.iter_content(chunk_size=8192): | |
| file.write(chunk) | |
| print(f"File downloaded successfully: {save_path}") | |
| return save_path | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error downloading file for task {task_id}: {e}") | |
| raise | |
| def launch_ui(self): | |
| GradioUI(self.agent).launch() | |
| # if __name__ == "__main__": | |
| # my_agent = MyAgent() | |
| # my_agent.launch_ui() | |