JabrilJacobs commited on
Commit
8abdc5b
·
verified ·
1 Parent(s): 7c80c82

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +25 -1
tools.py CHANGED
@@ -9,6 +9,8 @@ from langchain_core.messages import HumanMessage
9
  from langchain_openai import ChatOpenAI
10
  import requests
11
  import os
 
 
12
 
13
  def download_file(task_id: str, file_name: str) -> str:
14
  """Downloads a file associated with a task_id and returns the local file path"""
@@ -145,4 +147,26 @@ def read_excel_file(file_path: str) -> Dict[str, Any]:
145
  return result
146
 
147
  except Exception as e:
148
- return {"error": f"Failed to read Excel file: {str(e)}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  from langchain_openai import ChatOpenAI
10
  import requests
11
  import os
12
+ import subprocess
13
+ import tempfile
14
 
15
  def download_file(task_id: str, file_name: str) -> str:
16
  """Downloads a file associated with a task_id and returns the local file path"""
 
147
  return result
148
 
149
  except Exception as e:
150
+ return {"error": f"Failed to read Excel file: {str(e)}"}
151
+
152
+ def execute_python_code(file_path: str, timeout: int = 30) -> str:
153
+ """Execute Python code safely with subprocess"""
154
+ try:
155
+ # Run in isolated subprocess with timeout
156
+ result = subprocess.run(
157
+ ['python', file_path],
158
+ capture_output=True,
159
+ text=True,
160
+ timeout=timeout,
161
+ cwd=tempfile.gettempdir() # Run in temp directory
162
+ )
163
+
164
+ if result.returncode == 0:
165
+ return result.stdout.strip()
166
+ else:
167
+ return f"Error: {result.stderr}"
168
+
169
+ except subprocess.TimeoutExpired:
170
+ return "Error: Code execution timed out"
171
+ except Exception as e:
172
+ return f"Error executing code: {str(e)}"