Spaces:
Sleeping
Sleeping
Ihor Kozar commited on
Commit ·
af545f6
1
Parent(s): 76c1973
fix: run_python_file
Browse files- agent_tools.py +32 -15
- test.py +1 -1
agent_tools.py
CHANGED
|
@@ -301,38 +301,55 @@ Execute Python code from a file identified by task_id and file_name.
|
|
| 301 |
Returns the numeric result if defined, otherwise stdout.
|
| 302 |
""")
|
| 303 |
def run_python_file(task_id: str, file_name: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
try:
|
| 305 |
-
# Завантажуємо файл
|
| 306 |
file_url = f"{DEFAULT_API_URL}/files/{task_id}"
|
| 307 |
r = requests.get(file_url, timeout=15, allow_redirects=True)
|
| 308 |
if r.status_code != 200:
|
| 309 |
return f"❌ Failed to download file: {r.status_code}"
|
| 310 |
|
| 311 |
-
|
| 312 |
-
with open(file_name, "wb") as f:
|
| 313 |
f.write(r.content)
|
| 314 |
|
| 315 |
-
|
| 316 |
-
with open(file_name, "r") as f:
|
| 317 |
code = f.read()
|
| 318 |
|
| 319 |
-
# Виконуємо код і зберігаємо stdout
|
| 320 |
-
buffer = io.StringIO()
|
| 321 |
sys.stdout = buffer
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
|
|
|
| 329 |
else:
|
| 330 |
output = buffer.getvalue().strip()
|
| 331 |
return output or "No output produced."
|
| 332 |
|
| 333 |
except Exception as e:
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
return f"❌ Error executing Python file: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
|
| 338 |
TOOLS: List[Callable[..., Any]] = [
|
|
|
|
| 301 |
Returns the numeric result if defined, otherwise stdout.
|
| 302 |
""")
|
| 303 |
def run_python_file(task_id: str, file_name: str) -> str:
|
| 304 |
+
file_path = file_name
|
| 305 |
+
buffer = io.StringIO()
|
| 306 |
+
old_stdout = sys.stdout
|
| 307 |
+
ns = {"__builtins__": __builtins__, "__name__": "__main__"}
|
| 308 |
try:
|
|
|
|
| 309 |
file_url = f"{DEFAULT_API_URL}/files/{task_id}"
|
| 310 |
r = requests.get(file_url, timeout=15, allow_redirects=True)
|
| 311 |
if r.status_code != 200:
|
| 312 |
return f"❌ Failed to download file: {r.status_code}"
|
| 313 |
|
| 314 |
+
with open(file_path, "wb") as f:
|
|
|
|
| 315 |
f.write(r.content)
|
| 316 |
|
| 317 |
+
with open(file_path, "r", encoding="utf-8", errors="replace") as f:
|
|
|
|
| 318 |
code = f.read()
|
| 319 |
|
|
|
|
|
|
|
| 320 |
sys.stdout = buffer
|
| 321 |
+
try:
|
| 322 |
+
compiled = compile(code, file_path, "exec")
|
| 323 |
+
exec(compiled, ns, ns)
|
| 324 |
+
finally:
|
| 325 |
+
sys.stdout = old_stdout
|
| 326 |
+
|
| 327 |
+
if "result" in ns:
|
| 328 |
+
return str(ns["result"])
|
| 329 |
else:
|
| 330 |
output = buffer.getvalue().strip()
|
| 331 |
return output or "No output produced."
|
| 332 |
|
| 333 |
except Exception as e:
|
| 334 |
+
# Prefer returning a computed result or any partial stdout if available
|
| 335 |
+
try:
|
| 336 |
+
sys.stdout = old_stdout
|
| 337 |
+
except Exception:
|
| 338 |
+
pass
|
| 339 |
+
if "result" in ns:
|
| 340 |
+
return str(ns["result"])
|
| 341 |
+
output = buffer.getvalue().strip()
|
| 342 |
+
if output:
|
| 343 |
+
return output
|
| 344 |
return f"❌ Error executing Python file: {e}"
|
| 345 |
+
finally:
|
| 346 |
+
# Ensure the downloaded code file is removed after execution
|
| 347 |
+
try:
|
| 348 |
+
if os.path.exists(file_path):
|
| 349 |
+
os.remove(file_path)
|
| 350 |
+
except Exception:
|
| 351 |
+
pass
|
| 352 |
+
|
| 353 |
|
| 354 |
|
| 355 |
TOOLS: List[Callable[..., Any]] = [
|
test.py
CHANGED
|
@@ -107,7 +107,7 @@ questions = [
|
|
| 107 |
# Test
|
| 108 |
if __name__ == "__main__":
|
| 109 |
agent = CUSTOM_AGENT()
|
| 110 |
-
q = questions[
|
| 111 |
answer = agent.run(q)
|
| 112 |
print("Answer:", answer)
|
| 113 |
# audio 9 13
|
|
|
|
| 107 |
# Test
|
| 108 |
if __name__ == "__main__":
|
| 109 |
agent = CUSTOM_AGENT()
|
| 110 |
+
q = questions[11]
|
| 111 |
answer = agent.run(q)
|
| 112 |
print("Answer:", answer)
|
| 113 |
# audio 9 13
|