Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,8 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
import requests
|
8 |
import json
|
9 |
from datetime import datetime
|
|
|
|
|
10 |
|
11 |
from Gradio_UI import GradioUI
|
12 |
import os
|
@@ -29,69 +31,62 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
29 |
|
30 |
@tool
|
31 |
def query_weather(location: str, days: int = 1) -> str:
|
32 |
-
"""
|
|
|
33 |
|
34 |
Args:
|
35 |
-
location: 要查询天气的地点名称,如"北京"、"上海"
|
36 |
-
days: 要查询的天数,默认为1
|
37 |
|
38 |
Returns:
|
39 |
包含天气预报信息的字符串。
|
40 |
"""
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
# 使用wttr
|
46 |
base_url = "https://wttr.in/"
|
47 |
-
|
48 |
-
# 构建API请求URL
|
49 |
params = {
|
50 |
-
"format": "j1",
|
51 |
-
"lang": "zh-cn"
|
52 |
}
|
53 |
-
|
54 |
try:
|
55 |
# 发送请求
|
56 |
response = requests.get(f"{base_url}{location}", params=params)
|
57 |
-
|
58 |
-
|
59 |
-
if response.status_code != 200:
|
60 |
-
return f"查询失败: HTTP状态码 {response.status_code}"
|
61 |
-
|
62 |
data = response.json()
|
63 |
-
|
64 |
# 处理天气数据
|
65 |
-
result = f"{location}天气预报:\n\n"
|
66 |
-
|
67 |
-
# 处理预报数据
|
68 |
for i, day in enumerate(data["weather"]):
|
69 |
if i >= days:
|
70 |
break
|
71 |
-
|
72 |
date = day["date"]
|
73 |
-
|
74 |
result += f"【{date}】\n"
|
75 |
-
|
76 |
# 添加当天的天气信息
|
77 |
for period in day["hourly"]:
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
89 |
result += "\n"
|
90 |
-
|
91 |
return result
|
92 |
-
|
93 |
-
except
|
94 |
-
return f"
|
95 |
|
96 |
|
97 |
|
|
|
7 |
import requests
|
8 |
import json
|
9 |
from datetime import datetime
|
10 |
+
from typing import Optional
|
11 |
+
|
12 |
|
13 |
from Gradio_UI import GradioUI
|
14 |
import os
|
|
|
31 |
|
32 |
@tool
|
33 |
def query_weather(location: str, days: int = 1) -> str:
|
34 |
+
"""
|
35 |
+
查询指定地点的天气预报。
|
36 |
|
37 |
Args:
|
38 |
+
location: 要查询天气的地点名称,如 "北京"、"上海"。
|
39 |
+
days: 要查询的天数,默认为 1 天(最多 5 天)。
|
40 |
|
41 |
Returns:
|
42 |
包含天气预报信息的字符串。
|
43 |
"""
|
44 |
+
if days < 1 or days > 5:
|
45 |
+
return "查询天数必须在 1-5 之间。"
|
46 |
+
|
|
|
|
|
47 |
base_url = "https://wttr.in/"
|
|
|
|
|
48 |
params = {
|
49 |
+
"format": "j1", # 返回 JSON 格式
|
50 |
+
"lang": "zh-cn" # 使用中文
|
51 |
}
|
52 |
+
|
53 |
try:
|
54 |
# 发送请求
|
55 |
response = requests.get(f"{base_url}{location}", params=params)
|
56 |
+
response.raise_for_status() # 检查 HTTP 状态码
|
57 |
+
|
|
|
|
|
|
|
58 |
data = response.json()
|
59 |
+
|
60 |
# 处理天气数据
|
61 |
+
result = f"{location} 天气预报:\n\n"
|
|
|
|
|
62 |
for i, day in enumerate(data["weather"]):
|
63 |
if i >= days:
|
64 |
break
|
65 |
+
|
66 |
date = day["date"]
|
|
|
67 |
result += f"【{date}】\n"
|
68 |
+
|
69 |
# 添加当天的天气信息
|
70 |
for period in day["hourly"]:
|
71 |
+
time_str = f"{int(period['time'])//100:02d}:00"
|
72 |
+
temp = period["tempC"]
|
73 |
+
weather_desc = period["weatherDesc"][0]["value"]
|
74 |
+
humidity = period["humidity"]
|
75 |
+
wind_speed = period["windspeedKmph"]
|
76 |
+
wind_dir = period["winddir16Point"]
|
77 |
+
|
78 |
+
result += (
|
79 |
+
f"- {time_str}: {weather_desc}, "
|
80 |
+
f"温度 {temp}°C, 湿度 {humidity}%, "
|
81 |
+
f"风速 {wind_speed}km/h, 风向 {wind_dir}\n"
|
82 |
+
)
|
83 |
+
|
84 |
result += "\n"
|
85 |
+
|
86 |
return result
|
87 |
+
|
88 |
+
except requests.exceptions.RequestException as e:
|
89 |
+
return f"查询天气失败: {str(e)}"
|
90 |
|
91 |
|
92 |
|