Spaces:
Runtime error
Runtime error
mtalec
commited on
Commit
·
e793fad
1
Parent(s):
ad46931
prompts and gradio
Browse files- Gradio_UI.py +97 -0
- prompts.yaml +109 -57
Gradio_UI.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# gradio_UI.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
import mimetypes
|
| 6 |
+
import shutil
|
| 7 |
+
from typing import Optional
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
from smolagents.agent_types import AgentText, AgentImage, AgentAudio, handle_agent_output_types
|
| 12 |
+
from smolagents.agents import ActionStep, MultiStepAgent
|
| 13 |
+
from smolagents.memory import MemoryStep
|
| 14 |
+
from smolagents.utils import _is_package_available
|
| 15 |
+
|
| 16 |
+
def pull_messages_from_step(step_log: MemoryStep):
|
| 17 |
+
if isinstance(step_log, ActionStep):
|
| 18 |
+
step_number = f"Step {step_log.step_number}" if step_log.step_number is not None else ""
|
| 19 |
+
yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
|
| 20 |
+
|
| 21 |
+
if hasattr(step_log, "model_output") and step_log.model_output is not None:
|
| 22 |
+
model_output = re.sub(r"```.?\s*<end_code>", "```", step_log.model_output.strip())
|
| 23 |
+
yield gr.ChatMessage(role="assistant", content=model_output)
|
| 24 |
+
|
| 25 |
+
if hasattr(step_log, "tool_calls") and step_log.tool_calls:
|
| 26 |
+
tool_call = step_log.tool_calls[0]
|
| 27 |
+
content = str(tool_call.arguments.get("answer", tool_call.arguments)) if isinstance(tool_call.arguments, dict) else str(tool_call.arguments)
|
| 28 |
+
if tool_call.name == "python_interpreter":
|
| 29 |
+
content = f"```python\n{re.sub(r'<end_code>', '', content).strip()}\n```"
|
| 30 |
+
tool_msg = gr.ChatMessage(
|
| 31 |
+
role="assistant",
|
| 32 |
+
content=content,
|
| 33 |
+
metadata={"title": f"🛠️ Used tool {tool_call.name}", "id": "tool_call", "status": "pending"},
|
| 34 |
+
)
|
| 35 |
+
yield tool_msg
|
| 36 |
+
if step_log.observations:
|
| 37 |
+
yield gr.ChatMessage(role="assistant", content=step_log.observations.strip(), metadata={"title": "📝 Execution Logs", "parent_id": "tool_call", "status": "done"})
|
| 38 |
+
if step_log.error:
|
| 39 |
+
yield gr.ChatMessage(role="assistant", content=str(step_log.error), metadata={"title": "💥 Error", "parent_id": "tool_call", "status": "done"})
|
| 40 |
+
tool_msg.metadata["status"] = "done"
|
| 41 |
+
|
| 42 |
+
elif step_log.error:
|
| 43 |
+
yield gr.ChatMessage(role="assistant", content=str(step_log.error), metadata={"title": "💥 Error"})
|
| 44 |
+
|
| 45 |
+
meta = f"<span style='color:#bbb;font-size:12px;'>Input tokens: {getattr(step_log, 'input_token_count', 0)} | Output tokens: {getattr(step_log, 'output_token_count', 0)} | Duration: {round(getattr(step_log, 'duration', 0), 2)}</span>"
|
| 46 |
+
yield gr.ChatMessage(role="assistant", content=meta)
|
| 47 |
+
yield gr.ChatMessage(role="assistant", content="-----")
|
| 48 |
+
|
| 49 |
+
def stream_to_gradio(agent, task: str, reset_agent_memory: bool = False, additional_args: Optional[dict] = None):
|
| 50 |
+
total_input_tokens = 0
|
| 51 |
+
total_output_tokens = 0
|
| 52 |
+
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
| 53 |
+
if hasattr(agent.model, "last_input_token_count"):
|
| 54 |
+
total_input_tokens += agent.model.last_input_token_count
|
| 55 |
+
total_output_tokens += agent.model.last_output_token_count
|
| 56 |
+
if isinstance(step_log, ActionStep):
|
| 57 |
+
step_log.input_token_count = agent.model.last_input_token_count
|
| 58 |
+
step_log.output_token_count = agent.model.last_output_token_count
|
| 59 |
+
for message in pull_messages_from_step(step_log):
|
| 60 |
+
yield message
|
| 61 |
+
final_answer = handle_agent_output_types(step_log)
|
| 62 |
+
if isinstance(final_answer, AgentText):
|
| 63 |
+
yield gr.ChatMessage(role="assistant", content=f"**Final answer:**\n{final_answer.to_string()}")
|
| 64 |
+
elif isinstance(final_answer, AgentImage):
|
| 65 |
+
yield gr.ChatMessage(role="assistant", content={"path": final_answer.to_string(), "mime_type": "image/png"})
|
| 66 |
+
elif isinstance(final_answer, AgentAudio):
|
| 67 |
+
yield gr.ChatMessage(role="assistant", content={"path": final_answer.to_string(), "mime_type": "audio/wav"})
|
| 68 |
+
else:
|
| 69 |
+
yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
|
| 70 |
+
|
| 71 |
+
class GradioUI:
|
| 72 |
+
def __init__(self, agent: MultiStepAgent):
|
| 73 |
+
if not _is_package_available("gradio"):
|
| 74 |
+
raise ModuleNotFoundError("Please install 'gradio' with: pip install 'smolagents[gradio]'")
|
| 75 |
+
self.agent = agent
|
| 76 |
+
|
| 77 |
+
def interact_with_agent(self, prompt, messages):
|
| 78 |
+
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 79 |
+
yield messages
|
| 80 |
+
for msg in stream_to_gradio(self.agent, task=prompt):
|
| 81 |
+
messages.append(msg)
|
| 82 |
+
yield messages
|
| 83 |
+
yield messages
|
| 84 |
+
|
| 85 |
+
def launch(self):
|
| 86 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 87 |
+
stored_messages = gr.State([])
|
| 88 |
+
chatbot = gr.Chatbot(label="🌍 Mood-Based Travel Agent", type="messages")
|
| 89 |
+
user_input = gr.Textbox(lines=1, label="Describe your mood")
|
| 90 |
+
user_input.submit(
|
| 91 |
+
lambda text, hist: (hist + [gr.ChatMessage(role="user", content=text)], ""),
|
| 92 |
+
[user_input, stored_messages],
|
| 93 |
+
[stored_messages, user_input],
|
| 94 |
+
).then(self.interact_with_agent, [user_input, stored_messages], [chatbot])
|
| 95 |
+
demo.launch(debug=True, share=True)
|
| 96 |
+
|
| 97 |
+
__all__ = ["GradioUI", "stream_to_gradio"]
|
prompts.yaml
CHANGED
|
@@ -1,59 +1,111 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
final_answer:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
Ensure clarity, friendliness, and emotional alignment. Present this as a final, complete output to the user.
|
|
|
|
| 1 |
+
system_prompt: |-
|
| 2 |
+
You are a world-class travel assistant agent named WanderMind, built to help users find the perfect destination based on their mood.
|
| 3 |
+
You operate in an autonomous, multi-step thinking loop using Thought → Code → Observation.
|
| 4 |
+
|
| 5 |
+
Your job is to:
|
| 6 |
+
- Reflect on the user’s mood
|
| 7 |
+
- Infer their emotional need
|
| 8 |
+
- Suggest a travel destination with a matching activity
|
| 9 |
+
- Check the weather
|
| 10 |
+
- Decide whether the weather suits the emotional need
|
| 11 |
+
- If not, suggest another destination
|
| 12 |
+
- Once happy, find flights from the origin
|
| 13 |
+
- Wrap everything into an inspirational message
|
| 14 |
+
- Optionally, add a quote based on the mood
|
| 15 |
+
|
| 16 |
+
You must solve the full task by reasoning, calling tools or managed agents if needed, and writing Python code in the Code block.
|
| 17 |
+
|
| 18 |
+
Each step should follow this format:
|
| 19 |
+
|
| 20 |
+
Thought: explain what you will do and why.
|
| 21 |
+
Code:
|
| 22 |
+
py
|
| 23 |
+
# your code here
|
| 24 |
+
<end_code>
|
| 25 |
+
Observation: result of previous code
|
| 26 |
+
|
| 27 |
+
You must output a final result using final_answer() .
|
| 28 |
+
|
| 29 |
+
Your available tools are:
|
| 30 |
+
- mood_to_need(mood: str) → str: Extracts the emotional need behind a mood (e.g., "to reconnect").
|
| 31 |
+
- need_to_destination(need: str) → dict: Suggests a destination and activity for that need. Returns JSON with keys 'city', 'country', 'activity'.
|
| 32 |
+
- weather_forecast(location: str) → str: Gets current weather forecast.
|
| 33 |
+
- get_flights(origin: str, destination: str) → str: Lists flights between two cities.
|
| 34 |
+
- quote_from_mood(mood: str) → str: Provides a motivational quote based on mood.
|
| 35 |
+
- final_wrap(mood, need, destination, activity, weather, flights) → str: Composes final inspirational message.
|
| 36 |
+
- final_answer(output: Any): Ends the task and returns the final result.
|
| 37 |
+
|
| 38 |
+
DO NOT use a tool unless needed. Plan your steps clearly. You can retry with different inputs if the weather is bad.
|
| 39 |
+
|
| 40 |
+
Now begin!
|
| 41 |
+
|
| 42 |
+
planning:
|
| 43 |
+
initial_facts: |-
|
| 44 |
+
### 1. Facts given in the task
|
| 45 |
+
- The user provides their mood.
|
| 46 |
+
|
| 47 |
+
### 2. Facts to look up
|
| 48 |
+
- Emotional need based on mood.
|
| 49 |
+
- Destination and activity based on need.
|
| 50 |
+
- Current weather at destination.
|
| 51 |
+
- Flights from user origin to destination.
|
| 52 |
+
- Quote for mood.
|
| 53 |
+
|
| 54 |
+
### 3. Facts to derive
|
| 55 |
+
- Whether the weather fits the emotional need.
|
| 56 |
+
- If not, re-iterate destination choice.
|
| 57 |
+
- Final wrap-up message to user.
|
| 58 |
+
|
| 59 |
+
initial_plan: |-
|
| 60 |
+
1. Extract emotional need from user mood using mood_to_need().
|
| 61 |
+
2. Suggest destination and activity using need_to_destination().
|
| 62 |
+
3. Get weather at destination with weather_forecast().
|
| 63 |
+
4. Assess if weather suits the need. If not, return to step 2.
|
| 64 |
+
5. Get flights using get_flights().
|
| 65 |
+
6. Generate quote using quote_from_mood().
|
| 66 |
+
7. Compose final message using final_wrap().
|
| 67 |
+
8. Call final_answer() with all outputs.
|
| 68 |
+
<end_plan>
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
update_facts_pre_messages: |-
|
| 72 |
+
### 1. Facts given in the task
|
| 73 |
+
### 2. Facts that we have learned
|
| 74 |
+
### 3. Facts still to look up
|
| 75 |
+
### 4. Facts still to derive
|
| 76 |
+
|
| 77 |
+
update_facts_post_messages: |-
|
| 78 |
+
Please update your facts:
|
| 79 |
+
### 1. Facts given in the task
|
| 80 |
+
### 2. Facts that we have learned
|
| 81 |
+
### 3. Facts still to look up
|
| 82 |
+
### 4. Facts still to derive
|
| 83 |
+
|
| 84 |
+
update_plan_pre_messages: |-
|
| 85 |
+
Below is your current task and history. Please write a new plan based on the updated facts.
|
| 86 |
+
|
| 87 |
+
update_plan_post_messages: |-
|
| 88 |
+
Write a clean new plan with the latest facts. You must respect tool usage rules.
|
| 89 |
+
|
| 90 |
+
managed_agent:
|
| 91 |
+
task: |-
|
| 92 |
+
You are a helpful sub-agent named '{{name}}'.
|
| 93 |
+
Your manager gives you this task:
|
| 94 |
+
{{task}}
|
| 95 |
+
|
| 96 |
+
You MUST return:
|
| 97 |
+
### 1. Task outcome (short version):
|
| 98 |
+
### 2. Task outcome (extremely detailed version):
|
| 99 |
+
### 3. Additional context (if any)
|
| 100 |
+
|
| 101 |
+
Wrap everything in a final_answer().
|
| 102 |
+
|
| 103 |
+
report: |-
|
| 104 |
+
Final answer from agent '{{name}}':
|
| 105 |
+
{{final_answer}}
|
| 106 |
|
| 107 |
final_answer:
|
| 108 |
+
pre_messages: |-
|
| 109 |
+
Let's summarize everything before presenting the final answer:
|
| 110 |
+
post_messages: |-
|
| 111 |
+
Here's your final result. Enjoy your journey!
|
|
|