abdullahzunorain
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,177 +1,102 @@
|
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
import streamlit as st
|
3 |
-
import
|
4 |
|
5 |
# Access API keys from Streamlit secrets
|
6 |
openweather_api_key = st.secrets["weather_api_key"]
|
7 |
groq_api_key = st.secrets["groq_api_key"]
|
8 |
|
9 |
-
# Function to get user's IP-based location
|
10 |
-
def get_location_by_ip():
|
11 |
-
try:
|
12 |
-
response = requests.get("https://ipinfo.io")
|
13 |
-
location_data = response.json()
|
14 |
-
return location_data['city']
|
15 |
-
except Exception as e:
|
16 |
-
st.warning("Could not auto-detect location.")
|
17 |
-
return None
|
18 |
-
|
19 |
# Function to get weather data from OpenWeatherMap
|
20 |
def get_weather_data(city):
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
24 |
return response.json()
|
25 |
-
|
26 |
-
st.error("
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
return None
|
45 |
|
46 |
-
#
|
47 |
st.title("Weather-Based Outfit Suggestion App")
|
48 |
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
|
52 |
if city:
|
53 |
weather_data = get_weather_data(city)
|
54 |
-
if weather_data:
|
55 |
-
weather_condition = weather_data["weather"][0]["main"]
|
56 |
-
temperature = weather_data["main"]["temp"]
|
57 |
-
|
58 |
-
st.subheader(f"Weather in {city}")
|
59 |
-
st.write(f"Condition: {weather_condition}")
|
60 |
-
st.write(f"Temperature: {temperature}°C")
|
61 |
-
|
62 |
-
# Get outfit suggestion based on weather
|
63 |
-
outfit_suggestion = get_outfit_suggestion(weather_condition)
|
64 |
-
if outfit_suggestion:
|
65 |
-
st.subheader("Outfit Suggestion")
|
66 |
-
st.write(outfit_suggestion)
|
67 |
-
else:
|
68 |
-
st.warning("No outfit suggestion available for this weather condition.")
|
69 |
-
else:
|
70 |
-
st.info("Please enter your location to get weather and outfit suggestions.")
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
# import requests
|
81 |
-
# import streamlit as st
|
82 |
-
# import groq
|
83 |
-
|
84 |
-
# # Access API keys from Streamlit secrets
|
85 |
-
# openweather_api_key = st.secrets["weather_api_key"]
|
86 |
-
# groq_api_key = st.secrets["groq_api_key"]
|
87 |
-
|
88 |
-
# # Function to get weather data from OpenWeatherMap
|
89 |
-
# def get_weather_data(city):
|
90 |
-
# api_key = openweather_api_key # Use the secret API key
|
91 |
-
# url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
92 |
-
# try:
|
93 |
-
# response = requests.get(url)
|
94 |
-
# response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
|
95 |
-
# return response.json()
|
96 |
-
# except requests.exceptions.HTTPError as err:
|
97 |
-
# st.error(f"HTTP error occurred: {err}")
|
98 |
-
# except Exception as err:
|
99 |
-
# st.error(f"An error occurred: {err}")
|
100 |
-
# return None
|
101 |
-
|
102 |
-
# # Function to parse weather data
|
103 |
-
# def parse_weather_data(weather_data):
|
104 |
-
# temperature = weather_data["main"]["temp"]
|
105 |
-
# weather_description = weather_data["weather"][0]["description"]
|
106 |
-
# return temperature, weather_description
|
107 |
-
|
108 |
-
# # Function to get outfit suggestion using Groq's LLaMA model
|
109 |
-
# def get_outfit_suggestion(temperature, description, style, fabric):
|
110 |
-
# # Initialize Groq's API
|
111 |
-
# try:
|
112 |
-
# client = groq.Groq(api_key=groq_api_key) # Use the secret API key
|
113 |
-
|
114 |
-
# prompt = f"The current weather is {description} with a temperature of {temperature}°C. Suggest an outfit. The user prefers a {style} style and {fabric} fabric."
|
115 |
-
|
116 |
-
# # Use Groq's chat completion to get the text response
|
117 |
-
# response = client.chat.completions.create(
|
118 |
-
# messages=[{"role": "user", "content": prompt}],
|
119 |
-
# model="llama3-8b-8192", # Change to a valid Groq model if necessary
|
120 |
-
# )
|
121 |
-
# return response.choices[0].message.content.strip()
|
122 |
-
# except Exception as e:
|
123 |
-
# st.error(f"Error using Groq API: {e}")
|
124 |
-
# return None
|
125 |
-
|
126 |
-
# # Streamlit UI for user input
|
127 |
-
# st.title("Weather-Based Outfit Suggestion App")
|
128 |
-
|
129 |
-
# city = st.text_input("Enter your location:")
|
130 |
-
|
131 |
-
# # Add style and fabric input options
|
132 |
-
# style = st.selectbox("Select your preferred style", ["Casual", "Formal", "Sporty", "Business", "Chic"])
|
133 |
-
# fabric = st.selectbox("Select your preferred fabric", ["Cotton", "Linen", "Wool", "Polyester", "Silk", "Leather"])
|
134 |
-
|
135 |
-
# if city:
|
136 |
-
# weather_data = get_weather_data(city)
|
137 |
|
138 |
-
|
139 |
-
|
140 |
|
141 |
-
#
|
142 |
-
|
143 |
-
|
144 |
|
145 |
-
#
|
146 |
-
|
147 |
|
148 |
-
|
149 |
-
#
|
150 |
-
|
151 |
-
|
152 |
|
153 |
-
#
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
|
160 |
-
#
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
|
176 |
|
177 |
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
import requests
|
6 |
import streamlit as st
|
7 |
+
import groq
|
8 |
|
9 |
# Access API keys from Streamlit secrets
|
10 |
openweather_api_key = st.secrets["weather_api_key"]
|
11 |
groq_api_key = st.secrets["groq_api_key"]
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Function to get weather data from OpenWeatherMap
|
14 |
def get_weather_data(city):
|
15 |
+
api_key = openweather_api_key # Use the secret API key
|
16 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
17 |
+
try:
|
18 |
+
response = requests.get(url)
|
19 |
+
response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
|
20 |
return response.json()
|
21 |
+
except requests.exceptions.HTTPError as err:
|
22 |
+
st.error(f"HTTP error occurred: {err}")
|
23 |
+
except Exception as err:
|
24 |
+
st.error(f"An error occurred: {err}")
|
25 |
+
return None
|
26 |
+
|
27 |
+
# Function to parse weather data
|
28 |
+
def parse_weather_data(weather_data):
|
29 |
+
temperature = weather_data["main"]["temp"]
|
30 |
+
weather_description = weather_data["weather"][0]["description"]
|
31 |
+
return temperature, weather_description
|
32 |
+
|
33 |
+
# Function to get outfit suggestion using Groq's LLaMA model
|
34 |
+
def get_outfit_suggestion(temperature, description, style, fabric):
|
35 |
+
# Initialize Groq's API
|
36 |
+
try:
|
37 |
+
client = groq.Groq(api_key=groq_api_key) # Use the secret API key
|
38 |
+
|
39 |
+
prompt = f"The current weather is {description} with a temperature of {temperature}°C. Suggest an outfit. The user prefers a {style} style and {fabric} fabric."
|
40 |
+
|
41 |
+
# Use Groq's chat completion to get the text response
|
42 |
+
response = client.chat.completions.create(
|
43 |
+
messages=[{"role": "user", "content": prompt}],
|
44 |
+
model="llama3-8b-8192", # Change to a valid Groq model if necessary
|
45 |
+
)
|
46 |
+
return response.choices[0].message.content.strip()
|
47 |
+
except Exception as e:
|
48 |
+
st.error(f"Error using Groq API: {e}")
|
49 |
return None
|
50 |
|
51 |
+
# Streamlit UI for user input
|
52 |
st.title("Weather-Based Outfit Suggestion App")
|
53 |
|
54 |
+
city = st.text_input("Enter your location:")
|
55 |
+
|
56 |
+
# Add style and fabric input options
|
57 |
+
style = st.selectbox("Select your preferred style", ["Casual", "Formal", "Sporty", "Business", "Chic"])
|
58 |
+
fabric = st.selectbox("Select your preferred fabric", ["Cotton", "Linen", "Wool", "Polyester", "Silk", "Leather"])
|
59 |
|
60 |
if city:
|
61 |
weather_data = get_weather_data(city)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
if weather_data and weather_data["cod"] == 200:
|
64 |
+
temperature, description = parse_weather_data(weather_data)
|
65 |
|
66 |
+
# Display current weather info
|
67 |
+
st.write(f"Current temperature in {city}: {temperature}°C")
|
68 |
+
st.write(f"Weather: {description}")
|
69 |
|
70 |
+
# Get outfit suggestion based on user preferences
|
71 |
+
outfit_suggestion = get_outfit_suggestion(temperature, description, style, fabric)
|
72 |
|
73 |
+
if outfit_suggestion:
|
74 |
+
# Display outfit suggestion
|
75 |
+
st.write("Outfit Suggestion:")
|
76 |
+
st.write(outfit_suggestion)
|
77 |
|
78 |
+
# Display weather icon
|
79 |
+
icon_code = weather_data["weather"][0]["icon"]
|
80 |
+
icon_url = f"http://openweathermap.org/img/wn/{icon_code}.png"
|
81 |
+
st.image(icon_url)
|
82 |
+
else:
|
83 |
+
st.write("Could not retrieve weather data. Please check the location.")
|
84 |
|
85 |
+
# Optional: Add CSS for styling
|
86 |
+
st.markdown(
|
87 |
+
"""
|
88 |
+
<style>
|
89 |
+
.reportview-container {
|
90 |
+
background: #f5f5f5;
|
91 |
+
}
|
92 |
+
.stButton>button {
|
93 |
+
background-color: #ff5733;
|
94 |
+
color: white;
|
95 |
+
}
|
96 |
+
</style>
|
97 |
+
""",
|
98 |
+
unsafe_allow_html=True
|
99 |
+
)
|
100 |
|
101 |
|
102 |
|