Spaces:
Sleeping
Sleeping
Update app.py
#1
by Leospencer1 - opened
app.py
CHANGED
|
@@ -34,11 +34,11 @@ else:
|
|
| 34 |
print("Configuring Gemini API...")
|
| 35 |
genai.configure(api_key=gemini_api_key)
|
| 36 |
|
| 37 |
-
# OPTIMIZATION: Use faster model configuration
|
| 38 |
generation_config = {
|
| 39 |
-
"temperature": 0.
|
| 40 |
-
"top_p": 0.
|
| 41 |
-
"top_k":
|
| 42 |
"max_output_tokens": 2048,
|
| 43 |
}
|
| 44 |
|
|
@@ -233,7 +233,12 @@ class WeeklyBudgetRecipeGenerator:
|
|
| 233 |
self.current_recipe = None
|
| 234 |
self.recent_titles = []
|
| 235 |
self.recipe_history = []
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
self.current_cuisine_index = 0
|
| 238 |
self.ingredient_overlap_enabled = True
|
| 239 |
self.common_ingredients = []
|
|
@@ -280,7 +285,8 @@ class WeeklyBudgetRecipeGenerator:
|
|
| 280 |
|
| 281 |
def get_variation_context(self, meal_type, keep_titles=None):
|
| 282 |
recent_for_type = [title for title in self.recipe_history[-20:] if meal_type.lower() in title.lower()]
|
| 283 |
-
|
|
|
|
| 284 |
exclusions = []
|
| 285 |
if keep_titles:
|
| 286 |
exclusions.extend(keep_titles)
|
|
@@ -329,32 +335,52 @@ class WeeklyBudgetRecipeGenerator:
|
|
| 329 |
"- Onions {1} 1.20/kg bag (use in ALL recipes)\n"
|
| 330 |
"- Garlic {1} 0.65/bulb (use in ALL recipes)\n"
|
| 331 |
"- Tinned tomatoes {1} 0.45 (buy multiple tins)\n"
|
| 332 |
-
"- Rice {1} 2/kg OR Pasta {1} 0.90/500g (as base)\n\n"
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
prompt = "Generate {0} {1} RECIPE NAMES that share ingredients to minimize weekly shopping cost.\n\n".format(num_to_generate, meal_type.lower())
|
| 339 |
prompt += "Weekly Budget: {0} {1}/meal x {2} meals for {3} people\n".format(currency, cost_per_meal, meals_per_week, people_count)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
if preferences:
|
| 341 |
prompt += "Preferences: {0}\n\n".format(preferences)
|
| 342 |
else:
|
| 343 |
prompt += "\n"
|
|
|
|
| 344 |
prompt += ingredient_list
|
| 345 |
-
prompt +=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
prompt += self.get_budget_optimization_prompt(budget_preference, cost_per_meal, currency)
|
| 347 |
prompt += "\n\nCRITICAL: Return ONLY {0} recipe names. NO explanations, NO preamble, NO descriptions.\n".format(num_to_generate)
|
| 348 |
prompt += "Format: One recipe name per line (2-4 words each).\n"
|
| 349 |
-
prompt += "Each recipe
|
| 350 |
-
prompt += "Examples:\n"
|
| 351 |
prompt += "Chicken Curry\n"
|
| 352 |
-
prompt += "Garlic
|
| 353 |
-
prompt += "Tomato
|
| 354 |
-
prompt += "Spanish Chicken
|
| 355 |
-
prompt += "
|
| 356 |
-
prompt += "Creamy
|
| 357 |
-
prompt += "
|
|
|
|
| 358 |
|
| 359 |
response = self.model.generate_content(prompt)
|
| 360 |
titles_text = response.text.strip()
|
|
@@ -425,11 +451,11 @@ class WeeklyBudgetRecipeGenerator:
|
|
| 425 |
clean_titles.append(clean_title)
|
| 426 |
seen_titles_lower.add(clean_title_lower)
|
| 427 |
|
| 428 |
-
# Fallback titles
|
| 429 |
fallback_titles = {
|
| 430 |
-
"Breakfast": ["Scrambled Eggs", "Toast and Jam", "Porridge"],
|
| 431 |
-
"Lunch": ["Ham Sandwich", "Soup and Roll", "Pasta Salad"],
|
| 432 |
-
"Dinner": ["Beef Stew", "Chicken Curry", "Fish Pie"],
|
| 433 |
}
|
| 434 |
while len(clean_titles) < num_to_generate:
|
| 435 |
meal_fallbacks = fallback_titles.get(meal_type, ["Simple Meal"])
|
|
@@ -760,7 +786,8 @@ with gr.Blocks(title="Bitesized AI Meal Planner") as demo:
|
|
| 760 |
|
| 761 |
gr.Markdown("### Step 1: Set Your Budget")
|
| 762 |
with gr.Row():
|
| 763 |
-
|
|
|
|
| 764 |
people_count = gr.Slider(label="👥 People", minimum=1, maximum=10, value=4, step=1, scale=1)
|
| 765 |
meals_per_week = gr.Slider(label="📅 Meals This Week", minimum=3, maximum=21, value=7, step=1, scale=1)
|
| 766 |
|
|
@@ -806,22 +833,18 @@ with gr.Blocks(title="Bitesized AI Meal Planner") as demo:
|
|
| 806 |
with gr.Column(scale=2):
|
| 807 |
recipe_markdown = gr.Markdown("""
|
| 808 |
### Welcome to Bitesized AI Meal Planner! 👋
|
| 809 |
-
|
| 810 |
**Here's how it works:**
|
| 811 |
-
|
| 812 |
1. **Set your budget** - Enter cost per meal and number of people
|
| 813 |
2. **Click Generate** - AI creates recipes that share ingredients
|
| 814 |
3. **View recipes** - Click any recipe to see full details
|
| 815 |
4. **Generate shopping list** - Get a complete weekly shopping list
|
| 816 |
5. **Save favorites** - Save recipes you love
|
| 817 |
-
|
| 818 |
**Why ingredient sharing?** 🛒
|
| 819 |
Instead of buying different ingredients for each meal, all your recipes use the same core ingredients. This means:
|
| 820 |
- Lower weekly shopping costs
|
| 821 |
- Less food waste
|
| 822 |
- Simpler shopping trips
|
| 823 |
- Realistic meal planning
|
| 824 |
-
|
| 825 |
👆 **Get started by setting your budget above!**
|
| 826 |
""")
|
| 827 |
|
|
@@ -899,7 +922,7 @@ Instead of buying different ingredients for each meal, all your recipes use the
|
|
| 899 |
|
| 900 |
with gr.Row():
|
| 901 |
with gr.Column():
|
| 902 |
-
supermarket_cost_per_meal = gr.Number(label="£ Cost Per Meal", value=3.50)
|
| 903 |
supermarket_people = gr.Slider(label="👥 People", minimum=1, maximum=10, value=4)
|
| 904 |
|
| 905 |
with gr.Column():
|
|
@@ -1184,4 +1207,4 @@ print("✓ Weekly shopping list generation")
|
|
| 1184 |
print("✓ Budget-friendly meal planning")
|
| 1185 |
print("="*60 + "\n")
|
| 1186 |
|
| 1187 |
-
demo.queue(max_size=10).launch(debug=True, share=True, max_threads=40)
|
|
|
|
| 34 |
print("Configuring Gemini API...")
|
| 35 |
genai.configure(api_key=gemini_api_key)
|
| 36 |
|
| 37 |
+
# OPTIMIZATION: Use faster model configuration with HIGHER temperature for more variety
|
| 38 |
generation_config = {
|
| 39 |
+
"temperature": 0.9, # INCREASED from 0.7 for more randomness
|
| 40 |
+
"top_p": 0.95, # INCREASED from 0.8 for more variety
|
| 41 |
+
"top_k": 50, # INCREASED from 40 for more options
|
| 42 |
"max_output_tokens": 2048,
|
| 43 |
}
|
| 44 |
|
|
|
|
| 233 |
self.current_recipe = None
|
| 234 |
self.recent_titles = []
|
| 235 |
self.recipe_history = []
|
| 236 |
+
# EXPANDED cuisine list for MORE VARIETY
|
| 237 |
+
self.cuisine_rotation = [
|
| 238 |
+
"British", "Italian", "Indian", "Chinese", "Mexican", "Mediterranean",
|
| 239 |
+
"Thai", "Japanese", "Greek", "Spanish", "French", "Middle Eastern",
|
| 240 |
+
"American", "Korean", "Vietnamese", "Caribbean"
|
| 241 |
+
]
|
| 242 |
self.current_cuisine_index = 0
|
| 243 |
self.ingredient_overlap_enabled = True
|
| 244 |
self.common_ingredients = []
|
|
|
|
| 285 |
|
| 286 |
def get_variation_context(self, meal_type, keep_titles=None):
|
| 287 |
recent_for_type = [title for title in self.recipe_history[-20:] if meal_type.lower() in title.lower()]
|
| 288 |
+
# Pick a RANDOM cuisine for more variety
|
| 289 |
+
current_cuisine = random.choice(self.cuisine_rotation)
|
| 290 |
exclusions = []
|
| 291 |
if keep_titles:
|
| 292 |
exclusions.extend(keep_titles)
|
|
|
|
| 335 |
"- Onions {1} 1.20/kg bag (use in ALL recipes)\n"
|
| 336 |
"- Garlic {1} 0.65/bulb (use in ALL recipes)\n"
|
| 337 |
"- Tinned tomatoes {1} 0.45 (buy multiple tins)\n"
|
| 338 |
+
"- Rice {1} 2/kg OR Pasta {1} 0.90/500g (as base)\n\n").format(num_to_generate, currency)
|
| 339 |
+
|
| 340 |
+
# ADD EXCLUSIONS to prompt for more variety
|
| 341 |
+
exclusions_text = ""
|
| 342 |
+
if exclusions:
|
| 343 |
+
exclusions_text = "\n\nDO NOT GENERATE THESE (already used recently):\n"
|
| 344 |
+
for exc in exclusions[-10:]: # Show last 10
|
| 345 |
+
exclusions_text += "- {0}\n".format(exc)
|
| 346 |
+
exclusions_text += "\nMake sure ALL {0} recipes are COMPLETELY DIFFERENT from the above!\n".format(num_to_generate)
|
| 347 |
|
| 348 |
prompt = "Generate {0} {1} RECIPE NAMES that share ingredients to minimize weekly shopping cost.\n\n".format(num_to_generate, meal_type.lower())
|
| 349 |
prompt += "Weekly Budget: {0} {1}/meal x {2} meals for {3} people\n".format(currency, cost_per_meal, meals_per_week, people_count)
|
| 350 |
+
|
| 351 |
+
# ADD CUISINE VARIETY
|
| 352 |
+
prompt += "CUISINE INSPIRATION: Focus on {0} style dishes for variety\n".format(current_cuisine)
|
| 353 |
+
|
| 354 |
if preferences:
|
| 355 |
prompt += "Preferences: {0}\n\n".format(preferences)
|
| 356 |
else:
|
| 357 |
prompt += "\n"
|
| 358 |
+
|
| 359 |
prompt += ingredient_list
|
| 360 |
+
prompt += exclusions_text # ADD EXCLUSIONS
|
| 361 |
+
|
| 362 |
+
# EMPHASIZE VARIETY MORE
|
| 363 |
+
prompt += "\n\nCRITICAL VARIETY REQUIREMENTS:\n"
|
| 364 |
+
prompt += "- Each recipe must use DIFFERENT cooking methods (stir-fry, bake, curry, pasta, rice, soup, etc.)\n"
|
| 365 |
+
prompt += "- Each recipe must have UNIQUE flavors and styles\n"
|
| 366 |
+
prompt += "- NO similar names (e.g. don't do 'Chicken Curry' AND 'Spicy Chicken Curry')\n"
|
| 367 |
+
prompt += "- Mix cooking styles: some quick (stir-fry), some slow (stew), some baked, etc.\n"
|
| 368 |
+
prompt += "- Use {0} cuisine inspiration for authentic variety\n\n".format(current_cuisine)
|
| 369 |
+
|
| 370 |
+
prompt += "Same ingredients, COMPLETELY DIFFERENT dishes = MAXIMUM SAVINGS!\n"
|
| 371 |
prompt += self.get_budget_optimization_prompt(budget_preference, cost_per_meal, currency)
|
| 372 |
prompt += "\n\nCRITICAL: Return ONLY {0} recipe names. NO explanations, NO preamble, NO descriptions.\n".format(num_to_generate)
|
| 373 |
prompt += "Format: One recipe name per line (2-4 words each).\n"
|
| 374 |
+
prompt += "Each recipe MUST be UNIQUE with DIFFERENT cooking methods.\n"
|
| 375 |
+
prompt += "Examples of VARIETY:\n"
|
| 376 |
prompt += "Chicken Curry\n"
|
| 377 |
+
prompt += "Garlic Pasta Bake\n"
|
| 378 |
+
prompt += "Tomato Rice Soup\n"
|
| 379 |
+
prompt += "Spanish Chicken Skillet\n"
|
| 380 |
+
prompt += "Spicy Stir Fry\n"
|
| 381 |
+
prompt += "Creamy One-Pot\n"
|
| 382 |
+
prompt += "Herb Roasted Dinner\n\n"
|
| 383 |
+
prompt += "Now generate {0} COMPLETELY DIFFERENT recipe names (NO other text):".format(num_to_generate)
|
| 384 |
|
| 385 |
response = self.model.generate_content(prompt)
|
| 386 |
titles_text = response.text.strip()
|
|
|
|
| 451 |
clean_titles.append(clean_title)
|
| 452 |
seen_titles_lower.add(clean_title_lower)
|
| 453 |
|
| 454 |
+
# Fallback titles with MORE VARIETY
|
| 455 |
fallback_titles = {
|
| 456 |
+
"Breakfast": ["Scrambled Eggs", "Toast and Jam", "Porridge", "Pancakes", "Omelette", "Breakfast Burrito"],
|
| 457 |
+
"Lunch": ["Ham Sandwich", "Soup and Roll", "Pasta Salad", "Quesadilla", "Wrap", "Salad Bowl"],
|
| 458 |
+
"Dinner": ["Beef Stew", "Chicken Curry", "Fish Pie", "Pasta Bake", "Stir Fry", "Shepherd's Pie", "Tacos"],
|
| 459 |
}
|
| 460 |
while len(clean_titles) < num_to_generate:
|
| 461 |
meal_fallbacks = fallback_titles.get(meal_type, ["Simple Meal"])
|
|
|
|
| 786 |
|
| 787 |
gr.Markdown("### Step 1: Set Your Budget")
|
| 788 |
with gr.Row():
|
| 789 |
+
# FIXED: Added step=0.10 to show 3.50 instead of 3.5
|
| 790 |
+
cost_per_meal = gr.Number(label="£ Cost Per Meal", value=3.50, minimum=0.50, step=0.10, scale=1)
|
| 791 |
people_count = gr.Slider(label="👥 People", minimum=1, maximum=10, value=4, step=1, scale=1)
|
| 792 |
meals_per_week = gr.Slider(label="📅 Meals This Week", minimum=3, maximum=21, value=7, step=1, scale=1)
|
| 793 |
|
|
|
|
| 833 |
with gr.Column(scale=2):
|
| 834 |
recipe_markdown = gr.Markdown("""
|
| 835 |
### Welcome to Bitesized AI Meal Planner! 👋
|
|
|
|
| 836 |
**Here's how it works:**
|
|
|
|
| 837 |
1. **Set your budget** - Enter cost per meal and number of people
|
| 838 |
2. **Click Generate** - AI creates recipes that share ingredients
|
| 839 |
3. **View recipes** - Click any recipe to see full details
|
| 840 |
4. **Generate shopping list** - Get a complete weekly shopping list
|
| 841 |
5. **Save favorites** - Save recipes you love
|
|
|
|
| 842 |
**Why ingredient sharing?** 🛒
|
| 843 |
Instead of buying different ingredients for each meal, all your recipes use the same core ingredients. This means:
|
| 844 |
- Lower weekly shopping costs
|
| 845 |
- Less food waste
|
| 846 |
- Simpler shopping trips
|
| 847 |
- Realistic meal planning
|
|
|
|
| 848 |
👆 **Get started by setting your budget above!**
|
| 849 |
""")
|
| 850 |
|
|
|
|
| 922 |
|
| 923 |
with gr.Row():
|
| 924 |
with gr.Column():
|
| 925 |
+
supermarket_cost_per_meal = gr.Number(label="£ Cost Per Meal", value=3.50, step=0.10)
|
| 926 |
supermarket_people = gr.Slider(label="👥 People", minimum=1, maximum=10, value=4)
|
| 927 |
|
| 928 |
with gr.Column():
|
|
|
|
| 1207 |
print("✓ Budget-friendly meal planning")
|
| 1208 |
print("="*60 + "\n")
|
| 1209 |
|
| 1210 |
+
demo.queue(max_size=10).launch(debug=True, share=True, max_threads=40)
|