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 API
def get_outfit_suggestion(weather_condition):
url = "https://api.groq.com/v1/suggest-outfit" # Replace with the actual Groq API endpoint
headers = {
"Authorization": f"Bearer {groq_api_key}",
"Content-Type": "application/json"
}
payload = {
"weather_condition": weather_condition
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
if response.status_code == 200:
return response.json().get("outfit")
else:
st.error("Could not retrieve outfit suggestions. Please check your Groq API.")
return None
# Main app UI
st.title("Weather-Based Outfit Suggestion App")
# Try auto-detecting location if city is empty
city = st.text_input("Enter your location:", value=get_location_by_ip() or "")
if city:
weather_data = get_weather_data(city)
if weather_data:
weather_condition = weather_data["weather"][0]["main"]
temperature = weather_data["main"]["temp"]
st.subheader(f"Weather in {city}")
st.write(f"Condition: {weather_condition}")
st.write(f"Temperature: {temperature}°C")
# Get outfit suggestion based on weather
outfit_suggestion = get_outfit_suggestion(weather_condition)
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
# )