Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import requests
|
|
2 |
import structlog
|
3 |
import openai
|
4 |
import os
|
|
|
5 |
import random
|
6 |
import tiktoken
|
7 |
import enum
|
@@ -96,19 +97,15 @@ class Weather:
|
|
96 |
return requests.get(url, headers=headers).json()
|
97 |
|
98 |
@cachetools.func.ttl_cache(maxsize=128, ttl=15*60)
|
99 |
-
def get_info(self):
|
100 |
weather = self.get_weather()
|
101 |
-
curr_hour = None
|
102 |
next_hour = None
|
103 |
for hour_data in weather['forecast']['forecastday'][0]["hour"]:
|
104 |
if abs(hour_data["time_epoch"] - time.time()) < 60 * 60:
|
105 |
-
if curr_hour is None: curr_hour = hour_data
|
106 |
next_hour = hour_data
|
107 |
return {
|
108 |
"now": weather["current"],
|
109 |
"day": weather["forecast"]["forecastday"][0]["day"],
|
110 |
-
"
|
111 |
-
"next_hour": next_hour,
|
112 |
}
|
113 |
|
114 |
|
@@ -218,6 +215,47 @@ Do not include specific numbers.'''.replace('\n', ' '))
|
|
218 |
# return overlay_text_on_image(img, text, 'bottom-left')
|
219 |
return img, txt
|
220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
def step(self, zip_code='10001', **kwargs):
|
222 |
forecast = Weather(zip_code).get_info()
|
223 |
images, texts = [], []
|
@@ -229,8 +267,9 @@ Do not include specific numbers.'''.replace('\n', ' '))
|
|
229 |
img, txt = r.result()
|
230 |
time, data = runs[r]
|
231 |
images.append(overlay_text_on_image(img, time, 'top-right', decode=True))
|
|
|
232 |
texts.append(txt)
|
233 |
-
return create_collage(*images), *texts
|
234 |
|
235 |
|
236 |
# Define Gradio interface
|
|
|
2 |
import structlog
|
3 |
import openai
|
4 |
import os
|
5 |
+
import io
|
6 |
import random
|
7 |
import tiktoken
|
8 |
import enum
|
|
|
97 |
return requests.get(url, headers=headers).json()
|
98 |
|
99 |
@cachetools.func.ttl_cache(maxsize=128, ttl=15*60)
|
|
|
100 |
weather = self.get_weather()
|
|
|
101 |
next_hour = None
|
102 |
for hour_data in weather['forecast']['forecastday'][0]["hour"]:
|
103 |
if abs(hour_data["time_epoch"] - time.time()) < 60 * 60:
|
|
|
104 |
next_hour = hour_data
|
105 |
return {
|
106 |
"now": weather["current"],
|
107 |
"day": weather["forecast"]["forecastday"][0]["day"],
|
108 |
+
"hour": next_hour,
|
|
|
109 |
}
|
110 |
|
111 |
|
|
|
215 |
# return overlay_text_on_image(img, text, 'bottom-left')
|
216 |
return img, txt
|
217 |
|
218 |
+
def weather_img(self, weather_data):
|
219 |
+
import io
|
220 |
+
# Create a new image with white background
|
221 |
+
image = PIL.Image.new('RGB', (256, 256), (255, 255, 255))
|
222 |
+
draw = PIL.ImageDraw.Draw(image)
|
223 |
+
|
224 |
+
# Load a font
|
225 |
+
font = PIL.ImageFont.truetype(font_path, 12)
|
226 |
+
|
227 |
+
# Draw text on the image
|
228 |
+
y_text = 5
|
229 |
+
items_to_display = {
|
230 |
+
'Now': {'Temperature': weather_data['now']['temp_f'],
|
231 |
+
'Condition': weather_data['now']['condition']['text'],
|
232 |
+
'AQI': max(v for c, v in weather_data['now']['air_quality'].items() if c != 'co')},
|
233 |
+
'Hour': {'Condition': weather_data['hour']['condition']['text']},
|
234 |
+
'Day': {'High': weather_data['day']['maxtemp_f'],
|
235 |
+
'Low': weather_data['day']['mintemp_f'],
|
236 |
+
'Condition': weather_data['day']['condition']['text']},
|
237 |
+
}
|
238 |
+
|
239 |
+
for category, values in items_to_display.items():
|
240 |
+
draw.text((5, y_text), category, font=font, fill=(0, 0, 0))
|
241 |
+
y_text += 15
|
242 |
+
for key, value in values.items():
|
243 |
+
text = f"{key}: {value}"
|
244 |
+
draw.text((10, y_text), text, font=font, fill=(0, 0, 0))
|
245 |
+
y_text += 15
|
246 |
+
|
247 |
+
# Download the weather condition icon for now, day and next hour
|
248 |
+
for index, time in enumerate(items_to_display.keys()):
|
249 |
+
icon_url = "https:" + weather_data[time.lower()]['condition']['icon']
|
250 |
+
response = requests.get(icon_url)
|
251 |
+
icon = PIL.Image.open(io.BytesIO(response.content))
|
252 |
+
# Resize the icon
|
253 |
+
icon = icon.resize((60, 60))
|
254 |
+
# Paste the icon on the image
|
255 |
+
image.paste(icon, (180, index*80))
|
256 |
+
|
257 |
+
return image
|
258 |
+
|
259 |
def step(self, zip_code='10001', **kwargs):
|
260 |
forecast = Weather(zip_code).get_info()
|
261 |
images, texts = [], []
|
|
|
267 |
img, txt = r.result()
|
268 |
time, data = runs[r]
|
269 |
images.append(overlay_text_on_image(img, time, 'top-right', decode=True))
|
270 |
+
# images.append(overlay_text_on_image(img, '', 'top-right', decode=True))
|
271 |
texts.append(txt)
|
272 |
+
return create_collage(*images, self.weather_img(forecast)), *texts
|
273 |
|
274 |
|
275 |
# Define Gradio interface
|