Pijush2023 commited on
Commit
3eee156
·
verified ·
1 Parent(s): 62768a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +174 -170
app.py CHANGED
@@ -60,6 +60,180 @@ conversational_memory = ConversationBufferWindowMemory(
60
  return_messages=True
61
  )
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  # Define prompt templates
64
  # template1 = """You are an expert concierge who is helpful and a renowned guide for Omaha, Nebraska. Based on today's weather being a sunny bright day and the date is 18th June 2024, use the following pieces of context,
65
  # memory, and message history, along with your knowledge of perennial events in Omaha, Nebraska, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
@@ -178,12 +352,7 @@ def bot(history, choice):
178
 
179
  from datetime import datetime
180
 
181
- def get_current_time_and_date():
182
- now = datetime.now()
183
- return now.strftime("%Y-%m-%d %H:%M:%S")
184
 
185
- # Example usage
186
- current_time_and_date = get_current_time_and_date()
187
 
188
 
189
  def add_message(history, message):
@@ -308,171 +477,6 @@ def fetch_local_news():
308
  else:
309
  return "<p>Failed to fetch local news</p>"
310
 
311
- def fetch_local_events():
312
- api_key = os.environ['SERP_API']
313
- url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Omaha&hl=en&gl=us&api_key={api_key}'
314
-
315
- response = requests.get(url)
316
- if response.status_code == 200:
317
- events_results = response.json().get("events_results", [])
318
- events_html = """
319
- <h2 style="font-family: 'Georgia', serif; color: #4CAF50; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Events</h2>
320
- <style>
321
- .event-item {
322
- font-family: 'Verdana', sans-serif;
323
- color: #333;
324
- background-color: #f0f8ff;
325
- margin-bottom: 15px;
326
- padding: 10px;
327
- border: 1px solid #ddd;
328
- border-radius: 5px;
329
- transition: box-shadow 0.3s ease, background-color 0.3s ease;
330
- font-weight: bold;
331
- }
332
- .event-item:hover {
333
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
334
- background-color: #e6f7ff;
335
- }
336
- .event-item a {
337
- color: #1E90FF;
338
- text-decoration: none;
339
- font-weight: bold;
340
- }
341
- .event-item a:hover {
342
- text-decoration: underline;
343
- }
344
- .event-preview {
345
- position: absolute;
346
- display: none;
347
- border: 1px solid #ccc;
348
- border-radius: 5px;
349
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
350
- background-color: white;
351
- z-index: 1000;
352
- max-width: 300px;
353
- padding: 10px;
354
- font-family: 'Verdana', sans-serif;
355
- color: #333;
356
- }
357
- </style>
358
- <script>
359
- function showPreview(event, previewContent) {
360
- var previewBox = document.getElementById('event-preview');
361
- previewBox.innerHTML = previewContent;
362
- previewBox.style.left = event.pageX + 'px';
363
- previewBox.style.top = event.pageY + 'px';
364
- previewBox.style.display = 'block';
365
- }
366
- function hidePreview() {
367
- var previewBox = document.getElementById('event-preview');
368
- previewBox.style.display = 'none';
369
- }
370
- </script>
371
- <div id="event-preview" class="event-preview"></div>
372
- """
373
- for index, event in enumerate(events_results):
374
- title = event.get("title", "No title")
375
- date = event.get("date", "No date")
376
- location = event.get("address", "No location")
377
- link = event.get("link", "#")
378
- events_html += f"""
379
- <div class="event-item" onmouseover="showPreview(event, 'Date: {date}<br>Location: {location}')" onmouseout="hidePreview()">
380
- <a href='{link}' target='_blank'>{index + 1}. {title}</a>
381
- <p>Date: {date}<br>Location: {location}</p>
382
- </div>
383
- """
384
- return events_html
385
- else:
386
- return "<p>Failed to fetch local events</p>"
387
-
388
- def fetch_local_weather():
389
- try:
390
- api_key = os.environ['WEATHER_API']
391
- url = f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/omaha?unitGroup=metric&include=events%2Calerts%2Chours%2Cdays%2Ccurrent&key={api_key}'
392
- response = requests.get(url)
393
- response.raise_for_status()
394
- jsonData = response.json()
395
-
396
- current_conditions = jsonData.get("currentConditions", {})
397
- temp_celsius = current_conditions.get("temp", "N/A")
398
- if temp_celsius != "N/A":
399
- temp_fahrenheit = (temp_celsius * 9/5) + 32
400
- else:
401
- temp_fahrenheit = "N/A"
402
- condition = current_conditions.get("conditions", "N/A")
403
- humidity = current_conditions.get("humidity", "N/A")
404
-
405
- weather_html = f"""
406
- <div class="weather-theme">
407
- <h2 style="font-family: 'Georgia', serif; color: #4CAF50; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Weather</h2>
408
- <div class="weather-content">
409
- <div class="weather-icon">
410
- <img src="https://www.weatherbit.io/static/img/icons/{get_weather_icon(condition)}.png" alt="{condition}" style="width: 100px; height: 100px;">
411
- </div>
412
- <div class="weather-details">
413
- <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Temperature: {temp_fahrenheit}°F</p>
414
- <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Condition: {condition}</p>
415
- <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Humidity: {humidity}%</p>
416
- </div>
417
- </div>
418
- </div>
419
- <style>
420
- .weather-theme {{
421
- animation: backgroundAnimation 10s infinite alternate;
422
- border: 1px solid #ddd;
423
- border-radius: 10px;
424
- padding: 10px;
425
- margin-bottom: 15px;
426
- background: linear-gradient(45deg, #ffcc33, #ff6666, #ffcc33, #ff6666);
427
- background-size: 400% 400%;
428
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
429
- transition: box-shadow 0.3s ease, background-color 0.3s ease;
430
- }}
431
- .weather-theme:hover {{
432
- box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
433
- background-position: 100% 100%;
434
- }}
435
- @keyframes backgroundAnimation {{
436
- 0% {{ background-position: 0% 50%; }}
437
- 100% {{ background-position: 100% 50%; }}
438
- }}
439
- .weather-content {{
440
- display: flex;
441
- align-items: center;
442
- }}
443
- .weather-icon {{
444
- flex: 1;
445
- }}
446
- .weather-details {{
447
- flex: 3;
448
- }}
449
- </style>
450
- """
451
- return weather_html
452
- except requests.exceptions.RequestException as e:
453
- return f"<p>Failed to fetch local weather: {e}</p>"
454
-
455
- def get_weather_icon(condition):
456
- condition_map = {
457
- "Clear": "c01d",
458
- "Partly Cloudy": "c02d",
459
- "Cloudy": "c03d",
460
- "Overcast": "c04d",
461
- "Mist": "a01d",
462
- "Patchy rain possible": "r01d",
463
- "Light rain": "r02d",
464
- "Moderate rain": "r03d",
465
- "Heavy rain": "r04d",
466
- "Snow": "s01d",
467
- "Thunderstorm": "t01d",
468
- "Fog": "a05d",
469
- }
470
- return condition_map.get(condition, "c04d")
471
-
472
- # Update prompt templates to include fetched details
473
- weather_details = fetch_local_weather()
474
- current_time_and_date = get_current_time_and_date()
475
- local_events_details = fetch_local_events()
476
 
477
  # Voice Control
478
  import numpy as np
 
60
  return_messages=True
61
  )
62
 
63
+ def get_current_time_and_date():
64
+ now = datetime.now()
65
+ return now.strftime("%Y-%m-%d %H:%M:%S")
66
+
67
+ # Example usage
68
+ current_time_and_date = get_current_time_and_date()
69
+
70
+ def fetch_local_events():
71
+ api_key = os.environ['SERP_API']
72
+ url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Omaha&hl=en&gl=us&api_key={api_key}'
73
+
74
+ response = requests.get(url)
75
+ if response.status_code == 200:
76
+ events_results = response.json().get("events_results", [])
77
+ events_html = """
78
+ <h2 style="font-family: 'Georgia', serif; color: #4CAF50; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Events</h2>
79
+ <style>
80
+ .event-item {
81
+ font-family: 'Verdana', sans-serif;
82
+ color: #333;
83
+ background-color: #f0f8ff;
84
+ margin-bottom: 15px;
85
+ padding: 10px;
86
+ border: 1px solid #ddd;
87
+ border-radius: 5px;
88
+ transition: box-shadow 0.3s ease, background-color 0.3s ease;
89
+ font-weight: bold;
90
+ }
91
+ .event-item:hover {
92
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
93
+ background-color: #e6f7ff;
94
+ }
95
+ .event-item a {
96
+ color: #1E90FF;
97
+ text-decoration: none;
98
+ font-weight: bold;
99
+ }
100
+ .event-item a:hover {
101
+ text-decoration: underline;
102
+ }
103
+ .event-preview {
104
+ position: absolute;
105
+ display: none;
106
+ border: 1px solid #ccc;
107
+ border-radius: 5px;
108
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
109
+ background-color: white;
110
+ z-index: 1000;
111
+ max-width: 300px;
112
+ padding: 10px;
113
+ font-family: 'Verdana', sans-serif;
114
+ color: #333;
115
+ }
116
+ </style>
117
+ <script>
118
+ function showPreview(event, previewContent) {
119
+ var previewBox = document.getElementById('event-preview');
120
+ previewBox.innerHTML = previewContent;
121
+ previewBox.style.left = event.pageX + 'px';
122
+ previewBox.style.top = event.pageY + 'px';
123
+ previewBox.style.display = 'block';
124
+ }
125
+ function hidePreview() {
126
+ var previewBox = document.getElementById('event-preview');
127
+ previewBox.style.display = 'none';
128
+ }
129
+ </script>
130
+ <div id="event-preview" class="event-preview"></div>
131
+ """
132
+ for index, event in enumerate(events_results):
133
+ title = event.get("title", "No title")
134
+ date = event.get("date", "No date")
135
+ location = event.get("address", "No location")
136
+ link = event.get("link", "#")
137
+ events_html += f"""
138
+ <div class="event-item" onmouseover="showPreview(event, 'Date: {date}<br>Location: {location}')" onmouseout="hidePreview()">
139
+ <a href='{link}' target='_blank'>{index + 1}. {title}</a>
140
+ <p>Date: {date}<br>Location: {location}</p>
141
+ </div>
142
+ """
143
+ return events_html
144
+ else:
145
+ return "<p>Failed to fetch local events</p>"
146
+
147
+ def fetch_local_weather():
148
+ try:
149
+ api_key = os.environ['WEATHER_API']
150
+ url = f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/omaha?unitGroup=metric&include=events%2Calerts%2Chours%2Cdays%2Ccurrent&key={api_key}'
151
+ response = requests.get(url)
152
+ response.raise_for_status()
153
+ jsonData = response.json()
154
+
155
+ current_conditions = jsonData.get("currentConditions", {})
156
+ temp_celsius = current_conditions.get("temp", "N/A")
157
+ if temp_celsius != "N/A":
158
+ temp_fahrenheit = (temp_celsius * 9/5) + 32
159
+ else:
160
+ temp_fahrenheit = "N/A"
161
+ condition = current_conditions.get("conditions", "N/A")
162
+ humidity = current_conditions.get("humidity", "N/A")
163
+
164
+ weather_html = f"""
165
+ <div class="weather-theme">
166
+ <h2 style="font-family: 'Georgia', serif; color: #4CAF50; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Weather</h2>
167
+ <div class="weather-content">
168
+ <div class="weather-icon">
169
+ <img src="https://www.weatherbit.io/static/img/icons/{get_weather_icon(condition)}.png" alt="{condition}" style="width: 100px; height: 100px;">
170
+ </div>
171
+ <div class="weather-details">
172
+ <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Temperature: {temp_fahrenheit}°F</p>
173
+ <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Condition: {condition}</p>
174
+ <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Humidity: {humidity}%</p>
175
+ </div>
176
+ </div>
177
+ </div>
178
+ <style>
179
+ .weather-theme {{
180
+ animation: backgroundAnimation 10s infinite alternate;
181
+ border: 1px solid #ddd;
182
+ border-radius: 10px;
183
+ padding: 10px;
184
+ margin-bottom: 15px;
185
+ background: linear-gradient(45deg, #ffcc33, #ff6666, #ffcc33, #ff6666);
186
+ background-size: 400% 400%;
187
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
188
+ transition: box-shadow 0.3s ease, background-color 0.3s ease;
189
+ }}
190
+ .weather-theme:hover {{
191
+ box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
192
+ background-position: 100% 100%;
193
+ }}
194
+ @keyframes backgroundAnimation {{
195
+ 0% {{ background-position: 0% 50%; }}
196
+ 100% {{ background-position: 100% 50%; }}
197
+ }}
198
+ .weather-content {{
199
+ display: flex;
200
+ align-items: center;
201
+ }}
202
+ .weather-icon {{
203
+ flex: 1;
204
+ }}
205
+ .weather-details {{
206
+ flex: 3;
207
+ }}
208
+ </style>
209
+ """
210
+ return weather_html
211
+ except requests.exceptions.RequestException as e:
212
+ return f"<p>Failed to fetch local weather: {e}</p>"
213
+
214
+ def get_weather_icon(condition):
215
+ condition_map = {
216
+ "Clear": "c01d",
217
+ "Partly Cloudy": "c02d",
218
+ "Cloudy": "c03d",
219
+ "Overcast": "c04d",
220
+ "Mist": "a01d",
221
+ "Patchy rain possible": "r01d",
222
+ "Light rain": "r02d",
223
+ "Moderate rain": "r03d",
224
+ "Heavy rain": "r04d",
225
+ "Snow": "s01d",
226
+ "Thunderstorm": "t01d",
227
+ "Fog": "a05d",
228
+ }
229
+ return condition_map.get(condition, "c04d")
230
+
231
+ # Update prompt templates to include fetched details
232
+ weather_details = fetch_local_weather()
233
+ current_time_and_date = get_current_time_and_date()
234
+ local_events_details = fetch_local_events()
235
+
236
+
237
  # Define prompt templates
238
  # template1 = """You are an expert concierge who is helpful and a renowned guide for Omaha, Nebraska. Based on today's weather being a sunny bright day and the date is 18th June 2024, use the following pieces of context,
239
  # memory, and message history, along with your knowledge of perennial events in Omaha, Nebraska, to answer the question at the end. If you don't know the answer, just say "Homie, I need to get more data for this," and don't try to make up an answer.
 
352
 
353
  from datetime import datetime
354
 
 
 
 
355
 
 
 
356
 
357
 
358
  def add_message(history, message):
 
477
  else:
478
  return "<p>Failed to fetch local news</p>"
479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
 
481
  # Voice Control
482
  import numpy as np