Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,16 @@ 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 |
|
14 |
# Define function for feature extraction
|
15 |
def feature_extraction(img_path, model):
|
@@ -39,10 +49,10 @@ def save_uploaded_file(uploaded_file):
|
|
39 |
with open(file_path, 'wb') as f:
|
40 |
f.write(uploaded_file.getbuffer())
|
41 |
st.success(f"File saved to {file_path}")
|
42 |
-
return
|
43 |
except Exception as e:
|
44 |
st.error(f"Error saving file: {e}")
|
45 |
-
return
|
46 |
|
47 |
# Function to show dashboard content
|
48 |
def show_dashboard():
|
@@ -63,32 +73,51 @@ def show_dashboard():
|
|
63 |
st.error(f"Error loading pickle files: {e}")
|
64 |
return
|
65 |
|
|
|
|
|
|
|
|
|
66 |
# File upload section
|
67 |
uploaded_file = st.file_uploader("Choose an image")
|
68 |
if uploaded_file is not None:
|
69 |
-
|
|
|
70 |
# Display the uploaded image
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
73 |
|
74 |
# Feature extraction
|
75 |
-
|
|
|
|
|
|
|
|
|
76 |
|
77 |
# Recommendation
|
78 |
-
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# Display recommended products
|
81 |
col1, col2, col3, col4, col5 = st.columns(5)
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
92 |
else:
|
93 |
st.error("Some error occurred in file upload")
|
94 |
|
@@ -120,4 +149,4 @@ def main():
|
|
120 |
|
121 |
# Run the main app
|
122 |
if __name__ == "__main__":
|
123 |
-
main()
|
|
|
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):
|
|
|
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():
|
|
|
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 |
|
|
|
149 |
|
150 |
# Run the main app
|
151 |
if __name__ == "__main__":
|
152 |
+
main()
|