mikethor007 commited on
Commit
e82194b
·
verified ·
1 Parent(s): 0511f38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -9,6 +9,7 @@ import base64
9
  import json
10
  import sys
11
  import contextlib
 
12
  #from tavily import TavilyClient
13
  from io import StringIO
14
  from langchain_tavily import TavilySearch
@@ -20,6 +21,7 @@ from langchain_core.tools import tool
20
  from langchain_core.messages import HumanMessage, SystemMessage, BaseMessage
21
  from langchain.chat_models import init_chat_model
22
  from typing import Annotated,Sequence, TypedDict, Literal, Dict
 
23
 
24
  # (Keep Constants as is)
25
  # --- Constants ---
@@ -86,15 +88,6 @@ web_search = TavilySearch(
86
  topic="general",
87
  )
88
 
89
- @contextlib.contextmanager
90
- def stdoutIO(stdout=None):
91
- old = sys.stdout
92
- if stdout is None:
93
- stdout = StringIO()
94
- sys.stdout = stdout
95
- yield stdout
96
- sys.stdout = old
97
-
98
  def python_code_runner_tool(task_id:str) -> str:
99
  """
100
  Download and run python code, capturing the output.
@@ -110,6 +103,26 @@ def python_code_runner_tool(task_id:str) -> str:
110
 
111
  print(f"python_code: {python_code}")
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  #with stdoutIO() as s:
114
  # exec(python_code)
115
 
 
9
  import json
10
  import sys
11
  import contextlib
12
+ import traceback
13
  #from tavily import TavilyClient
14
  from io import StringIO
15
  from langchain_tavily import TavilySearch
 
21
  from langchain_core.messages import HumanMessage, SystemMessage, BaseMessage
22
  from langchain.chat_models import init_chat_model
23
  from typing import Annotated,Sequence, TypedDict, Literal, Dict
24
+ from contextlib import redirect_stdout, redirect_stderr
25
 
26
  # (Keep Constants as is)
27
  # --- Constants ---
 
88
  topic="general",
89
  )
90
 
 
 
 
 
 
 
 
 
 
91
  def python_code_runner_tool(task_id:str) -> str:
92
  """
93
  Download and run python code, capturing the output.
 
103
 
104
  print(f"python_code: {python_code}")
105
 
106
+ saida = io.StringIO()
107
+ erros = io.StringIO()
108
+
109
+ try:
110
+ # Captura stdout e stderr usando contexto
111
+ with redirect_stdout(saida), redirect_stderr(erros):
112
+ exec(python_code, {'__name__': '__main__'})
113
+
114
+ # Pega o conteúdo das saídas
115
+ saida_valor = saida.getvalue()
116
+ erro_valor = erros.getvalue()
117
+
118
+ if erro_valor:
119
+ return f"[ERRO DE EXECUÇÃO]:\n{erro_valor}"
120
+
121
+ return saida_valor if saida_valor.strip() else "[SEM SAÍDA]"
122
+
123
+ except Exception:
124
+ return f"[EXCEÇÃO DURANTE EXECUÇÃO]:\n{traceback.format_exc()}"
125
+
126
  #with stdoutIO() as s:
127
  # exec(python_code)
128