Spaces:
Sleeping
Sleeping
refactored
Browse files
app.py
CHANGED
@@ -1,7 +1,10 @@
|
|
1 |
import os
|
2 |
import sys
|
|
|
3 |
from utils import load_embeddings
|
4 |
-
from
|
|
|
|
|
5 |
|
6 |
# Path to the embeddings file
|
7 |
EMBEDDINGS_PATH = "ingredient_embeddings_voyageai.pkl"
|
@@ -12,18 +15,186 @@ if not os.path.exists(EMBEDDINGS_PATH):
|
|
12 |
print(f"Please ensure the file exists at {os.path.abspath(EMBEDDINGS_PATH)}")
|
13 |
sys.exit(1)
|
14 |
|
15 |
-
# Load embeddings
|
16 |
try:
|
17 |
embeddings_data = load_embeddings(EMBEDDINGS_PATH)
|
18 |
-
#
|
19 |
import ui
|
20 |
ui.embeddings = embeddings_data
|
21 |
except Exception as e:
|
22 |
print(f"Error loading embeddings: {e}")
|
23 |
sys.exit(1)
|
24 |
|
25 |
-
#
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
+
import gradio as gr
|
4 |
from utils import load_embeddings
|
5 |
+
from embeddings import create_product_embeddings
|
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"
|
|
|
15 |
print(f"Please ensure the file exists at {os.path.abspath(EMBEDDINGS_PATH)}")
|
16 |
sys.exit(1)
|
17 |
|
18 |
+
# Load embeddings globally
|
19 |
try:
|
20 |
embeddings_data = load_embeddings(EMBEDDINGS_PATH)
|
21 |
+
# Make embeddings available to the UI functions
|
22 |
import ui
|
23 |
ui.embeddings = embeddings_data
|
24 |
except Exception as e:
|
25 |
print(f"Error loading embeddings: {e}")
|
26 |
sys.exit(1)
|
27 |
|
28 |
+
# Basic CSS theme
|
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 |
+
submit_button = gr.Button("Categorize Products", variant="primary")
|
132 |
+
|
133 |
+
with gr.Column(scale=3):
|
134 |
+
text_output = gr.HTML(label="Categorization Results",
|
135 |
+
value="<div style='height: 450px; display: flex; justify-content: center; align-items: center; color: #666;'>Results will appear here</div>")
|
136 |
+
|
137 |
+
submit_button.click(
|
138 |
+
fn=categorize_products_from_text,
|
139 |
+
inputs=[text_input, top_n, confidence],
|
140 |
+
outputs=text_output
|
141 |
+
)
|
142 |
+
|
143 |
+
with gr.TabItem("File Upload"):
|
144 |
+
with gr.Row():
|
145 |
+
with gr.Column(scale=2):
|
146 |
+
file_input = gr.File(
|
147 |
+
label="Upload JSON or text file with products",
|
148 |
+
file_types=[".json", ".txt"]
|
149 |
+
)
|
150 |
+
|
151 |
+
with gr.Accordion("Help", open=False):
|
152 |
+
gr.Markdown("""
|
153 |
+
- JSON files should contain either:
|
154 |
+
- A list of objects with a 'name' field for each product
|
155 |
+
- A simple array of product name strings
|
156 |
+
- Text files should have one product name per line
|
157 |
+
""")
|
158 |
+
|
159 |
+
with gr.Row():
|
160 |
+
with gr.Column(scale=1):
|
161 |
+
file_top_n = gr.Slider(
|
162 |
+
minimum=1,
|
163 |
+
maximum=10,
|
164 |
+
value=5,
|
165 |
+
step=1,
|
166 |
+
label="Number of Top Categories"
|
167 |
+
)
|
168 |
+
with gr.Column(scale=1):
|
169 |
+
file_confidence = gr.Slider(
|
170 |
+
minimum=0.1,
|
171 |
+
maximum=0.9,
|
172 |
+
value=0.5,
|
173 |
+
step=0.05,
|
174 |
+
label="Confidence Threshold"
|
175 |
+
)
|
176 |
+
|
177 |
+
file_button = gr.Button("Process File", variant="primary")
|
178 |
+
|
179 |
+
with gr.Column(scale=3):
|
180 |
+
file_output = gr.HTML(
|
181 |
+
label="Categorization Results",
|
182 |
+
value="<div style='height: 450px; display: flex; justify-content: center; align-items: center; color: #666;'>Upload a file to see results</div>"
|
183 |
+
)
|
184 |
+
|
185 |
+
file_button.click(
|
186 |
+
fn=categorize_products_from_file,
|
187 |
+
inputs=[file_input, file_top_n, file_confidence],
|
188 |
+
outputs=file_output
|
189 |
+
)
|
190 |
+
|
191 |
+
# Footer
|
192 |
+
gr.HTML("""
|
193 |
+
<div style="margin-top: 20px; text-align: center; color: #666;">
|
194 |
+
Powered by Voyage AI embeddings • Built with Gradio
|
195 |
+
</div>
|
196 |
+
""")
|
197 |
+
|
198 |
+
# For Hugging Face Spaces compatibility - do not remove
|
199 |
+
if __name__ == "__main__":
|
200 |
+
app.launch()
|