jcleee commited on
Commit
56ea8ae
·
verified ·
1 Parent(s): ec905bc

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +148 -68
Gradio_UI.py CHANGED
@@ -10,32 +10,46 @@ from smolagents.memory import MemoryStep
10
  from smolagents.utils import _is_package_available
11
 
12
 
13
- def pull_messages_from_step(step_log: MemoryStep):
 
 
14
  """Extract ChatMessage objects from agent steps with proper nesting"""
15
  import gradio as gr
16
 
17
  if isinstance(step_log, ActionStep):
 
18
  step_number = f"Step {step_log.step_number}" if step_log.step_number is not None else ""
19
  yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
20
 
21
- if hasattr(step_log, "model_output") and step_log.model_output:
 
 
22
  model_output = step_log.model_output.strip()
23
- model_output = re.sub(r"```\s*<end_code>", "```", model_output)
24
- model_output = re.sub(r"<end_code>\s*```", "```", model_output)
25
- model_output = re.sub(r"```\s*\n\s*<end_code>", "```", model_output)
 
 
26
  yield gr.ChatMessage(role="assistant", content=model_output)
27
 
28
- if hasattr(step_log, "tool_calls") and step_log.tool_calls:
 
29
  first_tool_call = step_log.tool_calls[0]
30
  used_code = first_tool_call.name == "python_interpreter"
31
  parent_id = f"call_{len(step_log.tool_calls)}"
32
 
 
 
33
  args = first_tool_call.arguments
34
- content = str(args.get("answer", str(args))) if isinstance(args, dict) else str(args).strip()
 
 
 
35
 
36
  if used_code:
37
- content = re.sub(r"```.*?\n", "", content)
38
- content = re.sub(r"\s*<end_code>\s*", "", content)
 
39
  content = content.strip()
40
  if not content.startswith("```python"):
41
  content = f"```python\n{content}\n```"
@@ -43,51 +57,75 @@ def pull_messages_from_step(step_log: MemoryStep):
43
  parent_message_tool = gr.ChatMessage(
44
  role="assistant",
45
  content=content,
46
- metadata={"title": f"🛠️ Used tool {first_tool_call.name}", "id": parent_id, "status": "pending"},
 
 
 
 
47
  )
48
  yield parent_message_tool
49
 
50
- if hasattr(step_log, "observations") and step_log.observations:
51
- log_content = re.sub(r"^Execution logs:\s*", "", step_log.observations.strip())
52
- yield gr.ChatMessage(
53
- role="assistant",
54
- content=log_content,
55
- metadata={"title": "📝 Execution Logs", "parent_id": parent_id, "status": "done"},
56
- )
57
-
58
- if hasattr(step_log, "error") and step_log.error:
 
 
 
 
 
 
59
  yield gr.ChatMessage(
60
  role="assistant",
61
  content=str(step_log.error),
62
  metadata={"title": "💥 Error", "parent_id": parent_id, "status": "done"},
63
  )
64
 
 
65
  parent_message_tool.metadata["status"] = "done"
66
 
67
- elif hasattr(step_log, "error") and step_log.error:
 
68
  yield gr.ChatMessage(role="assistant", content=str(step_log.error), metadata={"title": "💥 Error"})
69
 
 
70
  step_footnote = f"{step_number}"
71
  if hasattr(step_log, "input_token_count") and hasattr(step_log, "output_token_count"):
72
- step_footnote += f" | Input-tokens:{step_log.input_token_count:,} | Output-tokens:{step_log.output_token_count:,}"
 
 
 
73
  if hasattr(step_log, "duration"):
74
- step_footnote += f" | Duration: {round(float(step_log.duration), 2)}"
 
75
  step_footnote = f"""<span style="color: #bbbbc2; font-size: 12px;">{step_footnote}</span> """
76
- yield gr.ChatMessage(role="assistant", content=step_footnote)
77
  yield gr.ChatMessage(role="assistant", content="-----")
78
 
79
 
80
- def stream_to_gradio(agent, task: str, reset_agent_memory: bool = False, additional_args: Optional[dict] = None):
 
 
 
 
 
 
81
  if not _is_package_available("gradio"):
82
- raise ModuleNotFoundError("Please install 'gradio': `pip install 'smolagents[gradio]'`")
83
-
 
84
  import gradio as gr
85
- from smolagents.agents import FinalAnswerStep
86
 
87
  total_input_tokens = 0
88
  total_output_tokens = 0
89
 
90
  for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
 
91
  if hasattr(agent.model, "last_input_token_count"):
92
  total_input_tokens += agent.model.last_input_token_count
93
  total_output_tokens += agent.model.last_output_token_count
@@ -95,13 +133,18 @@ def stream_to_gradio(agent, task: str, reset_agent_memory: bool = False, additio
95
  step_log.input_token_count = agent.model.last_input_token_count
96
  step_log.output_token_count = agent.model.last_output_token_count
97
 
98
- for message in pull_messages_from_step(step_log):
 
 
99
  yield message
100
 
101
- final_output = None
 
 
102
  if isinstance(step_log, FinalAnswerStep):
103
  final_output = step_log.final_answer
104
  else:
 
105
  final_output = getattr(step_log, "tool_output", None)
106
  if not final_output and hasattr(step_log, "tool_calls"):
107
  for call in step_log.tool_calls:
@@ -109,30 +152,47 @@ def stream_to_gradio(agent, task: str, reset_agent_memory: bool = False, additio
109
  final_output = call.arguments.get("answer")
110
  break
111
 
 
112
  final_output = handle_agent_output_types(final_output)
113
 
114
  if isinstance(final_output, AgentText):
115
- yield gr.ChatMessage(role="assistant", content=final_output.to_string().strip())
 
 
 
116
  elif isinstance(final_output, AgentImage):
117
- yield gr.ChatMessage(role="assistant", content={"path": final_output.to_string(), "mime_type": "image/png"})
 
 
 
118
  elif isinstance(final_output, AgentAudio):
119
- yield gr.ChatMessage(role="assistant", content={"path": final_output.to_string(), "mime_type": "audio/wav"})
 
 
 
120
  else:
121
  yield gr.ChatMessage(role="assistant", content=str(final_output).strip())
122
 
123
 
 
 
124
  class GradioUI:
125
- def __init__(self, agent: MultiStepAgent, file_upload_folder: Optional[str] = None, submit_fn=None):
 
 
126
  if not _is_package_available("gradio"):
127
- raise ModuleNotFoundError("Please install 'gradio': `pip install 'smolagents[gradio]'`")
 
 
128
  self.agent = agent
129
  self.file_upload_folder = file_upload_folder
130
- self.submit_fn = submit_fn
131
- if self.file_upload_folder and not os.path.exists(file_upload_folder):
132
- os.mkdir(file_upload_folder)
133
 
134
  def interact_with_agent(self, prompt, messages):
135
  import gradio as gr
 
136
  messages.append(gr.ChatMessage(role="user", content=prompt))
137
  yield messages
138
  for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
@@ -140,14 +200,21 @@ class GradioUI:
140
  yield messages
141
  yield messages
142
 
143
- def upload_file(self, file, file_uploads_log, allowed_file_types=None):
 
 
 
 
 
 
 
 
 
 
 
 
144
  import gradio as gr
145
- if allowed_file_types is None:
146
- allowed_file_types = [
147
- "application/pdf",
148
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
149
- "text/plain",
150
- ]
151
  if file is None:
152
  return gr.Textbox("No file uploaded", visible=True), file_uploads_log
153
 
@@ -159,11 +226,24 @@ class GradioUI:
159
  if mime_type not in allowed_file_types:
160
  return gr.Textbox("File type disallowed", visible=True), file_uploads_log
161
 
162
- sanitized_name = re.sub(r"[^\w\-.]", "_", os.path.basename(file.name))
163
- ext = mimetypes.guess_extension(mime_type)
164
- sanitized_name = f"{os.path.splitext(sanitized_name)[0]}{ext or ''}"
 
 
 
 
 
 
 
165
 
166
- file_path = os.path.join(self.file_upload_folder, sanitized_name)
 
 
 
 
 
 
167
  shutil.copy(file.name, file_path)
168
 
169
  return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
@@ -171,8 +251,11 @@ class GradioUI:
171
  def log_user_message(self, text_input, file_uploads_log):
172
  return (
173
  text_input
174
- + (f"\nYou have been provided with these files, which might be helpful or not: {file_uploads_log}"
175
- if file_uploads_log else ""),
 
 
 
176
  "",
177
  )
178
 
@@ -182,36 +265,33 @@ class GradioUI:
182
  with gr.Blocks(fill_height=True) as demo:
183
  stored_messages = gr.State([])
184
  file_uploads_log = gr.State([])
185
-
186
  chatbot = gr.Chatbot(
187
  label="Agent",
188
  type="messages",
189
- avatar_images=(None, "https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png"),
 
 
 
 
 
190
  )
191
-
192
- if self.file_upload_folder:
193
  upload_file = gr.File(label="Upload a file")
194
  upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
195
- upload_file.change(self.upload_file, [upload_file, file_uploads_log], [upload_status, file_uploads_log])
196
-
 
 
 
197
  text_input = gr.Textbox(lines=1, label="Chat Message")
198
  text_input.submit(
199
  self.log_user_message,
200
  [text_input, file_uploads_log],
201
  [stored_messages, text_input],
202
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
203
-
204
- submit_btn = gr.Button("📤 Submit All GAIA Answers")
205
- submit_status = gr.Textbox(label="Submission Status", interactive=False)
206
-
207
- if self.submit_fn:
208
- submit_btn.click(
209
- fn=lambda: self.submit_fn() or "✅ Submitted to GAIA scoring API",
210
- inputs=[],
211
- outputs=[submit_status],
212
- )
213
-
214
  demo.launch(debug=True, share=True, show_error=True, **kwargs)
215
 
216
 
217
- __all__ = ["stream_to_gradio", "GradioUI"]
 
10
  from smolagents.utils import _is_package_available
11
 
12
 
13
+ def pull_messages_from_step(
14
+ step_log: MemoryStep,
15
+ ):
16
  """Extract ChatMessage objects from agent steps with proper nesting"""
17
  import gradio as gr
18
 
19
  if isinstance(step_log, ActionStep):
20
+ # Output the step number
21
  step_number = f"Step {step_log.step_number}" if step_log.step_number is not None else ""
22
  yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
23
 
24
+ # First yield the thought/reasoning from the LLM
25
+ if hasattr(step_log, "model_output") and step_log.model_output is not None:
26
+ # Clean up the LLM output
27
  model_output = step_log.model_output.strip()
28
+ # Remove any trailing <end_code> and extra backticks, handling multiple possible formats
29
+ model_output = re.sub(r"```\s*<end_code>", "```", model_output) # handles ```<end_code>
30
+ model_output = re.sub(r"<end_code>\s*```", "```", model_output) # handles <end_code>```
31
+ model_output = re.sub(r"```\s*\n\s*<end_code>", "```", model_output) # handles ```\n<end_code>
32
+ model_output = model_output.strip()
33
  yield gr.ChatMessage(role="assistant", content=model_output)
34
 
35
+ # For tool calls, create a parent message
36
+ if hasattr(step_log, "tool_calls") and step_log.tool_calls is not None:
37
  first_tool_call = step_log.tool_calls[0]
38
  used_code = first_tool_call.name == "python_interpreter"
39
  parent_id = f"call_{len(step_log.tool_calls)}"
40
 
41
+ # Tool call becomes the parent message with timing info
42
+ # First we will handle arguments based on type
43
  args = first_tool_call.arguments
44
+ if isinstance(args, dict):
45
+ content = str(args.get("answer", str(args)))
46
+ else:
47
+ content = str(args).strip()
48
 
49
  if used_code:
50
+ # Clean up the content by removing any end code tags
51
+ content = re.sub(r"```.*?\n", "", content) # Remove existing code blocks
52
+ content = re.sub(r"\s*<end_code>\s*", "", content) # Remove end_code tags
53
  content = content.strip()
54
  if not content.startswith("```python"):
55
  content = f"```python\n{content}\n```"
 
57
  parent_message_tool = gr.ChatMessage(
58
  role="assistant",
59
  content=content,
60
+ metadata={
61
+ "title": f"🛠️ Used tool {first_tool_call.name}",
62
+ "id": parent_id,
63
+ "status": "pending",
64
+ },
65
  )
66
  yield parent_message_tool
67
 
68
+ # Nesting execution logs under the tool call if they exist
69
+ if hasattr(step_log, "observations") and (
70
+ step_log.observations is not None and step_log.observations.strip()
71
+ ): # Only yield execution logs if there's actual content
72
+ log_content = step_log.observations.strip()
73
+ if log_content:
74
+ log_content = re.sub(r"^Execution logs:\s*", "", log_content)
75
+ yield gr.ChatMessage(
76
+ role="assistant",
77
+ content=f"{log_content}",
78
+ metadata={"title": "📝 Execution Logs", "parent_id": parent_id, "status": "done"},
79
+ )
80
+
81
+ # Nesting any errors under the tool call
82
+ if hasattr(step_log, "error") and step_log.error is not None:
83
  yield gr.ChatMessage(
84
  role="assistant",
85
  content=str(step_log.error),
86
  metadata={"title": "💥 Error", "parent_id": parent_id, "status": "done"},
87
  )
88
 
89
+ # Update parent message metadata to done status without yielding a new message
90
  parent_message_tool.metadata["status"] = "done"
91
 
92
+ # Handle standalone errors but not from tool calls
93
+ elif hasattr(step_log, "error") and step_log.error is not None:
94
  yield gr.ChatMessage(role="assistant", content=str(step_log.error), metadata={"title": "💥 Error"})
95
 
96
+ # Calculate duration and token information
97
  step_footnote = f"{step_number}"
98
  if hasattr(step_log, "input_token_count") and hasattr(step_log, "output_token_count"):
99
+ token_str = (
100
+ f" | Input-tokens:{step_log.input_token_count:,} | Output-tokens:{step_log.output_token_count:,}"
101
+ )
102
+ step_footnote += token_str
103
  if hasattr(step_log, "duration"):
104
+ step_duration = f" | Duration: {round(float(step_log.duration), 2)}" if step_log.duration else None
105
+ step_footnote += step_duration
106
  step_footnote = f"""<span style="color: #bbbbc2; font-size: 12px;">{step_footnote}</span> """
107
+ yield gr.ChatMessage(role="assistant", content=f"{step_footnote}")
108
  yield gr.ChatMessage(role="assistant", content="-----")
109
 
110
 
111
+ def stream_to_gradio(
112
+ agent,
113
+ task: str,
114
+ reset_agent_memory: bool = False,
115
+ additional_args: Optional[dict] = None,
116
+ ):
117
+ """Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
118
  if not _is_package_available("gradio"):
119
+ raise ModuleNotFoundError(
120
+ "Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
121
+ )
122
  import gradio as gr
 
123
 
124
  total_input_tokens = 0
125
  total_output_tokens = 0
126
 
127
  for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
128
+ # Track tokens if model provides them
129
  if hasattr(agent.model, "last_input_token_count"):
130
  total_input_tokens += agent.model.last_input_token_count
131
  total_output_tokens += agent.model.last_output_token_count
 
133
  step_log.input_token_count = agent.model.last_input_token_count
134
  step_log.output_token_count = agent.model.last_output_token_count
135
 
136
+ for message in pull_messages_from_step(
137
+ step_log,
138
+ ):
139
  yield message
140
 
141
+ from smolagents.agents import FinalAnswerStep
142
+
143
+ # Safely extract the final output from the last step
144
  if isinstance(step_log, FinalAnswerStep):
145
  final_output = step_log.final_answer
146
  else:
147
+ # If not a FinalAnswerStep, fallback to tool_output or tool_calls
148
  final_output = getattr(step_log, "tool_output", None)
149
  if not final_output and hasattr(step_log, "tool_calls"):
150
  for call in step_log.tool_calls:
 
152
  final_output = call.arguments.get("answer")
153
  break
154
 
155
+
156
  final_output = handle_agent_output_types(final_output)
157
 
158
  if isinstance(final_output, AgentText):
159
+ yield gr.ChatMessage(
160
+ role="assistant",
161
+ content=final_output.to_string().strip(),
162
+ )
163
  elif isinstance(final_output, AgentImage):
164
+ yield gr.ChatMessage(
165
+ role="assistant",
166
+ content={"path": final_output.to_string(), "mime_type": "image/png"},
167
+ )
168
  elif isinstance(final_output, AgentAudio):
169
+ yield gr.ChatMessage(
170
+ role="assistant",
171
+ content={"path": final_output.to_string(), "mime_type": "audio/wav"},
172
+ )
173
  else:
174
  yield gr.ChatMessage(role="assistant", content=str(final_output).strip())
175
 
176
 
177
+
178
+
179
  class GradioUI:
180
+ """A one-line interface to launch your agent in Gradio"""
181
+
182
+ def __init__(self, agent: MultiStepAgent, file_upload_folder: str | None = None):
183
  if not _is_package_available("gradio"):
184
+ raise ModuleNotFoundError(
185
+ "Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
186
+ )
187
  self.agent = agent
188
  self.file_upload_folder = file_upload_folder
189
+ if self.file_upload_folder is not None:
190
+ if not os.path.exists(file_upload_folder):
191
+ os.mkdir(file_upload_folder)
192
 
193
  def interact_with_agent(self, prompt, messages):
194
  import gradio as gr
195
+
196
  messages.append(gr.ChatMessage(role="user", content=prompt))
197
  yield messages
198
  for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
 
200
  yield messages
201
  yield messages
202
 
203
+ def upload_file(
204
+ self,
205
+ file,
206
+ file_uploads_log,
207
+ allowed_file_types=[
208
+ "application/pdf",
209
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
210
+ "text/plain",
211
+ ],
212
+ ):
213
+ """
214
+ Handle file uploads, default allowed types are .pdf, .docx, and .txt
215
+ """
216
  import gradio as gr
217
+
 
 
 
 
 
218
  if file is None:
219
  return gr.Textbox("No file uploaded", visible=True), file_uploads_log
220
 
 
226
  if mime_type not in allowed_file_types:
227
  return gr.Textbox("File type disallowed", visible=True), file_uploads_log
228
 
229
+ # Sanitize file name
230
+ original_name = os.path.basename(file.name)
231
+ sanitized_name = re.sub(
232
+ r"[^\w\-.]", "_", original_name
233
+ ) # Replace any non-alphanumeric, non-dash, or non-dot characters with underscores
234
+
235
+ type_to_ext = {}
236
+ for ext, t in mimetypes.types_map.items():
237
+ if t not in type_to_ext:
238
+ type_to_ext[t] = ext
239
 
240
+ # Ensure the extension correlates to the mime type
241
+ sanitized_name = sanitized_name.split(".")[:-1]
242
+ sanitized_name.append("" + type_to_ext[mime_type])
243
+ sanitized_name = "".join(sanitized_name)
244
+
245
+ # Save the uploaded file to the specified folder
246
+ file_path = os.path.join(self.file_upload_folder, os.path.basename(sanitized_name))
247
  shutil.copy(file.name, file_path)
248
 
249
  return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
 
251
  def log_user_message(self, text_input, file_uploads_log):
252
  return (
253
  text_input
254
+ + (
255
+ f"\nYou have been provided with these files, which might be helpful or not: {file_uploads_log}"
256
+ if len(file_uploads_log) > 0
257
+ else ""
258
+ ),
259
  "",
260
  )
261
 
 
265
  with gr.Blocks(fill_height=True) as demo:
266
  stored_messages = gr.State([])
267
  file_uploads_log = gr.State([])
 
268
  chatbot = gr.Chatbot(
269
  label="Agent",
270
  type="messages",
271
+ avatar_images=(
272
+ None,
273
+ "https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
274
+ ),
275
+ resizeable=True,
276
+ scale=1,
277
  )
278
+ # If an upload folder is provided, enable the upload feature
279
+ if self.file_upload_folder is not None:
280
  upload_file = gr.File(label="Upload a file")
281
  upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
282
+ upload_file.change(
283
+ self.upload_file,
284
+ [upload_file, file_uploads_log],
285
+ [upload_status, file_uploads_log],
286
+ )
287
  text_input = gr.Textbox(lines=1, label="Chat Message")
288
  text_input.submit(
289
  self.log_user_message,
290
  [text_input, file_uploads_log],
291
  [stored_messages, text_input],
292
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
293
+
 
 
 
 
 
 
 
 
 
 
294
  demo.launch(debug=True, share=True, show_error=True, **kwargs)
295
 
296
 
297
+ __all__ = ["stream_to_gradio", "GradioUI"]