Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,9 @@ import os
|
|
| 4 |
from geopy.geocoders import Nominatim
|
| 5 |
from smolagents import tool, CodeAgent, HfApiModel, GoogleSearchTool, VisitWebpageTool
|
| 6 |
|
| 7 |
-
# Lấy API
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
# Định nghĩa tool get_weather
|
| 11 |
@tool
|
|
@@ -15,10 +16,8 @@ def get_weather(lat: float, lon: float) -> dict | None:
|
|
| 15 |
Args:
|
| 16 |
lat: latitude of the location
|
| 17 |
lon: longitude of the location
|
| 18 |
-
|
| 19 |
-
Return: dictionary about weather information or None.
|
| 20 |
"""
|
| 21 |
-
url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={
|
| 22 |
try:
|
| 23 |
response = requests.get(url)
|
| 24 |
data = response.json()
|
|
@@ -34,20 +33,47 @@ def get_weather(lat: float, lon: float) -> dict | None:
|
|
| 34 |
print(f"Weather API Error: {e}")
|
| 35 |
return None
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Khởi tạo agent
|
| 38 |
weather_agent = CodeAgent(
|
| 39 |
model=HfApiModel("deepseek-ai/DeepSeek-R1", max_tokens=8096),
|
| 40 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
name="weather_agent",
|
| 42 |
-
description="Weather information provider"
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
# Hàm xử lý chat
|
| 48 |
def respond(message, history):
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Tạo giao diện Gradio
|
| 53 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
@@ -57,7 +83,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 57 |
chatbot = gr.ChatInterface(
|
| 58 |
respond,
|
| 59 |
examples=["Hà Nội", "New York", "Tokyo", "Paris"],
|
| 60 |
-
title="Weather Chatbot"
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
|
|
|
| 4 |
from geopy.geocoders import Nominatim
|
| 5 |
from smolagents import tool, CodeAgent, HfApiModel, GoogleSearchTool, VisitWebpageTool
|
| 6 |
|
| 7 |
+
# Lấy API keys từ biến môi trường
|
| 8 |
+
OPENWEATHER_API_KEY = os.environ.get("OPENWEATHER_API_KEY")
|
| 9 |
+
SERPER_API_KEY = os.environ.get("SERPER_API_KEY")
|
| 10 |
|
| 11 |
# Định nghĩa tool get_weather
|
| 12 |
@tool
|
|
|
|
| 16 |
Args:
|
| 17 |
lat: latitude of the location
|
| 18 |
lon: longitude of the location
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={OPENWEATHER_API_KEY}&units=metric"
|
| 21 |
try:
|
| 22 |
response = requests.get(url)
|
| 23 |
data = response.json()
|
|
|
|
| 33 |
print(f"Weather API Error: {e}")
|
| 34 |
return None
|
| 35 |
|
| 36 |
+
# Hàm chuyển đổi địa điểm thành tọa độ
|
| 37 |
+
@tool
|
| 38 |
+
def get_coordinates(location: str) -> tuple[float, float] | None:
|
| 39 |
+
"""Convert location name to geographic coordinates"""
|
| 40 |
+
geolocator = Nominatim(user_agent="weather_agent")
|
| 41 |
+
location = geolocator.geocode(location)
|
| 42 |
+
return (location.latitude, location.longitude) if location else None
|
| 43 |
+
|
| 44 |
# Khởi tạo agent
|
| 45 |
weather_agent = CodeAgent(
|
| 46 |
model=HfApiModel("deepseek-ai/DeepSeek-R1", max_tokens=8096),
|
| 47 |
+
tools=[
|
| 48 |
+
get_weather,
|
| 49 |
+
get_coordinates,
|
| 50 |
+
GoogleSearchTool(provider="serper", api_key=SERPER_API_KEY),
|
| 51 |
+
VisitWebpageTool()
|
| 52 |
+
],
|
| 53 |
name="weather_agent",
|
| 54 |
+
description="Weather information provider",
|
| 55 |
+
additional_authorized_imports=["geopy"]
|
| 56 |
)
|
| 57 |
|
|
|
|
|
|
|
| 58 |
# Hàm xử lý chat
|
| 59 |
def respond(message, history):
|
| 60 |
+
try:
|
| 61 |
+
# Thêm prompt engineering để hướng dẫn agent
|
| 62 |
+
system_prompt = """Bạn là trợ lý thời tiết chuyên nghiệp. Hãy:
|
| 63 |
+
1. Xác định tọa độ địa lý từ tên địa điểm
|
| 64 |
+
2. Lấy thông tin thời tiết từ API
|
| 65 |
+
3. Trả lời bằng tiếng Việt với định dạng markdown
|
| 66 |
+
4. Kiểm tra độ chính xác của dữ liệu"""
|
| 67 |
+
|
| 68 |
+
response = weather_agent.run(
|
| 69 |
+
f"{system_prompt}\n\nYêu cầu người dùng: {message}"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
return response if response else "Không thể lấy thông tin thời tiết"
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"Error: {e}")
|
| 76 |
+
return "Đã xảy ra lỗi khi xử lý yêu cầu"
|
| 77 |
|
| 78 |
# Tạo giao diện Gradio
|
| 79 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 83 |
chatbot = gr.ChatInterface(
|
| 84 |
respond,
|
| 85 |
examples=["Hà Nội", "New York", "Tokyo", "Paris"],
|
| 86 |
+
title="Weather Chatbot",
|
| 87 |
+
additional_inputs_accordion_name="Advanced Options"
|
| 88 |
)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|