-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
158 lines (130 loc) · 5.76 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import streamlit as st
import requests
import logging
import emails
from emails.template import JinjaTemplate as T
import openai
import os
import secrets
# Define your OpenWeather API key here
openweather_api_key = st.secrets["OPENWEATHER_API_KEY"]
# List of weather conditions and icons
weather_icons = {
"Clear": "☀️",
"Clouds": "☁️",
"Drizzle": "🌧️",
"Rain": "🌧️",
"Thunderstorm": "⛈️",
"Snow": "❄️",
"Mist": "🌫️",
"Smoke": "🌫️",
"Haze": "🌫️",
"Dust": "🌫️",
"Fog": "🌫️",
"Sand": "🌫️",
"Ash": "🌫️",
"Squall": "🌫️",
"Tornado": "🌪️"
}
def fetch_temperature(location, api_key):
base_url = "https://api.openweathermap.org/data/2.5/weather"
params = {
"q": location,
"appid": api_key,
"units": "metric",
}
try:
response = requests.get(base_url, params=params)
response.raise_for_status()
data = response.json()
if "main" in data and "temp" in data["main"]:
temperature = data["main"]["temp"]
return temperature
else:
return None
except requests.exceptions.RequestException as e:
logging.error(f"Error fetching temperature: {str(e)}")
return None
def send_temperature_alert(user_email, location, current_temperature, threshold, alert_type):
subject = T(f"Temperature {alert_type} Alert")
html_body = T(f"<p>Temperature in {location} is {alert_type} {threshold}°C.</p>"
f"<p>Current temperature: {current_temperature}°C</p>")
message = emails.html(html=html_body, subject=subject, mail_from=("Temperature Alert", "alert@mycompany.com"))
try:
response = message.send(to=(user_email,))
if response.status_code == 250:
return True
except Exception as e:
logging.error(f"Error sending email: {str(e)}")
return False
def fetch_weather_condition(location, api_key):
base_url = "https://api.openweathermap.org/data/2.5/weather"
params = {
"q": location,
"appid": api_key,
"units": "metric",
}
try:
response = requests.get(base_url, params=params)
response.raise_for_status()
data = response.json()
if "weather" in data and len(data["weather"]) > 0:
weather_condition = data["weather"][0]["main"]
return weather_condition
else:
return None
except requests.exceptions.RequestException as e:
logging.error(f"Error fetching weather condition: {str(e)}")
return None
def generate_suggestions(location, current_temperature, min_temp_threshold, max_temp_threshold, weather_condition):
openai.api_key = st.secrets["openai_api_key"]
if min_temp_threshold <= current_temperature <= max_temp_threshold:
prompt = f"Provide suggestions for someone in {location} where it's {current_temperature}°C and {weather_condition}."
elif current_temperature < min_temp_threshold:
prompt = f"Provide suggestions for someone in {location} where it's {current_temperature}°C and {weather_condition}. " \
f"Recommend suitable clothing and precautions for cold weather."
else:
prompt = f"Provide suggestions for someone in {location} where it's {current_temperature}°C and {weather_condition}. " \
f"Recommend suitable clothing and precautions for hot weather."
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=600,
n=1,
stop=None,
temperature=0.7,
)
suggestions = response.choices[0].text
return suggestions
def main():
st.title("Fetch Alerts : Personalized Weather Suggestions App")
user_location = st.text_input("Enter your preferred location (e.g., Paris, France):")
user_email = st.text_input("Enter your email address:")
col1, col2 = st.columns(2)
min_temp_threshold = col1.number_input("Minimum Temp (°C)", value=27)
max_temp_threshold = col2.number_input("Maximum Temp (°C)", value=30)
check_button = st.button("Check Temperature and Weather")
if check_button:
weather_condition = fetch_weather_condition(user_location, openweather_api_key)
if weather_condition is not None:
st.header(f"Results of {user_location} {weather_icons[weather_condition]}")
current_temperature = fetch_temperature(user_location, openweather_api_key)
if current_temperature is not None:
st.write(f"Current temperature: {current_temperature}°C")
if current_temperature < min_temp_threshold:
if user_email:
if send_temperature_alert(user_email, user_location, current_temperature, min_temp_threshold, "Low"):
st.success("Low temperature email alert sent successfully!")
else:
st.error(f"Failed to send a low temperature alert to {user_email}")
elif current_temperature > max_temp_threshold:
if user_email:
if send_temperature_alert(user_email, user_location, current_temperature, max_temp_threshold, "High"):
st.success("High temperature email alert sent successfully!")
else:
st.error(f"Failed to send a high temperature alert to {user_email}")
suggestions = generate_suggestions(user_location, current_temperature, min_temp_threshold, max_temp_threshold, weather_condition)
st.subheader("Personalized Suggestions:")
st.write(suggestions)
if __name__ == "__main__":
main()