Prathamesh1420 commited on
Commit
d3955b3
·
verified ·
1 Parent(s): eb7c94b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -45
app.py CHANGED
@@ -3,31 +3,8 @@ import os
3
  from PIL import Image
4
  import numpy as np
5
  import pickle
6
- import tensorflow as tf
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
 
14
- # Define function for feature extraction
15
- def feature_extraction(img_path, model):
16
- img = image.load_img(img_path, target_size=(224, 224))
17
- img_array = image.img_to_array(img)
18
- expanded_img_array = np.expand_dims(img_array, axis=0)
19
- preprocessed_img = preprocess_input(expanded_img_array)
20
- result = model.predict(preprocessed_img).flatten()
21
- normalized_result = result / norm(result)
22
- return normalized_result
23
-
24
- # Define function for recommendation
25
- def recommend(features, feature_list):
26
- neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
27
- neighbors.fit(feature_list)
28
- distances, indices = neighbors.kneighbors([features])
29
- return indices
30
-
31
  # Function to save uploaded file
32
  def save_uploaded_file(uploaded_file):
33
  try:
@@ -46,17 +23,6 @@ def show_dashboard():
46
  chatbot = Chatbot()
47
  chatbot.load_data()
48
 
49
- # Load ResNet model for image feature extraction
50
- model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
51
- model.trainable = False
52
- model = tf.keras.Sequential([
53
- model,
54
- GlobalMaxPooling2D()
55
- ])
56
-
57
- feature_list = np.array(pickle.load(open('embeddings.pkl', 'rb')))
58
- filenames = pickle.load(open('filenames.pkl', 'rb'))
59
-
60
  # File upload section
61
  uploaded_file = st.file_uploader("Choose an image")
62
  if uploaded_file is not None:
@@ -65,24 +31,26 @@ def show_dashboard():
65
  display_image = Image.open(uploaded_file)
66
  st.image(display_image)
67
 
68
- # Feature extraction
69
- features = feature_extraction(os.path.join("uploads", uploaded_file.name), model)
 
 
70
 
71
- # Recommendation
72
- indices = recommend(features, feature_list)
73
 
74
- # Display recommended products using loaded images from the dataset
75
  col1, col2, col3, col4, col5 = st.columns(5)
76
  with col1:
77
- st.image(chatbot.images[indices[0][0]])
78
  with col2:
79
- st.image(chatbot.images[indices[0][1]])
80
  with col3:
81
- st.image(chatbot.images[indices[0][2]])
82
  with col4:
83
- st.image(chatbot.images[indices[0][3]])
84
  with col5:
85
- st.image(chatbot.images[indices[0][4]])
86
 
87
  else:
88
  st.header("Some error occurred in file upload")
@@ -115,4 +83,4 @@ def main():
115
 
116
  # Run the main app
117
  if __name__ == "__main__":
118
- main()
 
3
  from PIL import Image
4
  import numpy as np
5
  import pickle
 
 
 
 
 
 
6
  from chatbot import Chatbot # Assuming you have a chatbot module
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Function to save uploaded file
9
  def save_uploaded_file(uploaded_file):
10
  try:
 
23
  chatbot = Chatbot()
24
  chatbot.load_data()
25
 
 
 
 
 
 
 
 
 
 
 
 
26
  # File upload section
27
  uploaded_file = st.file_uploader("Choose an image")
28
  if uploaded_file is not None:
 
31
  display_image = Image.open(uploaded_file)
32
  st.image(display_image)
33
 
34
+ # Generate image caption
35
+ image_path = os.path.join("uploads", uploaded_file.name)
36
+ caption = chatbot.generate_image_caption(image_path)
37
+ st.write("Generated Caption:", caption)
38
 
39
+ # Use caption to get product recommendations
40
+ _, recommended_products = chatbot.generate_response(caption)
41
 
42
+ # Display recommended products
43
  col1, col2, col3, col4, col5 = st.columns(5)
44
  with col1:
45
+ st.image(chatbot.images[recommended_products[0]['corpus_id']])
46
  with col2:
47
+ st.image(chatbot.images[recommended_products[1]['corpus_id']])
48
  with col3:
49
+ st.image(chatbot.images[recommended_products[2]['corpus_id']])
50
  with col4:
51
+ st.image(chatbot.images[recommended_products[3]['corpus_id']])
52
  with col5:
53
+ st.image(chatbot.images[recommended_products[4]['corpus_id']])
54
 
55
  else:
56
  st.header("Some error occurred in file upload")
 
83
 
84
  # Run the main app
85
  if __name__ == "__main__":
86
+ main()