mohammedelfeky-ai commited on
Commit
9a2a3bc
·
verified ·
1 Parent(s): dad63d9

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +234 -244
Gradio_UI.py CHANGED
@@ -18,110 +18,82 @@ import os
18
  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 # Ensure MultiStepAgent is correctly referenced
24
  from smolagents.memory import MemoryStep
25
  from smolagents.utils import _is_package_available
 
26
 
27
 
28
- def pull_messages_from_step(
29
- step_log: MemoryStep,
30
- ):
31
- """Extract ChatMessage objects from agent steps with proper nesting"""
32
- import gradio as gr
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
40
  if hasattr(step_log, "model_output") and step_log.model_output is not None:
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
-
50
- # For tool calls, create a parent message
51
- if hasattr(step_log, "tool_calls") and step_log.tool_calls is not None:
52
- first_tool_call = step_log.tool_calls[0]
53
- used_code = first_tool_call.name == "python_interpreter"
54
- parent_id = f"call_{len(step_log.tool_calls)}_{step_log.step_number or 'x'}" # Make parent_id more unique
55
 
56
- # Tool call becomes the parent message with timing info
57
- args = first_tool_call.arguments
 
 
 
58
  if isinstance(args, dict):
59
- content = str(args.get("answer", str(args)))
60
  else:
61
- content = str(args).strip()
62
-
63
- if used_code:
64
- content = re.sub(r"```.*?\n", "", content) # Remove existing code blocks
65
- content = re.sub(r"\s*<end_code>\s*", "", content) # Remove end_code tags
66
- content = content.strip()
67
- if not content.startswith("```python"): # Ensure it's a python block
68
- content = f"```python\n{content}\n```"
69
- else: # If it is, ensure newlines are correct
70
- content = content.replace("```python", "```python\n").replace("\n```", "\n```")
71
-
72
-
73
- parent_message_tool = gr.ChatMessage(
74
- role="assistant",
75
- content=content,
76
- metadata={
77
- "title": f"🛠️ Used tool {first_tool_call.name}",
78
- "id": parent_id,
79
- "status": "pending",
80
- },
81
- )
82
- yield parent_message_tool
83
-
84
- if hasattr(step_log, "observations") and (
85
- step_log.observations is not None and step_log.observations.strip()
86
- ):
87
- log_content = step_log.observations.strip()
88
- if log_content: # Only yield if there's actual content
89
- log_content = re.sub(r"^Execution logs:\s*", "", log_content)
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
- if hasattr(step_log, "error") and step_log.error is not None:
97
- yield gr.ChatMessage(
98
- role="assistant",
99
- content=str(step_log.error),
100
- metadata={"title": "💥 Error", "parent_id": parent_id, "status": "done"},
101
- )
102
- # This direct update might not work as expected as yield creates new objects.
103
- # Status update is visual; actual logic might be more complex.
104
- parent_message_tool.metadata["status"] = "done"
105
-
106
- elif hasattr(step_log, "error") and step_log.error is not None: # Standalone errors
107
- yield gr.ChatMessage(role="assistant", content=str(step_log.error), metadata={"title": "💥 Error"})
108
-
109
- step_footnote_parts = [step_number]
110
- if hasattr(step_log, "input_token_count") and step_log.input_token_count is not None and \
111
- hasattr(step_log, "output_token_count") and step_log.output_token_count is not None:
112
- token_str = (
113
- f" | Input-tokens:{step_log.input_token_count:,} | Output-tokens:{step_log.output_token_count:,}"
114
- )
115
- step_footnote_parts.append(token_str)
116
  if hasattr(step_log, "duration") and step_log.duration is not None:
117
- step_duration = f" | Duration: {round(float(step_log.duration), 2)}s"
118
- step_footnote_parts.append(step_duration)
 
 
 
119
 
120
- step_footnote_text = "".join(filter(None, step_footnote_parts))
121
- if step_footnote_text:
122
- step_footnote = f"""<span style="color: #bbbbc2; font-size: 12px;">{step_footnote_text}</span> """
123
- yield gr.ChatMessage(role="assistant", content=f"{step_footnote}")
124
- yield gr.ChatMessage(role="assistant", content="-----")
125
 
126
 
127
  def stream_to_gradio(
@@ -130,40 +102,65 @@ def stream_to_gradio(
130
  reset_agent_memory: bool = False,
131
  additional_args: Optional[dict] = None,
132
  ):
133
- """Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
134
  if not _is_package_available("gradio"):
135
- raise ModuleNotFoundError(
136
- "Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
137
- )
138
- import gradio as gr
139
 
140
- # Reset interaction logs for the new run if the agent has this attribute
141
- if hasattr(agent, 'interaction_logs'):
142
  agent.interaction_logs.clear()
143
- print("DEBUG: Cleared agent interaction_logs for new run.")
144
-
145
 
146
  for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
147
- if hasattr(agent.model, "last_input_token_count") and agent.model.last_input_token_count is not None: # Check for None
148
- if isinstance(step_log, ActionStep): # Only add token counts to ActionSteps
149
  step_log.input_token_count = agent.model.last_input_token_count
150
  step_log.output_token_count = agent.model.last_output_token_count
 
 
 
151
 
152
- for message in pull_messages_from_step(step_log):
153
- yield message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
- # After the loop, step_log holds the final answer or the last step's log
156
- final_answer_content = step_log
157
  final_answer_processed = handle_agent_output_types(final_answer_content)
158
 
159
  if isinstance(final_answer_processed, AgentText):
160
- yield gr.ChatMessage(role="assistant", content=f"**Final answer:**\n{final_answer_processed.to_string()}\n")
161
  elif isinstance(final_answer_processed, AgentImage):
162
- yield gr.ChatMessage(role="assistant", content={"path": final_answer_processed.to_string(), "mime_type": "image/png"})
 
 
 
 
 
 
 
163
  elif isinstance(final_answer_processed, AgentAudio):
164
- yield gr.ChatMessage(role="assistant", content={"path": final_answer_processed.to_string(), "mime_type": "audio/wav"})
165
- else:
166
- yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer_processed)}")
 
 
 
 
 
 
 
167
 
168
 
169
  class GradioUI:
@@ -171,206 +168,199 @@ class GradioUI:
171
 
172
  def __init__(self, agent: MultiStepAgent, file_upload_folder: str | None = None):
173
  if not _is_package_available("gradio"):
174
- raise ModuleNotFoundError(
175
- "Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
176
- )
177
  self.agent = agent
178
  self.file_upload_folder = file_upload_folder
179
  if self.file_upload_folder is not None:
180
- if not os.path.exists(file_upload_folder):
181
- os.makedirs(self.file_upload_folder, exist_ok=True) # Use makedirs
182
-
183
- self._latest_file_path_for_download = None # For download button state
184
 
185
  def _check_for_created_file(self):
186
- """Helper function to check interaction logs for a created file path."""
187
- self._latest_file_path_for_download = None # Reset
188
  if hasattr(self.agent, 'interaction_logs') and self.agent.interaction_logs:
189
- print(f"DEBUG UI: Checking {len(self.agent.interaction_logs)} interaction log entries.")
190
- for log_entry in self.agent.interaction_logs:
191
- if log_entry.get("tool_name") == "create_document":
192
- tool_output_value = log_entry.get("tool_output")
193
- print(f"DEBUG UI: Log for 'create_document', output: {tool_output_value}")
194
- if tool_output_value and isinstance(tool_output_value, str):
195
- if not tool_output_value.strip().startswith("ERROR:"):
196
- normalized_path = os.path.normpath(tool_output_value)
197
- if os.path.exists(normalized_path):
198
- self._latest_file_path_for_download = normalized_path
199
- print(f"DEBUG UI: File path for download set: {self._latest_file_path_for_download}")
200
- return True # Found a valid file
201
- else:
202
- print(f"DEBUG UI: Path from log ('{normalized_path}') does not exist.")
203
- else:
204
- print(f"DEBUG UI: 'create_document' tool reported error: {tool_output_value}")
 
 
 
 
 
 
 
 
205
  return False
206
 
 
 
 
 
 
 
 
207
 
208
- def interact_with_agent(self, prompt, messages_history, download_btn_state, file_output_state):
209
- import gradio as gr
210
 
211
- messages_history.append(gr.ChatMessage(role="user", content=prompt))
212
- yield messages_history, gr.update(visible=False), gr.update(value=None, visible=False) # Hide download items initially
213
 
214
- # Stream agent messages to chatbot
215
- for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
216
- messages_history.append(msg)
217
- yield messages_history, gr.update(visible=False), gr.update(value=None, visible=False) # Keep hidden during streaming
 
 
218
 
219
  # After streaming all agent messages, check for created file
220
  file_found = self._check_for_created_file()
221
 
222
- # Update UI based on whether a file was found
223
- # Yielding final state for chatbot, download button, and file component
224
- yield messages_history, gr.update(visible=file_found), gr.update(value=None, visible=False)
 
225
 
226
 
227
- def upload_file(
228
- self,
229
- file,
230
- file_uploads_log,
231
- allowed_file_types=[
 
 
 
 
232
  "application/pdf",
233
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
234
  "text/plain",
235
- ],
236
- ):
237
- import gradio as gr
238
-
239
- if file is None:
240
- return gr.Textbox("No file uploaded", visible=True), file_uploads_log
241
-
242
- try:
243
- mime_type, _ = mimetypes.guess_type(file.name)
244
- if mime_type is None: # Fallback if guess_type returns None
245
- mime_type = file.type # Gradio File object has a 'type' attribute
246
- except Exception as e:
247
- return gr.Textbox(f"Error: {e}", visible=True), file_uploads_log
248
 
 
 
 
 
 
 
 
 
249
  if mime_type not in allowed_file_types:
250
- return gr.Textbox(f"File type '{mime_type}' disallowed", visible=True), file_uploads_log
251
 
252
- original_name = os.path.basename(file.name)
253
  sanitized_name = re.sub(r"[^\w\-.]", "_", original_name)
254
-
255
- # Ensure correct extension based on mime type, if possible
256
  base_name, current_ext = os.path.splitext(sanitized_name)
257
 
258
- type_to_ext_map = {
 
259
  "application/pdf": ".pdf",
260
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
261
- "text/plain": ".txt",
262
- }
263
  expected_ext = type_to_ext_map.get(mime_type)
264
- if expected_ext and current_ext.lower() != expected_ext:
 
265
  sanitized_name = base_name + expected_ext
266
 
267
- file_path = os.path.join(self.file_upload_folder, sanitized_name)
268
- shutil.copy(file.name, file_path) # file.name is the temp path of the uploaded file
269
-
270
- return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
271
-
272
- def log_user_message(self, text_input, file_uploads_log):
273
- # This function prepares the prompt that goes to the agent.
274
- # It also clears the text_input box.
275
- full_prompt = text_input
276
- if file_uploads_log: # Check if list is not empty
277
- full_prompt += (
278
- f"\nYou have been provided with these files, which might be helpful or not: {file_uploads_log}"
279
- )
280
- return full_prompt, "" # Return the full prompt and an empty string to clear input
 
 
 
 
 
281
 
282
  def prepare_and_show_download_file(self):
283
- import gradio as gr
284
  if self._latest_file_path_for_download and os.path.exists(self._latest_file_path_for_download):
285
- print(f"DEBUG UI: Preparing download for UI: {self._latest_file_path_for_download}")
286
  return gr.File.update(value=self._latest_file_path_for_download,
287
  label=os.path.basename(self._latest_file_path_for_download),
288
  visible=True)
289
  else:
290
- print("DEBUG UI: No valid file path to prepare for download component.")
291
  gr.Warning("No file available for download or path is invalid.")
292
  return gr.File.update(visible=False)
293
 
294
  def launch(self, **kwargs):
295
- import gradio as gr
296
-
297
- with gr.Blocks(fill_height=True, theme=gr.themes.Soft()) as demo: # Added a theme
298
- # --- State Variables ---
299
- # stored_messages is used to build the prompt for the agent, not directly for chatbot display here.
300
- # The chatbot takes messages directly from interact_with_agent.
301
- # We'll use chat_history_state for the chatbot's message list.
302
- chat_history_state = gr.State([])
303
- file_uploads_log = gr.State([]) # Tracks paths of uploaded files
304
 
305
- # --- UI Layout ---
306
- gr.Markdown("# Smol Talk with your Agent") # Title
307
 
308
  with gr.Row():
309
- with gr.Column(scale=3): # Main chat area
310
- chatbot = gr.Chatbot(
311
  label="Agent Interaction",
312
- type="messages",
313
- # Bubble full width can make text hard to read, try default
314
- # bubble_full_width=False,
315
- avatar_images=(
316
- None, # User avatar
317
- "https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo-round.png" # Agent avatar
318
- ),
319
- height=600
320
  )
321
- text_input = gr.Textbox(
322
  lines=1,
323
  label="Your Message to the Agent",
324
- placeholder="Type your message and press Enter..."
325
  )
326
 
327
- with gr.Column(scale=1): # Sidebar for uploads and downloads
328
  if self.file_upload_folder is not None:
329
  gr.Markdown("### File Upload")
330
- upload_file_component = gr.File(label="Upload a supporting file")
331
- upload_status_display = gr.Textbox(label="Upload Status", interactive=False, visible=True, lines=2) # Make visible by default
332
- upload_file_component.upload( # Use 'upload' event for gr.File
333
  self.upload_file,
334
- [upload_file_component, file_uploads_log],
335
- [upload_status_display, file_uploads_log],
336
  )
337
 
338
  gr.Markdown("### Generated File")
339
- # This button becomes visible if a file is created by the agent
340
- download_btn = gr.Button("Download Generated File", visible=False)
341
- # This gr.File component becomes visible and populated when the button above is clicked
342
- file_output_display = gr.File(label="Downloadable Document", visible=False, interactive=False)
343
-
344
- # --- Event Handling ---
345
-
346
- # When user submits text_input:
347
- # 1. log_user_message: prepares the prompt (text + file info), clears text_input.
348
- # The output 'prepared_prompt' is then passed to interact_with_agent.
349
- # 2. interact_with_agent: streams agent's responses to chatbot, updates download button.
350
-
351
- # We need a state to hold the prepared prompt temporarily if log_user_message is separate
352
- prepared_prompt_state = gr.State("")
353
-
354
- text_input.submit(
355
- self.log_user_message,
356
- [text_input, file_uploads_log],
357
- [prepared_prompt_state, text_input] # prepared_prompt_state gets the full prompt, text_input is cleared
358
  ).then(
359
- self.interact_with_agent,
360
- [prepared_prompt_state, chat_history_state, download_btn, file_output_display], # Pass current UI states
361
- [chat_history_state, download_btn, file_output_display] # Update these UI states
362
  )
363
 
364
- # When download_btn is clicked:
365
- download_btn.click(
366
  self.prepare_and_show_download_file,
367
- [], # No inputs needed from UI for this action
368
- [file_output_display] # Update the file_output_display component
369
  )
370
 
371
- # Launch the Gradio app
372
- # Set share=False if running locally or on Spaces where share=True might be an issue
373
  demo.launch(debug=True, share=kwargs.get("share", False), **kwargs)
374
 
375
-
376
  __all__ = ["stream_to_gradio", "GradioUI"]
 
18
  import re
19
  import shutil
20
  from typing import Optional
21
+ import tempfile # Added for PIL image saving
22
+ from PIL import Image as PILImage # Added for PIL image handling
23
 
24
  from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
25
+ from smolagents.agents import ActionStep, MultiStepAgent
26
  from smolagents.memory import MemoryStep
27
  from smolagents.utils import _is_package_available
28
+ import gradio as gr # Ensure gradio is imported at the top level
29
 
30
 
31
+ def pull_messages_from_step_dict(step_log: MemoryStep):
32
+ """Extract messages as dicts for Gradio type='messages' Chatbot"""
 
 
 
 
33
  if isinstance(step_log, ActionStep):
34
+ step_number_str = f"Step {step_log.step_number}" if step_log.step_number is not None else "Processing"
35
+ yield {"role": "assistant", "content": f"**{step_number_str}**"}
 
36
 
 
37
  if hasattr(step_log, "model_output") and step_log.model_output is not None:
 
38
  model_output = step_log.model_output.strip()
39
+ # More robust cleaning for <end_code> potentially wrapped in backticks or with newlines
40
+ model_output = re.sub(r"```\s*<end_code>[\s\S]*|[\s\S]*<end_code>\s*```", "```", model_output, flags=re.DOTALL)
41
+ model_output = re.sub(r"<end_code>", "", model_output) # Remove standalone tag
 
42
  model_output = model_output.strip()
43
+ yield {"role": "assistant", "content": model_output}
 
 
 
 
 
 
44
 
45
+ if hasattr(step_log, "tool_calls") and step_log.tool_calls:
46
+ tc = step_log.tool_calls[0] # Process first tool call for simplicity in this format
47
+ tool_info_md = f"🛠️ **Tool Used: {tc.name}**\n"
48
+
49
+ args = tc.arguments
50
  if isinstance(args, dict):
51
+ args_str = str(args.get("answer", str(args)))
52
  else:
53
+ args_str = str(args).strip()
54
+
55
+ if tc.name == "python_interpreter":
56
+ code_content = args_str
57
+ # Clean up common wrapping issues
58
+ code_content = re.sub(r"^```python\s*\n?", "", code_content)
59
+ code_content = re.sub(r"\n?```\s*$", "", code_content)
60
+ code_content = re.sub(r"^\s*<end_code>\s*", "", code_content)
61
+ code_content = re.sub(r"\s*<end_code>\s*$", "", code_content)
62
+ code_content = code_content.strip()
63
+ tool_info_md += f"Executing Code:\n```python\n{code_content}\n```\n"
64
+ else:
65
+ tool_info_md += f"Arguments: `{args_str}`\n"
66
+
67
+ if hasattr(step_log, "observations") and step_log.observations and step_log.observations.strip():
68
+ obs_content = step_log.observations.strip()
69
+ # Remove "Execution logs:" prefix if present for cleaner display
70
+ obs_content = re.sub(r"^Execution logs:\s*", "", obs_content).strip()
71
+ if obs_content: # Only show if there's something after stripping
72
+ tool_info_md += f"📝 **Tool Output/Logs:**\n```\n{obs_content}\n```\n"
73
+
74
+ if hasattr(step_log, "error") and step_log.error:
75
+ tool_info_md += f"💥 **Error:** {str(step_log.error)}\n"
76
+
77
+ yield {"role": "assistant", "content": tool_info_md.strip()}
 
 
 
 
 
 
 
 
 
78
 
79
+ elif hasattr(step_log, "error") and step_log.error: # Standalone error not from a tool call
80
+ yield {"role": "assistant", "content": f"💥 **Error:** {str(step_log.error)}"}
81
+
82
+ # --- Minimal footnote for type="messages" ---
83
+ footnote_parts = []
84
+ if step_log.step_number is not None:
85
+ footnote_parts.append(f"Step {step_log.step_number}")
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  if hasattr(step_log, "duration") and step_log.duration is not None:
87
+ footnote_parts.append(f"Duration: {round(float(step_log.duration), 2)}s")
88
+ if hasattr(step_log, "input_token_count") and step_log.input_token_count is not None: # Check for None
89
+ footnote_parts.append(f"InTokens: {step_log.input_token_count:,}")
90
+ if hasattr(step_log, "output_token_count") and step_log.output_token_count is not None: # Check for None
91
+ footnote_parts.append(f"OutTokens: {step_log.output_token_count:,}")
92
 
93
+ if footnote_parts:
94
+ footnote_text = " | ".join(footnote_parts)
95
+ yield {"role": "assistant", "content": f"""<p style="color: #999; font-size: 0.8em; margin-top:0; margin-bottom:0;">{footnote_text}</p>"""}
96
+ yield {"role": "assistant", "content": "---"} # Separator
 
97
 
98
 
99
  def stream_to_gradio(
 
102
  reset_agent_memory: bool = False,
103
  additional_args: Optional[dict] = None,
104
  ):
105
+ """Runs an agent, yields message dicts for Gradio type='messages' Chatbot."""
106
  if not _is_package_available("gradio"):
107
+ raise ModuleNotFoundError("Install 'gradio': `pip install 'smolagents[gradio]'`")
 
 
 
108
 
109
+ if hasattr(agent, 'interaction_logs'): # Clear logs for this new agent run
 
110
  agent.interaction_logs.clear()
111
+ print("DEBUG Gradio: Cleared agent interaction_logs for new run.")
 
112
 
113
  for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
114
+ if hasattr(agent.model, "last_input_token_count") and agent.model.last_input_token_count is not None:
115
+ if isinstance(step_log, ActionStep):
116
  step_log.input_token_count = agent.model.last_input_token_count
117
  step_log.output_token_count = agent.model.last_output_token_count
118
+
119
+ for msg_dict in pull_messages_from_step_dict(step_log): # Use new dict-yielding function
120
+ yield msg_dict
121
 
122
+ final_answer_content = step_log # Last step_log is the final output/state
123
+
124
+ # --- Handle final answer for type="messages" ---
125
+ if isinstance(final_answer_content, PILImage.Image):
126
+ print("DEBUG Gradio (stream_to_gradio): Final answer is raw PIL Image.")
127
+ try:
128
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
129
+ final_answer_content.save(tmp_file, format="PNG")
130
+ image_path_for_gradio = tmp_file.name
131
+ print(f"DEBUG Gradio: Saved PIL image to temp path: {image_path_for_gradio}")
132
+ # For Gradio type="messages", image content is just the path string
133
+ yield {"role": "assistant", "content": image_path_for_gradio}
134
+ return
135
+ except Exception as e:
136
+ print(f"DEBUG Gradio: Error saving PIL image from final_answer_content: {e}")
137
+ yield {"role": "assistant", "content": f"**Final Answer (Error displaying image):** {e}"}
138
+ return
139
 
 
 
140
  final_answer_processed = handle_agent_output_types(final_answer_content)
141
 
142
  if isinstance(final_answer_processed, AgentText):
143
+ yield {"role": "assistant", "content": f"**Final Answer:**\n{final_answer_processed.to_string()}"}
144
  elif isinstance(final_answer_processed, AgentImage):
145
+ image_path = final_answer_processed.to_string()
146
+ print(f"DEBUG Gradio (stream_to_gradio): AgentImage path: {image_path}")
147
+ if image_path and os.path.exists(image_path):
148
+ yield {"role": "assistant", "content": image_path}
149
+ else:
150
+ err_msg = f"Error: Image path from AgentImage not found or invalid ('{image_path}')"
151
+ print(f"DEBUG Gradio: {err_msg}")
152
+ yield {"role": "assistant", "content": f"**Final Answer ({err_msg})**"}
153
  elif isinstance(final_answer_processed, AgentAudio):
154
+ audio_path = final_answer_processed.to_string()
155
+ print(f"DEBUG Gradio (stream_to_gradio): AgentAudio path: {audio_path}")
156
+ if audio_path and os.path.exists(audio_path):
157
+ yield {"role": "assistant", "content": audio_path}
158
+ else:
159
+ err_msg = f"Error: Audio path from AgentAudio not found ('{audio_path}')"
160
+ print(f"DEBUG Gradio: {err_msg}")
161
+ yield {"role": "assistant", "content": f"**Final Answer ({err_msg})**"}
162
+ else:
163
+ yield {"role": "assistant", "content": f"**Final Answer:**\n{str(final_answer_processed)}"}
164
 
165
 
166
  class GradioUI:
 
168
 
169
  def __init__(self, agent: MultiStepAgent, file_upload_folder: str | None = None):
170
  if not _is_package_available("gradio"):
171
+ raise ModuleNotFoundError("Install 'gradio': `pip install 'smolagents[gradio]'`")
 
 
172
  self.agent = agent
173
  self.file_upload_folder = file_upload_folder
174
  if self.file_upload_folder is not None:
175
+ if not os.path.exists(self.file_upload_folder):
176
+ os.makedirs(self.file_upload_folder, exist_ok=True)
177
+ self._latest_file_path_for_download = None
 
178
 
179
  def _check_for_created_file(self):
180
+ self._latest_file_path_for_download = None
 
181
  if hasattr(self.agent, 'interaction_logs') and self.agent.interaction_logs:
182
+ print(f"DEBUG Gradio UI: Checking {len(self.agent.interaction_logs)} interaction log entries for created files.")
183
+ for log_entry in reversed(self.agent.interaction_logs): # Check recent logs first
184
+ if isinstance(log_entry, ActionStep) and hasattr(log_entry, 'tool_calls') and log_entry.tool_calls:
185
+ for tool_call in log_entry.tool_calls:
186
+ if tool_call.name == "create_document":
187
+ tool_output_value = getattr(log_entry, 'observations', None)
188
+ print(f"DEBUG Gradio UI: Log for 'create_document' call, observed output: {tool_output_value}")
189
+ if tool_output_value and isinstance(tool_output_value, str):
190
+ # Try to extract path if it's wrapped, e.g. by "Execution logs:"
191
+ cleaned_output = re.sub(r"^Execution logs:\s*", "", tool_output_value).strip()
192
+ path_match = re.search(r"(/tmp/[a-zA-Z0-9_]+/generated_document\.(?:docx|pdf|txt))", cleaned_output)
193
+ extracted_path = path_match.group(1) if path_match else cleaned_output
194
+
195
+ if not extracted_path.lower().startswith("error:"):
196
+ normalized_path = os.path.normpath(extracted_path)
197
+ if os.path.exists(normalized_path):
198
+ self._latest_file_path_for_download = normalized_path
199
+ print(f"DEBUG Gradio UI: File path for download set: {self._latest_file_path_for_download}")
200
+ return True
201
+ else:
202
+ print(f"DEBUG Gradio UI: Path from 'create_document' log ('{normalized_path}') does not exist.")
203
+ else:
204
+ print(f"DEBUG Gradio UI: 'create_document' tool reported error in observations: {extracted_path}")
205
+ print("DEBUG Gradio UI: No valid 'create_document' output found for download.")
206
  return False
207
 
208
+ def interact_with_agent(self, prompt_text: str, current_chat_tuples: list):
209
+ # current_chat_tuples is the history from the chatbot (list of lists/tuples)
210
+ # Convert to 'messages' format if needed, or adapt stream_to_gradio if chatbot is not type="messages"
211
+ # For type="messages", current_chat_tuples is already list of dicts.
212
+
213
+ print(f"DEBUG Gradio: interact_with_agent called with prompt: '{prompt_text}'")
214
+ print(f"DEBUG Gradio: Current chat history (input): {current_chat_tuples}")
215
 
216
+ # Add user's new message to the chat history list
217
+ current_chat_messages = current_chat_tuples + [{"role": "user", "content": prompt_text}]
218
 
219
+ # Initial yield to show user message immediately and hide download items
220
+ yield current_chat_messages, gr.update(visible=False), gr.update(value=None, visible=False)
221
 
222
+ # Stream agent messages
223
+ agent_responses_for_history = []
224
+ for msg_dict in stream_to_gradio(self.agent, task=prompt_text, reset_agent_memory=False):
225
+ agent_responses_for_history.append(msg_dict)
226
+ # Yield progressively: current user message + all agent messages so far
227
+ yield current_chat_messages + agent_responses_for_history, gr.update(visible=False), gr.update(value=None, visible=False)
228
 
229
  # After streaming all agent messages, check for created file
230
  file_found = self._check_for_created_file()
231
 
232
+ # Final state for UI components
233
+ final_chat_display = current_chat_messages + agent_responses_for_history
234
+ print(f"DEBUG Gradio: Final chat history for display: {final_chat_display}")
235
+ yield final_chat_display, gr.update(visible=file_found), gr.update(value=None, visible=False)
236
 
237
 
238
+ def upload_file(self, file, file_uploads_log_state):
239
+ if file is None: # No file selected
240
+ return gr.update(value="No file uploaded.", visible=True), file_uploads_log_state
241
+
242
+ # Ensure file_upload_folder exists (it should from __init__)
243
+ if not self.file_upload_folder or not os.path.exists(self.file_upload_folder):
244
+ os.makedirs(self.file_upload_folder, exist_ok=True) # Defensive check
245
+
246
+ allowed_file_types = [
247
  "application/pdf",
248
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
249
  "text/plain",
250
+ "image/jpeg", "image/png", # Added image types
251
+ ]
 
 
 
 
 
 
 
 
 
 
 
252
 
253
+ # Gradio File object has 'name' (temp path) and 'orig_name'
254
+ original_name = file.orig_name if hasattr(file, 'orig_name') else os.path.basename(file.name)
255
+
256
+ # Try to guess mime type from temp file name first, then from original name if needed
257
+ mime_type, _ = mimetypes.guess_type(file.name)
258
+ if mime_type is None: # Fallback
259
+ mime_type, _ = mimetypes.guess_type(original_name)
260
+
261
  if mime_type not in allowed_file_types:
262
+ return gr.update(value=f"File type '{mime_type or 'unknown'}' for '{original_name}' is disallowed.", visible=True), file_uploads_log_state
263
 
 
264
  sanitized_name = re.sub(r"[^\w\-.]", "_", original_name)
 
 
265
  base_name, current_ext = os.path.splitext(sanitized_name)
266
 
267
+ type_to_ext_map = {v: k for k, v_list in mimetypes. প্রেফারেন্সেস.items() for v in v_list} # More robust ext map
268
+ type_to_ext_map.update({ # Manual overrides / common types
269
  "application/pdf": ".pdf",
270
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
271
+ "text/plain": ".txt", "image/jpeg": ".jpg", "image/png": ".png"
272
+ })
273
  expected_ext = type_to_ext_map.get(mime_type)
274
+
275
+ if expected_ext and current_ext.lower() != expected_ext.lower():
276
  sanitized_name = base_name + expected_ext
277
 
278
+ destination_path = os.path.join(self.file_upload_folder, sanitized_name)
279
+
280
+ try:
281
+ shutil.copy(file.name, destination_path) # file.name is the temp path from Gradio
282
+ print(f"DEBUG Gradio: File '{original_name}' copied to '{destination_path}'")
283
+ updated_log = file_uploads_log_state + [destination_path]
284
+ return gr.update(value=f"Uploaded: {original_name} (as {sanitized_name})", visible=True), updated_log
285
+ except Exception as e:
286
+ print(f"DEBUG Gradio: Error copying uploaded file: {e}")
287
+ return gr.update(value=f"Error uploading {original_name}: {e}", visible=True), file_uploads_log_state
288
+
289
+
290
+ def log_user_message(self, text_input_value: str, current_file_uploads: list):
291
+ full_prompt = text_input_value
292
+ if current_file_uploads:
293
+ files_str = ", ".join([os.path.basename(f) for f in current_file_uploads])
294
+ full_prompt += f"\n\n[Uploaded files for context: {files_str}]"
295
+ print(f"DEBUG Gradio: Prepared prompt for agent: {full_prompt}")
296
+ return full_prompt, "" # Clears the text input box
297
 
298
  def prepare_and_show_download_file(self):
 
299
  if self._latest_file_path_for_download and os.path.exists(self._latest_file_path_for_download):
300
+ print(f"DEBUG Gradio UI: Preparing download for UI component: {self._latest_file_path_for_download}")
301
  return gr.File.update(value=self._latest_file_path_for_download,
302
  label=os.path.basename(self._latest_file_path_for_download),
303
  visible=True)
304
  else:
305
+ print("DEBUG Gradio UI: No valid file path to prepare for download component.")
306
  gr.Warning("No file available for download or path is invalid.")
307
  return gr.File.update(visible=False)
308
 
309
  def launch(self, **kwargs):
310
+ with gr.Blocks(fill_height=True, theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue)) as demo:
311
+ file_uploads_log_state = gr.State([])
312
+ prepared_prompt_for_agent = gr.State("")
 
 
 
 
 
 
313
 
314
+ gr.Markdown("# agente inteligente")
 
315
 
316
  with gr.Row():
317
+ with gr.Column(scale=3):
318
+ chatbot_display = gr.Chatbot(
319
  label="Agent Interaction",
320
+ type="messages",
321
+ avatar_images=(None, "https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo-round.png"),
322
+ height=600,
323
+ show_copy_button=True,
324
+ bubble_full_width=False
 
 
 
325
  )
326
+ text_message_input = gr.Textbox(
327
  lines=1,
328
  label="Your Message to the Agent",
329
+ placeholder="Type your message and press Enter, or Shift+Enter for new line..."
330
  )
331
 
332
+ with gr.Column(scale=1):
333
  if self.file_upload_folder is not None:
334
  gr.Markdown("### File Upload")
335
+ file_uploader = gr.File(label="Upload a supporting file (PDF, DOCX, TXT, JPG, PNG)")
336
+ upload_status_text = gr.Textbox(label="Upload Status", interactive=False, lines=2, max_lines=4)
337
+ file_uploader.upload(
338
  self.upload_file,
339
+ [file_uploader, file_uploads_log_state],
340
+ [upload_status_text, file_uploads_log_state],
341
  )
342
 
343
  gr.Markdown("### Generated File")
344
+ download_action_button = gr.Button("Download Generated File", visible=False)
345
+ file_download_display_component = gr.File(label="Downloadable Document", visible=False, interactive=False)
346
+
347
+ # Event Handling Chain for Text Submission
348
+ text_message_input.submit(
349
+ self.log_user_message, # Step 1: Prepare prompt, clear input
350
+ [text_message_input, file_uploads_log_state],
351
+ [prepared_prompt_for_agent, text_message_input]
 
 
 
 
 
 
 
 
 
 
 
352
  ).then(
353
+ self.interact_with_agent, # Step 2: Run agent, stream to chatbot, update download button
354
+ [prepared_prompt_for_agent, chatbot_display],
355
+ [chatbot_display, download_action_button, file_download_display_component]
356
  )
357
 
358
+ download_action_button.click(
 
359
  self.prepare_and_show_download_file,
360
+ [],
361
+ [file_download_display_component]
362
  )
363
 
 
 
364
  demo.launch(debug=True, share=kwargs.get("share", False), **kwargs)
365
 
 
366
  __all__ = ["stream_to_gradio", "GradioUI"]