import requests import streamlit as st import json # 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 user's IP-based location def get_location_by_ip(): try: response = requests.get("https://ipinfo.io") location_data = response.json() return location_data['city'] except Exception as e: st.warning("Could not auto-detect location.") return None # Function to get weather data from OpenWeatherMap def get_weather_data(city): weather_url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={openweather_api_key}&units=metric" response = requests.get(weather_url) if response.status_code == 200: return response.json() else: st.error("Could not retrieve weather data. Please check your API key and city name.") return None # Function to get outfit suggestion from Groq text generation model def get_outfit_suggestion(weather_condition, temperature, city, gender): url = "https://api.groq.com/v1/completions" # Groq's text generation endpoint headers = { "Authorization": f"Bearer {groq_api_key}", "Content-Type": "application/json" } # Construct a prompt based on the weather and user details prompt = ( f"Suggest an outfit for a {gender} in {city} where the weather is {weather_condition} " f"with a temperature of {temperature}°C. Make it practical and stylish." ) payload = { "model": "llama3-8b-8192", # Use an appropriate Groq model "messages": [{"role": "user", "content": prompt}] } response = requests.post(url, headers=headers, data=json.dumps(payload)) if response.status_code == 200: return response.json().get("choices")[0]["message"]["content"] else: st.error("Could not retrieve outfit suggestions. Please check your Groq API.") return None # Enhanced UI with background color and icons st.markdown( """ """, unsafe_allow_html=True ) # Main app UI st.markdown("

Weather-Based Outfit Suggestion App

", unsafe_allow_html=True) # Try auto-detecting location if city is empty city = st.text_input("Enter your location:", value=get_location_by_ip() or "") gender = st.radio("Select Gender", options=["Male", "Female"], index=0) if city: weather_data = get_weather_data(city) if weather_data: # Extract weather information weather_condition = weather_data["weather"][0]["main"] temperature = weather_data["main"]["temp"] feels_like = weather_data["main"]["feels_like"] humidity = weather_data["main"]["humidity"] wind_speed = weather_data["wind"]["speed"] # Display weather information with icons and enhancements st.markdown("
", unsafe_allow_html=True) st.subheader(f"🌍 Weather in {city}") st.write(f"🌤️ Condition: {weather_condition}") st.write(f"🌡️ Temperature: {temperature}°C (Feels like: {feels_like}°C)") st.write(f"💧 Humidity: {humidity}%") st.write(f"💨 Wind Speed: {wind_speed} m/s") st.markdown("
", unsafe_allow_html=True) # Get outfit suggestion based on weather and gender outfit_suggestion = get_outfit_suggestion(weather_condition, temperature, city, gender) if outfit_suggestion: st.subheader("👗 Outfit Suggestion") st.write(outfit_suggestion) else: st.warning("No outfit suggestion available for this weather condition.") else: st.info("Please enter your location to get weather and outfit suggestions.") # 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 # 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) # Use the secret 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( # """ # # """, # unsafe_allow_html=True # ) # -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # 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( # """ # # """, # unsafe_allow_html=True # )