File size: 847 Bytes
0a06561
 
 
 
f3d4327
 
 
 
 
0a06561
f3d4327
0a06561
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from smolagents import Tool
import subprocess

class PythonRunnerTool(Tool):
    name = "python_runner"
    description = "Runs Python code from a file and returns the output."
    inputs = {
        "file_path": {"type": "string", "description": "Path to the Python file to run."}
    }
    output_type = "string"
    
    def forward(self, file_path: str) -> str:
        try:
            result = subprocess.run(["python3", file_path],
                                    capture_output=True, 
                                    text = True, 
                                    timeout=10
                                   )
            if result.stderr:
                return f"Error: {result.stderr}"
            return result.stdout
        except Exception as e:
            return f"Execution failed: {e}"