strickvl commited on
Commit
f025d0e
·
unverified ·
1 Parent(s): bc7ee1f

Refactor Gradio_UI.py: Improve code formatting and logging

Browse files

- Added logging configuration with logger initialization
- Improved code formatting with black-style line breaks and indentation
- Enhanced error handling in stream_to_gradio function
- Cleaned up code blocks and regex handling in pull_messages_from_step function

Files changed (1) hide show
  1. Gradio_UI.py +65 -24
Gradio_UI.py CHANGED
@@ -19,10 +19,17 @@ import re
19
  import shutil
20
  from typing import Optional
21
 
22
- from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
 
 
 
 
 
23
  from smolagents.agents import ActionStep, MultiStepAgent
24
  from smolagents.memory import MemoryStep
25
  from smolagents.utils import _is_package_available
 
 
26
 
27
 
28
  def pull_messages_from_step(
@@ -33,7 +40,9 @@ def pull_messages_from_step(
33
 
34
  if isinstance(step_log, ActionStep):
35
  # Output the step number
36
- step_number = f"Step {step_log.step_number}" if step_log.step_number is not None else ""
 
 
37
  yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
38
 
39
  # First yield the thought/reasoning from the LLM
@@ -41,9 +50,15 @@ def pull_messages_from_step(
41
  # Clean up the LLM output
42
  model_output = step_log.model_output.strip()
43
  # Remove any trailing <end_code> and extra backticks, handling multiple possible formats
44
- model_output = re.sub(r"```\s*<end_code>", "```", model_output) # handles ```<end_code>
45
- model_output = re.sub(r"<end_code>\s*```", "```", model_output) # handles <end_code>```
46
- model_output = re.sub(r"```\s*\n\s*<end_code>", "```", model_output) # handles ```\n<end_code>
 
 
 
 
 
 
47
  model_output = model_output.strip()
48
  yield gr.ChatMessage(role="assistant", content=model_output)
49
 
@@ -63,8 +78,12 @@ def pull_messages_from_step(
63
 
64
  if used_code:
65
  # Clean up the content by removing any end code tags
66
- content = re.sub(r"```.*?\n", "", content) # Remove existing code blocks
67
- content = re.sub(r"\s*<end_code>\s*", "", content) # Remove end_code tags
 
 
 
 
68
  content = content.strip()
69
  if not content.startswith("```python"):
70
  content = f"```python\n{content}\n```"
@@ -90,7 +109,11 @@ def pull_messages_from_step(
90
  yield gr.ChatMessage(
91
  role="assistant",
92
  content=f"{log_content}",
93
- metadata={"title": "📝 Execution Logs", "parent_id": parent_id, "status": "done"},
 
 
 
 
94
  )
95
 
96
  # Nesting any errors under the tool call
@@ -98,7 +121,11 @@ def pull_messages_from_step(
98
  yield gr.ChatMessage(
99
  role="assistant",
100
  content=str(step_log.error),
101
- metadata={"title": "💥 Error", "parent_id": parent_id, "status": "done"},
 
 
 
 
102
  )
103
 
104
  # Update parent message metadata to done status without yielding a new message
@@ -106,17 +133,25 @@ def pull_messages_from_step(
106
 
107
  # Handle standalone errors but not from tool calls
108
  elif hasattr(step_log, "error") and step_log.error is not None:
109
- yield gr.ChatMessage(role="assistant", content=str(step_log.error), metadata={"title": "💥 Error"})
 
 
 
 
110
 
111
  # Calculate duration and token information
112
  step_footnote = f"{step_number}"
113
- if hasattr(step_log, "input_token_count") and hasattr(step_log, "output_token_count"):
114
- token_str = (
115
- f" | Input-tokens:{step_log.input_token_count:,} | Output-tokens:{step_log.output_token_count:,}"
116
- )
117
  step_footnote += token_str
118
  if hasattr(step_log, "duration"):
119
- step_duration = f" | Duration: {round(float(step_log.duration), 2)}" if step_log.duration else None
 
 
 
 
120
  step_footnote += step_duration
121
  step_footnote = f"""<span style="color: #bbbbc2; font-size: 12px;">{step_footnote}</span> """
122
  yield gr.ChatMessage(role="assistant", content=f"{step_footnote}")
@@ -127,20 +162,20 @@ def stream_to_gradio(agent, task: str, reset_agent_memory: bool = True):
127
  """Stream agent responses to Gradio interface with better error handling"""
128
  total_input_tokens = 0
129
  total_output_tokens = 0
130
-
131
  try:
132
  for msg in agent.chat(task, reset_memory=reset_agent_memory):
133
  # Safely handle token counting
134
- if hasattr(agent.model, 'last_input_token_count'):
135
  input_tokens = agent.model.last_input_token_count or 0
136
  total_input_tokens += input_tokens
137
-
138
- if hasattr(agent.model, 'last_output_token_count'):
139
  output_tokens = agent.model.last_output_token_count or 0
140
  total_output_tokens += output_tokens
141
-
142
  yield msg
143
-
144
  except Exception as e:
145
  error_msg = f"Error during chat: {str(e)}"
146
  logger.error(error_msg)
@@ -214,10 +249,14 @@ class GradioUI:
214
  sanitized_name = "".join(sanitized_name)
215
 
216
  # Save the uploaded file to the specified folder
217
- file_path = os.path.join(self.file_upload_folder, os.path.basename(sanitized_name))
 
 
218
  shutil.copy(file.name, file_path)
219
 
220
- return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
 
 
221
 
222
  def log_user_message(self, text_input, file_uploads_log):
223
  return (
@@ -249,7 +288,9 @@ class GradioUI:
249
  # If an upload folder is provided, enable the upload feature
250
  if self.file_upload_folder is not None:
251
  upload_file = gr.File(label="Upload a file")
252
- upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
 
 
253
  upload_file.change(
254
  self.upload_file,
255
  [upload_file, file_uploads_log],
 
19
  import shutil
20
  from typing import Optional
21
 
22
+ from smolagents.agent_types import (
23
+ AgentAudio,
24
+ AgentImage,
25
+ AgentText,
26
+ handle_agent_output_types,
27
+ )
28
  from smolagents.agents import ActionStep, MultiStepAgent
29
  from smolagents.memory import MemoryStep
30
  from smolagents.utils import _is_package_available
31
+ import logging
32
+ logger = logging.getLogger(__name__)
33
 
34
 
35
  def pull_messages_from_step(
 
40
 
41
  if isinstance(step_log, ActionStep):
42
  # Output the step number
43
+ step_number = (
44
+ f"Step {step_log.step_number}" if step_log.step_number is not None else ""
45
+ )
46
  yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
47
 
48
  # First yield the thought/reasoning from the LLM
 
50
  # Clean up the LLM output
51
  model_output = step_log.model_output.strip()
52
  # Remove any trailing <end_code> and extra backticks, handling multiple possible formats
53
+ model_output = re.sub(
54
+ r"```\s*<end_code>", "```", model_output
55
+ ) # handles ```<end_code>
56
+ model_output = re.sub(
57
+ r"<end_code>\s*```", "```", model_output
58
+ ) # handles <end_code>```
59
+ model_output = re.sub(
60
+ r"```\s*\n\s*<end_code>", "```", model_output
61
+ ) # handles ```\n<end_code>
62
  model_output = model_output.strip()
63
  yield gr.ChatMessage(role="assistant", content=model_output)
64
 
 
78
 
79
  if used_code:
80
  # Clean up the content by removing any end code tags
81
+ content = re.sub(
82
+ r"```.*?\n", "", content
83
+ ) # Remove existing code blocks
84
+ content = re.sub(
85
+ r"\s*<end_code>\s*", "", content
86
+ ) # Remove end_code tags
87
  content = content.strip()
88
  if not content.startswith("```python"):
89
  content = f"```python\n{content}\n```"
 
109
  yield gr.ChatMessage(
110
  role="assistant",
111
  content=f"{log_content}",
112
+ metadata={
113
+ "title": "📝 Execution Logs",
114
+ "parent_id": parent_id,
115
+ "status": "done",
116
+ },
117
  )
118
 
119
  # Nesting any errors under the tool call
 
121
  yield gr.ChatMessage(
122
  role="assistant",
123
  content=str(step_log.error),
124
+ metadata={
125
+ "title": "💥 Error",
126
+ "parent_id": parent_id,
127
+ "status": "done",
128
+ },
129
  )
130
 
131
  # Update parent message metadata to done status without yielding a new message
 
133
 
134
  # Handle standalone errors but not from tool calls
135
  elif hasattr(step_log, "error") and step_log.error is not None:
136
+ yield gr.ChatMessage(
137
+ role="assistant",
138
+ content=str(step_log.error),
139
+ metadata={"title": "💥 Error"},
140
+ )
141
 
142
  # Calculate duration and token information
143
  step_footnote = f"{step_number}"
144
+ if hasattr(step_log, "input_token_count") and hasattr(
145
+ step_log, "output_token_count"
146
+ ):
147
+ token_str = f" | Input-tokens:{step_log.input_token_count:,} | Output-tokens:{step_log.output_token_count:,}"
148
  step_footnote += token_str
149
  if hasattr(step_log, "duration"):
150
+ step_duration = (
151
+ f" | Duration: {round(float(step_log.duration), 2)}"
152
+ if step_log.duration
153
+ else None
154
+ )
155
  step_footnote += step_duration
156
  step_footnote = f"""<span style="color: #bbbbc2; font-size: 12px;">{step_footnote}</span> """
157
  yield gr.ChatMessage(role="assistant", content=f"{step_footnote}")
 
162
  """Stream agent responses to Gradio interface with better error handling"""
163
  total_input_tokens = 0
164
  total_output_tokens = 0
165
+
166
  try:
167
  for msg in agent.chat(task, reset_memory=reset_agent_memory):
168
  # Safely handle token counting
169
+ if hasattr(agent.model, "last_input_token_count"):
170
  input_tokens = agent.model.last_input_token_count or 0
171
  total_input_tokens += input_tokens
172
+
173
+ if hasattr(agent.model, "last_output_token_count"):
174
  output_tokens = agent.model.last_output_token_count or 0
175
  total_output_tokens += output_tokens
176
+
177
  yield msg
178
+
179
  except Exception as e:
180
  error_msg = f"Error during chat: {str(e)}"
181
  logger.error(error_msg)
 
249
  sanitized_name = "".join(sanitized_name)
250
 
251
  # Save the uploaded file to the specified folder
252
+ file_path = os.path.join(
253
+ self.file_upload_folder, os.path.basename(sanitized_name)
254
+ )
255
  shutil.copy(file.name, file_path)
256
 
257
+ return gr.Textbox(
258
+ f"File uploaded: {file_path}", visible=True
259
+ ), file_uploads_log + [file_path]
260
 
261
  def log_user_message(self, text_input, file_uploads_log):
262
  return (
 
288
  # If an upload folder is provided, enable the upload feature
289
  if self.file_upload_folder is not None:
290
  upload_file = gr.File(label="Upload a file")
291
+ upload_status = gr.Textbox(
292
+ label="Upload Status", interactive=False, visible=False
293
+ )
294
  upload_file.change(
295
  self.upload_file,
296
  [upload_file, file_uploads_log],