piyushdev commited on
Commit
5eeb695
·
verified ·
1 Parent(s): b22c36b

Updated Gradio_UI.py

Browse files

Streaming the image in Gradio UI

Files changed (1) hide show
  1. Gradio_UI.py +41 -26
Gradio_UI.py CHANGED
@@ -20,7 +20,7 @@ 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
 
@@ -138,8 +138,13 @@ def stream_to_gradio(
138
 
139
  total_input_tokens = 0
140
  total_output_tokens = 0
 
141
 
142
  for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
 
 
 
 
143
  # Track tokens if model provides them
144
  if hasattr(agent.model, "last_input_token_count"):
145
  total_input_tokens += agent.model.last_input_token_count
@@ -152,27 +157,33 @@ def stream_to_gradio(
152
  step_log,
153
  ):
154
  yield message
 
 
 
 
 
 
 
 
155
 
156
- final_answer = step_log # Last log is the run's final_answer
157
- final_answer = handle_agent_output_types(final_answer)
158
-
159
- if isinstance(final_answer, AgentText):
160
- yield gr.ChatMessage(
161
- role="assistant",
162
- content=f"**Final answer:**\n{final_answer.to_string()}\n",
163
- )
164
- elif isinstance(final_answer, AgentImage):
165
- yield gr.ChatMessage(
166
- role="assistant",
167
- content={"path": final_answer.to_string(), "mime_type": "image/png"},
168
- )
169
- elif isinstance(final_answer, AgentAudio):
170
- yield gr.ChatMessage(
171
- role="assistant",
172
- content={"path": final_answer.to_string(), "mime_type": "audio/wav"},
173
- )
174
- else:
175
- yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
176
 
177
 
178
  class GradioUI:
@@ -266,12 +277,13 @@ class GradioUI:
266
  file_uploads_log = gr.State([])
267
  chatbot = gr.Chatbot(
268
  label="Agent",
269
- type="messages",
270
  avatar_images=(
271
  None,
272
- "https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
273
  ),
274
- resizeable=True,
 
275
  scale=1,
276
  )
277
  # If an upload folder is provided, enable the upload feature
@@ -283,13 +295,16 @@ class GradioUI:
283
  [upload_file, file_uploads_log],
284
  [upload_status, file_uploads_log],
285
  )
286
- text_input = gr.Textbox(lines=1, label="Chat Message")
287
  text_input.submit(
288
  self.log_user_message,
289
  [text_input, file_uploads_log],
290
  [stored_messages, text_input],
291
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
292
-
 
 
 
293
  allowed_paths = kwargs.pop("allowed_paths", [])
294
  if self.file_upload_folder and self.file_upload_folder not in allowed_paths:
295
  allowed_paths.append(self.file_upload_folder)
 
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, FinalAnswerStep, MultiStepAgent
24
  from smolagents.memory import MemoryStep
25
  from smolagents.utils import _is_package_available
26
 
 
138
 
139
  total_input_tokens = 0
140
  total_output_tokens = 0
141
+ final_answer_step = None
142
 
143
  for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
144
+ if isinstance(step_log, FinalAnswerStep):
145
+ final_answer_step = step_log
146
+ continue # Don't display the final answer step itself, process it at the end
147
+
148
  # Track tokens if model provides them
149
  if hasattr(agent.model, "last_input_token_count"):
150
  total_input_tokens += agent.model.last_input_token_count
 
157
  step_log,
158
  ):
159
  yield message
160
+
161
+ # FIX: Process the final answer correctly after the loop
162
+ if final_answer_step:
163
+ # Extract the actual value from the FinalAnswerStep object
164
+ final_answer_value = getattr(final_answer_step, 'final_answer', final_answer_step)
165
+
166
+ # Convert the value (e.g., a file path string) into the correct Agent type (e.g., AgentImage)
167
+ processed_answer = handle_agent_output_types(final_answer_value)
168
 
169
+ if isinstance(processed_answer, AgentText):
170
+ yield gr.ChatMessage(
171
+ role="assistant",
172
+ content=f"**Final answer:**\n{processed_answer.to_string()}\n",
173
+ )
174
+ elif isinstance(processed_answer, AgentImage):
175
+ yield gr.ChatMessage(
176
+ role="assistant",
177
+ content=processed_answer.to_string(), # Gradio automatically handles file paths
178
+ )
179
+ elif isinstance(processed_answer, AgentAudio):
180
+ yield gr.ChatMessage(
181
+ role="assistant",
182
+ content=processed_answer.to_string(), # Gradio automatically handles file paths
183
+ )
184
+ else:
185
+ # Fallback for any other type
186
+ yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(processed_answer)}")
 
 
187
 
188
 
189
  class GradioUI:
 
277
  file_uploads_log = gr.State([])
278
  chatbot = gr.Chatbot(
279
  label="Agent",
280
+ render_markdown=True,
281
  avatar_images=(
282
  None,
283
+ "[https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png)",
284
  ),
285
+ bubble_full_width=False,
286
+ show_copy_button=True,
287
  scale=1,
288
  )
289
  # If an upload folder is provided, enable the upload feature
 
295
  [upload_file, file_uploads_log],
296
  [upload_status, file_uploads_log],
297
  )
298
+ text_input = gr.Textbox(lines=1, label="Chat Message", scale=4)
299
  text_input.submit(
300
  self.log_user_message,
301
  [text_input, file_uploads_log],
302
  [stored_messages, text_input],
303
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
304
+
305
+ # Add allowed_paths to the launch command
306
+ # This gives Gradio permission to access and display files from the current directory
307
+ # and the specified file upload folder.
308
  allowed_paths = kwargs.pop("allowed_paths", [])
309
  if self.file_upload_folder and self.file_upload_folder not in allowed_paths:
310
  allowed_paths.append(self.file_upload_folder)