Spaces:
Sleeping
Sleeping
| # wardrobe_ai_app.py | |
| import streamlit as st | |
| import os | |
| import json | |
| import bcrypt | |
| from dotenv import load_dotenv | |
| from PIL import Image | |
| import openai | |
| # ---- Load API Keys ---- | |
| load_dotenv() | |
| OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | |
| WEATHER_API_KEY = os.getenv("WEATHER_API_KEY") | |
| PINTEREST_API_KEY = os.getenv("PINTEREST_API_KEY") | |
| openai.api_key = OPENAI_API_KEY | |
| # ---- User DB Setup ---- | |
| USER_DB_PATH = "users.json" | |
| if not os.path.exists(USER_DB_PATH): | |
| with open(USER_DB_PATH, "w") as f: | |
| json.dump({}, f) | |
| def load_users(): | |
| with open(USER_DB_PATH, "r") as f: | |
| return json.load(f) | |
| def save_users(users): | |
| with open(USER_DB_PATH, "w") as f: | |
| json.dump(users, f, indent=4) | |
| def hash_password(password): | |
| return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() | |
| def check_password(password, hashed): | |
| return bcrypt.checkpw(password.encode(), hashed.encode()) | |
| # ---- Session State ---- | |
| if "logged_in" not in st.session_state: | |
| st.session_state.logged_in = False | |
| if "username" not in st.session_state: | |
| st.session_state.username = "" | |
| # ---- Login / Sign-Up ---- | |
| users = load_users() | |
| if not st.session_state.logged_in: | |
| st.title("π Wardrobe AI Login") | |
| username = st.text_input("Username") | |
| password = st.text_input("Password", type="password") | |
| col1, col2 = st.columns(2) | |
| if col1.button("Login"): | |
| if username in users and check_password(password, users[username]["password"]): | |
| st.session_state.logged_in = True | |
| st.session_state.username = username | |
| st.success("Logged in!") | |
| else: | |
| st.error("Invalid credentials") | |
| if col2.button("Sign Up"): | |
| if username in users: | |
| st.warning("Username already exists") | |
| else: | |
| users[username] = { | |
| "password": hash_password(password), | |
| "style": None, | |
| "wardrobe": [] | |
| } | |
| save_users(users) | |
| st.success("Account created! Please answer onboarding questions.") | |
| st.session_state.logged_in = True | |
| st.session_state.username = username | |
| # ---- Onboarding ---- | |
| if st.session_state.logged_in and users[st.session_state.username]["style"] is None: | |
| st.subheader("π¨ Tell us about your style!") | |
| style = st.selectbox("What's your style?", ["Elegant", "Sporty", "Trendy", "Casual", "Minimalist"]) | |
| users[st.session_state.username]["style"] = style | |
| save_users(users) | |
| st.success("Preferences saved! Redirecting to main app...") | |
| # ---- Main App ---- | |
| if st.session_state.logged_in and users[st.session_state.username]["style"] is not None: | |
| st.sidebar.title("π Wardrobe AI") | |
| page = st.sidebar.radio("Navigate", ["π Home", "π§₯ My Wardrobe", "π Outfit Generator", "βοΈ Modify Outfit", "β€οΈ Favorites", "π AI Impact"]) | |
| if page == "π Home": | |
| st.title("π Welcome to Wardrobe AI") | |
| st.subheader("Your smart, stylish assistant for daily outfit planning.") | |
| st.image("assets/hero_image.jpg", use_column_width=True) | |
| st.markdown(""" | |
| - AI-powered outfit recommendations based on **weather**, **event**, and **style** | |
| - Personalized results using your **wardrobe** and **Pinterest moodboards** | |
| - Modify suggestions, save favorites, and build your digital closet | |
| """) | |
| elif page == "π§₯ My Wardrobe": | |
| st.title("π§₯ Your Digital Wardrobe") | |
| uploaded = st.file_uploader("Upload a clothing item", type=["jpg", "jpeg", "png"]) | |
| if uploaded: | |
| st.image(uploaded, caption="Uploaded item", width=200) | |
| users[st.session_state.username]["wardrobe"].append(uploaded.name) | |
| save_users(users) | |
| st.success("Clothing item saved!") | |
| elif page == "π Outfit Generator": | |
| st.title("π Today's Outfit Recommendation") | |
| event = st.selectbox("Whatβs the event?", ["Work", "Casual", "Formal", "Party"]) | |
| style = st.selectbox("How do you feel today?", ["Elegant", "Trendy", "Minimalist", "Sporty"]) | |
| location = st.text_input("Enter your city for weather-based suggestions") | |
| if st.button("Generate Outfit"): | |
| # For demo, simulate weather (you can replace with real fetch_weather()) | |
| weather = "Clear" | |
| prompt = f"Suggest an outfit for a {event} with a {style} vibe. Weather is {weather}." | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[{"role": "user", "content": prompt}] | |
| ) | |
| outfit = response.choices[0].message.content | |
| st.success("π Here's your look:") | |
| st.write(outfit) | |
| st.image("assets/sample_outfit.jpg", caption="AI Generated Outfit", width=300) | |
| st.button("Save to Favorites") | |
| except Exception as e: | |
| st.error(f"Error generating outfit: {e}") | |
| elif page == "βοΈ Modify Outfit": | |
| st.title("βοΈ Customize Your Look") | |
| st.image("assets/sample_outfit.jpg", width=300) | |
| new_top = st.selectbox("Choose a different top:", ["Blouse", "Sweater", "T-shirt"]) | |
| new_bottom = st.selectbox("Choose new bottoms:", ["Jeans", "Skirt", "Trousers"]) | |
| if st.button("Update Outfit"): | |
| st.success(f"Outfit updated to: {new_top} and {new_bottom}") | |
| elif page == "β€οΈ Favorites": | |
| st.title("β€οΈ Saved Outfits") | |
| st.info("Favorites feature coming soon!") | |
| elif page == "π AI Impact": | |
| st.title("π AI in Fashion and Retail") | |
| st.markdown(""" | |
| AI is revolutionizing how consumers engage with fashion: | |
| - **Personalization**: Tailored outfit recommendations improve satisfaction. | |
| - **Inventory Optimization**: Brands can predict trends. | |
| - **Virtual Try-ons**: AI + AR lets users try clothes virtually. | |
| - **Sustainability**: Encourages reusing wardrobe instead of buying new. | |
| """) | |
| st.image("assets/ai_fashion.jpg", use_column_width=True) | |