Leonardo commited on
Commit
8b27f3a
·
verified ·
1 Parent(s): de1d827

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -42
app.py CHANGED
@@ -33,7 +33,7 @@ from smolagents import (
33
  GoogleSearchTool,
34
  Tool,
35
  )
36
- from smolagents.agent_types import AgentText # Removed: AgentImage, AgentAudio
37
  from smolagents.gradio_ui import pull_messages_from_step, handle_agent_output_types
38
 
39
 
@@ -193,7 +193,7 @@ class ToolRegistry:
193
  name="image_generator",
194
  description=(
195
  "Generates high-quality AgentImage. "
196
- "with text prompt (77 token limit).",
197
  ),
198
  )
199
  except Exception as e:
@@ -202,9 +202,9 @@ class ToolRegistry:
202
 
203
  @staticmethod
204
  def load_clean_text_tool():
205
- """Initialize and return image generation tools."""
206
  try:
207
- return TextCleanerTool
208
  except Exception as e:
209
  print(f"✗ Couldn't initialize clean text tool: {e}")
210
  raise
@@ -227,15 +227,14 @@ def create_agent():
227
  image_generator = ToolRegistry.load_image_generation_tools()
228
  clean_text = TextCleanerTool() # Instantiate TextCleanerTool
229
 
230
- # Combine all tools into a single list (not a tuple)
231
- all_tools = [visualizer] + web_tools + [image_generator] + [clean_text]
232
 
233
  # Validate tools before creating agent
234
  for tool in all_tools:
235
  if not isinstance(tool, Tool):
236
  raise ValueError(
237
- "Invalid tool type: "
238
- f"{type(tool)}. All tools must be instances of Tool class."
239
  )
240
 
241
  return CodeAgent(
@@ -626,57 +625,55 @@ class GradioUI:
626
  "", label="Status", interactive=False, visible=True
627
  )
628
 
629
- def _connect_event_handlers(self):
630
- """Connect event handlers with stop functionality."""
 
 
 
 
 
 
 
 
631
  # Define the job handler for stopping generation
632
  self.job = None
633
 
634
  def start_processing(prompt, chat_history):
635
- # Update UI for processing state
636
- self.status.update(value="⏳ Processing request...", visible=True)
637
- self.submit_btn.update(interactive=False)
638
- self.stop_btn.update(visible=True)
639
  return prompt, chat_history
640
 
641
  def stop_generation():
642
  if self.job:
643
  self.job.cancel()
644
- self.status.update(value="✋ Generation stopped by user", visible=True)
645
- self.submit_btn.update(interactive=True)
646
- self.stop_btn.update(visible=False)
647
 
648
  def clear_chat():
649
  return [], gr.Textbox(interactive=True), gr.Button(interactive=True), ""
650
 
651
- # Connect event handlers
652
- process_event = (
653
- self.text_input.submit(
654
- start_processing,
655
- [self.text_input, self.chatbot],
656
- [self.text_input, self.chatbot],
657
- )
658
- .then(
659
- self._interact_with_agent,
660
- [self.text_input, self.chatbot, self.session_state],
661
- [self.chatbot],
662
- )
663
- .then(
664
- lambda: (gr.Textbox(interactive=True), gr.Button(interactive=True), ""),
665
- None,
666
- [self.text_input, self.submit_btn, self.status],
667
- )
668
  )
669
 
670
- # Store the job for cancellation
671
- self.job = process_event
672
-
673
- # Connect stop and clear buttons
674
- self.stop_btn.click(stop_generation)
675
- self.clear_btn.click(
676
- clear_chat,
677
- outputs=[self.chatbot, self.text_input, self.submit_btn, self.status],
 
678
  )
679
 
 
 
 
680
 
681
  def main():
682
  """Main entry point for the application."""
 
33
  GoogleSearchTool,
34
  Tool,
35
  )
36
+ from smolagents.agent_types import AgentText # AgentImage, AgentAudio
37
  from smolagents.gradio_ui import pull_messages_from_step, handle_agent_output_types
38
 
39
 
 
193
  name="image_generator",
194
  description=(
195
  "Generates high-quality AgentImage. "
196
+ "with text prompt (77 token limit)."
197
  ),
198
  )
199
  except Exception as e:
 
202
 
203
  @staticmethod
204
  def load_clean_text_tool():
205
+ """Initialize and return text cleaning tool."""
206
  try:
207
+ return TextCleanerTool()
208
  except Exception as e:
209
  print(f"✗ Couldn't initialize clean text tool: {e}")
210
  raise
 
227
  image_generator = ToolRegistry.load_image_generation_tools()
228
  clean_text = TextCleanerTool() # Instantiate TextCleanerTool
229
 
230
+ # Combine all tools into a single list
231
+ all_tools = [visualizer] + web_tools + [image_generator, clean_text]
232
 
233
  # Validate tools before creating agent
234
  for tool in all_tools:
235
  if not isinstance(tool, Tool):
236
  raise ValueError(
237
+ f"Invalid tool type: {type(tool)}. All tools must be instances of Tool class."
 
238
  )
239
 
240
  return CodeAgent(
 
625
  "", label="Status", interactive=False, visible=True
626
  )
627
 
628
+ def _connect_event_handlers(
629
+ self,
630
+ text_input,
631
+ launch_research_btn,
632
+ file_uploads_log,
633
+ stored_messages,
634
+ chatbot,
635
+ session_state,
636
+ ):
637
+ """Connect event handlers with appropriate parameters."""
638
  # Define the job handler for stopping generation
639
  self.job = None
640
 
641
  def start_processing(prompt, chat_history):
642
+ # We'll use the passed components directly rather than self.status
 
 
 
643
  return prompt, chat_history
644
 
645
  def stop_generation():
646
  if self.job:
647
  self.job.cancel()
 
 
 
648
 
649
  def clear_chat():
650
  return [], gr.Textbox(interactive=True), gr.Button(interactive=True), ""
651
 
652
+ # Connect text input submission
653
+ text_input.submit(
654
+ self.log_user_message,
655
+ [text_input, file_uploads_log],
656
+ [stored_messages, text_input, launch_research_btn],
657
+ ).then(
658
+ self.interact_with_agent,
659
+ [stored_messages, chatbot, session_state],
660
+ [chatbot],
 
 
 
 
 
 
 
 
661
  )
662
 
663
+ # Connect button click
664
+ launch_research_btn.click(
665
+ self.log_user_message,
666
+ [text_input, file_uploads_log],
667
+ [stored_messages, text_input, launch_research_btn],
668
+ ).then(
669
+ self.interact_with_agent,
670
+ [stored_messages, chatbot, session_state],
671
+ [chatbot],
672
  )
673
 
674
+ # Store the job for cancellation if needed
675
+ self.job = None # This would need to be assigned to an actual event
676
+
677
 
678
  def main():
679
  """Main entry point for the application."""