File size: 4,169 Bytes
8bdecbc 69f8906 0ace04f 1b8d4f7 7a35d6d 1b8d4f7 be68110 1b8d4f7 20ab3bc 1b8d4f7 682642b 1b8d4f7 d1591c4 1b8d4f7 d1591c4 1b8d4f7 0d5ccc9 a9b6931 0d5ccc9 a9b6931 0d5ccc9 a9b6931 0d5ccc9 b659a7a 1b8d4f7 b659a7a 1b8d4f7 f39917c 1b8d4f7 ca3309b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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()
|