Spaces:
Sleeping
Sleeping
File size: 3,008 Bytes
4743dcc 5030b1f 6042e7f 4743dcc 6042e7f 445bced 38c5e64 4743dcc 426bbc0 5030b1f d3505a8 6042e7f d3505a8 426bbc0 d3505a8 6042e7f d3505a8 38c5e64 5030b1f e8093cc 5030b1f 6042e7f 5030b1f 6042e7f 5030b1f 2844849 e8093cc 6042e7f 5030b1f 4743dcc 5030b1f 38c5e64 6042e7f e8093cc 6042e7f 38c5e64 6042e7f 38c5e64 6042e7f 38c5e64 e8093cc 6042e7f 4743dcc d499824 e8093cc 6042e7f d499824 5030b1f d499824 5030b1f 4743dcc 5030b1f |
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 82 83 84 85 86 87 88 89 90 |
import gradio as gr
import requests
import os
from smolagents import tool, CodeAgent, HfApiModel, GoogleSearchTool
# Get API keys from environment
OPENWEATHER_API_KEY = os.environ.get("WEATHER_API_KEY")
SERPER_API_KEY = os.environ.get("SERPER_API_KEY")
@tool
def get_weather(lat: float, lon: float) -> dict | None:
"""
Fetches current weather data from OpenWeatherMap API using geographic coordinates
Args:
lat: Latitude of the location (-90 to 90)
lon: Longitude of the location (-180 to 180)
Returns:
dict | None: Weather data dictionary containing:
- condition (str): Weather description
- temperature (float): Temperature in Celsius
- humidity (float): Humidity percentage
- wind_speed (float): Wind speed in m/s
"""
url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={OPENWEATHER_API_KEY}&units=metric"
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return {
"condition": data["weather"][0]["description"],
"temperature": data["main"]["temp"],
"humidity": data["main"]["humidity"],
"wind_speed": data["wind"]["speed"]
}
return None
except Exception as e:
print(f"Weather API Error: {e}")
return None
weather_agent = CodeAgent(
model=HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct", max_tokens=8096),
tools=[GoogleSearchTool(provider="serper"), get_weather],
name="Weather Expert",
description="Processes weather queries through coordinate search and API calls"
)
def respond(message, history):
try:
system_prompt = """YOU ARE A WEATHER SPECIALIST:
1. Analyze location from user input
2. Google search "coordinates of [location]"
3. Extract LAT/LON numeric values
4. Call get_weather(LAT, LON)
5. Respond in Vietnamese markdown
NOTES:
- Always verify data accuracy
- Ask for clarification if coordinates not found
- Units: °C, %, m/s"""
response = weather_agent.run(
f"[System] {system_prompt}\n[User] {message}"
)
return response if response else "Could not process request"
except Exception as e:
print(f"[DEBUG] {str(e)}")
return "Sorry, I encountered an error processing your request"
with gr.Blocks(theme=gr.themes.Soft(), css="styles.css") as demo:
gr.Markdown("# 🌦️ Weather Assistant")
gr.Markdown("Enter any location to check weather conditions")
chatbot = gr.Chatbot(
elem_classes=["chatbot"],
)
# Giao diện chat
chat_interface = gr.ChatInterface(
respond,
chatbot=chatbot,
additional_inputs=None,
submit_btn=gr.Button("Submit", variant="primary"),
)
if __name__ == "__main__":
demo.launch() |