Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,13 @@ import streamlit as st
|
|
| 2 |
import os
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Function to save uploaded file
|
| 8 |
def save_uploaded_file(uploaded_file):
|
|
@@ -18,83 +24,167 @@ def save_uploaded_file(uploaded_file):
|
|
| 18 |
|
| 19 |
# Function to show dashboard content
|
| 20 |
def show_dashboard():
|
| 21 |
-
st.title("Fashion Recommender System")
|
| 22 |
-
st.write("Welcome to our Fashion Recommender System! Upload an image
|
| 23 |
-
|
| 24 |
-
chatbot
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
if uploaded_file:
|
| 31 |
if save_uploaded_file(uploaded_file):
|
| 32 |
-
st.sidebar.header("Uploaded Image")
|
| 33 |
display_image = Image.open(uploaded_file)
|
| 34 |
st.sidebar.image(display_image, caption='Uploaded Image', use_column_width=True)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
else:
|
| 52 |
st.error("Error in uploading the file.")
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
st.write("
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
product_info = chatbot.product_data[pid]
|
| 67 |
-
st.markdown("""
|
| 68 |
-
<div style='border: 1px solid #ddd; padding: 10px; margin: 10px 0; border-radius: 5px;'>
|
| 69 |
-
<p><strong>Product Name:</strong> {product_name}</p>
|
| 70 |
-
<p><strong>Category:</strong> {category}</p>
|
| 71 |
-
<p><strong>Article Type:</strong> {article_type}</p>
|
| 72 |
-
<p><strong>Usage:</strong> {usage}</p>
|
| 73 |
-
<p><strong>Season:</strong> {season}</p>
|
| 74 |
-
<p><strong>Gender:</strong> {gender}</p>
|
| 75 |
-
<img src="{image_url}" width="150" />
|
| 76 |
-
</div>
|
| 77 |
-
""".format(
|
| 78 |
-
product_name=product_info['productDisplayName'],
|
| 79 |
-
category=product_info['masterCategory'],
|
| 80 |
-
article_type=product_info['articleType'],
|
| 81 |
-
usage=product_info['usage'],
|
| 82 |
-
season=product_info['season'],
|
| 83 |
-
gender=product_info['gender'],
|
| 84 |
-
image_url="uploads/" + uploaded_file.name # assuming images are saved in uploads folder
|
| 85 |
-
), unsafe_allow_html=True)
|
| 86 |
|
| 87 |
# Main Streamlit app
|
| 88 |
def main():
|
| 89 |
# Set page configuration
|
| 90 |
st.set_page_config(
|
| 91 |
page_title="Fashion Recommender System",
|
| 92 |
-
page_icon="
|
| 93 |
layout="wide",
|
| 94 |
initial_sidebar_state="expanded"
|
| 95 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
# Show dashboard content
|
| 98 |
show_dashboard()
|
| 99 |
|
| 100 |
# Run the main app
|
|
|
|
| 2 |
import os
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
from chatbot import Chatbot
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
# Set environment variables to force CPU usage
|
| 10 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
| 11 |
+
os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1'
|
| 12 |
|
| 13 |
# Function to save uploaded file
|
| 14 |
def save_uploaded_file(uploaded_file):
|
|
|
|
| 24 |
|
| 25 |
# Function to show dashboard content
|
| 26 |
def show_dashboard():
|
| 27 |
+
st.title("π Fashion Recommender System")
|
| 28 |
+
st.write("Welcome to our Fashion Recommender System! Upload an image or describe what you're looking for to get personalized fashion recommendations.")
|
| 29 |
+
|
| 30 |
+
# Initialize chatbot with loading state
|
| 31 |
+
with st.spinner("Loading fashion assistant..."):
|
| 32 |
+
try:
|
| 33 |
+
chatbot = Chatbot()
|
| 34 |
+
chatbot.load_data()
|
| 35 |
+
st.success("β
Fashion assistant loaded successfully!")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
st.error(f"β Error initializing chatbot: {str(e)}")
|
| 38 |
+
st.info("The system is running in limited mode. Some features may not be available.")
|
| 39 |
+
return
|
| 40 |
|
| 41 |
+
# Sidebar for uploaded image
|
| 42 |
+
st.sidebar.header("πΈ Image Upload")
|
| 43 |
+
uploaded_file = st.sidebar.file_uploader("Choose an image", type=['jpg', 'jpeg', 'png'])
|
| 44 |
|
| 45 |
+
if uploaded_file is not None:
|
| 46 |
if save_uploaded_file(uploaded_file):
|
|
|
|
| 47 |
display_image = Image.open(uploaded_file)
|
| 48 |
st.sidebar.image(display_image, caption='Uploaded Image', use_column_width=True)
|
| 49 |
|
| 50 |
+
# Process image and get recommendations
|
| 51 |
+
with st.spinner("Analyzing your image and finding recommendations..."):
|
| 52 |
+
try:
|
| 53 |
+
image_path = os.path.join("uploads", uploaded_file.name)
|
| 54 |
+
caption = chatbot.generate_image_caption(image_path)
|
| 55 |
+
|
| 56 |
+
st.write("### πΌοΈ Image Analysis")
|
| 57 |
+
col1, col2 = st.columns([1, 2])
|
| 58 |
+
with col1:
|
| 59 |
+
st.image(display_image, width=200)
|
| 60 |
+
with col2:
|
| 61 |
+
st.write("**Generated Caption:**")
|
| 62 |
+
st.info(caption)
|
| 63 |
+
|
| 64 |
+
# Get recommendations based on caption
|
| 65 |
+
bot_response, recommended_products = chatbot.generate_response(caption)
|
| 66 |
+
|
| 67 |
+
st.write("### π« Recommended Products")
|
| 68 |
+
if recommended_products:
|
| 69 |
+
cols = st.columns(3)
|
| 70 |
+
for i, product in enumerate(recommended_products[:3]):
|
| 71 |
+
with cols[i]:
|
| 72 |
+
product_info = chatbot.get_product_info(product['corpus_id'])
|
| 73 |
+
if product_info:
|
| 74 |
+
st.image(
|
| 75 |
+
product_info['image'],
|
| 76 |
+
caption=product_info['name'],
|
| 77 |
+
width=150
|
| 78 |
+
)
|
| 79 |
+
st.write(f"**{product_info['name']}**")
|
| 80 |
+
st.caption(f"Category: {product_info['category']}")
|
| 81 |
+
st.caption(f"Type: {product_info['article_type']}")
|
| 82 |
+
else:
|
| 83 |
+
st.info("Product info not available")
|
| 84 |
+
else:
|
| 85 |
+
st.warning("No products found matching your image.")
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
st.error(f"Error processing image: {str(e)}")
|
| 89 |
else:
|
| 90 |
st.error("Error in uploading the file.")
|
| 91 |
|
| 92 |
+
# Main content area for chat
|
| 93 |
+
st.write("---")
|
| 94 |
+
st.write("### π¬ Chat with Fashion Assistant")
|
| 95 |
+
st.write("Describe what you're looking for or ask for fashion advice!")
|
| 96 |
+
|
| 97 |
+
# Initialize chat history
|
| 98 |
+
if "messages" not in st.session_state:
|
| 99 |
+
st.session_state.messages = []
|
| 100 |
+
|
| 101 |
+
# Display chat messages from history
|
| 102 |
+
for message in st.session_state.messages:
|
| 103 |
+
with st.chat_message(message["role"]):
|
| 104 |
+
st.markdown(message["content"])
|
| 105 |
+
|
| 106 |
+
# Chat input
|
| 107 |
+
if prompt := st.chat_input("What are you looking for today?"):
|
| 108 |
+
# Add user message to chat history
|
| 109 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 110 |
+
with st.chat_message("user"):
|
| 111 |
+
st.markdown(prompt)
|
| 112 |
+
|
| 113 |
+
# Generate response
|
| 114 |
+
with st.chat_message("assistant"):
|
| 115 |
+
with st.spinner("Finding the perfect fashion items..."):
|
| 116 |
+
try:
|
| 117 |
+
bot_response, recommended_products = chatbot.generate_response(prompt)
|
| 118 |
+
|
| 119 |
+
# Display bot response
|
| 120 |
+
st.markdown(bot_response)
|
| 121 |
+
|
| 122 |
+
# Display recommended products
|
| 123 |
+
if recommended_products:
|
| 124 |
+
st.write("**π― Recommended for you:**")
|
| 125 |
+
|
| 126 |
+
# Display products in columns
|
| 127 |
+
product_cols = st.columns(3)
|
| 128 |
+
for i, product in enumerate(recommended_products[:3]):
|
| 129 |
+
with product_cols[i]:
|
| 130 |
+
product_info = chatbot.get_product_info(product['corpus_id'])
|
| 131 |
+
if product_info:
|
| 132 |
+
st.image(
|
| 133 |
+
product_info['image'],
|
| 134 |
+
caption=product_info['name'],
|
| 135 |
+
width=150
|
| 136 |
+
)
|
| 137 |
+
st.write(f"**{product_info['name']}**")
|
| 138 |
+
st.caption(f"Category: {product_info['category']}")
|
| 139 |
+
st.caption(f"Type: {product_info['article_type']}")
|
| 140 |
+
st.caption(f"Season: {product_info['season']}")
|
| 141 |
+
else:
|
| 142 |
+
st.info("Product info not available")
|
| 143 |
+
else:
|
| 144 |
+
st.info("No specific products found. Try describing what you're looking for in more detail!")
|
| 145 |
+
|
| 146 |
+
# Add assistant response to chat history
|
| 147 |
+
st.session_state.messages.append({"role": "assistant", "content": bot_response})
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
error_msg = "Sorry, I encountered an error while processing your request. Please try again."
|
| 151 |
+
st.error(error_msg)
|
| 152 |
+
st.session_state.messages.append({"role": "assistant", "content": error_msg})
|
| 153 |
|
| 154 |
+
# Clear chat button
|
| 155 |
+
if st.button("Clear Chat History"):
|
| 156 |
+
st.session_state.messages = []
|
| 157 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
# Main Streamlit app
|
| 160 |
def main():
|
| 161 |
# Set page configuration
|
| 162 |
st.set_page_config(
|
| 163 |
page_title="Fashion Recommender System",
|
| 164 |
+
page_icon="π",
|
| 165 |
layout="wide",
|
| 166 |
initial_sidebar_state="expanded"
|
| 167 |
)
|
| 168 |
+
|
| 169 |
+
# Add custom CSS
|
| 170 |
+
st.markdown("""
|
| 171 |
+
<style>
|
| 172 |
+
.stChatMessage {
|
| 173 |
+
padding: 1rem;
|
| 174 |
+
border-radius: 0.5rem;
|
| 175 |
+
margin-bottom: 1rem;
|
| 176 |
+
}
|
| 177 |
+
.product-card {
|
| 178 |
+
border: 1px solid #ddd;
|
| 179 |
+
border-radius: 10px;
|
| 180 |
+
padding: 1rem;
|
| 181 |
+
margin: 0.5rem 0;
|
| 182 |
+
background-color: #f9f9f9;
|
| 183 |
+
}
|
| 184 |
+
</style>
|
| 185 |
+
""", unsafe_allow_html=True)
|
| 186 |
|
| 187 |
+
# Show dashboard content
|
| 188 |
show_dashboard()
|
| 189 |
|
| 190 |
# Run the main app
|