File size: 3,076 Bytes
fe8bf03
 
 
deafbd7
 
fe8bf03
deafbd7
fe8bf03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deafbd7
fe8bf03
 
1172b96
fe8bf03
 
f3b2369
 
 
27f8993
f3b2369
 
 
 
 
fe8bf03
f3b2369
fe8bf03
f3b2369
 
 
 
fe8bf03
 
deafbd7
fe8bf03
 
 
 
 
deafbd7
fe8bf03
 
 
 
 
 
 
deafbd7
fe8bf03
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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("# 🤖 City Weather")
            gr.Markdown("""
            This agent can:
            - Get weather information for cities (using OpenWeatherMap API)
            - Search the web using DuckDuckGo
            - Provide natural conversational responses
            
            Weather Tool Limitations:
            - Cannot disambiguate between cities with the same name
            - Only accepts letters, digits, spaces, and hyphens in city names
            - Does not support additional location information (state, country, etc.)
            - Returns temperatures in Fahrenheit and wind speeds in mph
            
            Example queries:
            - "What's the weather like in Tokyo?"
            - "Tell me the temperature in New York"
            - "Search for the latest news about AI"
            
            Note: Make sure both HUGGINGFACE_TOKEN and OPENWEATHERMAP_API_KEY environment variables are set.
            """)
            
            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)