elliesleightholm commited on
Commit
843da58
1 Parent(s): 95e9582

adding both embedding models

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -9,15 +9,12 @@ from dotenv import load_dotenv
9
 
10
  load_dotenv()
11
 
12
- # Initialize Marqo client (for local deployment)
13
- # mq = marqo.Client("http://localhost:8882", api_key=None)
14
-
15
  # Initialize Marqo client (for Marqo Cloud deployment)
16
  api_key = os.getenv("MARQO_API_KEY")
17
  mq = marqo.Client("https://api.marqo.ai", api_key=api_key)
18
 
19
  @spaces.GPU
20
- def search_marqo(query, themes, negatives):
21
  # Build query weights
22
  query_weights = {query: 1.0}
23
  if themes:
@@ -25,8 +22,8 @@ def search_marqo(query, themes, negatives):
25
  if negatives:
26
  query_weights[negatives] = -1.1
27
 
28
- # Perform search with Marqo
29
- res = mq.index("marqo-ecommerce-b").search(query_weights, limit=10) # limit to top 10 results
30
 
31
  # Prepare results
32
  products = []
@@ -42,7 +39,7 @@ def search_marqo(query, themes, negatives):
42
  image = Image.open(io.BytesIO(response.content))
43
 
44
  # Append product details for Gradio display
45
- product_info = f'{title}\n{description}\nPrice: {price}\nScore: {score:.4f}'
46
  products.append((image, product_info))
47
 
48
  return products
@@ -54,27 +51,31 @@ def clear_inputs():
54
  # Gradio Blocks Interface for Custom Layout
55
  with gr.Blocks(css=".orange-button { background-color: orange; color: black; }") as interface:
56
  gr.Markdown("<h1 style='text-align: center;'>Multimodal Ecommerce Search with Marqo's SOTA Embedding Models</h1>")
57
- gr.Markdown("### This ecommerce search demo uses:")
58
  gr.Markdown("### 1. [Marqo Cloud](https://www.marqo.ai/cloud) for the Search Engine.")
59
- gr.Markdown("### 2. [Marqo-Ecommerce-Embeddings](https://huggingface.co/collections/Marqo/marqo-ecommerce-embeddings-66f611b9bb9d035a8d164fbb) for the multimodal embedding model.")
60
- gr.Markdown("### 3. 100k products from the [Marqo-GS-10M](https://huggingface.co/datasets/Marqo/marqo-GS-10M) dataset.")
61
 
62
  gr.Markdown("")
63
 
64
  with gr.Row():
65
- query_input = gr.Textbox(placeholder="Coffee machine", label="Search Query")
66
- themes_input = gr.Textbox(placeholder="Silver", label="More of...")
67
- negatives_input = gr.Textbox(placeholder="Buttons", label="Less of...")
 
68
 
69
  with gr.Row():
70
  search_button = gr.Button("Submit", elem_classes="orange-button")
71
 
72
  results_gallery = gr.Gallery(label="Top 10 Results", columns=4)
73
 
74
- search_button.click(fn=search_marqo, inputs=[query_input, themes_input, negatives_input], outputs=results_gallery)
 
75
 
76
- query_input.submit(fn=search_marqo, inputs=[query_input, themes_input, negatives_input], outputs=results_gallery)
77
- themes_input.submit(fn=search_marqo, inputs=[query_input, themes_input, negatives_input], outputs=results_gallery)
78
- negatives_input.submit(fn=search_marqo, inputs=[query_input, themes_input, negatives_input], outputs=results_gallery)
 
79
 
 
80
  interface.launch()
 
9
 
10
  load_dotenv()
11
 
 
 
 
12
  # Initialize Marqo client (for Marqo Cloud deployment)
13
  api_key = os.getenv("MARQO_API_KEY")
14
  mq = marqo.Client("https://api.marqo.ai", api_key=api_key)
15
 
16
  @spaces.GPU
17
+ def search_marqo(model, query, themes, negatives):
18
  # Build query weights
19
  query_weights = {query: 1.0}
20
  if themes:
 
22
  if negatives:
23
  query_weights[negatives] = -1.1
24
 
25
+ # Perform search with Marqo using the selected model
26
+ res = mq.index(model).search(query_weights, limit=10) # limit to top 10 results
27
 
28
  # Prepare results
29
  products = []
 
39
  image = Image.open(io.BytesIO(response.content))
40
 
41
  # Append product details for Gradio display
42
+ product_info = f'{title}'
43
  products.append((image, product_info))
44
 
45
  return products
 
51
  # Gradio Blocks Interface for Custom Layout
52
  with gr.Blocks(css=".orange-button { background-color: orange; color: black; }") as interface:
53
  gr.Markdown("<h1 style='text-align: center;'>Multimodal Ecommerce Search with Marqo's SOTA Embedding Models</h1>")
54
+ gr.Markdown("### This search demo uses:")
55
  gr.Markdown("### 1. [Marqo Cloud](https://www.marqo.ai/cloud) for the Search Engine.")
56
+ gr.Markdown("### 2. [Marqo-Ecommerce-Embeddings](https://huggingface.co/collections/Marqo/marqo-ecommerce-embeddings-66f611b9bb9d035a8d164fbb) for the multimodal embedding models.")
57
+ gr.Markdown("### 3. Subsets of the [Marqo-GS-10M](https://huggingface.co/datasets/Marqo/marqo-GS-10M) dataset: [`marqo-ecommerce-l`](https://huggingface.co/Marqo/marqo-ecommerce-embeddings-L) uses a 200k sample of products across all categories and [`marqo-ecommerce-b`](https://huggingface.co/Marqo/marqo-ecommerce-embeddings-B) uses a random 100k subset.")
58
 
59
  gr.Markdown("")
60
 
61
  with gr.Row():
62
+ model_dropdown = gr.Dropdown(choices=["marqo-ecommerce-l", "marqo-ecommerce-b"], label="Select Model", value="marqo-ecommerce-l")
63
+ query_input = gr.Textbox(placeholder="coffee machine that takes pods", label="Search Query")
64
+ themes_input = gr.Textbox(placeholder="black machine", label="More of...")
65
+ negatives_input = gr.Textbox(placeholder="blurry images", label="Less of...")
66
 
67
  with gr.Row():
68
  search_button = gr.Button("Submit", elem_classes="orange-button")
69
 
70
  results_gallery = gr.Gallery(label="Top 10 Results", columns=4)
71
 
72
+ # Set up function call for search on button click or Enter key with model selection
73
+ search_button.click(fn=search_marqo, inputs=[model_dropdown, query_input, themes_input, negatives_input], outputs=results_gallery)
74
 
75
+ # Enable Enter key submission for all input fields
76
+ query_input.submit(fn=search_marqo, inputs=[model_dropdown, query_input, themes_input, negatives_input], outputs=results_gallery)
77
+ themes_input.submit(fn=search_marqo, inputs=[model_dropdown, query_input, themes_input, negatives_input], outputs=results_gallery)
78
+ negatives_input.submit(fn=search_marqo, inputs=[model_dropdown, query_input, themes_input, negatives_input], outputs=results_gallery)
79
 
80
+ # Launch the app
81
  interface.launch()