sampsong commited on
Commit
d9e880e
·
1 Parent(s): 1a2ad31

add comment for file url

Browse files
Files changed (2) hide show
  1. Tools/tools.py +52 -1
  2. app.py +3 -1
Tools/tools.py CHANGED
@@ -17,6 +17,10 @@ from io import BytesIO
17
  import sys
18
  import io
19
  import traceback
 
 
 
 
20
 
21
  DEFAULT_File_URL = "https://agents-course-unit4-scoring.hf.space/files/"
22
 
@@ -249,7 +253,7 @@ def read_excel_from_url(taskID: str) -> str:
249
  except Exception as e:
250
  return f"Error reading Excel file from URL: {str(e)}"
251
 
252
- @tool
253
  def run_python_code_from_url(taskID: str) -> str:
254
  """
255
  Downloads Python code from a URL, executes it, and returns the output or errors.
@@ -282,3 +286,50 @@ def run_python_code_from_url(taskID: str) -> str:
282
  except Exception:
283
  sys.stdout = old_stdout
284
  return "Error executing code:\n" + traceback.format_exc()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  import sys
18
  import io
19
  import traceback
20
+ import requests
21
+ import subprocess
22
+ import tempfile
23
+ import traceback
24
 
25
  DEFAULT_File_URL = "https://agents-course-unit4-scoring.hf.space/files/"
26
 
 
253
  except Exception as e:
254
  return f"Error reading Excel file from URL: {str(e)}"
255
 
256
+ '''@tool
257
  def run_python_code_from_url(taskID: str) -> str:
258
  """
259
  Downloads Python code from a URL, executes it, and returns the output or errors.
 
286
  except Exception:
287
  sys.stdout = old_stdout
288
  return "Error executing code:\n" + traceback.format_exc()
289
+ '''
290
+
291
+ @tool
292
+ def run_python_code_from_url(taskID: str) -> str:
293
+ """
294
+ Downloads Python code from a URL, runs it in a separate Python process,
295
+ and returns stdout/stderr or errors.
296
+ """
297
+ try:
298
+ formattedURL = DEFAULT_File_URL + taskID
299
+ print(f"pythonurl : {formattedURL}")
300
+
301
+ # Download the Python code
302
+ response = requests.get(formattedURL, timeout=10)
303
+ response.raise_for_status()
304
+ code = response.text
305
+
306
+ # Write code to a temporary file
307
+ with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as tmp:
308
+ tmp.write(code)
309
+ tmp_path = tmp.name
310
+
311
+ # Run the code in a separate process with timeout
312
+ result = subprocess.run(
313
+ ["python", tmp_path],
314
+ capture_output=True,
315
+ text=True,
316
+ timeout=5 # seconds
317
+ )
318
+
319
+ # Combine stdout and stderr
320
+ output = result.stdout.strip()
321
+ error = result.stderr.strip()
322
+
323
+ if output and error:
324
+ return f"Output:\n{output}\n\nErrors:\n{error}"
325
+ elif output:
326
+ return output
327
+ elif error:
328
+ return f"Error:\n{error}"
329
+ else:
330
+ return "Code executed successfully with no output."
331
+
332
+ except subprocess.TimeoutExpired:
333
+ return "Error: Code execution timed out."
334
+ except Exception:
335
+ return "Error executing code:\n" + traceback.format_exc()
app.py CHANGED
@@ -62,7 +62,9 @@ class BasicAgent:
62
  messages = self.graph.invoke({"messages": formattedMessage})
63
  else:
64
  print("active langfuse")
65
- formattedMessage = [{"role": "system", "content": systemPrompt,"additional_kwargs": {"taskID": taskID}}] + \
 
 
66
  [{"role":"user","content":msg.content,"additional_kwargs": {"taskID": taskID}} for msg in messages]
67
  #formattedMessage.append({"role": "system", "content": systemPrompt})
68
  #formattedMessage.append(SystemMessage(content="systemPrompt"))
 
62
  messages = self.graph.invoke({"messages": formattedMessage})
63
  else:
64
  print("active langfuse")
65
+ additionalDetails = " The taskID is : {taskID}"
66
+ print(f"additionalDetials: {additionalDetails}")
67
+ formattedMessage = [{"role": "system", "content": systemPrompt + "\n" + additionalDetails,"additional_kwargs": {"taskID": taskID}}] + \
68
  [{"role":"user","content":msg.content,"additional_kwargs": {"taskID": taskID}} for msg in messages]
69
  #formattedMessage.append({"role": "system", "content": systemPrompt})
70
  #formattedMessage.append(SystemMessage(content="systemPrompt"))