Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from collections import Counter
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
# 1. Load an instruction-tuned model for text generation
|
| 7 |
+
generator = pipeline(
|
| 8 |
+
"text2text-generation",
|
| 9 |
+
model="google/flan-t5-small",
|
| 10 |
+
device=0 # set to -1 for CPU
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def plan_and_list(ingredients, days, goal):
|
| 14 |
+
# 2. Build the prompt
|
| 15 |
+
prompt = (
|
| 16 |
+
f"Plan {days} days of breakfast, lunch, and dinner menus "
|
| 17 |
+
f"using these ingredients: {ingredients}. "
|
| 18 |
+
f"Meet {goal}."
|
| 19 |
+
)
|
| 20 |
+
# 3. Generate the meal plan
|
| 21 |
+
result = generator(prompt, max_length=512, do_sample=False)[0]["generated_text"]
|
| 22 |
+
|
| 23 |
+
# 4. Parse meals and build grocery list
|
| 24 |
+
# (naïve parsing—split lines, extract items after colon)
|
| 25 |
+
grocery_counter = Counter()
|
| 26 |
+
for line in result.split("\n"):
|
| 27 |
+
# e.g. "Day 1 Lunch: Grilled chicken with quinoa and spinach"
|
| 28 |
+
if ":" in line and any(meal in line for meal in ["Breakfast", "Lunch", "Dinner"]):
|
| 29 |
+
items = re.split(r"with|and|,", line.split(":",1)[1])
|
| 30 |
+
for item in items:
|
| 31 |
+
item = item.strip().lower()
|
| 32 |
+
if item:
|
| 33 |
+
grocery_counter[item] += 1
|
| 34 |
+
|
| 35 |
+
shopping_list = "\n".join(f"{item}: {qty}" for item, qty in grocery_counter.items())
|
| 36 |
+
return result, shopping_list
|
| 37 |
+
|
| 38 |
+
# 5. Gradio UI
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=plan_and_list,
|
| 41 |
+
inputs=[
|
| 42 |
+
gr.Textbox(label="Ingredients (comma-separated)", placeholder="e.g. eggs, spinach, quinoa…"),
|
| 43 |
+
gr.Slider(minimum=1, maximum=7, step=1, label="Days to plan"),
|
| 44 |
+
gr.Textbox(label="Calorie Goal / Notes", placeholder="e.g. 1800 kcal/day, vegetarian…")
|
| 45 |
+
],
|
| 46 |
+
outputs=[
|
| 47 |
+
gr.Textbox(label="Generated Meal Plan"),
|
| 48 |
+
gr.Textbox(label="Consolidated Grocery List")
|
| 49 |
+
],
|
| 50 |
+
title="🍽️ Smart Meal Planner & Grocery List Generator",
|
| 51 |
+
description="Enter what you have (or your dietary goals) and get a weekly menu plus a unified shopping list.",
|
| 52 |
+
)
|
| 53 |
+
iface.launch()
|