Update app.py
Browse files
app.py
CHANGED
@@ -3,98 +3,88 @@ import os
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
from chatbot import Chatbot # Assuming you have a chatbot module
|
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 |
-
|
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
from chatbot import Chatbot # Assuming you have a chatbot module
|
|
|
6 |
|
7 |
+
# Function to save uploaded file
|
8 |
+
def save_uploaded_file(uploaded_file):
|
9 |
+
try:
|
10 |
+
if not os.path.exists('uploads'):
|
11 |
+
os.makedirs('uploads')
|
12 |
+
with open(os.path.join('uploads', uploaded_file.name), 'wb') as f:
|
13 |
+
f.write(uploaded_file.getbuffer())
|
14 |
+
return True
|
15 |
+
except Exception as e:
|
16 |
+
st.error(f"Error: {e}")
|
17 |
+
return False
|
|
|
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 and get personalized product recommendations based on your image and queries.")
|
|
|
23 |
|
24 |
+
chatbot = Chatbot()
|
25 |
+
chatbot.load_data()
|
|
|
26 |
|
27 |
+
# Load and set up the ResNet model
|
28 |
+
uploaded_file = st.file_uploader("Upload an Image", type=['jpg', 'jpeg', 'png'])
|
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 |
+
# Generate image caption
|
37 |
+
image_path = os.path.join("uploads", uploaded_file.name)
|
38 |
+
caption = chatbot.generate_image_caption(image_path)
|
39 |
+
st.write("### Generated Caption")
|
40 |
+
st.write(caption)
|
41 |
+
|
42 |
+
# Use caption to get product recommendations
|
43 |
+
_, recommended_products = chatbot.generate_response(caption)
|
|
|
44 |
|
45 |
+
st.write("### Recommended Products")
|
46 |
+
col1, col2, col3, col4, col5 = st.columns(5)
|
47 |
+
for i, idx in enumerate(recommended_products[:5]):
|
48 |
+
with col1 if i == 0 else col2 if i == 1 else col3 if i == 2 else col4 if i == 3 else col5:
|
49 |
+
product_image = chatbot.images[idx['corpus_id']]
|
50 |
+
st.image(product_image, caption=f"Product {i+1}", width=150)
|
51 |
+
else:
|
52 |
+
st.error("Error in uploading the file.")
|
|
|
53 |
|
54 |
+
# Chatbot section
|
55 |
+
st.write("### Chat with our Fashion Assistant")
|
56 |
+
user_question = st.text_input("Ask a question about fashion:")
|
57 |
+
if user_question:
|
58 |
+
bot_response, recommended_products = chatbot.generate_response(user_question)
|
59 |
+
st.write("**Chatbot Response:**")
|
60 |
+
st.write(bot_response)
|
|
|
61 |
|
62 |
+
# Display recommended products based on the user question
|
63 |
+
st.write("**Recommended Products:**")
|
64 |
+
for result in recommended_products:
|
65 |
+
pid = result['corpus_id']
|
66 |
+
product_info = chatbot.product_data[pid]
|
67 |
+
st.write(f"**Product Name:** {product_info['productDisplayName']}")
|
68 |
+
st.write(f"**Category:** {product_info['masterCategory']}")
|
69 |
+
st.write(f"**Article Type:** {product_info['articleType']}")
|
70 |
+
st.write(f"**Usage:** {product_info['usage']}")
|
71 |
+
st.write(f"**Season:** {product_info['season']}")
|
72 |
+
st.write(f"**Gender:** {product_info['gender']}")
|
73 |
+
st.image(chatbot.images[pid], width=150)
|
|
|
74 |
|
75 |
+
# Main Streamlit app
|
76 |
+
def main():
|
77 |
+
# Set page configuration
|
78 |
+
st.set_page_config(
|
79 |
+
page_title="Fashion Recommender System",
|
80 |
+
page_icon=":dress:",
|
81 |
+
layout="wide",
|
82 |
+
initial_sidebar_state="expanded"
|
83 |
+
)
|
|
|
84 |
|
85 |
+
# Show dashboard content directly
|
86 |
+
show_dashboard()
|
|
|
87 |
|
88 |
+
# Run the main app
|
89 |
+
if __name__ == "__main__":
|
90 |
+
main()
|