Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -66,10 +66,7 @@ def find_similar_recipes_list(query_text):
|
|
| 66 |
for idx in top_indices:
|
| 67 |
row = df_recipes.iloc[idx]
|
| 68 |
title = row['Title']
|
| 69 |
-
|
| 70 |
-
desc = str(row['Raw_Output'])[:800] + "..." # Increased text limit
|
| 71 |
-
|
| 72 |
-
# Wrapped in sim-scroll for the 400px height
|
| 73 |
card_content = f"### ๐ {title}\n\n<div class='sim-scroll'>{desc}</div>"
|
| 74 |
results_list.append(card_content)
|
| 75 |
|
|
@@ -86,37 +83,20 @@ def format_recipe(json_data):
|
|
| 86 |
text = f"๐ฝ๏ธ {title}\n\n๐ INGREDIENTS:\n{ing}\n\n๐ณ INSTRUCTIONS:\n{inst}"
|
| 87 |
return text, f"{title} {ing} {inst}"
|
| 88 |
|
| 89 |
-
# --- NEW: PIPELINE WRAPPER TO HANDLE VISIBILITY UPDATES ---
|
| 90 |
def ui_update_pipeline(image_path):
|
| 91 |
if not hf_token:
|
| 92 |
return "Error: HF_TOKEN missing", "", gr.update(), gr.update(), "", gr.update(), ""
|
| 93 |
-
|
| 94 |
try:
|
| 95 |
os.environ["HF_TOKEN"] = hf_token
|
| 96 |
digitizer = RecipeDigitalizerPipeline()
|
| 97 |
json_res = digitizer.run_pipeline(image_path)
|
| 98 |
readable, query = format_recipe(json_res)
|
| 99 |
-
|
| 100 |
if query:
|
| 101 |
sim_list = find_similar_recipes_list(query)
|
| 102 |
else:
|
| 103 |
sim_list = ["No query generated.", "", ""]
|
| 104 |
|
| 105 |
-
|
| 106 |
-
# 1. Digitized Text
|
| 107 |
-
# 2. Sim Card 1 Text
|
| 108 |
-
# 3. Sim Card 1 Buttons (Visible)
|
| 109 |
-
# 4. Sim Card 2 Container (Visible)
|
| 110 |
-
# 5. Sim Card 2 Text
|
| 111 |
-
# 6. Sim Card 3 Container (Visible)
|
| 112 |
-
# 7. Sim Card 3 Text
|
| 113 |
-
return (
|
| 114 |
-
readable,
|
| 115 |
-
sim_list[0], gr.update(visible=True), # Card 1
|
| 116 |
-
gr.update(visible=True), sim_list[1], # Card 2
|
| 117 |
-
gr.update(visible=True), sim_list[2] # Card 3
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
except Exception as e:
|
| 121 |
return f"Error: {e}", "Error", gr.update(), gr.update(), "", gr.update(), ""
|
| 122 |
|
|
@@ -182,14 +162,17 @@ body, .gradio-container { background-color: #f0f2f5; }
|
|
| 182 |
border-left: 4px solid #1877f2 !important;
|
| 183 |
}
|
| 184 |
|
| 185 |
-
/* General Cards */
|
| 186 |
.content-card {
|
| 187 |
background: white;
|
| 188 |
border-radius: 12px;
|
| 189 |
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 190 |
border: 1px solid #ddd;
|
| 191 |
padding: 20px;
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
| 193 |
height: 100%;
|
| 194 |
overflow: visible !important;
|
| 195 |
}
|
|
@@ -207,7 +190,6 @@ body, .gradio-container { background-color: #f0f2f5; }
|
|
| 207 |
justify-content: space-between;
|
| 208 |
}
|
| 209 |
|
| 210 |
-
/* FIX 1: Increased height to 400px */
|
| 211 |
.sim-scroll {
|
| 212 |
height: 400px;
|
| 213 |
overflow-y: auto;
|
|
@@ -231,7 +213,7 @@ body, .gradio-container { background-color: #f0f2f5; }
|
|
| 231 |
/* Gap Fix */
|
| 232 |
.gap-fix { gap: 25px !important; }
|
| 233 |
|
| 234 |
-
/*
|
| 235 |
.gradio-examples {
|
| 236 |
display: flex;
|
| 237 |
justify-content: center;
|
|
@@ -267,7 +249,7 @@ with gr.Blocks(title="Legacy Kitchen") as demo:
|
|
| 267 |
<span class="app-slogan">Turning Handwritten Notes into a Digital Cookbook.</span>
|
| 268 |
</div>
|
| 269 |
</div>
|
| 270 |
-
<div style="color: #65676b; font-weight: 600;">v3.
|
| 271 |
</div>
|
| 272 |
""")
|
| 273 |
|
|
@@ -292,17 +274,11 @@ with gr.Blocks(title="Legacy Kitchen") as demo:
|
|
| 292 |
|
| 293 |
# === VIEW 1: AI DIGITALIZER ===
|
| 294 |
with gr.Group(visible=True) as digitalizer_view:
|
| 295 |
-
|
| 296 |
-
# SECTION 1: UPLOAD & TEXT
|
| 297 |
with gr.Row(elem_classes=["gap-fix"]):
|
| 298 |
-
|
| 299 |
-
# Left: Upload & Examples
|
| 300 |
with gr.Column(scale=1):
|
| 301 |
with gr.Group(elem_classes=["content-card"]):
|
| 302 |
input_img = gr.Image(type="filepath", label="Upload", height=300)
|
| 303 |
magic_btn = gr.Button("โจ Convert to Digital", variant="primary", size="lg")
|
| 304 |
-
|
| 305 |
-
# Examples (Centered via CSS)
|
| 306 |
gr.Examples(
|
| 307 |
examples=[
|
| 308 |
["quick_tries_images/applecrisp.jpg"],
|
|
@@ -313,65 +289,45 @@ with gr.Blocks(title="Legacy Kitchen") as demo:
|
|
| 313 |
label="Or try these examples:",
|
| 314 |
cache_examples=False
|
| 315 |
)
|
| 316 |
-
|
| 317 |
-
# Right: Text
|
| 318 |
with gr.Column(scale=1):
|
| 319 |
with gr.Group(elem_classes=["content-card"]):
|
| 320 |
-
out_text = gr.Textbox(
|
| 321 |
-
label="Result",
|
| 322 |
-
value="Here your digitalized recipe will be presented",
|
| 323 |
-
lines=20,
|
| 324 |
-
interactive=False,
|
| 325 |
-
show_label=False
|
| 326 |
-
)
|
| 327 |
|
| 328 |
gr.HTML("<div style='height: 35px;'></div>")
|
| 329 |
-
|
| 330 |
-
# SECTION 2: SIMILAR RECIPES
|
| 331 |
gr.Markdown("### 3. Similar Recipes from Database")
|
| 332 |
|
| 333 |
with gr.Row():
|
| 334 |
-
# CARD 1 (Always Visible, Buttons Hidden initially)
|
| 335 |
with gr.Column(elem_classes=["sim-card"]) as c1_box:
|
| 336 |
sim1 = gr.Markdown("Once you will upload your scanned recipe, we will share similar recipes!")
|
| 337 |
-
# FIX 2: Hidden initially
|
| 338 |
with gr.Row(visible=False) as c1_btns:
|
| 339 |
gr.Button("๐ Like", size="sm", variant="secondary")
|
| 340 |
gr.Button("โ๏ธ Share", size="sm", variant="secondary")
|
| 341 |
|
| 342 |
-
# CARD 2 (Hidden initially)
|
| 343 |
with gr.Column(elem_classes=["sim-card"], visible=False) as c2_box:
|
| 344 |
sim2 = gr.Markdown("")
|
| 345 |
with gr.Row():
|
| 346 |
gr.Button("๐ Like", size="sm", variant="secondary")
|
| 347 |
gr.Button("โ๏ธ Share", size="sm", variant="secondary")
|
| 348 |
|
| 349 |
-
# CARD 3 (Hidden initially)
|
| 350 |
with gr.Column(elem_classes=["sim-card"], visible=False) as c3_box:
|
| 351 |
sim3 = gr.Markdown("")
|
| 352 |
with gr.Row():
|
| 353 |
gr.Button("๐ Like", size="sm", variant="secondary")
|
| 354 |
gr.Button("โ๏ธ Share", size="sm", variant="secondary")
|
| 355 |
|
| 356 |
-
|
| 357 |
-
magic_btn.click(
|
| 358 |
-
ui_update_pipeline,
|
| 359 |
-
input_img,
|
| 360 |
-
[out_text, sim1, c1_btns, c2_box, sim2, c3_box, sim3]
|
| 361 |
-
)
|
| 362 |
|
| 363 |
# === VIEW 2: FEED ===
|
| 364 |
with gr.Group(visible=False) as feed_view:
|
| 365 |
-
|
| 366 |
-
with gr.Row():
|
| 367 |
-
gr.HTML(f'<img src="data:image/jpeg;base64,{profile_b64}" style="width:40px; height:40px; border-radius:50%; object-fit:cover;">')
|
| 368 |
-
gr.Textbox(placeholder=f"What recipe are you cooking today?", show_label=False, container=False, scale=10)
|
| 369 |
|
| 370 |
if not df_recipes.empty:
|
| 371 |
feed_samples = df_recipes.sample(10)
|
| 372 |
for index, row in feed_samples.iterrows():
|
| 373 |
user_name = random.choice(["Grandma Rose", "Chef Mike", "Sarah J."])
|
| 374 |
emoji = random.choice(["๐ฅ", "๐ฅ", "๐ฐ", "๐ฎ"])
|
|
|
|
|
|
|
| 375 |
with gr.Group(elem_classes=["content-card"]):
|
| 376 |
gr.HTML(f"""
|
| 377 |
<div style="display:flex; gap:10px; align-items:center; margin-bottom:12px;">
|
|
|
|
| 66 |
for idx in top_indices:
|
| 67 |
row = df_recipes.iloc[idx]
|
| 68 |
title = row['Title']
|
| 69 |
+
desc = str(row['Raw_Output'])[:800] + "..."
|
|
|
|
|
|
|
|
|
|
| 70 |
card_content = f"### ๐ {title}\n\n<div class='sim-scroll'>{desc}</div>"
|
| 71 |
results_list.append(card_content)
|
| 72 |
|
|
|
|
| 83 |
text = f"๐ฝ๏ธ {title}\n\n๐ INGREDIENTS:\n{ing}\n\n๐ณ INSTRUCTIONS:\n{inst}"
|
| 84 |
return text, f"{title} {ing} {inst}"
|
| 85 |
|
|
|
|
| 86 |
def ui_update_pipeline(image_path):
|
| 87 |
if not hf_token:
|
| 88 |
return "Error: HF_TOKEN missing", "", gr.update(), gr.update(), "", gr.update(), ""
|
|
|
|
| 89 |
try:
|
| 90 |
os.environ["HF_TOKEN"] = hf_token
|
| 91 |
digitizer = RecipeDigitalizerPipeline()
|
| 92 |
json_res = digitizer.run_pipeline(image_path)
|
| 93 |
readable, query = format_recipe(json_res)
|
|
|
|
| 94 |
if query:
|
| 95 |
sim_list = find_similar_recipes_list(query)
|
| 96 |
else:
|
| 97 |
sim_list = ["No query generated.", "", ""]
|
| 98 |
|
| 99 |
+
return (readable, sim_list[0], gr.update(visible=True), gr.update(visible=True), sim_list[1], gr.update(visible=True), sim_list[2])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
return f"Error: {e}", "Error", gr.update(), gr.update(), "", gr.update(), ""
|
| 102 |
|
|
|
|
| 162 |
border-left: 4px solid #1877f2 !important;
|
| 163 |
}
|
| 164 |
|
| 165 |
+
/* General Cards (Used for Feed Posts) */
|
| 166 |
.content-card {
|
| 167 |
background: white;
|
| 168 |
border-radius: 12px;
|
| 169 |
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 170 |
border: 1px solid #ddd;
|
| 171 |
padding: 20px;
|
| 172 |
+
|
| 173 |
+
/* FIX: 7px Spacing between blocks */
|
| 174 |
+
margin-bottom: 7px !important;
|
| 175 |
+
|
| 176 |
height: 100%;
|
| 177 |
overflow: visible !important;
|
| 178 |
}
|
|
|
|
| 190 |
justify-content: space-between;
|
| 191 |
}
|
| 192 |
|
|
|
|
| 193 |
.sim-scroll {
|
| 194 |
height: 400px;
|
| 195 |
overflow-y: auto;
|
|
|
|
| 213 |
/* Gap Fix */
|
| 214 |
.gap-fix { gap: 25px !important; }
|
| 215 |
|
| 216 |
+
/* Center Quick Start Images */
|
| 217 |
.gradio-examples {
|
| 218 |
display: flex;
|
| 219 |
justify-content: center;
|
|
|
|
| 249 |
<span class="app-slogan">Turning Handwritten Notes into a Digital Cookbook.</span>
|
| 250 |
</div>
|
| 251 |
</div>
|
| 252 |
+
<div style="color: #65676b; font-weight: 600;">v3.6</div>
|
| 253 |
</div>
|
| 254 |
""")
|
| 255 |
|
|
|
|
| 274 |
|
| 275 |
# === VIEW 1: AI DIGITALIZER ===
|
| 276 |
with gr.Group(visible=True) as digitalizer_view:
|
|
|
|
|
|
|
| 277 |
with gr.Row(elem_classes=["gap-fix"]):
|
|
|
|
|
|
|
| 278 |
with gr.Column(scale=1):
|
| 279 |
with gr.Group(elem_classes=["content-card"]):
|
| 280 |
input_img = gr.Image(type="filepath", label="Upload", height=300)
|
| 281 |
magic_btn = gr.Button("โจ Convert to Digital", variant="primary", size="lg")
|
|
|
|
|
|
|
| 282 |
gr.Examples(
|
| 283 |
examples=[
|
| 284 |
["quick_tries_images/applecrisp.jpg"],
|
|
|
|
| 289 |
label="Or try these examples:",
|
| 290 |
cache_examples=False
|
| 291 |
)
|
|
|
|
|
|
|
| 292 |
with gr.Column(scale=1):
|
| 293 |
with gr.Group(elem_classes=["content-card"]):
|
| 294 |
+
out_text = gr.Textbox(label="Result", value="Here your digitalized recipe will be presented", lines=20, interactive=False, show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
gr.HTML("<div style='height: 35px;'></div>")
|
|
|
|
|
|
|
| 297 |
gr.Markdown("### 3. Similar Recipes from Database")
|
| 298 |
|
| 299 |
with gr.Row():
|
|
|
|
| 300 |
with gr.Column(elem_classes=["sim-card"]) as c1_box:
|
| 301 |
sim1 = gr.Markdown("Once you will upload your scanned recipe, we will share similar recipes!")
|
|
|
|
| 302 |
with gr.Row(visible=False) as c1_btns:
|
| 303 |
gr.Button("๐ Like", size="sm", variant="secondary")
|
| 304 |
gr.Button("โ๏ธ Share", size="sm", variant="secondary")
|
| 305 |
|
|
|
|
| 306 |
with gr.Column(elem_classes=["sim-card"], visible=False) as c2_box:
|
| 307 |
sim2 = gr.Markdown("")
|
| 308 |
with gr.Row():
|
| 309 |
gr.Button("๐ Like", size="sm", variant="secondary")
|
| 310 |
gr.Button("โ๏ธ Share", size="sm", variant="secondary")
|
| 311 |
|
|
|
|
| 312 |
with gr.Column(elem_classes=["sim-card"], visible=False) as c3_box:
|
| 313 |
sim3 = gr.Markdown("")
|
| 314 |
with gr.Row():
|
| 315 |
gr.Button("๐ Like", size="sm", variant="secondary")
|
| 316 |
gr.Button("โ๏ธ Share", size="sm", variant="secondary")
|
| 317 |
|
| 318 |
+
magic_btn.click(ui_update_pipeline, input_img, [out_text, sim1, c1_btns, c2_box, sim2, c3_box, sim3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
# === VIEW 2: FEED ===
|
| 321 |
with gr.Group(visible=False) as feed_view:
|
| 322 |
+
# FIX: Removed the "What's Cooking" search bar section entirely
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
if not df_recipes.empty:
|
| 325 |
feed_samples = df_recipes.sample(10)
|
| 326 |
for index, row in feed_samples.iterrows():
|
| 327 |
user_name = random.choice(["Grandma Rose", "Chef Mike", "Sarah J."])
|
| 328 |
emoji = random.choice(["๐ฅ", "๐ฅ", "๐ฐ", "๐ฎ"])
|
| 329 |
+
|
| 330 |
+
# Each post is in its own 'content-card' Group, which now has 7px margin
|
| 331 |
with gr.Group(elem_classes=["content-card"]):
|
| 332 |
gr.HTML(f"""
|
| 333 |
<div style="display:flex; gap:10px; align-items:center; margin-bottom:12px;">
|