Leospencer1 commited on
Commit
eed2423
Β·
verified Β·
1 Parent(s): abcf098

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -38
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
- try:
2355
- print(f"Processing recipe {i+1}/{len(recipe_generator.recent_titles)}: {title}")
2356
-
2357
- # Generate full recipe using the index
2358
- recipe_content = recipe_generator.generate_full_recipe(
2359
- i, cost_per_meal, postcode, preferences,
2360
- meals_per_week, people_count, meal_type, budget_preference
2361
- )
2362
-
2363
- if recipe_content and "Error" not in recipe_content:
2364
- # Now explicitly save the recipe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- ingredients = shopping_list_manager.extract_ingredients_from_recipe(recipe_content)
2374
- print(f"Found {len(ingredients)} ingredients in {title}")
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
- category = shopping_list_manager.categorize_ingredient(shopping_ingredient)
2382
- result = shopping_list_manager.add_item(shopping_ingredient, "", "From: {0}".format(title), category)
2383
- if "already in" in result:
2384
- skipped_ingredients += 1
2385
- else:
2386
- total_ingredients += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2387
  else:
2388
- failed_count += 1
2389
- error_messages.append(f"{title}: {save_result}")
2390
- print(f"❌ Failed to save: {title} - {save_result}")
2391
- else:
2392
- failed_count += 1
2393
- error_msg = f"{title}: Could not generate recipe content"
2394
- error_messages.append(error_msg)
2395
- print(f"❌ {error_msg}")
 
 
2396
 
2397
- except Exception as e:
 
 
 
 
 
 
 
 
2398
  failed_count += 1
2399
- error_msg = f"{title}: {str(e)}"
2400
- error_messages.append(error_msg)
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