Spaces:
Runtime error
Runtime error
Download tools/PythonCalcTool.py from Sborole/Sborole-Final-Assignment: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/Sborole/Sborole-Final-Assignment/resolve/12b457dcdc6927a2e920342df7d8f6d373b5cadd/tools/PythonCalcTool.py
- Command line
-
hf download hf://spaces/Sborole/Sborole-Final-Assignment@12b457dcdc6927a2e920342df7d8f6d373b5cadd/tools/PythonCalcTool.py
-
curl -L -o PythonCalcTool.py https://huggingface.co/spaces/Sborole/Sborole-Final-Assignment/resolve/12b457dcdc6927a2e920342df7d8f6d373b5cadd/tools/PythonCalcTool.py
1.17 kB
| from smolagents import Tool | |
| class PythonCalcTool(Tool): | |
| name = "python_calc" | |
| description = ( | |
| "Can use a normal calculator for probability questions. If needed Execute Python code to compute numeric answers for calculation problems. Use tool to solve math, physics, or distance/time problems. This tool is used for **all mathematical calculations, complex logic processing, and numerical comparisons (e.g., comparing probabilities or finding maximum values)." | |
| ) | |
| inputs = { | |
| "code": {"type": "string", "description": "Python code that sets a variable 'result' with the answer."} | |
| } | |
| output_type = "string" | |
| def forward(self, code: str) -> str: | |
| """ | |
| Executes the code safely and returns the result as a string. | |
| Code must set a variable 'result' which is returned. | |
| """ | |
| try: | |
| local_vars = {} | |
| exec(code, {}, local_vars) | |
| # The agent should always set 'result' | |
| answer = local_vars.get("result", None) | |
| if answer is None: | |
| return "NA" | |
| return str(answer) | |
| except Exception as e: | |
| return "NA" | |