Spaces:
Runtime error
Runtime error
Leonardo
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,7 +33,7 @@ from smolagents import (
|
|
| 33 |
GoogleSearchTool,
|
| 34 |
Tool,
|
| 35 |
)
|
| 36 |
-
from smolagents.agent_types import AgentText
|
| 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
|
| 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
|
| 231 |
-
all_tools = [visualizer] + web_tools + [image_generator
|
| 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(
|
| 630 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
# Define the job handler for stopping generation
|
| 632 |
self.job = None
|
| 633 |
|
| 634 |
def start_processing(prompt, chat_history):
|
| 635 |
-
#
|
| 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
|
| 652 |
-
|
| 653 |
-
self.
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 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 |
-
#
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
|
|
|
| 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."""
|