compare to chicory-ui
Browse files
app.py
CHANGED
@@ -2,9 +2,7 @@ import os
|
|
2 |
import sys
|
3 |
import gradio as gr
|
4 |
from utils import load_embeddings
|
5 |
-
from
|
6 |
-
from similarity import compute_similarities
|
7 |
-
from ui import categorize_products_from_text, categorize_products_from_file
|
8 |
|
9 |
# Path to the embeddings file
|
10 |
EMBEDDINGS_PATH = "ingredient_embeddings_voyageai.pkl"
|
@@ -25,182 +23,7 @@ except Exception as e:
|
|
25 |
print(f"Error loading embeddings: {e}")
|
26 |
sys.exit(1)
|
27 |
|
28 |
-
#
|
29 |
-
css = """
|
30 |
-
.container {
|
31 |
-
max-width: 1200px;
|
32 |
-
margin: auto;
|
33 |
-
padding: 0;
|
34 |
-
}
|
35 |
-
footer {display: none !important;}
|
36 |
-
.header {
|
37 |
-
background-color: #0d47a1;
|
38 |
-
padding: 15px 20px;
|
39 |
-
border-radius: 10px;
|
40 |
-
color: white;
|
41 |
-
margin-bottom: 20px;
|
42 |
-
display: flex;
|
43 |
-
align-items: center;
|
44 |
-
}
|
45 |
-
.header svg {
|
46 |
-
margin-right: 10px;
|
47 |
-
height: 30px;
|
48 |
-
width: 30px;
|
49 |
-
}
|
50 |
-
.header h1 {
|
51 |
-
margin: 0;
|
52 |
-
font-size: 24px;
|
53 |
-
}
|
54 |
-
.description {
|
55 |
-
margin-bottom: 20px;
|
56 |
-
padding: 15px;
|
57 |
-
background-color: #f5f5f5;
|
58 |
-
border-radius: 5px;
|
59 |
-
}
|
60 |
-
"""
|
61 |
-
|
62 |
-
# Custom theme
|
63 |
-
theme = gr.themes.Soft(
|
64 |
-
primary_hue="blue",
|
65 |
-
secondary_hue="indigo",
|
66 |
-
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"]
|
67 |
-
).set(
|
68 |
-
button_primary_background_fill="*primary_500",
|
69 |
-
button_primary_background_fill_hover="*primary_600",
|
70 |
-
button_secondary_background_fill="*neutral_200",
|
71 |
-
block_title_text_size="lg",
|
72 |
-
block_label_text_size="md"
|
73 |
-
)
|
74 |
-
|
75 |
-
# Create the Gradio interface directly in this file for Spaces
|
76 |
-
with gr.Blocks(css=css, theme=theme) as app:
|
77 |
-
# Header with icon
|
78 |
-
gr.HTML("""
|
79 |
-
<div class="header">
|
80 |
-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white">
|
81 |
-
<path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"></path>
|
82 |
-
</svg>
|
83 |
-
<h1>Product Categorization Tool</h1>
|
84 |
-
</div>
|
85 |
-
<div class="description">
|
86 |
-
This tool analyzes products and finds the most similar ingredients using AI embeddings.
|
87 |
-
Just enter product names or upload a file to get started.
|
88 |
-
</div>
|
89 |
-
""")
|
90 |
-
|
91 |
-
with gr.Tabs():
|
92 |
-
with gr.TabItem("Text Input"):
|
93 |
-
with gr.Row():
|
94 |
-
with gr.Column(scale=2):
|
95 |
-
example_products = [
|
96 |
-
"Tomato Sauce\nApple Pie\nGreek Yogurt\nChocolate Chip Cookies",
|
97 |
-
"Banana Bread\nOrange Juice\nGrilled Chicken\nCaesar Salad",
|
98 |
-
"Vanilla Ice Cream\nPizza Dough\nStrawberry Jam\nGrilled Salmon"
|
99 |
-
]
|
100 |
-
|
101 |
-
text_input = gr.Textbox(
|
102 |
-
lines=10,
|
103 |
-
placeholder="Enter product names, one per line",
|
104 |
-
label="Product Names"
|
105 |
-
)
|
106 |
-
|
107 |
-
gr.Examples(
|
108 |
-
examples=example_products,
|
109 |
-
inputs=text_input,
|
110 |
-
label="Example Product Sets"
|
111 |
-
)
|
112 |
-
|
113 |
-
with gr.Row():
|
114 |
-
with gr.Column(scale=1):
|
115 |
-
top_n = gr.Slider(
|
116 |
-
minimum=1,
|
117 |
-
maximum=10,
|
118 |
-
value=5,
|
119 |
-
step=1,
|
120 |
-
label="Number of Top Categories"
|
121 |
-
)
|
122 |
-
with gr.Column(scale=1):
|
123 |
-
confidence = gr.Slider(
|
124 |
-
minimum=0.1,
|
125 |
-
maximum=0.9,
|
126 |
-
value=0.5,
|
127 |
-
step=0.05,
|
128 |
-
label="Confidence Threshold"
|
129 |
-
)
|
130 |
-
|
131 |
-
compare_chicory = gr.Checkbox(
|
132 |
-
label="Compare with Chicory Parser V3",
|
133 |
-
value=False,
|
134 |
-
info="When enabled, results will include comparisons with Chicory Parser V3 API"
|
135 |
-
)
|
136 |
-
|
137 |
-
submit_button = gr.Button("Categorize Products", variant="primary")
|
138 |
-
|
139 |
-
with gr.Column(scale=3):
|
140 |
-
text_output = gr.HTML(label="Categorization Results",
|
141 |
-
value="<div style='height: 450px; display: flex; justify-content: center; align-items: center; color: #666;'>Results will appear here</div>")
|
142 |
-
|
143 |
-
submit_button.click(
|
144 |
-
fn=categorize_products_from_text,
|
145 |
-
inputs=[text_input, top_n, confidence],
|
146 |
-
outputs=text_output
|
147 |
-
)
|
148 |
-
|
149 |
-
with gr.TabItem("File Upload"):
|
150 |
-
with gr.Row():
|
151 |
-
with gr.Column(scale=2):
|
152 |
-
file_input = gr.File(
|
153 |
-
label="Upload JSON or text file with products",
|
154 |
-
file_types=[".json", ".txt"]
|
155 |
-
)
|
156 |
-
|
157 |
-
with gr.Accordion("Help", open=False):
|
158 |
-
gr.Markdown("""
|
159 |
-
- JSON files should contain either:
|
160 |
-
- A list of objects with a 'name' field for each product
|
161 |
-
- A simple array of product name strings
|
162 |
-
- Text files should have one product name per line
|
163 |
-
""")
|
164 |
-
|
165 |
-
with gr.Row():
|
166 |
-
with gr.Column(scale=1):
|
167 |
-
file_top_n = gr.Slider(
|
168 |
-
minimum=1,
|
169 |
-
maximum=10,
|
170 |
-
value=5,
|
171 |
-
step=1,
|
172 |
-
label="Number of Top Categories"
|
173 |
-
)
|
174 |
-
with gr.Column(scale=1):
|
175 |
-
file_confidence = gr.Slider(
|
176 |
-
minimum=0.1,
|
177 |
-
maximum=0.9,
|
178 |
-
value=0.5,
|
179 |
-
step=0.05,
|
180 |
-
label="Confidence Threshold"
|
181 |
-
)
|
182 |
-
|
183 |
-
file_button = gr.Button("Process File", variant="primary")
|
184 |
-
|
185 |
-
with gr.Column(scale=3):
|
186 |
-
file_output = gr.HTML(
|
187 |
-
label="Categorization Results",
|
188 |
-
value="<div style='height: 450px; display: flex; justify-content: center; align-items: center; color: #666;'>Upload a file to see results</div>"
|
189 |
-
)
|
190 |
-
|
191 |
-
file_button.click(
|
192 |
-
fn=categorize_products_from_file,
|
193 |
-
inputs=[file_input, file_top_n, file_confidence],
|
194 |
-
outputs=file_output
|
195 |
-
)
|
196 |
-
|
197 |
-
# Footer
|
198 |
-
gr.HTML("""
|
199 |
-
<div style="margin-top: 20px; text-align: center; color: #666;">
|
200 |
-
Powered by Voyage AI embeddings • Built with Gradio
|
201 |
-
</div>
|
202 |
-
""")
|
203 |
-
|
204 |
-
# For Hugging Face Spaces compatibility - do not remove
|
205 |
if __name__ == "__main__":
|
206 |
-
|
|
|
|
2 |
import sys
|
3 |
import gradio as gr
|
4 |
from utils import load_embeddings
|
5 |
+
from ui import categorize_products, create_demo # Updated imports
|
|
|
|
|
6 |
|
7 |
# Path to the embeddings file
|
8 |
EMBEDDINGS_PATH = "ingredient_embeddings_voyageai.pkl"
|
|
|
23 |
print(f"Error loading embeddings: {e}")
|
24 |
sys.exit(1)
|
25 |
|
26 |
+
# Launch the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
if __name__ == "__main__":
|
28 |
+
demo = create_demo()
|
29 |
+
demo.launch()
|