Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse filesfixed Pydantic validations
- Gradio_UI.py +20 -40
Gradio_UI.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
# coding=utf-8
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
-
from typing import Optional, List
|
| 6 |
from smolagents.agent_types import AgentText, handle_agent_output_types
|
| 7 |
from smolagents.agents import ActionStep, MultiStepAgent
|
| 8 |
from smolagents.memory import MemoryStep
|
|
@@ -21,12 +21,8 @@ def pull_messages_from_step(step_log: MemoryStep):
|
|
| 21 |
model_output = re.sub(r"```\s*<end_code>.*", "```", step_log.model_output.strip())
|
| 22 |
model_output = re.sub(r"<end_code>\s*```", "```", model_output)
|
| 23 |
if model_output.strip():
|
| 24 |
-
# Prevent code generation display - show as plain text
|
| 25 |
-
if model_output.startswith("```"):
|
| 26 |
-
model_output = model_output.replace("```python", "```\n💭 Reasoning:").replace("```", "")
|
| 27 |
yield gr.ChatMessage(role="assistant", content=model_output)
|
| 28 |
|
| 29 |
-
# Initialize parent_id to None for safe referencing
|
| 30 |
parent_id = None
|
| 31 |
|
| 32 |
# Handle tool calls
|
|
@@ -34,42 +30,36 @@ def pull_messages_from_step(step_log: MemoryStep):
|
|
| 34 |
tool_call = step_log.tool_calls[0]
|
| 35 |
parent_id = f"tool_{step_log.step_number}"
|
| 36 |
|
| 37 |
-
# Format tool arguments cleanly
|
| 38 |
args = tool_call.arguments
|
| 39 |
if isinstance(args, dict):
|
| 40 |
content = "\n".join(f"• {k}: {v}" for k, v in args.items() if v and k != 'self')
|
| 41 |
else:
|
| 42 |
content = str(args).strip()
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
"status": "running",
|
| 51 |
-
},
|
| 52 |
-
)
|
| 53 |
|
| 54 |
-
# Show observations/results
|
| 55 |
if hasattr(step_log, "observations") and step_log.observations:
|
| 56 |
obs = step_log.observations.strip()
|
| 57 |
if obs and not obs.startswith("Execution logs:"):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
)
|
| 63 |
|
| 64 |
-
# Show errors
|
| 65 |
if hasattr(step_log, "error") and step_log.error:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
)
|
| 71 |
|
| 72 |
-
# Step footer
|
| 73 |
footer_parts = [step_number]
|
| 74 |
if hasattr(step_log, "duration") and step_log.duration:
|
| 75 |
footer_parts.append(f"⏱️ {float(step_log.duration):.1f}s")
|
|
@@ -97,7 +87,6 @@ def stream_to_gradio(
|
|
| 97 |
try:
|
| 98 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
| 99 |
if isinstance(step_log, ActionStep):
|
| 100 |
-
# Track tokens if available
|
| 101 |
if hasattr(agent.model, "last_input_token_count") and hasattr(agent.model, "last_output_token_count"):
|
| 102 |
step_log.input_token_count = agent.model.last_input_token_count
|
| 103 |
step_log.output_token_count = agent.model.last_output_token_count
|
|
@@ -105,16 +94,10 @@ def stream_to_gradio(
|
|
| 105 |
for message in pull_messages_from_step(step_log):
|
| 106 |
yield message
|
| 107 |
|
| 108 |
-
# Final answer handling
|
| 109 |
final = handle_agent_output_types(step_log)
|
| 110 |
if isinstance(final, AgentText):
|
| 111 |
content = final.to_string()
|
| 112 |
-
|
| 113 |
-
yield gr.ChatMessage(
|
| 114 |
-
role="assistant",
|
| 115 |
-
content=content,
|
| 116 |
-
metadata={"react": True}
|
| 117 |
-
)
|
| 118 |
else:
|
| 119 |
yield gr.ChatMessage(role="assistant", content=f"**Final Answer:** {str(final)}")
|
| 120 |
|
|
@@ -122,7 +105,6 @@ def stream_to_gradio(
|
|
| 122 |
yield gr.ChatMessage(
|
| 123 |
role="assistant",
|
| 124 |
content=f"⚠️ **Error during planning:** {str(e)}\n\nPlease try again with a clearer trip description (include destination, dates, origin city, and budget).",
|
| 125 |
-
metadata={"title": "❌ Planning Failed"}
|
| 126 |
)
|
| 127 |
|
| 128 |
class GradioUI:
|
|
@@ -176,11 +158,9 @@ class GradioUI:
|
|
| 176 |
gr.Markdown("""
|
| 177 |
### 💡 Tips for best results:
|
| 178 |
• Include: destination, dates, origin city, budget amount + currency
|
| 179 |
-
• Example: *"5-day Barcelona trip from NYC, Oct 15-19, budget $1500 USD"*
|
| 180 |
-
• Agent will research attractions, check weather, convert currency, and generate images
|
| 181 |
""")
|
| 182 |
|
| 183 |
-
# Submit on button click or Enter
|
| 184 |
submit_btn.click(
|
| 185 |
self.interact_with_agent,
|
| 186 |
inputs=[text_input, chatbot],
|
|
|
|
| 2 |
# coding=utf-8
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
+
from typing import Optional, List, Dict
|
| 6 |
from smolagents.agent_types import AgentText, handle_agent_output_types
|
| 7 |
from smolagents.agents import ActionStep, MultiStepAgent
|
| 8 |
from smolagents.memory import MemoryStep
|
|
|
|
| 21 |
model_output = re.sub(r"```\s*<end_code>.*", "```", step_log.model_output.strip())
|
| 22 |
model_output = re.sub(r"<end_code>\s*```", "```", model_output)
|
| 23 |
if model_output.strip():
|
|
|
|
|
|
|
|
|
|
| 24 |
yield gr.ChatMessage(role="assistant", content=model_output)
|
| 25 |
|
|
|
|
| 26 |
parent_id = None
|
| 27 |
|
| 28 |
# Handle tool calls
|
|
|
|
| 30 |
tool_call = step_log.tool_calls[0]
|
| 31 |
parent_id = f"tool_{step_log.step_number}"
|
| 32 |
|
|
|
|
| 33 |
args = tool_call.arguments
|
| 34 |
if isinstance(args, dict):
|
| 35 |
content = "\n".join(f"• {k}: {v}" for k, v in args.items() if v and k != 'self')
|
| 36 |
else:
|
| 37 |
content = str(args).strip()
|
| 38 |
|
| 39 |
+
metadata: Dict = {
|
| 40 |
+
"title": f"🛠️ Using: {tool_call.name}",
|
| 41 |
+
"id": parent_id,
|
| 42 |
+
"status": "running",
|
| 43 |
+
}
|
| 44 |
+
yield gr.ChatMessage(role="assistant", content=content, metadata=metadata)
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Show observations/results (only include parent_id if defined)
|
| 47 |
if hasattr(step_log, "observations") and step_log.observations:
|
| 48 |
obs = step_log.observations.strip()
|
| 49 |
if obs and not obs.startswith("Execution logs:"):
|
| 50 |
+
metadata = {"title": "✅ Result"}
|
| 51 |
+
if parent_id is not None:
|
| 52 |
+
metadata["parent_id"] = parent_id
|
| 53 |
+
yield gr.ChatMessage(role="assistant", content=obs, metadata=metadata)
|
|
|
|
| 54 |
|
| 55 |
+
# Show errors (only include parent_id if defined)
|
| 56 |
if hasattr(step_log, "error") and step_log.error:
|
| 57 |
+
metadata = {"title": "⚠️ Warning"}
|
| 58 |
+
if parent_id is not None:
|
| 59 |
+
metadata["parent_id"] = parent_id
|
| 60 |
+
yield gr.ChatMessage(role="assistant", content=str(step_log.error), metadata=metadata)
|
|
|
|
| 61 |
|
| 62 |
+
# Step footer
|
| 63 |
footer_parts = [step_number]
|
| 64 |
if hasattr(step_log, "duration") and step_log.duration:
|
| 65 |
footer_parts.append(f"⏱️ {float(step_log.duration):.1f}s")
|
|
|
|
| 87 |
try:
|
| 88 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
| 89 |
if isinstance(step_log, ActionStep):
|
|
|
|
| 90 |
if hasattr(agent.model, "last_input_token_count") and hasattr(agent.model, "last_output_token_count"):
|
| 91 |
step_log.input_token_count = agent.model.last_input_token_count
|
| 92 |
step_log.output_token_count = agent.model.last_output_token_count
|
|
|
|
| 94 |
for message in pull_messages_from_step(step_log):
|
| 95 |
yield message
|
| 96 |
|
|
|
|
| 97 |
final = handle_agent_output_types(step_log)
|
| 98 |
if isinstance(final, AgentText):
|
| 99 |
content = final.to_string()
|
| 100 |
+
yield gr.ChatMessage(role="assistant", content=content, metadata={"react": True})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
else:
|
| 102 |
yield gr.ChatMessage(role="assistant", content=f"**Final Answer:** {str(final)}")
|
| 103 |
|
|
|
|
| 105 |
yield gr.ChatMessage(
|
| 106 |
role="assistant",
|
| 107 |
content=f"⚠️ **Error during planning:** {str(e)}\n\nPlease try again with a clearer trip description (include destination, dates, origin city, and budget).",
|
|
|
|
| 108 |
)
|
| 109 |
|
| 110 |
class GradioUI:
|
|
|
|
| 158 |
gr.Markdown("""
|
| 159 |
### 💡 Tips for best results:
|
| 160 |
• Include: destination, dates, origin city, budget amount + currency
|
| 161 |
+
• Example: *"5-day Barcelona trip from NYC, Oct 15-19, budget $1500 USD"*
|
|
|
|
| 162 |
""")
|
| 163 |
|
|
|
|
| 164 |
submit_btn.click(
|
| 165 |
self.interact_with_agent,
|
| 166 |
inputs=[text_input, chatbot],
|