Spaces:
Sleeping
Sleeping
from langchain_core.tools import tool, ToolException | |
from loguru import logger | |
from rizaio import Riza | |
from tools.load_file import load_file | |
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")) | |