Spaces:
Running
Running
File size: 1,281 Bytes
64a08a8 |
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from langchain_core.tools import tool, ToolException
from loguru import logger
from rizaio import Riza
from tools.load_file import load_file
@tool("execute_python_code_from_file_tool", parse_docstring=True)
def execute_python_code_from_file(path_or_url: str) -> str:
"""
Executes a given Python code snippet.
Args:
path_or_url (str): A string containing the Python code to execute.
Returns:
str: The standard output resulting from the execution of the code.
"""
logger.info(f"execute_python_code_from_file with param: {path_or_url}")
code = load_file(path_or_url).read().decode("utf-8")
logger.debug(f"Python code:\n{code}")
client = Riza()
output = client.command.exec(
language="python",
code=code,
)
if output.exit_code > 0:
raise ToolException(
f"Riza code execution returned a non-zero exit code. "
f"The output captured from stderr was:\n{output.stderr}"
)
return output.stdout
if __name__ == "__main__":
print(execute_python_code_from_file.invoke("../data/f918266a-b3e0-4914-865d-4faa564f1aef.py"))
print(execute_python_code_from_file.invoke("https://agents-course-unit4-scoring.hf.space/files/f918266a-b3e0-4914-865d-4faa564f1aef"))
|