| from smolagents import Tool | |
| import requests | |
| from io import BytesIO | |
| from typing import Any | |
| #AUTHORIZED_TYPES = [ | |
| # "string", | |
| # "boolean", | |
| # "integer", | |
| # "number", | |
| # "image", | |
| # "audio", | |
| # "array", | |
| # "object", | |
| # "any", | |
| # "null", | |
| #] | |
| class PythonLoadTool(Tool): | |
| name = "_my_tool_python_load" | |
| description = """ | |
| Load file with Python source code for the provided task id | |
| To invoke the tool use code as below | |
| <code> | |
| contents = _my_tool_python_load(task_id="dummy") | |
| </code> | |
| """ | |
| inputs = { | |
| "task_id": { | |
| "type": "string", | |
| "description": "task id to load Python source code file", | |
| } | |
| } | |
| output_type = "string" | |
| api_url = "https://agents-course-unit4-scoring.hf.space" | |
| def forward(self, task_id: str) -> Any: | |
| headers = { | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" | |
| } | |
| url = f"{self.api_url}/files/{task_id}" | |
| response = requests.get(url, headers=headers) | |
| return response.content.decode('utf-8') | |