hgmiya commited on
Commit
c118378
·
verified ·
1 Parent(s): 4d4070f

add weather forecast

Browse files
Files changed (1) hide show
  1. app.py +73 -1
app.py CHANGED
@@ -5,6 +5,8 @@ import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
 
 
 
8
  from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity!
@@ -34,6 +36,76 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  final_answer = FinalAnswerTool()
38
  model = HfApiModel(
39
  max_tokens=2096,
@@ -51,7 +123,7 @@ with open("prompts.yaml", 'r') as stream:
51
 
52
  agent = CodeAgent(
53
  model=model,
54
- tools=[final_answer, image_generation_tool, get_current_time_in_timezone], # add your tools here (don't remove final_answer)
55
  max_steps=6,
56
  verbosity_level=1,
57
  grammar=None,
 
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
 
8
+ import xml.etree.ElementTree as ET
9
+
10
  from Gradio_UI import GradioUI
11
 
12
  # Below is an example of a tool that does nothing. Amaze us with your creativity!
 
36
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
37
 
38
 
39
+ @tool
40
+ def get_city_id(url, city_name):
41
+ """A tool that get city ID from city name.
42
+
43
+ Args:
44
+ city_name: The city name that want to get city id written in Japanese. (e.g., '東京', '横浜')
45
+
46
+ Returns:
47
+ city id. If not get, return None.
48
+ """
49
+
50
+ # XMLのURL
51
+ xml_url = "https://weather.tsukumijima.net/primary_area.xml"
52
+
53
+ try:
54
+ response = requests.get(url)
55
+ response.raise_for_status()
56
+ response.encoding = response.apparent_encoding
57
+ xml_string = response.text
58
+
59
+ root = ET.fromstring(xml_string)
60
+ for city in root.findall('.//city'):
61
+ if city.get('title') == city_name:
62
+ return city.get('id')
63
+ return None
64
+ except requests.exceptions.RequestException as e:
65
+ print(f"Error during request: {e}")
66
+ return None
67
+ except ET.ParseError as e:
68
+ print(f"Error parsing XML: {e}")
69
+ return None
70
+
71
+ @tool
72
+ def get_weather(city_id):
73
+ """Get city weather forecast from city id.
74
+
75
+ Args:
76
+ city_id: city id. (e.g., 140010)
77
+
78
+ Returns:
79
+ Weather forecast
80
+ """
81
+ try:
82
+ url = f"https://weather.tsukumijima.net/api/forecast?city={city_id}"
83
+ response = requests.get(url)
84
+ response.raise_for_status()
85
+
86
+ data_json = response.json()
87
+
88
+ # 今日の予報を取得 (forecasts[0])
89
+ date_str = data_json["forecasts"][0]["date"]
90
+ date = datetime.strptime(date_str, "%Y-%m-%d").strftime("%Y年%m月%d日")
91
+ title = data_json["title"]
92
+ weather = data_json["forecasts"][0]["telop"]
93
+
94
+ # 明日の予報を取得 (forecasts[1])
95
+ weather2 = data_json["forecasts"][1]["telop"]
96
+
97
+ results = f"{date}の{title}は{weather}です。明日は{weather2}でしょう。"
98
+ return results
99
+
100
+ except requests.exceptions.RequestException as e:
101
+ return f"天気情報の取得に失敗しました: {e}"
102
+
103
+ except KeyError as e:
104
+ return f"予期しないデータ形式です: {e}"
105
+ except TypeError as e:
106
+ return f"気温データが取得できませんでした: {e}"
107
+
108
+
109
  final_answer = FinalAnswerTool()
110
  model = HfApiModel(
111
  max_tokens=2096,
 
123
 
124
  agent = CodeAgent(
125
  model=model,
126
+ tools=[final_answer, image_generation_tool, get_current_time_in_timezone, get_weather, get_city_id], # add your tools here (don't remove final_answer)
127
  max_steps=6,
128
  verbosity_level=1,
129
  grammar=None,