Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
import pickle
|
3 |
import numpy as np
|
|
|
4 |
import streamlit as st
|
5 |
from PIL import Image
|
6 |
from tensorflow.keras.preprocessing import image
|
@@ -8,8 +9,8 @@ from tensorflow.keras.layers import GlobalMaxPooling2D
|
|
8 |
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
|
9 |
from sklearn.neighbors import NearestNeighbors
|
10 |
from numpy.linalg import norm
|
11 |
-
from chatbot import Chatbot
|
12 |
-
|
13 |
|
14 |
# Define function for feature extraction
|
15 |
def feature_extraction(img_path, model):
|
@@ -44,12 +45,33 @@ def save_uploaded_file(uploaded_file):
|
|
44 |
st.error(f"Error saving file: {e}")
|
45 |
return None
|
46 |
|
47 |
-
# Function to show
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
def show_dashboard():
|
49 |
st.header("Fashion Recommender System")
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
54 |
model.trainable = False
|
55 |
model = tf.keras.Sequential([
|
@@ -64,59 +86,37 @@ def show_dashboard():
|
|
64 |
st.error(f"Error loading pickle files: {e}")
|
65 |
return
|
66 |
|
67 |
-
# Print the filenames to verify
|
68 |
-
st.write("List of filenames loaded:")
|
69 |
-
st.write(filenames)
|
70 |
-
|
71 |
# File upload section
|
72 |
uploaded_file = st.file_uploader("Choose an image")
|
73 |
if uploaded_file is not None:
|
74 |
file_path = save_uploaded_file(uploaded_file)
|
75 |
if file_path:
|
76 |
-
# Display the uploaded image
|
77 |
try:
|
78 |
display_image = Image.open(file_path)
|
79 |
st.image(display_image)
|
80 |
except Exception as e:
|
81 |
st.error(f"Error displaying uploaded image: {e}")
|
82 |
|
83 |
-
# Feature extraction
|
84 |
try:
|
85 |
features = feature_extraction(file_path, model)
|
86 |
except Exception as e:
|
87 |
st.error(f"Error extracting features: {e}")
|
88 |
return
|
89 |
|
90 |
-
# Recommendation
|
91 |
try:
|
92 |
indices = recommend(features, feature_list)
|
|
|
93 |
except Exception as e:
|
94 |
st.error(f"Error in recommendation: {e}")
|
95 |
return
|
96 |
|
97 |
-
# Display recommended products
|
98 |
-
col1, col2, col3, col4, col5 = st.columns(5)
|
99 |
-
columns = [col1, col2, col3, col4, col5]
|
100 |
-
|
101 |
-
for col, idx in zip(columns, indices[0]):
|
102 |
-
# Directly access images from the dataset instead of file paths
|
103 |
-
image_data = chatbot.images[idx]
|
104 |
-
if image_data is not None:
|
105 |
-
try:
|
106 |
-
with col:
|
107 |
-
st.image(image_data)
|
108 |
-
except Exception as e:
|
109 |
-
st.error(f"Error opening image index {idx}: {e}")
|
110 |
-
else:
|
111 |
-
st.error("Some error occurred in file upload")
|
112 |
-
|
113 |
# Chatbot section
|
114 |
user_question = st.text_input("Ask a question:")
|
115 |
if user_question:
|
|
|
116 |
bot_response, recommended_products = chatbot.generate_response(user_question)
|
117 |
st.write("Chatbot:", bot_response)
|
118 |
|
119 |
-
# Display recommended products
|
120 |
for result in recommended_products:
|
121 |
pid = result['corpus_id']
|
122 |
product_info = chatbot.product_data[pid]
|
@@ -128,14 +128,10 @@ def show_dashboard():
|
|
128 |
st.write("Gender:", product_info['gender'])
|
129 |
st.image(chatbot.images[pid])
|
130 |
|
131 |
-
# Main Streamlit app
|
132 |
-
def main():
|
133 |
-
# Give title to the app
|
134 |
-
st.title("Fashion Recommender System")
|
135 |
-
|
136 |
-
# Show dashboard content directly
|
137 |
-
show_dashboard()
|
138 |
-
|
139 |
-
# Run the main app
|
140 |
if __name__ == "__main__":
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import pickle
|
3 |
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
import streamlit as st
|
6 |
from PIL import Image
|
7 |
from tensorflow.keras.preprocessing import image
|
|
|
9 |
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
|
10 |
from sklearn.neighbors import NearestNeighbors
|
11 |
from numpy.linalg import norm
|
12 |
+
from chatbot import Chatbot
|
13 |
+
from datasets import load_dataset
|
14 |
|
15 |
# Define function for feature extraction
|
16 |
def feature_extraction(img_path, model):
|
|
|
45 |
st.error(f"Error saving file: {e}")
|
46 |
return None
|
47 |
|
48 |
+
# Function to show similar images
|
49 |
+
def display_similar_images(indices, filenames, images):
|
50 |
+
col1, col2, col3, col4, col5 = st.columns(5)
|
51 |
+
columns = [col1, col2, col3, col4, col5]
|
52 |
+
|
53 |
+
for col, idx in zip(columns, indices[0]):
|
54 |
+
try:
|
55 |
+
img = images[idx]
|
56 |
+
with col:
|
57 |
+
st.image(img)
|
58 |
+
except Exception as e:
|
59 |
+
st.error(f"Error displaying image {idx}: {e}")
|
60 |
+
|
61 |
+
# Load the dataset
|
62 |
+
def load_image_data():
|
63 |
+
dataset = load_dataset("ashraq/fashion-product-images-small", split="train")
|
64 |
+
images = dataset["image"]
|
65 |
+
product_frame = dataset.remove_columns("image").to_pandas()
|
66 |
+
product_data = product_frame.reset_index(drop=True).to_dict(orient='index')
|
67 |
+
return images, product_data
|
68 |
+
|
69 |
+
# Show dashboard content
|
70 |
def show_dashboard():
|
71 |
st.header("Fashion Recommender System")
|
72 |
+
|
73 |
+
# Load the dataset and models
|
74 |
+
images, product_data = load_image_data()
|
75 |
model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
76 |
model.trainable = False
|
77 |
model = tf.keras.Sequential([
|
|
|
86 |
st.error(f"Error loading pickle files: {e}")
|
87 |
return
|
88 |
|
|
|
|
|
|
|
|
|
89 |
# File upload section
|
90 |
uploaded_file = st.file_uploader("Choose an image")
|
91 |
if uploaded_file is not None:
|
92 |
file_path = save_uploaded_file(uploaded_file)
|
93 |
if file_path:
|
|
|
94 |
try:
|
95 |
display_image = Image.open(file_path)
|
96 |
st.image(display_image)
|
97 |
except Exception as e:
|
98 |
st.error(f"Error displaying uploaded image: {e}")
|
99 |
|
|
|
100 |
try:
|
101 |
features = feature_extraction(file_path, model)
|
102 |
except Exception as e:
|
103 |
st.error(f"Error extracting features: {e}")
|
104 |
return
|
105 |
|
|
|
106 |
try:
|
107 |
indices = recommend(features, feature_list)
|
108 |
+
display_similar_images(indices, filenames, images)
|
109 |
except Exception as e:
|
110 |
st.error(f"Error in recommendation: {e}")
|
111 |
return
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
# Chatbot section
|
114 |
user_question = st.text_input("Ask a question:")
|
115 |
if user_question:
|
116 |
+
chatbot = Chatbot()
|
117 |
bot_response, recommended_products = chatbot.generate_response(user_question)
|
118 |
st.write("Chatbot:", bot_response)
|
119 |
|
|
|
120 |
for result in recommended_products:
|
121 |
pid = result['corpus_id']
|
122 |
product_info = chatbot.product_data[pid]
|
|
|
128 |
st.write("Gender:", product_info['gender'])
|
129 |
st.image(chatbot.images[pid])
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
if __name__ == "__main__":
|
132 |
+
st.set_page_config(
|
133 |
+
page_title="Fashion Recommender System",
|
134 |
+
page_icon="🗣️",
|
135 |
+
menu_items={'About': "# Made by Prathamesh Khade"}
|
136 |
+
)
|
137 |
+
show_dashboard()
|