Prathamesh1420 commited on
Commit
e8f45ee
·
verified ·
1 Parent(s): 614e738

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -19
app.py CHANGED
@@ -1,16 +1,16 @@
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
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
13
  from datasets import load_dataset
 
14
 
15
  # Define function for feature extraction
16
  def feature_extraction(img_path, model):
@@ -19,7 +19,7 @@ def feature_extraction(img_path, model):
19
  expanded_img_array = np.expand_dims(img_array, axis=0)
20
  preprocessed_img = preprocess_input(expanded_img_array)
21
  result = model.predict(preprocessed_img).flatten()
22
- normalized_result = result / norm(result)
23
  return normalized_result
24
 
25
  # Define function for recommendation
@@ -45,6 +45,14 @@ def save_uploaded_file(uploaded_file):
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)
@@ -58,15 +66,7 @@ def display_similar_images(indices, filenames, images):
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
 
@@ -113,10 +113,12 @@ def show_dashboard():
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,10 +130,14 @@ def show_dashboard():
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()
 
 
 
 
 
1
  import os
2
  import pickle
3
  import numpy as np
4
+ import pandas as pd
5
  import streamlit as st
6
  from PIL import Image
7
+ import tensorflow as tf
8
  from tensorflow.keras.preprocessing import image
9
  from tensorflow.keras.layers import GlobalMaxPooling2D
10
  from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
11
  from sklearn.neighbors import NearestNeighbors
 
 
12
  from datasets import load_dataset
13
+ import zipfile
14
 
15
  # Define function for feature extraction
16
  def feature_extraction(img_path, model):
 
19
  expanded_img_array = np.expand_dims(img_array, axis=0)
20
  preprocessed_img = preprocess_input(expanded_img_array)
21
  result = model.predict(preprocessed_img).flatten()
22
+ normalized_result = result / np.linalg.norm(result)
23
  return normalized_result
24
 
25
  # Define function for recommendation
 
45
  st.error(f"Error saving file: {e}")
46
  return None
47
 
48
+ # Function to load image data from dataset
49
+ def load_image_data():
50
+ dataset = load_dataset("ashraq/fashion-product-images-small", split="train")
51
+ images = dataset["image"]
52
+ product_frame = dataset.remove_columns("image").to_pandas()
53
+ product_data = product_frame.reset_index(drop=True).to_dict(orient='index')
54
+ return images, product_data
55
+
56
  # Function to show similar images
57
  def display_similar_images(indices, filenames, images):
58
  col1, col2, col3, col4, col5 = st.columns(5)
 
66
  except Exception as e:
67
  st.error(f"Error displaying image {idx}: {e}")
68
 
69
+ # Function to show dashboard content
 
 
 
 
 
 
 
 
70
  def show_dashboard():
71
  st.header("Fashion Recommender System")
72
 
 
113
  # Chatbot section
114
  user_question = st.text_input("Ask a question:")
115
  if user_question:
116
+ from chatbot import Chatbot
117
  chatbot = Chatbot()
118
  bot_response, recommended_products = chatbot.generate_response(user_question)
119
  st.write("Chatbot:", bot_response)
120
 
121
+ # Display recommended products
122
  for result in recommended_products:
123
  pid = result['corpus_id']
124
  product_info = chatbot.product_data[pid]
 
130
  st.write("Gender:", product_info['gender'])
131
  st.image(chatbot.images[pid])
132
 
133
+ # Main Streamlit app
134
+ def main():
135
+ # Give title to the app
136
+ st.title("Fashion Recommender System")
137
+
138
+ # Show dashboard content directly
139
  show_dashboard()
140
+
141
+ # Run the main app
142
+ if __name__ == "__main__":
143
+ main()