Spaces:
Running on Zero
Running on Zero
Download agent/tools/code_exe.py from AliHaider1/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 530 Bytes
-
https://huggingface.co/spaces/AliHaider1/Final_Assignment_Template/resolve/main/agent/tools/code_exe.py
- Command line
-
hf download hf://spaces/AliHaider1/Final_Assignment_Template/agent/tools/code_exe.py
-
curl -L -o code_exe.py https://huggingface.co/spaces/AliHaider1/Final_Assignment_Template/resolve/main/agent/tools/code_exe.py
530 Bytes
| import io | |
| import contextlib | |
| from langchain_core.tools import tool | |
| def python(code: str) -> str: | |
| """Execute Python code and return anything printed to stdout. Use print() to see results.""" | |
| output = io.StringIO() | |
| try: | |
| with contextlib.redirect_stdout(output): | |
| exec(code, {}) | |
| result = output.getvalue().strip() | |
| return result if result else "Code executed successfully (no output printed)." | |
| except Exception as e: | |
| return f"Python execution error: {e}" |