sabonzo commited on
Commit
76243e2
·
verified ·
1 Parent(s): 120ce98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -7
app.py CHANGED
@@ -257,13 +257,55 @@ class GaiaAgent:
257
  file_path = download_file(file_url, self.temp_dir, task_id)
258
  if file_path:
259
  try:
260
- with open(file_path, 'r') as f: python_code = f.read()
261
- python_tool = PythonREPLTool()
262
- exec_output = python_tool.run(python_code)
263
- response = self.llm.invoke([HumanMessage(content=f"Python output: ```{exec_output}``` What is final numeric output? Only the number.")])
264
- analysis_result = response.content.strip()
265
- except Exception as e: analysis_result = f"ERROR: Python execution failed. {e}"
266
- else: analysis_result = "ERROR: Failed download."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
  # Q14: Calculus Audio
268
  elif "homework.mp3" in q_lower:
269
  file_path = download_file(file_url, self.temp_dir, task_id)
 
257
  file_path = download_file(file_url, self.temp_dir, task_id)
258
  if file_path:
259
  try:
260
+ # Use subprocess to run the script and capture output reliably
261
+ logging.info(f"Executing Python script using subprocess: {file_path}")
262
+ # Ensure using the correct python executable for the environment
263
+ import sys
264
+ process = subprocess.run(
265
+ [sys.executable, str(file_path)], # Use python executable from sys
266
+ capture_output=True, # Capture stdout and stderr
267
+ text=True, # Decode stdout/stderr as text
268
+ timeout=15, # Add a reasonable timeout
269
+ check=False # Don't raise exception on non-zero exit code
270
+ )
271
+
272
+ stdout = process.stdout.strip()
273
+ stderr = process.stderr.strip()
274
+
275
+ if process.returncode != 0:
276
+ # Script failed
277
+ logging.error(f"Python script {file_path} failed (Code: {process.returncode}). Stderr: {stderr}")
278
+ analysis_result = f"ERROR: Python script failed with code {process.returncode}. Error: {stderr}"
279
+ elif not stdout and stderr:
280
+ # Script ran but only produced error messages
281
+ logging.warning(f"Python script {file_path} succeeded but produced only stderr: {stderr}")
282
+ analysis_result = f"ERROR: Python script produced errors: {stderr}"
283
+ elif not stdout:
284
+ # Script ran but produced no output at all
285
+ logging.warning(f"Python script {file_path} produced no standard output.")
286
+ analysis_result = "ERROR: Python script produced no output."
287
+ else:
288
+ # Script succeeded and produced output, assume stdout is the answer
289
+ logging.info(f"Python script {file_path} executed. Output: {stdout}")
290
+ analysis_result = stdout
291
+ # Optional: Validate if it looks like a number, but exact match might require raw output
292
+ try:
293
+ float(analysis_result) # Simple check
294
+ except ValueError:
295
+ logging.warning(f"Python script output '{analysis_result}' may not be purely numeric.")
296
+ # Still return the raw output as it might be the expected format
297
+
298
+ except FileNotFoundError:
299
+ logging.error(f"Python executable '{sys.executable}' not found? Error running script.")
300
+ analysis_result = "ERROR: Python interpreter not found."
301
+ except subprocess.TimeoutExpired:
302
+ logging.error(f"Python script {file_path} timed out after 15 seconds.")
303
+ analysis_result = "ERROR: Python script execution timed out."
304
+ except Exception as e:
305
+ logging.error(f"Error executing Python script {file_path} via subprocess: {e}", exc_info=True)
306
+ analysis_result = f"ERROR: Failed to execute Python script. Details: {str(e)}"
307
+ else:
308
+ analysis_result = "ERROR: Failed to download Python code file."
309
  # Q14: Calculus Audio
310
  elif "homework.mp3" in q_lower:
311
  file_path = download_file(file_url, self.temp_dir, task_id)