recipe-ai-fastapi / test_api.py
kingking111009's picture
πŸš€ Enhanced Recipe AI with Conversation Intelligence & Personalization
cb4c2f7
#!/usr/bin/env python3
"""
Simple test script to verify the FastAPI recipe service is working
"""
import requests
import json
# Test the API endpoints
BASE_URL = "https://nutrientartcd-recipe-ai-fastapi.hf.space" # Update this to your actual URL
def test_health_check():
"""Test the health check endpoint"""
try:
response = requests.get(f"{BASE_URL}/")
print("πŸ₯ Health Check:")
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Recipe count: {data.get('recipe_count', 'N/A')}")
print(f"Recipes loaded: {data.get('recipes_loaded', False)}")
return True
else:
print(f"Error: {response.text}")
return False
except Exception as e:
print(f"❌ Health check failed: {e}")
return False
def test_recipe_suggestions():
"""Test the recipe suggestions endpoint"""
try:
# Test the exact scenario from the screenshot
payload = {
"ingredients": "something chocolate for dessert",
"preferences": "",
"max_minutes": 30
}
response = requests.post(
f"{BASE_URL}/api/recipe-suggestions",
json=payload,
headers={"Content-Type": "application/json"}
)
print("\n🍫 Chocolate Dessert Test:")
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Status: {data.get('status')}")
recipes = data.get('recommendations', [])
print(f"Found {len(recipes)} recipes")
# Check if we got dessert recipes with chocolate
dessert_count = 0
chocolate_count = 0
for i, recipe in enumerate(recipes):
name = recipe.get('name', '').lower()
ingredients = ' '.join(recipe.get('ingredients', [])).lower()
print(f"\nRecipe {i+1}:")
print(f" ID: {recipe.get('id')}")
print(f" Name: {recipe.get('name')}")
print(f" Minutes: {recipe.get('minutes')}")
# Check for dessert indicators
if any(word in name for word in ['cake', 'cookie', 'brownie', 'dessert', 'sweet']):
dessert_count += 1
print(f" βœ… DESSERT DETECTED")
# Check for chocolate
if 'chocolate' in name or 'chocolate' in ingredients:
chocolate_count += 1
print(f" 🍫 CHOCOLATE DETECTED")
if not any(word in name for word in ['cake', 'cookie', 'brownie', 'dessert', 'sweet']):
print(f" ❌ NOT A DESSERT")
print(f"\nπŸ“Š Results:")
print(f" Dessert recipes: {dessert_count}/{len(recipes)}")
print(f" Chocolate recipes: {chocolate_count}/{len(recipes)}")
success = dessert_count > 0 or chocolate_count > 0
if success:
print("βœ… SUCCESS: Found relevant dessert/chocolate recipes!")
else:
print("❌ FAILED: No dessert or chocolate recipes found")
return success
else:
print(f"Error: {response.text}")
return False
except Exception as e:
print(f"❌ Recipe suggestions failed: {e}")
return False
if __name__ == "__main__":
print("πŸ§ͺ Testing FastAPI Recipe Service")
print(f"Base URL: {BASE_URL}")
print("-" * 50)
health_ok = test_health_check()
if health_ok:
recipes_ok = test_recipe_suggestions()
if recipes_ok:
print("\nβœ… All tests passed! The API is working correctly.")
else:
print("\n❌ Recipe suggestions test failed.")
else:
print("\n❌ Health check failed - service may not be running.")