Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script to validate the food recognition improvements. | |
| Tests the smart override system and validation rules. | |
| """ | |
| import sys | |
| import os | |
| sys.path.append(os.path.dirname(__file__)) | |
| # Import the constants from the main app | |
| from app import ( | |
| SMART_FOOD_OVERRIDES, | |
| FOOD_MODELS, | |
| MIN_CONFIDENCE_THRESHOLD, | |
| MIN_ALTERNATIVE_CONFIDENCE, | |
| COMPREHENSIVE_FOOD_CATEGORIES, | |
| BALKAN_TO_FOOD101_MAPPING | |
| ) | |
| def test_smart_overrides(): | |
| """Test the smart override system.""" | |
| print("π§ͺ Testing Smart Override System") | |
| print("=" * 50) | |
| # Test 1: Fried Food overrides | |
| if "Fried Food" in SMART_FOOD_OVERRIDES: | |
| fried_overrides = SMART_FOOD_OVERRIDES["Fried Food"] | |
| print(f"β Fried Food overrides: {len(fried_overrides)} items") | |
| # Check key items | |
| if "fish_and_chips" in fried_overrides: | |
| print(f" π Fish and Chips: {fried_overrides['fish_and_chips']}") | |
| if "pancakes" in fried_overrides: | |
| print(f" π₯ Pancakes: {fried_overrides['pancakes']}") | |
| # Test 2: Model priority changes | |
| print(f"\nπ Model Priorities:") | |
| for model_key, config in FOOD_MODELS.items(): | |
| priority = config["priority"] | |
| desc = config["description"] | |
| print(f" {priority}. {model_key}: {desc}") | |
| print(f"\nβοΈ Confidence Thresholds:") | |
| print(f" Minimum: {MIN_CONFIDENCE_THRESHOLD:.1%}") | |
| print(f" Alternative: {MIN_ALTERNATIVE_CONFIDENCE:.1%}") | |
| # Test model count | |
| active_models = len(FOOD_MODELS) | |
| print(f"\nπ― Active Models: {active_models}") | |
| if active_models == 1: | |
| print(" β PERFECT: Only Food-101 enabled (no generic models)") | |
| else: | |
| print(" β οΈ WARNING: Multiple models active") | |
| return True | |
| def test_food_categories(): | |
| """Test comprehensive food categories.""" | |
| print(f"\nπ½οΈ Food Categories: {len(COMPREHENSIVE_FOOD_CATEGORIES)} items") | |
| # Test key foods | |
| key_foods = ["pancakes", "fish_and_chips", "american_pancakes", "waffles"] | |
| for food in key_foods: | |
| if food in COMPREHENSIVE_FOOD_CATEGORIES: | |
| print(f" β {food} - Found") | |
| else: | |
| print(f" β {food} - Missing") | |
| # Test Balkan mapping | |
| print(f"\nπ§π¦ Balkan Food Mapping:") | |
| for balkan_food, food101_equiv in BALKAN_TO_FOOD101_MAPPING.items(): | |
| print(f" {balkan_food} β {food101_equiv}") | |
| def test_validation_rules(): | |
| """Test validation rules logic.""" | |
| print(f"\nπ Testing Validation Rules") | |
| # Simulate predictions that should be caught | |
| test_cases = [ | |
| {"label": "pancakes dessert", "expected": "FAIL", "reason": "Breakfast as dessert"}, | |
| {"label": "fish_and_chips", "expected": "PASS", "reason": "Correct classification"}, | |
| {"label": "american pancakes", "expected": "PASS", "reason": "Correct breakfast"}, | |
| {"label": "fried food", "expected": "PENALTY", "reason": "Too generic"} | |
| ] | |
| for case in test_cases: | |
| label = case["label"].lower().replace("_", " ") | |
| # Rule 1: Breakfast items classified as dessert | |
| is_breakfast_dessert = ( | |
| any(term in label for term in ['pancake', 'waffle', 'french_toast']) and | |
| any(term in label for term in ['dessert', 'cake', 'sweet']) | |
| ) | |
| # Rule 2: Generic high confidence items | |
| is_generic = label in ['food', 'meal', 'dish', 'fried food', 'dessert'] | |
| if is_breakfast_dessert: | |
| result = "FAIL (Breakfast as dessert)" | |
| elif is_generic: | |
| result = "PENALTY (Too generic)" | |
| else: | |
| result = "PASS" | |
| status = "β " if case["expected"] in result else "β" | |
| print(f" {status} {case['label']}: {result}") | |
| if __name__ == "__main__": | |
| print("π Testing Food Recognition Improvements") | |
| print("=" * 60) | |
| try: | |
| test_smart_overrides() | |
| test_food_categories() | |
| test_validation_rules() | |
| print("\n" + "=" * 60) | |
| print("β ALL TESTS COMPLETED - Model improvements validated!") | |
| print("\nπ Key Improvements:") | |
| print(" β’ Food-101 model prioritized for specific dishes") | |
| print(" β’ Smart overrides for fried food β specific dishes") | |
| print(" β’ Validation rules prevent breakfast β dessert mistakes") | |
| print(" β’ Enhanced confidence boosting for target foods") | |
| print(" β’ Stricter confidence thresholds for better quality") | |
| except Exception as e: | |
| print(f"β Test failed: {e}") | |
| sys.exit(1) |