Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2349,57 +2349,98 @@ with gr.Blocks(title="Bitesized AI Meal Planner") as demo:
|
|
| 2349 |
skipped_ingredients = 0
|
| 2350 |
error_messages = []
|
| 2351 |
|
| 2352 |
-
# Save all recipes
|
| 2353 |
for i, title in enumerate(recipe_generator.recent_titles):
|
| 2354 |
-
|
| 2355 |
-
|
| 2356 |
-
|
| 2357 |
-
|
| 2358 |
-
|
| 2359 |
-
|
| 2360 |
-
|
| 2361 |
-
|
| 2362 |
-
|
| 2363 |
-
|
| 2364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2365 |
save_result = recipe_generator.save_current_recipe()
|
| 2366 |
print(f"Save result for {title}: {save_result}")
|
| 2367 |
|
| 2368 |
if "successfully" in save_result.lower() or "saved" in save_result.lower():
|
| 2369 |
saved_count += 1
|
|
|
|
| 2370 |
print(f"β
Successfully saved: {title}")
|
| 2371 |
|
| 2372 |
# Extract and add ingredients
|
| 2373 |
-
|
| 2374 |
-
|
| 2375 |
-
|
| 2376 |
-
for ingredient in ingredients:
|
| 2377 |
-
# Convert to realistic shopping quantity
|
| 2378 |
-
shopping_ingredient = shopping_list_manager.convert_to_shopping_quantity(ingredient)
|
| 2379 |
-
print(f"Converted '{ingredient}' β '{shopping_ingredient}'")
|
| 2380 |
|
| 2381 |
-
|
| 2382 |
-
|
| 2383 |
-
|
| 2384 |
-
|
| 2385 |
-
|
| 2386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2387 |
else:
|
| 2388 |
-
|
| 2389 |
-
|
| 2390 |
-
|
| 2391 |
-
|
| 2392 |
-
|
| 2393 |
-
|
| 2394 |
-
|
| 2395 |
-
print(f"β {
|
|
|
|
|
|
|
| 2396 |
|
| 2397 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2398 |
failed_count += 1
|
| 2399 |
-
|
| 2400 |
-
|
| 2401 |
-
print(f"β Error processing recipe {title}: {str(e)}")
|
| 2402 |
-
continue
|
| 2403 |
|
| 2404 |
print(f"Final count - Saved: {saved_count}, Failed: {failed_count}, Total recipes: {len(recipe_generator.recent_titles)}")
|
| 2405 |
|
|
@@ -2425,6 +2466,7 @@ with gr.Blocks(title="Bitesized AI Meal Planner") as demo:
|
|
| 2425 |
if skipped_ingredients > 0:
|
| 2426 |
status_msg += " ({0} already in list)".format(skipped_ingredients)
|
| 2427 |
status_msg += "\n\n**Failed recipes:** " + ", ".join([msg.split(":")[0] for msg in error_messages[:3]])
|
|
|
|
| 2428 |
else:
|
| 2429 |
status_msg = "π **Success!** Saved all {0} recipes and added {1} ingredients to your shopping list!".format(
|
| 2430 |
saved_count, total_ingredients
|
|
|
|
| 2349 |
skipped_ingredients = 0
|
| 2350 |
error_messages = []
|
| 2351 |
|
| 2352 |
+
# Save all recipes with retry logic
|
| 2353 |
for i, title in enumerate(recipe_generator.recent_titles):
|
| 2354 |
+
max_retries = 2
|
| 2355 |
+
retry_count = 0
|
| 2356 |
+
recipe_saved = False
|
| 2357 |
+
|
| 2358 |
+
while retry_count < max_retries and not recipe_saved:
|
| 2359 |
+
try:
|
| 2360 |
+
if retry_count > 0:
|
| 2361 |
+
print(f"π Retry {retry_count}/{max_retries-1} for: {title}")
|
| 2362 |
+
|
| 2363 |
+
print(f"Processing recipe {i+1}/{len(recipe_generator.recent_titles)}: {title}")
|
| 2364 |
+
|
| 2365 |
+
# Try to generate full recipe
|
| 2366 |
+
recipe_content = recipe_generator.generate_full_recipe(
|
| 2367 |
+
i, cost_per_meal, postcode, preferences,
|
| 2368 |
+
meals_per_week, people_count, meal_type, budget_preference
|
| 2369 |
+
)
|
| 2370 |
+
|
| 2371 |
+
# Check if generation was successful
|
| 2372 |
+
if not recipe_content:
|
| 2373 |
+
retry_count += 1
|
| 2374 |
+
print(f"β No content generated for {title}, attempt {retry_count}/{max_retries}")
|
| 2375 |
+
import time
|
| 2376 |
+
time.sleep(1) # Brief delay before retry
|
| 2377 |
+
continue
|
| 2378 |
+
|
| 2379 |
+
if "Error" in recipe_content or "error" in recipe_content.lower():
|
| 2380 |
+
retry_count += 1
|
| 2381 |
+
print(f"β Error in content for {title}, attempt {retry_count}/{max_retries}")
|
| 2382 |
+
import time
|
| 2383 |
+
time.sleep(1)
|
| 2384 |
+
continue
|
| 2385 |
+
|
| 2386 |
+
# Content looks good, try to save
|
| 2387 |
save_result = recipe_generator.save_current_recipe()
|
| 2388 |
print(f"Save result for {title}: {save_result}")
|
| 2389 |
|
| 2390 |
if "successfully" in save_result.lower() or "saved" in save_result.lower():
|
| 2391 |
saved_count += 1
|
| 2392 |
+
recipe_saved = True
|
| 2393 |
print(f"β
Successfully saved: {title}")
|
| 2394 |
|
| 2395 |
# Extract and add ingredients
|
| 2396 |
+
try:
|
| 2397 |
+
ingredients = shopping_list_manager.extract_ingredients_from_recipe(recipe_content)
|
| 2398 |
+
print(f"Found {len(ingredients)} ingredients in {title}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2399 |
|
| 2400 |
+
for ingredient in ingredients:
|
| 2401 |
+
try:
|
| 2402 |
+
# Convert to realistic shopping quantity
|
| 2403 |
+
shopping_ingredient = shopping_list_manager.convert_to_shopping_quantity(ingredient)
|
| 2404 |
+
print(f"Converted '{ingredient}' β '{shopping_ingredient}'")
|
| 2405 |
+
|
| 2406 |
+
category = shopping_list_manager.categorize_ingredient(shopping_ingredient)
|
| 2407 |
+
result = shopping_list_manager.add_item(shopping_ingredient, "", "From: {0}".format(title), category)
|
| 2408 |
+
if "already in" in result:
|
| 2409 |
+
skipped_ingredients += 1
|
| 2410 |
+
else:
|
| 2411 |
+
total_ingredients += 1
|
| 2412 |
+
except Exception as ing_error:
|
| 2413 |
+
print(f"β οΈ Error processing ingredient '{ingredient}': {str(ing_error)}")
|
| 2414 |
+
continue
|
| 2415 |
+
except Exception as extract_error:
|
| 2416 |
+
print(f"β οΈ Error extracting ingredients from {title}: {str(extract_error)}")
|
| 2417 |
+
# Recipe is saved but ingredients failed - that's OK
|
| 2418 |
+
|
| 2419 |
+
break # Success, exit retry loop
|
| 2420 |
else:
|
| 2421 |
+
retry_count += 1
|
| 2422 |
+
print(f"β Failed to save {title}: {save_result}, attempt {retry_count}/{max_retries}")
|
| 2423 |
+
import time
|
| 2424 |
+
time.sleep(1)
|
| 2425 |
+
|
| 2426 |
+
except Exception as e:
|
| 2427 |
+
retry_count += 1
|
| 2428 |
+
print(f"β Exception processing {title}: {str(e)}, attempt {retry_count}/{max_retries}")
|
| 2429 |
+
import time
|
| 2430 |
+
time.sleep(1)
|
| 2431 |
|
| 2432 |
+
if retry_count >= max_retries:
|
| 2433 |
+
# Final failure after all retries
|
| 2434 |
+
failed_count += 1
|
| 2435 |
+
error_msg = f"{title}: {str(e)[:100]}" # Truncate long errors
|
| 2436 |
+
error_messages.append(error_msg)
|
| 2437 |
+
print(f"β FINAL FAILURE for {title} after {max_retries} attempts: {str(e)}")
|
| 2438 |
+
|
| 2439 |
+
# If we exhausted retries without success
|
| 2440 |
+
if not recipe_saved:
|
| 2441 |
failed_count += 1
|
| 2442 |
+
if not error_messages or error_messages[-1].split(":")[0] != title:
|
| 2443 |
+
error_messages.append(f"{title}: Failed after {max_retries} attempts")
|
|
|
|
|
|
|
| 2444 |
|
| 2445 |
print(f"Final count - Saved: {saved_count}, Failed: {failed_count}, Total recipes: {len(recipe_generator.recent_titles)}")
|
| 2446 |
|
|
|
|
| 2466 |
if skipped_ingredients > 0:
|
| 2467 |
status_msg += " ({0} already in list)".format(skipped_ingredients)
|
| 2468 |
status_msg += "\n\n**Failed recipes:** " + ", ".join([msg.split(":")[0] for msg in error_messages[:3]])
|
| 2469 |
+
status_msg += "\n\nπ‘ **Tip:** Try generating fewer recipes at once, or click Generate again to get different recipes."
|
| 2470 |
else:
|
| 2471 |
status_msg = "π **Success!** Saved all {0} recipes and added {1} ingredients to your shopping list!".format(
|
| 2472 |
saved_count, total_ingredients
|