OGOGOG commited on
Commit
de2cb80
Β·
verified Β·
1 Parent(s): 2f6e139

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -24
app.py CHANGED
@@ -12,6 +12,14 @@ DATASET_ID = "motimmom/cocktails_clean_nobrand" # <- change if needed
12
  EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
13
  FLAVOR_BOOST = 0.20
14
 
 
 
 
 
 
 
 
 
15
  # If dataset is private, add Space secret HF_TOKEN (read scope)
16
  HF_READ_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
17
  load_kwargs = {}
@@ -243,16 +251,14 @@ def _split_measure_name_line(line: str):
243
 
244
  def _format_ingredients_markdown(lines):
245
  """
246
- Bullet points with 'Ingredient (amount)'. Also removes any [ or ].
247
  """
248
  if not lines:
249
  return "β€”"
250
  formatted = []
251
  for ln in lines:
252
- # Strip any square brackets the dataset might include
253
  ln = ln.replace("[", "").replace("]", "")
254
  meas, name = _split_measure_name_line(ln)
255
- # Flip to Ingredient (amount)
256
  if name and meas:
257
  formatted.append(f"- {name} ({meas})")
258
  elif name:
@@ -306,28 +312,62 @@ def recommend(base_alcohol_text, flavor, top_k=3):
306
  return "\n\n---\n\n".join(blocks)
307
 
308
  # ========================
309
- # UI
310
  # ========================
311
- with gr.Blocks() as demo:
312
- gr.Markdown("# 🍹 AI Bartender β€” Type a Base + Flavor (Clear Ingredients)")
313
-
314
- with gr.Row():
315
- base_text = gr.Textbox(value="gin", label="Base alcohol (type any spirit, e.g., 'gin', 'white rum', 'bourbon')")
316
- flavor = gr.Dropdown(choices=FLAVOR_OPTIONS, value="citrus", label="Flavor")
317
- topk = gr.Slider(1, 10, value=3, step=1, label="Number of recommendations")
318
-
319
- with gr.Row():
320
- ex1 = gr.Button("Example: Gin + Citrus")
321
- ex2 = gr.Button("Example: Rum + Fruity")
322
- ex3 = gr.Button("Example: Mezcal + Smoky")
323
-
324
- out = gr.Markdown()
325
- gr.Button("Recommend").click(recommend, [base_text, flavor, topk], out)
326
-
327
- # Quick-fill examples
328
- ex1.click(lambda: ("gin", "citrus", 3), outputs=[base_text, flavor, topk])
329
- ex2.click(lambda: ("white rum", "fruity", 3), outputs=[base_text, flavor, topk])
330
- ex3.click(lambda: ("mezcal", "smoky", 3), outputs=[base_text, flavor, topk])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
332
  if __name__ == "__main__":
333
  demo.launch()
 
12
  EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
13
  FLAVOR_BOOST = 0.20
14
 
15
+ # Backgrounds (override default with env var BG_URL)
16
+ BACKGROUND_CHOICES = {
17
+ "Tiki": "https://images.unsplash.com/photo-1514362545857-3bc16c4c76cf?q=80&w=1920&auto=format&fit=crop",
18
+ "Martini": "https://images.unsplash.com/photo-1536935339748-021c8d927f21?q=80&w=1920&auto=format&fit=crop",
19
+ "Negroni": "https://images.unsplash.com/photo-1551024709-8f23befc6cf7?q=80&w=1920&auto=format&fit=crop",
20
+ }
21
+ DEFAULT_BG_URL = os.getenv("BG_URL") or BACKGROUND_CHOICES["Negroni"]
22
+
23
  # If dataset is private, add Space secret HF_TOKEN (read scope)
24
  HF_READ_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
25
  load_kwargs = {}
 
251
 
252
  def _format_ingredients_markdown(lines):
253
  """
254
+ Bullet points as 'Ingredient (amount)'. Strips [ and ] if present.
255
  """
256
  if not lines:
257
  return "β€”"
258
  formatted = []
259
  for ln in lines:
 
260
  ln = ln.replace("[", "").replace("]", "")
261
  meas, name = _split_measure_name_line(ln)
 
262
  if name and meas:
263
  formatted.append(f"- {name} ({meas})")
264
  elif name:
 
312
  return "\n\n---\n\n".join(blocks)
313
 
314
  # ========================
315
+ # Background + UI
316
  # ========================
317
+ def render_bg_div(url: str) -> str:
318
+ return f"<div id='app-bg' style=\"position:fixed;inset:0;z-index:-1;background-image:url('{url}');background-size:cover;background-position:center;filter:brightness(0.45);\"></div>"
319
+
320
+ CUSTOM_CSS = """
321
+ .gradio-container { background: transparent !important; }
322
+ .glass-card {
323
+ background: rgba(255, 255, 255, 0.08);
324
+ backdrop-filter: blur(6px);
325
+ -webkit-backdrop-filter: blur(6px);
326
+ border-radius: 14px;
327
+ padding: 18px;
328
+ border: 1px solid rgba(255, 255, 255, 0.12);
329
+ }
330
+ """
331
+
332
+ def choose_background(choice, custom_url):
333
+ if choice in BACKGROUND_CHOICES:
334
+ url = BACKGROUND_CHOICES[choice]
335
+ else:
336
+ url = custom_url.strip() or DEFAULT_BG_URL
337
+ return render_bg_div(url)
338
+
339
+ with gr.Blocks(css=CUSTOM_CSS) as demo:
340
+ bg_html = gr.HTML(render_bg_div(DEFAULT_BG_URL))
341
+
342
+ with gr.Column(elem_classes=["glass-card"]):
343
+ gr.Markdown("# 🍹 AI Bartender β€” Type a Base + Flavor (Clear Ingredients)")
344
+
345
+ with gr.Row():
346
+ base_text = gr.Textbox(value="gin", label="Base alcohol (type any spirit, e.g., 'gin', 'white rum', 'bourbon')")
347
+ flavor = gr.Dropdown(choices=FLAVOR_OPTIONS, value="citrus", label="Flavor")
348
+ topk = gr.Slider(1, 10, value=3, step=1, label="Number of recommendations")
349
+
350
+ with gr.Row():
351
+ ex1 = gr.Button("Example: Gin + Citrus")
352
+ ex2 = gr.Button("Example: Rum + Fruity")
353
+ ex3 = gr.Button("Example: Mezcal + Smoky")
354
+
355
+ out = gr.Markdown()
356
+ gr.Button("Recommend").click(recommend, [base_text, flavor, topk], out)
357
+
358
+ gr.Markdown("### 🎨 Background")
359
+ with gr.Row():
360
+ bg_choice = gr.Radio(choices=list(BACKGROUND_CHOICES.keys()) + ["Custom URL"], value="Negroni", label="Theme")
361
+ bg_custom = gr.Textbox(label="Custom background image URL", placeholder="https://... (used only if 'Custom URL' is selected)")
362
+
363
+ # Update background on change
364
+ bg_choice.change(choose_background, [bg_choice, bg_custom], bg_html)
365
+ bg_custom.change(choose_background, [bg_choice, bg_custom], bg_html)
366
+
367
+ # Quick-fill examples
368
+ ex1.click(lambda: ("gin", "citrus", 3), outputs=[base_text, flavor, topk])
369
+ ex2.click(lambda: ("white rum", "fruity", 3), outputs=[base_text, flavor, topk])
370
+ ex3.click(lambda: ("mezcal", "smoky", 3), outputs=[base_text, flavor, topk])
371
 
372
  if __name__ == "__main__":
373
  demo.launch()