Delete main.py
Browse files
main.py
DELETED
@@ -1,152 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
from PIL import Image
|
4 |
-
import numpy as np
|
5 |
-
import pickle
|
6 |
-
import tensorflow
|
7 |
-
from tensorflow.keras.preprocessing import image
|
8 |
-
from tensorflow.keras.layers import GlobalMaxPooling2D
|
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 # Assuming you have a chatbot module
|
13 |
-
import zipfile
|
14 |
-
|
15 |
-
# Define the path to the zip file and the directory to extract to
|
16 |
-
zip_file_path = 'images.zip'
|
17 |
-
extract_to = 'images'
|
18 |
-
|
19 |
-
# Check if the images directory already exists to avoid re-extracting
|
20 |
-
if not os.path.exists(extract_to):
|
21 |
-
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
22 |
-
zip_ref.extractall(extract_to)
|
23 |
-
|
24 |
-
# Define function for feature extraction
|
25 |
-
def feature_extraction(img_path, model):
|
26 |
-
img = image.load_img(img_path, target_size=(224, 224))
|
27 |
-
img_array = image.img_to_array(img)
|
28 |
-
expanded_img_array = np.expand_dims(img_array, axis=0)
|
29 |
-
preprocessed_img = preprocess_input(expanded_img_array)
|
30 |
-
result = model.predict(preprocessed_img).flatten()
|
31 |
-
normalized_result = result / norm(result)
|
32 |
-
return normalized_result
|
33 |
-
|
34 |
-
# Define function for recommendation
|
35 |
-
def recommend(features, feature_list):
|
36 |
-
neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
|
37 |
-
neighbors.fit(feature_list)
|
38 |
-
distances, indices = neighbors.kneighbors([features])
|
39 |
-
return indices
|
40 |
-
|
41 |
-
# Function to save uploaded file
|
42 |
-
def save_uploaded_file(uploaded_file):
|
43 |
-
try:
|
44 |
-
# Ensure the uploads directory exists
|
45 |
-
if not os.path.exists('uploads'):
|
46 |
-
os.makedirs('uploads')
|
47 |
-
|
48 |
-
file_path = os.path.join('uploads', uploaded_file.name)
|
49 |
-
with open(file_path, 'wb') as f:
|
50 |
-
f.write(uploaded_file.getbuffer())
|
51 |
-
st.success(f"File saved to {file_path}")
|
52 |
-
return file_path
|
53 |
-
except Exception as e:
|
54 |
-
st.error(f"Error saving file: {e}")
|
55 |
-
return None
|
56 |
-
|
57 |
-
# Function to show dashboard content
|
58 |
-
def show_dashboard():
|
59 |
-
st.header("Fashion Recommender System")
|
60 |
-
chatbot = Chatbot()
|
61 |
-
# Load ResNet model for image feature extraction
|
62 |
-
model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
63 |
-
model.trainable = False
|
64 |
-
model = tensorflow.keras.Sequential([
|
65 |
-
model,
|
66 |
-
GlobalMaxPooling2D()
|
67 |
-
])
|
68 |
-
|
69 |
-
try:
|
70 |
-
feature_list = np.array(pickle.load(open('embeddings.pkl', 'rb')))
|
71 |
-
filenames = pickle.load(open('filenames.pkl', 'rb'))
|
72 |
-
except Exception as e:
|
73 |
-
st.error(f"Error loading pickle files: {e}")
|
74 |
-
return
|
75 |
-
|
76 |
-
# Print the filenames to verify
|
77 |
-
st.write("List of filenames loaded:")
|
78 |
-
st.write(filenames)
|
79 |
-
|
80 |
-
# File upload section
|
81 |
-
uploaded_file = st.file_uploader("Choose an image")
|
82 |
-
if uploaded_file is not None:
|
83 |
-
file_path = save_uploaded_file(uploaded_file)
|
84 |
-
if file_path:
|
85 |
-
# Display the uploaded image
|
86 |
-
try:
|
87 |
-
display_image = Image.open(file_path)
|
88 |
-
st.image(display_image)
|
89 |
-
except Exception as e:
|
90 |
-
st.error(f"Error displaying uploaded image: {e}")
|
91 |
-
|
92 |
-
# Feature extraction
|
93 |
-
try:
|
94 |
-
features = feature_extraction(file_path, model)
|
95 |
-
except Exception as e:
|
96 |
-
st.error(f"Error extracting features: {e}")
|
97 |
-
return
|
98 |
-
|
99 |
-
# Recommendation
|
100 |
-
try:
|
101 |
-
indices = recommend(features, feature_list)
|
102 |
-
except Exception as e:
|
103 |
-
st.error(f"Error in recommendation: {e}")
|
104 |
-
return
|
105 |
-
|
106 |
-
# Display recommended products
|
107 |
-
col1, col2, col3, col4, col5 = st.columns(5)
|
108 |
-
columns = [col1, col2, col3, col4, col5]
|
109 |
-
|
110 |
-
for col, idx in zip(columns, indices[0]):
|
111 |
-
file_path = filenames[idx]
|
112 |
-
st.write(f"Trying to open file: {file_path}") # Add debug info
|
113 |
-
try:
|
114 |
-
if os.path.exists(file_path):
|
115 |
-
with col:
|
116 |
-
st.image(file_path)
|
117 |
-
else:
|
118 |
-
st.error(f"File does not exist: {file_path}")
|
119 |
-
except Exception as e:
|
120 |
-
st.error(f"Error opening file {file_path}: {e}")
|
121 |
-
else:
|
122 |
-
st.error("Some error occurred in file upload")
|
123 |
-
|
124 |
-
# Chatbot section
|
125 |
-
user_question = st.text_input("Ask a question:")
|
126 |
-
if user_question:
|
127 |
-
bot_response, recommended_products = chatbot.generate_response(user_question)
|
128 |
-
st.write("Chatbot:", bot_response)
|
129 |
-
|
130 |
-
# Display recommended products
|
131 |
-
for result in recommended_products:
|
132 |
-
pid = result['corpus_id']
|
133 |
-
product_info = chatbot.product_data[pid]
|
134 |
-
st.write("Product Name:", product_info['productDisplayName'])
|
135 |
-
st.write("Category:", product_info['masterCategory'])
|
136 |
-
st.write("Article Type:", product_info['articleType'])
|
137 |
-
st.write("Usage:", product_info['usage'])
|
138 |
-
st.write("Season:", product_info['season'])
|
139 |
-
st.write("Gender:", product_info['gender'])
|
140 |
-
st.image(chatbot.images[pid])
|
141 |
-
|
142 |
-
# Main Streamlit app
|
143 |
-
def main():
|
144 |
-
# Give title to the app
|
145 |
-
st.title("Fashion Recommender System")
|
146 |
-
|
147 |
-
# Show dashboard content directly
|
148 |
-
show_dashboard()
|
149 |
-
|
150 |
-
# Run the main app
|
151 |
-
if __name__ == "__main__":
|
152 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|