Prathamesh1420's picture
Update app.py
a9b6931 verified
import streamlit as st
import os
from PIL import Image
import numpy as np
from chatbot import Chatbot # Assuming you have a chatbot module
# Function to save uploaded file
def save_uploaded_file(uploaded_file):
try:
if not os.path.exists('uploads'):
os.makedirs('uploads')
with open(os.path.join('uploads', uploaded_file.name), 'wb') as f:
f.write(uploaded_file.getbuffer())
return True
except Exception as e:
st.error(f"Error: {e}")
return False
# Function to show dashboard content
def show_dashboard():
st.title("Fashion Recommender System")
st.write("Welcome to our Fashion Recommender System! Upload an image and get personalized product recommendations based on your image and queries.")
chatbot = Chatbot()
chatbot.load_data()
# Load and set up the ResNet model
uploaded_file = st.file_uploader("Upload an Image", type=['jpg', 'jpeg', 'png'])
if uploaded_file:
if save_uploaded_file(uploaded_file):
st.sidebar.header("Uploaded Image")
display_image = Image.open(uploaded_file)
st.sidebar.image(display_image, caption='Uploaded Image', use_column_width=True)
# Generate image caption
image_path = os.path.join("uploads", uploaded_file.name)
caption = chatbot.generate_image_caption(image_path)
st.write("### Generated Caption")
st.write(caption)
# Use caption to get product recommendations
_, recommended_products = chatbot.generate_response(caption)
st.write("### Recommended Products")
col1, col2, col3, col4, col5 = st.columns(5)
for i, idx in enumerate(recommended_products[:5]):
with col1 if i == 0 else col2 if i == 1 else col3 if i == 2 else col4 if i == 3 else col5:
product_image = chatbot.images[idx['corpus_id']]
st.image(product_image, caption=f"Product {i+1}", width=150)
else:
st.error("Error in uploading the file.")
# Chatbot section
st.write("### Chat with our Fashion Assistant")
user_question = st.text_input("Ask a question about fashion:")
if user_question:
bot_response, recommended_products = chatbot.generate_response(user_question)
st.write("**Chatbot Response:**")
st.write(bot_response)
# Display recommended products based on the user question
st.write("**Recommended Products:**")
for result in recommended_products:
pid = result['corpus_id']
product_info = chatbot.product_data[pid]
st.markdown("""
<div style='border: 1px solid #ddd; padding: 10px; margin: 10px 0; border-radius: 5px;'>
<p><strong>Product Name:</strong> {product_name}</p>
<p><strong>Category:</strong> {category}</p>
<p><strong>Article Type:</strong> {article_type}</p>
<p><strong>Usage:</strong> {usage}</p>
<p><strong>Season:</strong> {season}</p>
<p><strong>Gender:</strong> {gender}</p>
<img src="{image_url}" width="150" />
</div>
""".format(
product_name=product_info['productDisplayName'],
category=product_info['masterCategory'],
article_type=product_info['articleType'],
usage=product_info['usage'],
season=product_info['season'],
gender=product_info['gender'],
image_url="uploads/" + uploaded_file.name # assuming images are saved in uploads folder
), unsafe_allow_html=True)
# Main Streamlit app
def main():
# Set page configuration
st.set_page_config(
page_title="Fashion Recommender System",
page_icon=":dress:",
layout="wide",
initial_sidebar_state="expanded"
)
# Show dashboard content directly
show_dashboard()
# Run the main app
if __name__ == "__main__":
main()