Spaces:
Sleeping
Sleeping
zhang-3000
commited on
Commit
·
3bac7f4
1
Parent(s):
b78a768
weather
Browse files- app.py +71 -0
- requirments.txt +176 -0
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from lagent.actions.base_action import BaseAction, tool_api
|
4 |
+
from lagent.schema import ActionReturn, ActionStatusCode
|
5 |
+
|
6 |
+
class WeatherQuery(BaseAction):
|
7 |
+
def __init__(self):
|
8 |
+
super().__init__()
|
9 |
+
self.api_key = os.getenv("weather_token")
|
10 |
+
print(self.api_key)
|
11 |
+
if not self.api_key:
|
12 |
+
raise EnvironmentError("未找到环境变量 'token'。请设置你的和风天气 API Key 到 'weather_token' 环境变量中,比如export weather_token='xxx' ")
|
13 |
+
|
14 |
+
@tool_api
|
15 |
+
def run(self, location: str) -> dict:
|
16 |
+
"""
|
17 |
+
查询实时天气信息。
|
18 |
+
|
19 |
+
Args:
|
20 |
+
location (str): 要查询的地点名称、LocationID 或经纬度坐标(如 "101010100" 或 "116.41,39.92")。
|
21 |
+
|
22 |
+
Returns:
|
23 |
+
dict: 包含天气信息的字典
|
24 |
+
* location: 地点名称
|
25 |
+
* weather: 天气状况
|
26 |
+
* temperature: 当前温度
|
27 |
+
* wind_direction: 风向
|
28 |
+
* wind_speed: 风速(公里/小时)
|
29 |
+
* humidity: 相对湿度(%)
|
30 |
+
* report_time: 数据报告时间
|
31 |
+
"""
|
32 |
+
try:
|
33 |
+
# 如果 location 不是坐标格式(例如 "116.41,39.92"),则调用 GeoAPI 获取 LocationID
|
34 |
+
if not ("," in location and location.replace(",", "").replace(".", "").isdigit()):
|
35 |
+
# 使用 GeoAPI 获取 LocationID
|
36 |
+
geo_url = f"https://geoapi.qweather.com/v2/city/lookup?location={location}&key={self.api_key}"
|
37 |
+
geo_response = requests.get(geo_url)
|
38 |
+
geo_data = geo_response.json()
|
39 |
+
|
40 |
+
if geo_data.get("code") != "200" or not geo_data.get("location"):
|
41 |
+
raise Exception(f"GeoAPI 返回错误码:{geo_data.get('code')} 或未找到位置")
|
42 |
+
|
43 |
+
location = geo_data["location"][0]["id"]
|
44 |
+
|
45 |
+
# 构建天气查询的 API 请求 URL
|
46 |
+
weather_url = f"https://devapi.qweather.com/v7/weather/now?location={location}&key={self.api_key}"
|
47 |
+
response = requests.get(weather_url)
|
48 |
+
data = response.json()
|
49 |
+
|
50 |
+
# 检查 API 响应码
|
51 |
+
if data.get("code") != "200":
|
52 |
+
raise Exception(f"Weather API 返回错误码:{data.get('code')}")
|
53 |
+
|
54 |
+
# 解析和组织天气信息
|
55 |
+
weather_info = {
|
56 |
+
"location": location,
|
57 |
+
"weather": data["now"]["text"],
|
58 |
+
"temperature": data["now"]["temp"] + "°C",
|
59 |
+
"wind_direction": data["now"]["windDir"],
|
60 |
+
"wind_speed": data["now"]["windSpeed"] + " km/h",
|
61 |
+
"humidity": data["now"]["humidity"] + "%",
|
62 |
+
"report_time": data["updateTime"]
|
63 |
+
}
|
64 |
+
|
65 |
+
return {"result": weather_info}
|
66 |
+
|
67 |
+
except Exception as exc:
|
68 |
+
return ActionReturn(
|
69 |
+
errmsg=f"WeatherQuery 异常:{exc}",
|
70 |
+
state=ActionStatusCode.HTTP_ERROR
|
71 |
+
)
|
requirments.txt
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiohappyeyeballs==2.4.4
|
2 |
+
aiohttp==3.11.11
|
3 |
+
aiosignal==1.3.2
|
4 |
+
altair==5.5.0
|
5 |
+
annotated-types==0.7.0
|
6 |
+
anyio==4.7.0
|
7 |
+
argon2-cffi==23.1.0
|
8 |
+
argon2-cffi-bindings==21.2.0
|
9 |
+
arrow==1.3.0
|
10 |
+
arxiv==2.1.3
|
11 |
+
asttokens==3.0.0
|
12 |
+
async-lru==2.0.4
|
13 |
+
async-timeout==5.0.1
|
14 |
+
asyncache==0.3.1
|
15 |
+
asyncer==0.0.8
|
16 |
+
attrs==24.3.0
|
17 |
+
babel==2.16.0
|
18 |
+
backports.strenum==1.3.1
|
19 |
+
beautifulsoup4==4.12.3
|
20 |
+
bleach==6.2.0
|
21 |
+
blinker==1.9.0
|
22 |
+
Brotli
|
23 |
+
cachetools==5.5.0
|
24 |
+
certifi
|
25 |
+
cffi==1.17.1
|
26 |
+
charset-normalizer
|
27 |
+
class-registry==2.1.2
|
28 |
+
click==8.1.8
|
29 |
+
colorama==0.4.6
|
30 |
+
comm==0.2.2
|
31 |
+
datasets==3.1.0
|
32 |
+
debugpy==1.8.11
|
33 |
+
decorator==5.1.1
|
34 |
+
defusedxml==0.7.1
|
35 |
+
dill==0.3.8
|
36 |
+
distro==1.9.0
|
37 |
+
duckduckgo_search==5.3.1b1
|
38 |
+
exceptiongroup==1.2.2
|
39 |
+
executing==2.1.0
|
40 |
+
fastjsonschema==2.21.1
|
41 |
+
feedparser==6.0.11
|
42 |
+
filelock
|
43 |
+
fqdn==1.5.1
|
44 |
+
frozenlist==1.5.0
|
45 |
+
fsspec==2024.9.0
|
46 |
+
func_timeout==4.3.5
|
47 |
+
gitdb==4.0.11
|
48 |
+
GitPython==3.1.43
|
49 |
+
gmpy2
|
50 |
+
griffe==0.48.0
|
51 |
+
h11==0.14.0
|
52 |
+
h2==4.1.0
|
53 |
+
hpack==4.0.0
|
54 |
+
httpcore==1.0.7
|
55 |
+
httpx==0.28.1
|
56 |
+
huggingface-hub==0.27.0
|
57 |
+
hyperframe==6.0.1
|
58 |
+
idna
|
59 |
+
ipykernel==6.29.5
|
60 |
+
ipython==8.31.0
|
61 |
+
ipywidgets==8.1.5
|
62 |
+
isoduration==20.11.0
|
63 |
+
jedi==0.19.2
|
64 |
+
Jinja2
|
65 |
+
jiter==0.8.2
|
66 |
+
json5==0.10.0
|
67 |
+
jsonpointer==3.0.0
|
68 |
+
jsonschema==4.23.0
|
69 |
+
jsonschema-specifications==2024.10.1
|
70 |
+
jupyter==1.0.0
|
71 |
+
jupyter-console==6.6.3
|
72 |
+
jupyter-events==0.11.0
|
73 |
+
jupyter-lsp==2.2.5
|
74 |
+
jupyter_client==8.6.2
|
75 |
+
jupyter_core==5.7.2
|
76 |
+
jupyter_server==2.15.0
|
77 |
+
jupyter_server_terminals==0.5.3
|
78 |
+
jupyterlab==4.3.4
|
79 |
+
jupyterlab_pygments==0.3.0
|
80 |
+
jupyterlab_server==2.27.3
|
81 |
+
jupyterlab_widgets==3.0.13
|
82 |
+
-e git+https://github.com/InternLM/lagent.git@e304e5d323cdbb631257fac9187d16b99476bc2f#egg=lagent
|
83 |
+
markdown-it-py==3.0.0
|
84 |
+
MarkupSafe
|
85 |
+
matplotlib-inline==0.1.7
|
86 |
+
mdurl==0.1.2
|
87 |
+
mistune==3.0.2
|
88 |
+
mkl-service==2.4.0
|
89 |
+
mkl_fft
|
90 |
+
mkl_random
|
91 |
+
mpmath
|
92 |
+
multidict==6.1.0
|
93 |
+
multiprocess==0.70.16
|
94 |
+
narwhals==1.19.1
|
95 |
+
nbclient==0.10.2
|
96 |
+
nbconvert==7.16.4
|
97 |
+
nbformat==5.10.4
|
98 |
+
nest-asyncio==1.6.0
|
99 |
+
networkx
|
100 |
+
notebook==7.3.2
|
101 |
+
notebook_shim==0.2.4
|
102 |
+
numpy
|
103 |
+
overrides==7.7.0
|
104 |
+
packaging==24.2
|
105 |
+
pandas==2.2.3
|
106 |
+
pandocfilters==1.5.1
|
107 |
+
parso==0.8.4
|
108 |
+
pexpect==4.9.0
|
109 |
+
pillow==10.4.0
|
110 |
+
platformdirs==4.3.6
|
111 |
+
prometheus_client==0.21.1
|
112 |
+
prompt_toolkit==3.0.48
|
113 |
+
propcache==0.2.1
|
114 |
+
protobuf==5.29.2
|
115 |
+
psutil==6.1.1
|
116 |
+
ptyprocess==0.7.0
|
117 |
+
pure_eval==0.2.3
|
118 |
+
pyarrow==18.1.0
|
119 |
+
pycparser==2.22
|
120 |
+
pydantic==2.6.4
|
121 |
+
pydantic_core==2.16.3
|
122 |
+
pydeck==0.9.1
|
123 |
+
Pygments==2.18.0
|
124 |
+
PySocks
|
125 |
+
python-dateutil==2.9.0.post0
|
126 |
+
python-json-logger==3.2.1
|
127 |
+
pytz==2024.2
|
128 |
+
PyYAML
|
129 |
+
pyzmq==26.2.0
|
130 |
+
qtconsole==5.6.1
|
131 |
+
QtPy==2.4.2
|
132 |
+
referencing==0.35.1
|
133 |
+
regex==2024.11.6
|
134 |
+
requests
|
135 |
+
rfc3339-validator==0.1.4
|
136 |
+
rfc3986-validator==0.1.1
|
137 |
+
rich==13.9.4
|
138 |
+
rpds-py==0.22.3
|
139 |
+
Send2Trash==1.8.3
|
140 |
+
sgmllib3k==1.0.0
|
141 |
+
six==1.17.0
|
142 |
+
smmap==5.0.1
|
143 |
+
sniffio==1.3.1
|
144 |
+
socksio==1.0.0
|
145 |
+
soupsieve==2.6
|
146 |
+
stack-data==0.6.3
|
147 |
+
streamlit==1.39.0
|
148 |
+
sympy
|
149 |
+
tenacity==9.0.0
|
150 |
+
termcolor==2.4.0
|
151 |
+
terminado==0.18.1
|
152 |
+
tiktoken==0.8.0
|
153 |
+
timeout-decorator==0.5.0
|
154 |
+
tinycss2==1.4.0
|
155 |
+
toml==0.10.2
|
156 |
+
tomli==2.2.1
|
157 |
+
torch==2.1.2
|
158 |
+
torchaudio==2.1.2
|
159 |
+
torchvision==0.16.2
|
160 |
+
tornado==6.4.2
|
161 |
+
tqdm==4.67.1
|
162 |
+
traitlets==5.14.3
|
163 |
+
triton==2.1.0
|
164 |
+
types-python-dateutil==2.9.0.20241206
|
165 |
+
typing_extensions
|
166 |
+
tzdata==2024.2
|
167 |
+
uri-template==1.3.0
|
168 |
+
urllib3
|
169 |
+
watchdog==5.0.3
|
170 |
+
wcwidth==0.2.13
|
171 |
+
webcolors==24.11.1
|
172 |
+
webencodings==0.5.1
|
173 |
+
websocket-client==1.8.0
|
174 |
+
widgetsnbextension==4.0.13
|
175 |
+
xxhash==3.5.0
|
176 |
+
yarl==1.18.3
|