tdurzynski commited on
Commit
75e97f4
Β·
verified Β·
1 Parent(s): 7842b50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -40
app.py CHANGED
@@ -233,9 +233,20 @@ def save_metadata(user_id, s3_path, food_name, portion_size, portion_unit, cooki
233
  's3_path': s3_path,
234
  'tokens_awarded': tokens_awarded
235
  }
236
- # Add debug logging
 
237
  if debug_mode:
238
  st.sidebar.info(f"DynamoDB Save: {image_id} - {food_name}")
 
 
 
 
 
 
 
 
 
 
239
 
240
  # Save to DynamoDB
241
  metadata_table.put_item(Item=item)
@@ -468,49 +479,112 @@ if debug_mode and "processed_image" in st.session_state:
468
  else:
469
  st.error("❌ Processed upload failed!")
470
 
471
- # Add DynamoDB Test Panel for Debug Mode
472
  if debug_mode and "processed_image" in st.session_state:
473
- with st.expander("πŸ” DEBUG: DynamoDB Test", expanded=True):
474
- st.warning("⚠️ Debug Mode: Test DynamoDB functionality")
475
- test_db_col1, test_db_col2 = st.columns(2)
476
- with test_db_col1:
477
- test_food = st.text_input("Test Food Name", "TEST_ITEM")
478
- test_portion = st.number_input("Test Portion", value=1.0)
479
- with test_db_col2:
480
- test_unit = st.selectbox("Test Unit", UNIT_OPTIONS)
481
- test_cooking = st.selectbox("Test Cooking", [""] + COOKING_METHODS)
482
-
483
- test_ingredients = st_tags.st_tags(
484
- label="Test Ingredients",
485
- text="Press enter to add",
486
- value=["test_ingredient"],
487
- maxtags=3
488
- )
489
 
490
- test_db_button = st.button("Test DynamoDB Save")
491
- if test_db_button:
492
- # Use the processed path from testing if available
493
- s3_path = st.session_state.get("test_processed_s3_path", "test/processed-512x512/test_image.jpg")
494
 
495
- # Convert to Decimal for DynamoDB
496
- portion_size_decimal = Decimal(str(test_portion))
497
-
498
- # Call save_metadata with test data
499
- success = save_metadata(
500
- st.session_state["user_id"],
501
- s3_path,
502
- f"TEST_{test_food}",
503
- portion_size_decimal,
504
- test_unit,
505
- test_cooking,
506
- test_ingredients,
507
- 1 # Token value for test
508
  )
509
 
510
- if success:
511
- st.success("βœ… DynamoDB save successful!")
512
- else:
513
- st.error("❌ DynamoDB save failed!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514
 
515
  # Display existing food annotations if any
516
  if st.session_state["food_items"]:
@@ -726,4 +800,13 @@ if debug_mode and st.sidebar.checkbox("Show Cleanup Tools"):
726
 
727
  st.sidebar.success(f"Deleted {deleted_records}/{total_records} test records and their S3 objects")
728
  except Exception as e:
729
- st.sidebar.error(f"Cleanup error: {e}")
 
 
 
 
 
 
 
 
 
 
233
  's3_path': s3_path,
234
  'tokens_awarded': tokens_awarded
235
  }
236
+
237
+ # Add debug logging with more details
238
  if debug_mode:
239
  st.sidebar.info(f"DynamoDB Save: {image_id} - {food_name}")
240
+ if "test_debug_details" not in st.session_state:
241
+ st.session_state["test_debug_details"] = []
242
+
243
+ # Store details for debugging
244
+ st.session_state["test_debug_details"].append({
245
+ "time": datetime.now().isoformat(),
246
+ "image_id": image_id,
247
+ "food": food_name,
248
+ "s3_path": s3_path
249
+ })
250
 
251
  # Save to DynamoDB
252
  metadata_table.put_item(Item=item)
 
479
  else:
480
  st.error("❌ Processed upload failed!")
481
 
482
+ # Add DynamoDB Test Panel for Multiple Food Items
483
  if debug_mode and "processed_image" in st.session_state:
484
+ with st.expander("πŸ” DEBUG: Complete Food Annotation Test", expanded=True):
485
+ st.warning("⚠️ Debug Mode: Test Complete Flow with Multiple Food Items")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486
 
487
+ # Create a test session state if it doesn't exist
488
+ if "test_food_items" not in st.session_state:
489
+ st.session_state["test_food_items"] = []
 
490
 
491
+ # Form for adding test food items
492
+ with st.form(key="test_food_form"):
493
+ test_food = st.text_input("Food Name", "Test Pizza")
494
+ test_portion = st.number_input("Portion Size", value=1.0)
495
+ test_unit = st.selectbox("Unit", UNIT_OPTIONS)
496
+ test_cooking = st.selectbox("Cooking Method", COOKING_METHODS)
497
+ test_ingredients = st_tags.st_tags(
498
+ label="Ingredients",
499
+ text="Press enter to add",
500
+ value=["Test Ingredient"],
501
+ maxtags=5
 
 
502
  )
503
 
504
+ test_add_button = st.form_submit_button("Add Test Food Item")
505
+
506
+ if test_add_button:
507
+ st.session_state["test_food_items"].append({
508
+ "food_name": f"TEST_{test_food}",
509
+ "portion_size": test_portion,
510
+ "portion_unit": test_unit,
511
+ "cooking_method": test_cooking,
512
+ "ingredients": test_ingredients
513
+ })
514
+ st.success(f"Added test food item: {test_food}")
515
+ st.rerun()
516
+
517
+ # Display test food items
518
+ if st.session_state["test_food_items"]:
519
+ st.subheader("Test Food Items")
520
+ for i, item in enumerate(st.session_state["test_food_items"]):
521
+ with st.expander(f"Item #{i+1}: {item['food_name']}"):
522
+ st.write(f"**Portion:** {item['portion_size']} {item['portion_unit']}")
523
+ st.write(f"**Cooking Method:** {item['cooking_method']}")
524
+ st.write(f"**Ingredients:** {', '.join(item['ingredients'])}")
525
+ if st.button(f"Remove Test Item #{i+1}", key=f"remove_test_{i}"):
526
+ st.session_state["test_food_items"].pop(i)
527
+ st.rerun()
528
+
529
+ # Button to run the full test
530
+ test_full_button = st.button("Run Complete Annotation Test")
531
+ if test_full_button and st.session_state["test_food_items"]:
532
+ with st.spinner("Testing complete annotation flow..."):
533
+ # Upload both original and processed images
534
+ raw_s3_path = upload_to_s3(original_img, st.session_state["user_id"], folder="raw-uploads")
535
+ processed_s3_path = upload_to_s3(processed_img, st.session_state["user_id"], folder="processed-512x512")
536
+
537
+ all_saved = True
538
+ saved_items = 0
539
+
540
+ if raw_s3_path and processed_s3_path:
541
+ # Set up a results area
542
+ results_area = st.empty()
543
+
544
+ # Save each food item
545
+ for item in st.session_state["test_food_items"]:
546
+ try:
547
+ # Convert to Decimal
548
+ portion_size_decimal = Decimal(str(item["portion_size"]))
549
+
550
+ # Save to DynamoDB
551
+ success = save_metadata(
552
+ st.session_state["user_id"],
553
+ processed_s3_path,
554
+ item["food_name"],
555
+ portion_size_decimal,
556
+ item["portion_unit"],
557
+ item["cooking_method"],
558
+ item["ingredients"],
559
+ 1 # Test token value
560
+ )
561
+
562
+ if success:
563
+ saved_items += 1
564
+ else:
565
+ all_saved = False
566
+ break
567
+ except Exception as e:
568
+ results_area.error(f"Error saving {item['food_name']}: {e}")
569
+ all_saved = False
570
+ break
571
+
572
+ if all_saved:
573
+ st.success(f"βœ… Successfully saved {saved_items} food items!")
574
+ st.json({
575
+ "user_id": st.session_state["user_id"],
576
+ "s3_paths": {
577
+ "raw": raw_s3_path,
578
+ "processed": processed_s3_path
579
+ },
580
+ "food_items": st.session_state["test_food_items"]
581
+ })
582
+ else:
583
+ st.error("❌ Failed to save all food items")
584
+ else:
585
+ st.error("❌ Failed to upload images to S3")
586
+ else:
587
+ st.info("Add test food items above to run a complete annotation test")
588
 
589
  # Display existing food annotations if any
590
  if st.session_state["food_items"]:
 
800
 
801
  st.sidebar.success(f"Deleted {deleted_records}/{total_records} test records and their S3 objects")
802
  except Exception as e:
803
+ st.sidebar.error(f"Cleanup error: {e}")
804
+
805
+
806
+ # Add Debug Info Panel at the end of your app
807
+ if debug_mode and "test_debug_details" in st.session_state and st.session_state["test_debug_details"]:
808
+ with st.expander("πŸ” DEBUG: Last Test Results"):
809
+ st.json(st.session_state["test_debug_details"])
810
+ if st.button("Clear Debug History"):
811
+ st.session_state["test_debug_details"] = []
812
+ st.rerun()