vsrinivas commited on
Commit
f3145a5
·
verified ·
1 Parent(s): ed7db5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -20
app.py CHANGED
@@ -22,20 +22,17 @@ index = pinecone.Index(INDEX_NAME)
22
  bm25 = BM25Encoder()
23
  bm25.fit(metadata['productDisplayName'])
24
 
25
- # Function to display images in a grid layout
26
  def display_result(image_batch, match_batch):
27
  figures = []
28
  for img, title in zip(image_batch, match_batch):
29
- # Ensure the image is in the correct format for encoding
30
  if img.mode != 'RGB':
31
  img = img.convert('RGB')
32
 
33
- # Convert image to bytes and encode as base64
34
  b = BytesIO()
35
  img.save(b, format='PNG')
36
  img_str = b64encode(b.getvalue()).decode('utf-8')
37
 
38
- # Create HTML figure element with the image title
39
  figures.append(f'''
40
  <figure style="margin: 0; padding: 0; text-align: left;">
41
  <figcaption style="font-weight: bold; margin:0;">{title}</figcaption>
@@ -43,7 +40,6 @@ def display_result(image_batch, match_batch):
43
  </figure>
44
  ''')
45
 
46
- # Combine all figures into a single HTML string with reduced spacing
47
  html_content = f'''
48
  <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; align-items: start;">
49
  {''.join(figures)}
@@ -52,12 +48,10 @@ def display_result(image_batch, match_batch):
52
  return html_content
53
 
54
 
55
- # Function to scale vectors based on alpha for hybrid search
56
  def hybrid_scale(dense, sparse, alpha):
57
  if alpha < 0 or alpha > 1:
58
  raise ValueError("Alpha must be between 0 and 1")
59
 
60
- # Scale sparse and dense vectors to create hybrid search vectors
61
  hsparse = {
62
  'indices': sparse['indices'],
63
  'values': [v * (1 - alpha) for v in sparse['values']]
@@ -67,7 +61,6 @@ def hybrid_scale(dense, sparse, alpha):
67
  return hdense, hsparse
68
 
69
 
70
- # Function to process the input text and slider value, with error handling
71
  def process_input(query, slider_value):
72
  try:
73
  slider_value = float(slider_value)
@@ -90,29 +83,23 @@ def process_input(query, slider_value):
90
  return display_result(imgs, matches)
91
 
92
  except Exception as e:
93
- # Handle exceptions and return a friendly error message
94
- return f"<p style='color:red;'>Not found. Try another search: {str(e)}</p>"
95
 
96
 
97
- # Function to update the textbox value when a dropdown choice is selected
98
  def update_textbox(choice):
99
  return choice
100
 
101
 
102
  def text_process(search_string):
103
- # Split the search string into words
104
  search_words = search_string.title().split()
105
 
106
- # Create a regex pattern that matches all words in any order
107
  pattern = r"(?=.*\b" + r"\b)(?=.*\b".join(map(re.escape, search_words)) + r"\b)"
108
 
109
- # Filter the master list to find items matching the pattern
110
  filtered_items = [item for item in item_list if re.search(pattern, item)]
111
 
112
  return gr.update(visible=True), gr.update(choices=filtered_items, value=filtered_items[0] if filtered_items else "")
113
 
114
 
115
- # Gradio interface
116
  with gr.Blocks() as demo:
117
  gr.Markdown("# Get Fashion Items Recommended Based On Your Search..\n"
118
  "## Recommender System implemented based Pinecone Vector Database with Dense & Sparse Embeddings and Hybrid Search..")
@@ -124,17 +111,12 @@ with gr.Blocks() as demo:
124
  value= "Select an item from this list or start typing", allow_custom_value=True, interactive=True, visible=False)
125
  slider = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.5, label="Adjust the Slider to get better recommendations that suit what you are looking for..", interactive=True)
126
 
127
- # Automatically update the text input when a dropdown selection is made
128
  dropdown.change(fn=update_textbox, inputs=dropdown, outputs=text_input)
129
 
130
- # HTML output box to display images
131
  html_output = gr.HTML(label="Relevant Images")
132
 
133
  submit_btn.click(fn=text_process, inputs=[text_input], outputs=[dropdown, dropdown])
134
-
135
- # Process and display images based on text input or slider changes
136
  text_input.change(fn=process_input, inputs=[text_input, slider], outputs=html_output)
137
-
138
  slider.change(fn=process_input, inputs=[text_input, slider], outputs=html_output)
139
 
140
  demo.launch(debug=True, share=True)
 
22
  bm25 = BM25Encoder()
23
  bm25.fit(metadata['productDisplayName'])
24
 
 
25
  def display_result(image_batch, match_batch):
26
  figures = []
27
  for img, title in zip(image_batch, match_batch):
28
+
29
  if img.mode != 'RGB':
30
  img = img.convert('RGB')
31
 
 
32
  b = BytesIO()
33
  img.save(b, format='PNG')
34
  img_str = b64encode(b.getvalue()).decode('utf-8')
35
 
 
36
  figures.append(f'''
37
  <figure style="margin: 0; padding: 0; text-align: left;">
38
  <figcaption style="font-weight: bold; margin:0;">{title}</figcaption>
 
40
  </figure>
41
  ''')
42
 
 
43
  html_content = f'''
44
  <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; align-items: start;">
45
  {''.join(figures)}
 
48
  return html_content
49
 
50
 
 
51
  def hybrid_scale(dense, sparse, alpha):
52
  if alpha < 0 or alpha > 1:
53
  raise ValueError("Alpha must be between 0 and 1")
54
 
 
55
  hsparse = {
56
  'indices': sparse['indices'],
57
  'values': [v * (1 - alpha) for v in sparse['values']]
 
61
  return hdense, hsparse
62
 
63
 
 
64
  def process_input(query, slider_value):
65
  try:
66
  slider_value = float(slider_value)
 
83
  return display_result(imgs, matches)
84
 
85
  except Exception as e:
86
+ return f"<p style='color:red;'>Not found. Try another search</p>"
 
87
 
88
 
 
89
  def update_textbox(choice):
90
  return choice
91
 
92
 
93
  def text_process(search_string):
 
94
  search_words = search_string.title().split()
95
 
 
96
  pattern = r"(?=.*\b" + r"\b)(?=.*\b".join(map(re.escape, search_words)) + r"\b)"
97
 
 
98
  filtered_items = [item for item in item_list if re.search(pattern, item)]
99
 
100
  return gr.update(visible=True), gr.update(choices=filtered_items, value=filtered_items[0] if filtered_items else "")
101
 
102
 
 
103
  with gr.Blocks() as demo:
104
  gr.Markdown("# Get Fashion Items Recommended Based On Your Search..\n"
105
  "## Recommender System implemented based Pinecone Vector Database with Dense & Sparse Embeddings and Hybrid Search..")
 
111
  value= "Select an item from this list or start typing", allow_custom_value=True, interactive=True, visible=False)
112
  slider = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.5, label="Adjust the Slider to get better recommendations that suit what you are looking for..", interactive=True)
113
 
 
114
  dropdown.change(fn=update_textbox, inputs=dropdown, outputs=text_input)
115
 
 
116
  html_output = gr.HTML(label="Relevant Images")
117
 
118
  submit_btn.click(fn=text_process, inputs=[text_input], outputs=[dropdown, dropdown])
 
 
119
  text_input.change(fn=process_input, inputs=[text_input, slider], outputs=html_output)
 
120
  slider.change(fn=process_input, inputs=[text_input, slider], outputs=html_output)
121
 
122
  demo.launch(debug=True, share=True)