First_agent / Gradio_UI.py
cjb97's picture
reset
fe8bf03
raw
history blame
2.6 kB
import gradio as gr
import time
from typing import List, Dict, Any, Optional
class GradioUI:
def __init__(self, agent):
self.agent = agent
self.chat_history = []
def process_agent_response(self, response):
"""Process the agent's response for display in the UI."""
if isinstance(response, str):
return response
elif hasattr(response, "content"):
return response.content
else:
return str(response)
def chat(self, message, history):
"""Process a user message and return the agent's response."""
self.chat_history = history
# Add user message to history
self.chat_history.append((message, ""))
# Get response from agent
response = self.agent.run(message)
processed_response = self.process_agent_response(response)
# Update the last response in history
self.chat_history[-1] = (message, processed_response)
return "", self.chat_history
def launch(self, **kwargs):
"""Launch the Gradio interface."""
with gr.Blocks(css="footer {visibility: hidden}") as demo:
gr.Markdown("# 🤖 My First Agent with smolagents")
gr.Markdown("""
This agent can:
- Search the web using DuckDuckGo
- Get weather information for locations
Try asking it questions like:
- "What's the weather like in Tokyo?"
- "What are the latest news about AI?"
- "Who won the last World Cup?"
- "Tell me about the history of computers"
""")
chatbot = gr.Chatbot(
[],
elem_id="chatbot",
avatar_images=(None, "🤖"),
bubble_full_width=False,
height=500
)
with gr.Row():
txt = gr.Textbox(
scale=4,
show_label=False,
placeholder="Enter your message here...",
container=False
)
submit_btn = gr.Button("Send", variant="primary", scale=1)
txt.submit(self.chat, [txt, chatbot], [txt, chatbot])
submit_btn.click(self.chat, [txt, chatbot], [txt, chatbot])
gr.Markdown("### 📝 Created as part of the [Hugging Face Agents Course](https://huggingface.co/learn/agents)")
return demo.launch(**kwargs)