abdullahzunorain commited on
Commit
2bd6fe6
·
verified ·
1 Parent(s): 747d197

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -73
app.py CHANGED
@@ -1,102 +1,181 @@
1
  import requests
2
  import streamlit as st
3
- import groq
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 weather data from OpenWeatherMap
10
  def get_weather_data(city):
11
- api_key = openweather_api_key # Use the secret API key
12
- url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
13
- try:
14
- response = requests.get(url)
15
- response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
16
  return response.json()
17
- except requests.exceptions.HTTPError as err:
18
- st.error(f"HTTP error occurred: {err}")
19
- except Exception as err:
20
- st.error(f"An error occurred: {err}")
21
- return None
22
-
23
- # Function to parse weather data
24
- def parse_weather_data(weather_data):
25
- temperature = weather_data["main"]["temp"]
26
- weather_description = weather_data["weather"][0]["description"]
27
- return temperature, weather_description
28
-
29
- # Function to get outfit suggestion using Groq's LLaMA model
30
- def get_outfit_suggestion(temperature, description, style, fabric):
31
- # Initialize Groq's API
32
- try:
33
- client = groq.Groq(api_key=groq_api_key) # Use the secret API key
34
-
35
- 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."
36
-
37
- # Use Groq's chat completion to get the text response
38
- response = client.chat.completions.create(
39
- messages=[{"role": "user", "content": prompt}],
40
- model="llama3-8b-8192", # Change to a valid Groq model if necessary
41
- )
42
- return response.choices[0].message.content.strip()
43
- except Exception as e:
44
- st.error(f"Error using Groq API: {e}")
45
  return None
46
 
47
- # Streamlit UI for user input
48
- st.title("Weather-Based Outfit Suggestion App")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- city = st.text_input("Enter your location:")
 
51
 
52
- # Add style and fabric input options
53
- style = st.selectbox("Select your preferred style", ["Casual", "Formal", "Sporty", "Business", "Chic"])
54
- fabric = st.selectbox("Select your preferred fabric", ["Cotton", "Linen", "Wool", "Polyester", "Silk", "Leather"])
55
 
56
  if city:
57
  weather_data = get_weather_data(city)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- if weather_data and weather_data["cod"] == 200:
60
- temperature, description = parse_weather_data(weather_data)
61
 
62
- # Display current weather info
63
- st.write(f"Current temperature in {city}: {temperature}°C")
64
- st.write(f"Weather: {description}")
65
 
66
- # Get outfit suggestion based on user preferences
67
- outfit_suggestion = get_outfit_suggestion(temperature, description, style, fabric)
68
 
69
- if outfit_suggestion:
70
- # Display outfit suggestion
71
- st.write("Outfit Suggestion:")
72
- st.write(outfit_suggestion)
73
 
74
- # Display weather icon
75
- icon_code = weather_data["weather"][0]["icon"]
76
- icon_url = f"http://openweathermap.org/img/wn/{icon_code}.png"
77
- st.image(icon_url)
78
- else:
79
- st.write("Could not retrieve weather data. Please check the location.")
80
 
81
- # Optional: Add CSS for styling
82
- st.markdown(
83
- """
84
- <style>
85
- .reportview-container {
86
- background: #f5f5f5;
87
- }
88
- .stButton>button {
89
- background-color: #ff5733;
90
- color: white;
91
- }
92
- </style>
93
- """,
94
- unsafe_allow_html=True
95
- )
96
-
97
 
98
 
99
 
 
100
 
101
 
102
 
 
1
  import requests
2
  import streamlit as st
3
+ import json
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
+ weather_url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={openweather_api_key}&units=metric"
22
+ response = requests.get(weather_url)
23
+ if response.status_code == 200:
 
 
24
  return response.json()
25
+ else:
26
+ st.error("Could not retrieve weather data. Please check your API key and city name.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  return None
28
 
29
+ # Function to get outfit suggestion from Groq API
30
+ def get_outfit_suggestion(weather_condition):
31
+ url = "https://api.groq.com/v1/suggest-outfit" # Replace with the actual Groq API endpoint
32
+ headers = {
33
+ "Authorization": f"Bearer {groq_api_key}",
34
+ "Content-Type": "application/json"
35
+ }
36
+ payload = {
37
+ "weather_condition": weather_condition
38
+ }
39
+ response = requests.post(url, headers=headers, data=json.dumps(payload))
40
+ if response.status_code == 200:
41
+ return response.json().get("outfit")
42
+ else:
43
+ st.error("Could not retrieve outfit suggestions. Please check your Groq API.")
44
+ return None
45
 
46
+ # Main app UI
47
+ st.title("Weather-Based Outfit Suggestion App")
48
 
49
+ # Try auto-detecting location if city is empty
50
+ city = st.text_input("Enter your location:", value=get_location_by_ip() or "")
 
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
+ # if weather_data and weather_data["cod"] == 200:
139
+ # temperature, description = parse_weather_data(weather_data)
140
 
141
+ # # Display current weather info
142
+ # st.write(f"Current temperature in {city}: {temperature}°C")
143
+ # st.write(f"Weather: {description}")
144
 
145
+ # # Get outfit suggestion based on user preferences
146
+ # outfit_suggestion = get_outfit_suggestion(temperature, description, style, fabric)
147
 
148
+ # if outfit_suggestion:
149
+ # # Display outfit suggestion
150
+ # st.write("Outfit Suggestion:")
151
+ # st.write(outfit_suggestion)
152
 
153
+ # # Display weather icon
154
+ # icon_code = weather_data["weather"][0]["icon"]
155
+ # icon_url = f"http://openweathermap.org/img/wn/{icon_code}.png"
156
+ # st.image(icon_url)
157
+ # else:
158
+ # st.write("Could not retrieve weather data. Please check the location.")
159
 
160
+ # # Optional: Add CSS for styling
161
+ # st.markdown(
162
+ # """
163
+ # <style>
164
+ # .reportview-container {
165
+ # background: #f5f5f5;
166
+ # }
167
+ # .stButton>button {
168
+ # background-color: #ff5733;
169
+ # color: white;
170
+ # }
171
+ # </style>
172
+ # """,
173
+ # unsafe_allow_html=True
174
+ # )
 
175
 
176
 
177
 
178
+ # --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
179
 
180
 
181