File size: 18,013 Bytes
9638dc6 1aa3c59 94b8e94 747d197 94b8e94 747d197 9638dc6 1aa3c59 9638dc6 1aa3c59 317553b 1aa3c59 317553b 6d9f695 317553b 6d9f695 317553b 6d9f695 317553b 6d9f695 317553b 6d9f695 317553b 6d9f695 317553b 6d9f695 317553b 1aa3c59 fd2fe56 1aa3c59 6b1e6df fd2fe56 6b1e6df 1aa3c59 6d9f695 1aa3c59 6d9f695 9638dc6 1aa3c59 6d9f695 72cc5a6 6d9f695 6b1e6df 6d9f695 6b1e6df 6d9f695 6b1e6df 6d9f695 dd0da2b 6b1e6df 210041d 6b1e6df fd2fe56 9638dc6 6b1e6df fd2fe56 2bd6fe6 6b1e6df fd2fe56 6b1e6df 8eefad0 6b1e6df 317553b fd2fe56 2bd6fe6 747d197 |
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
import requests
import streamlit as st
import groq
# Access API keys from Streamlit secrets
openweather_api_key = st.secrets["weather_api_key"]
groq_api_key = st.secrets["groq_api_key"]
# Function to get weather data from OpenWeatherMap
def get_weather_data(city):
api_key = openweather_api_key # Use the secret API key
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
try:
response = requests.get(url)
response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
return response.json()
except requests.exceptions.HTTPError as err:
st.error(f"HTTP error occurred: {err}")
except Exception as err:
st.error(f"An error occurred: {err}")
return None
# Function to parse weather data and categorize weather
def parse_weather_data(weather_data):
temperature = weather_data["main"]["temp"]
weather_description = weather_data["weather"][0]["description"]
return temperature, weather_description
# Categorizing weather conditions
def categorize_weather(description):
description = description.lower()
if "clear" in description or "sun" in description:
return "Sunny", "โ๏ธ"
elif "rain" in description or "drizzle" in description or "shower" in description:
return "Rainy", "๐ง๏ธ"
elif "snow" in description or "sleet" in description:
return "Cold", "โ๏ธ"
elif "cloud" in description:
return "Cloudy", "โ๏ธ"
elif "wind" in description:
return "Windy", "๐จ"
elif "smoke" in description or "haze" in description:
return "Smoky", "๐ซ๏ธ"
else:
return "Uncategorized", "๐"
# Function to get outfit suggestion using Groq's LLaMA model
def get_outfit_suggestion(temperature, description, style, fabric, weather_category, weather_icon, time_of_day, activity):
# Initialize Groq's API
try:
client = groq.Groq(api_key=groq_api_key) # Use the secret API key
# Adjust the prompt based on the weather category
prompt = f"The current weather is {description} with a temperature of {temperature}ยฐC. The weather category is {weather_category}. The time of day is {time_of_day} and the user is planning to do {activity}. Suggest an outfit. The user prefers a {style} style and {fabric} fabric."
# Use Groq's chat completion to get the text response
response = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama3-8b-8192", # Change to a valid Groq model if necessary
)
return response.choices[0].message.content.strip(), weather_icon
except Exception as e:
st.error(f"Error using Groq API: {e}")
return None, None
# Streamlit UI for user input
st.set_page_config(page_title="Weather-Based Outfit Suggestion", page_icon="๐ค๏ธ", layout="wide")
# Custom styles
st.markdown("""<style>
.reportview-container {
background: linear-gradient(135deg, #ffcc00, #ff7b00);
}
.stButton>button {
background-color: #ff5733;
color: white;
font-size: 18px;
border-radius: 10px;
padding: 10px;
}
.stTextInput input {
font-size: 16px;
padding: 10px;
border-radius: 10px;
}
.stSelectbox select {
font-size: 16px;
padding: 10px;
border-radius: 10px;
}
.stWrite, .stImage {
font-family: "Arial", sans-serif;
font-size: 18px;
color: #333;
}
.weather-header {
text-align: center;
font-size: 36px;
color: #ffffff;
font-family: "Arial", sans-serif;
}
.outfit-header {
font-size: 24px;
color: #444;
font-family: "Arial", sans-serif;
margin-top: 30px;
}
.left-column {
padding-right: 20px;
}
.right-column {
padding-left: 20px;
}
</style>""", unsafe_allow_html=True)
# Title and layout for columns
st.title("๐ค๏ธ Weather-Based Outfit Suggestion App")
# Create two columns: one for the user input and one for displaying results
col1, col2 = st.columns([1, 2]) # 1: left column (user input), 2: right column (outfit suggestions)
# User input in the left column (col1)
with col1:
city = st.text_input("Enter your location:", placeholder="E.g. Peshawar")
gender = st.selectbox("Select your gender", ["Male", "Female"])
personalized_style = st.text_input("Enter your personalized style (optional)", placeholder="E.g. Peshawari")
fabric = st.selectbox("Select your preferred fabric", ["Cotton", "Linen", "Wool", "Polyester", "Silk", "Leather"])
time_of_day = st.selectbox("Select time of day", ["Morning", "Afternoon", "Evening"])
activity = st.selectbox("Select your activity", ["Work", "Outdoor", "Casual", "Exercise", "Other"])
# Result display in the right column (col2)
with col2:
if city:
with st.spinner("Fetching weather data..."):
weather_data = get_weather_data(city)
if weather_data and weather_data["cod"] == 200:
temperature, description = parse_weather_data(weather_data)
# Categorize the weather
weather_category, weather_icon = categorize_weather(description)
# Display current weather info
st.write(f"Current temperature in {city}: {temperature}ยฐC")
st.write(f"Weather: {description} {weather_icon}")
st.write(f"Weather Category: {weather_category} {weather_icon}")
# Get outfit suggestion based on user preferences
outfit_suggestion, weather_icon = get_outfit_suggestion(temperature, description, personalized_style, fabric, weather_category, weather_icon, time_of_day, activity)
if outfit_suggestion:
# Display outfit suggestion
st.markdown(f"### ๐ Outfit Suggestion ๐")
st.write(outfit_suggestion)
# Additional section for Health and Comfort Tips
st.markdown(f"### ๐ฟ Health & Comfort Tips ๐ฟ")
st.write(f"Given the {weather_category} weather, it's important to take care of your health:")
st.write("- **Breathing**: A face mask or scarf covering your nose and mouth can help protect you from smoke inhalation.")
st.write("- **Hydration**: Keep a water bottle with you, as smoke can dehydrate your body.")
st.write("- **Rest**: Try to avoid strenuous activity outdoors and take breaks if you're feeling fatigued.")
st.write("- **Eyes**: If you're feeling irritated, use eye drops to soothe any discomfort caused by smoke.")
# Display weather icon
icon_code = weather_data["weather"][0]["icon"]
icon_url = f"http://openweathermap.org/img/wn/{icon_code}.png"
st.image(icon_url)
else:
st.write("Could not retrieve weather data. Please check the location.")
# import requests
# import streamlit as st
# import groq
# # Access API keys from Streamlit secrets
# openweather_api_key = st.secrets["weather_api_key"]
# groq_api_key = st.secrets["groq_api_key"]
# # Function to get weather data from OpenWeatherMap
# def get_weather_data(city):
# api_key = openweather_api_key # Use the secret API key
# url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
# try:
# response = requests.get(url)
# response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
# return response.json()
# except requests.exceptions.HTTPError as err:
# st.error(f"HTTP error occurred: {err}")
# except Exception as err:
# st.error(f"An error occurred: {err}")
# return None
# # Function to parse weather data and categorize weather
# def parse_weather_data(weather_data):
# temperature = weather_data["main"]["temp"]
# weather_description = weather_data["weather"][0]["description"]
# return temperature, weather_description
# # Categorizing weather conditions
# def categorize_weather(description):
# description = description.lower()
# if "clear" in description or "sun" in description:
# return "Sunny", "โ๏ธ"
# elif "rain" in description or "drizzle" in description or "shower" in description:
# return "Rainy", "๐ง๏ธ"
# elif "snow" in description or "sleet" in description:
# return "Cold", "โ๏ธ"
# elif "cloud" in description:
# return "Cloudy", "โ๏ธ"
# elif "wind" in description:
# return "Windy", "๐จ"
# elif "smoke" in description or "haze" in description:
# return "Smoky", "๐ซ๏ธ"
# else:
# return "Uncategorized", "๐"
# # Function to get outfit suggestion using Groq's LLaMA model
# def get_outfit_suggestion(temperature, description, style, fabric, weather_category, weather_icon):
# # Initialize Groq's API
# try:
# client = groq.Groq(api_key=groq_api_key) # Use the secret API key
# # Adjust the prompt based on the weather category
# prompt = f"The current weather is {description} with a temperature of {temperature}ยฐC. The weather category is {weather_category}. Suggest an outfit. The user prefers a {style} style and {fabric} fabric."
# # Use Groq's chat completion to get the text response
# response = client.chat.completions.create(
# messages=[{"role": "user", "content": prompt}],
# model="llama3-8b-8192", # Change to a valid Groq model if necessary
# )
# return response.choices[0].message.content.strip(), weather_icon
# except Exception as e:
# st.error(f"Error using Groq API: {e}")
# return None, None
# # Streamlit UI for user input
# st.set_page_config(page_title="Weather-Based Outfit Suggestion", page_icon="๐ค๏ธ", layout="wide")
# # Custom styles
# st.markdown("""<style>
# .reportview-container {
# background: linear-gradient(135deg, #ffcc00, #ff7b00);
# }
# .stButton>button {
# background-color: #ff5733;
# color: white;
# font-size: 18px;
# border-radius: 10px;
# padding: 10px;
# }
# .stTextInput input {
# font-size: 16px;
# padding: 10px;
# border-radius: 10px;
# }
# .stSelectbox select {
# font-size: 16px;
# padding: 10px;
# border-radius: 10px;
# }
# .stWrite, .stImage {
# font-family: "Arial", sans-serif;
# font-size: 18px;
# color: #333;
# }
# .weather-header {
# text-align: center;
# font-size: 36px;
# color: #ffffff;
# font-family: "Arial", sans-serif;
# }
# .outfit-header {
# font-size: 24px;
# color: #444;
# font-family: "Arial", sans-serif;
# margin-top: 30px;
# }
# .left-column {
# padding-right: 20px;
# }
# .right-column {
# padding-left: 20px;
# }
# </style>""", unsafe_allow_html=True)
# # Title and layout for columns
# st.title("๐ค๏ธ Weather-Based Outfit Suggestion App")
# # Create two columns: one for the user input and one for displaying results
# col1, col2 = st.columns([1, 2]) # 1: left column (user input), 2: right column (outfit suggestions)
# # User input in the left column (col1)
# with col1:
# city = st.text_input("Enter your location:", placeholder="E.g. Peshawar")
# gender = st.selectbox("Select your gender", ["Male", "Female"])
# personalized_style = st.text_input("Enter your personalized style (optional)", placeholder="E.g. Peshawari")
# fabric = st.selectbox("Select your preferred fabric", ["Cotton", "Linen", "Wool", "Polyester", "Silk", "Leather"])
# # Result display in the right column (col2)
# with col2:
# if city:
# weather_data = get_weather_data(city)
# if weather_data and weather_data["cod"] == 200:
# temperature, description = parse_weather_data(weather_data)
# # Categorize the weather
# weather_category, weather_icon = categorize_weather(description)
# # Display current weather info
# st.write(f"Current temperature in {city}: {temperature}ยฐC")
# st.write(f"Weather: {description} {weather_icon}")
# st.write(f"Weather Category: {weather_category} {weather_icon}")
# # Get outfit suggestion based on user preferences
# outfit_suggestion, weather_icon = get_outfit_suggestion(temperature, description, personalized_style, fabric, weather_category, weather_icon)
# if outfit_suggestion:
# # Display outfit suggestion
# st.markdown(f"### ๐ Outfit Suggestion ๐")
# st.write(outfit_suggestion)
# # Additional section for Health and Comfort Tips
# st.markdown(f"### ๐ฟ Health & Comfort Tips ๐ฟ")
# st.write(f"Given the {weather_category} weather, it's important to take care of your health:")
# st.write("- **Breathing**: A face mask or scarf covering your nose and mouth can help protect you from smoke inhalation.")
# st.write("- **Hydration**: Keep a water bottle with you, as smoke can dehydrate your body.")
# st.write("- **Rest**: Try to avoid strenuous activity outdoors and take breaks if you're feeling fatigued.")
# st.write("- **Eyes**: If you're feeling irritated, use eye drops to soothe any discomfort caused by smoke.")
# # Display weather icon
# icon_code = weather_data["weather"][0]["icon"]
# icon_url = f"http://openweathermap.org/img/wn/{icon_code}.png"
# st.image(icon_url)
# else:
# st.write("Could not retrieve weather data. Please check the location.")
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# import requests
# import streamlit as st
# import groq
# import os
# # Function to get weather data from OpenWeatherMap
# import os
# # Replace with environment variables
# openweather_api_key = os.getenv("weather_api_key")
# groq_api_key = os.getenv("groq_api_key")
# def get_weather_data(city):
# api_key = openweather_api_key # Replace with your OpenWeatherMap API key
# url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
# try:
# response = requests.get(url)
# response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
# return response.json()
# except requests.exceptions.HTTPError as err:
# st.error(f"HTTP error occurred: {err}")
# except Exception as err:
# st.error(f"An error occurred: {err}")
# return None
# # Function to parse weather data
# def parse_weather_data(weather_data):
# temperature = weather_data["main"]["temp"]
# weather_description = weather_data["weather"][0]["description"]
# return temperature, weather_description
# # Function to get outfit suggestion using Groq's LLaMA model
# def get_outfit_suggestion(temperature, description, style, fabric):
# # Initialize Groq's API
# try:
# client = groq.Groq(api_key=groq_api_key) # Replace with your Groq API key
# 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."
# # Use Groq's chat completion to get the text response
# response = client.chat.completions.create(
# messages=[{"role": "user", "content": prompt}],
# model="llama3-8b-8192", # Change to a valid Groq model if necessary
# )
# return response.choices[0].message.content.strip()
# except Exception as e:
# st.error(f"Error using Groq API: {e}")
# return None
# # Streamlit UI for user input
# st.title("Weather-Based Outfit Suggestion App")
# city = st.text_input("Enter your location:")
# # Add style and fabric input options
# style = st.selectbox("Select your preferred style", ["Casual", "Formal", "Sporty", "Business", "Chic"])
# fabric = st.selectbox("Select your preferred fabric", ["Cotton", "Linen", "Wool", "Polyester", "Silk", "Leather"])
# if city:
# weather_data = get_weather_data(city)
# if weather_data and weather_data["cod"] == 200:
# temperature, description = parse_weather_data(weather_data)
# # Display current weather info
# st.write(f"Current temperature in {city}: {temperature}ยฐC")
# st.write(f"Weather: {description}")
# # Get outfit suggestion based on user preferences
# outfit_suggestion = get_outfit_suggestion(temperature, description, style, fabric)
# if outfit_suggestion:
# # Display outfit suggestion
# st.write("Outfit Suggestion:")
# st.write(outfit_suggestion)
# # Display weather icon
# icon_code = weather_data["weather"][0]["icon"]
# icon_url = f"http://openweathermap.org/img/wn/{icon_code}.png"
# st.image(icon_url)
# else:
# st.write("Could not retrieve weather data. Please check the location.")
# # Optional: Add CSS for styling
# st.markdown(
# """
# <style>
# .reportview-container {
# background: #f5f5f5;
# }
# .stButton>button {
# background-color: #ff5733;
# color: white;
# }
# </style>
# """,
# unsafe_allow_html=True
# )
|