xomatic commited on
Commit
8856e31
·
verified ·
1 Parent(s): ff9c8a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -36
app.py CHANGED
@@ -11,6 +11,21 @@ from Gradio_UI import GradioUI
11
  # LOCATION + TIMEZONE TOOLS
12
  # -----------------------------
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  @tool
15
  def get_location() -> dict:
16
  """Get user's approximate latitude and longitude."""
@@ -120,54 +135,112 @@ def weather_icon(code):
120
  # -----------------------------
121
  # PLOT TOOL (ICONS + BANDS)
122
  # -----------------------------
123
-
124
  @tool
125
  def plot_weather(weather_data: list) -> str:
126
- """Generate stylized weather visualization with icons and uncertainty bands.
127
 
128
  Args:
129
- weather_data: A list of dictionaries containing hourly weather data.
 
 
130
 
131
  Returns:
132
- A string file path to the generated image.
133
  """
134
-
135
- times = [entry["time"][11:16] for entry in weather_data]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  temps = [entry["temperature"] for entry in weather_data]
137
- temp_min = [entry["temp_min"] for entry in weather_data]
138
- temp_max = [entry["temp_max"] for entry in weather_data]
139
- precip = [entry["precipitation"] for entry in weather_data]
140
- codes = [entry["weathercode"] for entry in weather_data]
141
-
142
- icons = [weather_icon(c) for c in codes]
143
-
144
- plt.figure(figsize=(12, 6))
145
-
146
- # Temperature line
147
- plt.plot(times, temps, label="Temperature")
148
-
149
- # Uncertainty band
150
- plt.fill_between(times, temp_min, temp_max, alpha=0.2)
151
-
152
- # Precip bars
153
- plt.bar(times, precip, alpha=0.3, label="Precipitation")
154
-
155
- # Add icons above line
156
- for i, icon in enumerate(icons):
157
- plt.text(i, temps[i] + 1.5, icon, ha='center')
158
-
159
- plt.xticks(rotation=45)
160
- plt.title("24-Hour Weather Forecast")
161
- plt.legend()
162
- plt.tight_layout()
163
-
164
- file_path = "/mnt/data/weather_visual.png"
165
- plt.savefig(file_path)
166
- plt.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
  return file_path
169
 
170
 
 
171
  # -----------------------------
172
  # AGENT SETUP
173
  # -----------------------------
 
11
  # LOCATION + TIMEZONE TOOLS
12
  # -----------------------------
13
 
14
+ @tool
15
+ def get_current_time_in_timezone(timezone: str) -> str:
16
+ """A tool that fetches the current local time in a specified timezone.
17
+ Args:
18
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
19
+ """
20
+ try:
21
+ # Create timezone object
22
+ tz = pytz.timezone(timezone)
23
+ # Get current time in that timezone
24
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
25
+ return f"The current local time in {timezone} is: {local_time}"
26
+ except Exception as e:
27
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
28
+
29
  @tool
30
  def get_location() -> dict:
31
  """Get user's approximate latitude and longitude."""
 
135
  # -----------------------------
136
  # PLOT TOOL (ICONS + BANDS)
137
  # -----------------------------
 
138
  @tool
139
  def plot_weather(weather_data: list) -> str:
140
+ """Generate an SVG weather visualization with icons and uncertainty bands.
141
 
142
  Args:
143
+ weather_data: A list of dictionaries containing hourly weather data. Each dict
144
+ should have 'time', 'temperature', 'temp_min', 'temp_max',
145
+ 'precipitation', and 'weathercode'.
146
 
147
  Returns:
148
+ A string file path to the generated SVG image.
149
  """
150
+ import datetime
151
+ from xml.etree.ElementTree import Element, SubElement, ElementTree, tostring
152
+
153
+ # Helper: map weather code to icon
154
+ def weather_icon(code):
155
+ if code == 0:
156
+ return "☀️"
157
+ elif code in [1,2,3]:
158
+ return "⛅"
159
+ elif code in [45,48]:
160
+ return "🌫"
161
+ elif code in [51,53,55,61,63,65]:
162
+ return "🌧"
163
+ elif code in [71,73,75]:
164
+ return "❄️"
165
+ elif code in [95]:
166
+ return "⛈"
167
+ else:
168
+ return "🌈"
169
+
170
+ # SVG canvas size
171
+ width = 1200
172
+ height = 400
173
+ margin = 50
174
+
175
+ # Compute min/max for scaling
176
  temps = [entry["temperature"] for entry in weather_data]
177
+ temp_min_vals = [entry["temp_min"] for entry in weather_data]
178
+ temp_max_vals = [entry["temp_max"] for entry in weather_data]
179
+ overall_min = min(temp_min_vals) - 2
180
+ overall_max = max(temp_max_vals) + 2
181
+
182
+ n = len(weather_data)
183
+ step_x = (width - 2*margin) / (n-1) if n > 1 else 1
184
+
185
+ def y_scale(temp):
186
+ # invert y axis (SVG 0,0 is top-left)
187
+ return height - margin - (temp - overall_min)/(overall_max - overall_min)*(height - 2*margin)
188
+
189
+ # Create root SVG
190
+ svg = Element('svg', width=str(width), height=str(height), xmlns="http://www.w3.org/2000/svg")
191
+
192
+ # Draw uncertainty band
193
+ points = []
194
+ for i, entry in enumerate(weather_data):
195
+ x = margin + i*step_x
196
+ y = y_scale(entry["temp_max"])
197
+ points.append(f"{x},{y}")
198
+ for i, entry in reversed(list(enumerate(weather_data))):
199
+ x = margin + i*step_x
200
+ y = y_scale(entry["temp_min"])
201
+ points.append(f"{x},{y}")
202
+ band = SubElement(svg, "polygon", points=" ".join(points), fill="#a0c4ff", opacity="0.3")
203
+
204
+ # Draw temperature line
205
+ temp_line_points = []
206
+ for i, entry in enumerate(weather_data):
207
+ x = margin + i*step_x
208
+ y = y_scale(entry["temperature"])
209
+ temp_line_points.append(f"{x},{y}")
210
+ SubElement(svg, "polyline", points=" ".join(temp_line_points),
211
+ fill="none", stroke="#0077b6", stroke_width="2")
212
+
213
+ # Draw precipitation bars
214
+ max_precip = max([entry["precipitation"] for entry in weather_data] + [1])
215
+ for i, entry in enumerate(weather_data):
216
+ x = margin + i*step_x - step_x*0.2
217
+ y = y_scale(overall_min) - (entry["precipitation"]/max_precip)*(height - 2*margin)
218
+ bar_height = (entry["precipitation"]/max_precip)*(height - 2*margin)
219
+ SubElement(svg, "rect", x=str(x), y=str(y), width=str(step_x*0.4),
220
+ height=str(bar_height), fill="#00b4d8", opacity="0.4")
221
+
222
+ # Draw icons and x-axis labels
223
+ for i, entry in enumerate(weather_data):
224
+ x = margin + i*step_x
225
+ y_temp = y_scale(entry["temperature"])
226
+ # icon above temp
227
+ text = SubElement(svg, "text", x=str(x), y=str(y_temp - 10),
228
+ font_size="20", text_anchor="middle")
229
+ text.text = weather_icon(entry["weathercode"])
230
+ # time label below
231
+ t_label = SubElement(svg, "text", x=str(x), y=str(height - margin + 20),
232
+ font_size="12", text_anchor="middle")
233
+ t_label.text = entry["time"][11:16]
234
+
235
+ # Save SVG
236
+ file_path = "/mnt/data/weather_visual.svg"
237
+ tree = ElementTree(svg)
238
+ tree.write(file_path)
239
 
240
  return file_path
241
 
242
 
243
+
244
  # -----------------------------
245
  # AGENT SETUP
246
  # -----------------------------