CultriX commited on
Commit
2f7f9b6
·
verified ·
1 Parent(s): d8683fa

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +57 -10
run.py CHANGED
@@ -2,6 +2,7 @@ import argparse
2
  import os
3
  import threading
4
  import sys
 
5
  from io import StringIO
6
  from contextlib import redirect_stdout, redirect_stderr
7
 
@@ -36,6 +37,23 @@ AUTHORIZED_IMPORTS = [
36
  append_answer_lock = threading.Lock()
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  class StreamingCapture:
40
  """Captures stdout/stderr and yields content in real-time"""
41
  def __init__(self):
@@ -161,26 +179,55 @@ Additionally, if after some searching you find out that you need more informatio
161
  def run_agent_with_streaming(agent, question, stream_callback=None):
162
  """Run agent and stream output in real-time"""
163
 
164
- # Capture stdout and stderr
165
- stdout_capture = StreamingCapture()
166
- stderr_capture = StreamingCapture()
167
-
168
  if stream_callback:
169
- stdout_capture.add_callback(stream_callback)
170
- stderr_capture.add_callback(stream_callback)
 
 
 
 
 
 
 
171
 
172
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
174
- print(f"[STARTING] Running agent with question: {question}")
 
 
175
  answer = agent.run(question)
176
- print(f"[COMPLETED] Final answer: {answer}")
 
 
 
177
  return answer
 
178
  except Exception as e:
179
- error_msg = f"[ERROR] Exception occurred: {str(e)}"
180
- print(error_msg)
181
  if stream_callback:
182
  stream_callback(error_msg)
183
  raise
 
 
 
 
 
184
 
185
 
186
  def create_gradio_interface():
 
2
  import os
3
  import threading
4
  import sys
5
+ import logging
6
  from io import StringIO
7
  from contextlib import redirect_stdout, redirect_stderr
8
 
 
37
  append_answer_lock = threading.Lock()
38
 
39
 
40
+ class StreamingHandler(logging.Handler):
41
+ """Custom logging handler that captures agent logs"""
42
+ def __init__(self):
43
+ super().__init__()
44
+ self.callbacks = []
45
+ self.buffer = []
46
+
47
+ def add_callback(self, callback):
48
+ self.callbacks.append(callback)
49
+
50
+ def emit(self, record):
51
+ msg = self.format(record)
52
+ self.buffer.append(msg + '\n')
53
+ for callback in self.callbacks:
54
+ callback(msg + '\n')
55
+
56
+
57
  class StreamingCapture:
58
  """Captures stdout/stderr and yields content in real-time"""
59
  def __init__(self):
 
179
  def run_agent_with_streaming(agent, question, stream_callback=None):
180
  """Run agent and stream output in real-time"""
181
 
182
+ # Set up logging capture
183
+ log_handler = StreamingHandler()
 
 
184
  if stream_callback:
185
+ log_handler.add_callback(stream_callback)
186
+
187
+ # Add handler to root logger and smolagents loggers
188
+ root_logger = logging.getLogger()
189
+ smolagents_logger = logging.getLogger('smolagents')
190
+
191
+ # Store original handlers
192
+ original_handlers = root_logger.handlers[:]
193
+ original_level = root_logger.level
194
 
195
  try:
196
+ # Configure logging to capture everything
197
+ root_logger.setLevel(logging.DEBUG)
198
+ root_logger.addHandler(log_handler)
199
+ smolagents_logger.setLevel(logging.DEBUG)
200
+ smolagents_logger.addHandler(log_handler)
201
+
202
+ # Also capture stdout/stderr
203
+ stdout_capture = StreamingCapture()
204
+ stderr_capture = StreamingCapture()
205
+
206
+ if stream_callback:
207
+ stdout_capture.add_callback(stream_callback)
208
+ stderr_capture.add_callback(stream_callback)
209
+
210
  with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
211
+ if stream_callback:
212
+ stream_callback(f"[STARTING] Running agent with question: {question}\n")
213
+
214
  answer = agent.run(question)
215
+
216
+ if stream_callback:
217
+ stream_callback(f"[COMPLETED] Final answer: {answer}\n")
218
+
219
  return answer
220
+
221
  except Exception as e:
222
+ error_msg = f"[ERROR] Exception occurred: {str(e)}\n"
 
223
  if stream_callback:
224
  stream_callback(error_msg)
225
  raise
226
+ finally:
227
+ # Restore original logging configuration
228
+ root_logger.handlers = original_handlers
229
+ root_logger.setLevel(original_level)
230
+ smolagents_logger.removeHandler(log_handler)
231
 
232
 
233
  def create_gradio_interface():