Spaces:
Sleeping
Sleeping
Commit
Β·
0a28346
1
Parent(s):
35804b3
Upgrade to Database-Powered Recipe System
Browse files- Replace text generation with real database recipe search
- Add GPT-2 enhanced query understanding for better search
- Load recipes directly from nutrientartcd/recipe-dataset
- Return structured DatabaseRecipe objects with IDs, ingredients, steps
- Add TF-IDF semantic search with ingredient/cuisine boosting
- Include nutritional information and recipe metadata
- Add comprehensive fallback system and error handling
π€ Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
- app.py +480 -130
- requirements.txt +5 -1
- test_api.py +86 -0
app.py
CHANGED
|
@@ -7,12 +7,18 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 7 |
from peft import PeftModel
|
| 8 |
import uvicorn
|
| 9 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Initialize FastAPI app
|
| 12 |
app = FastAPI(
|
| 13 |
title="π³ Recipe AI Assistant API",
|
| 14 |
-
description="AI-powered recipe recommendations using
|
| 15 |
-
version="
|
| 16 |
)
|
| 17 |
|
| 18 |
# Add CORS middleware for web and mobile access
|
|
@@ -24,9 +30,12 @@ app.add_middleware(
|
|
| 24 |
allow_headers=["*"],
|
| 25 |
)
|
| 26 |
|
| 27 |
-
# Global variables
|
| 28 |
tokenizer = None
|
| 29 |
model = None
|
|
|
|
|
|
|
|
|
|
| 30 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
|
| 32 |
# Request/Response Models
|
|
@@ -35,16 +44,406 @@ class RecipeRequest(BaseModel):
|
|
| 35 |
preferences: Optional[str] = ""
|
| 36 |
max_minutes: int = 30
|
| 37 |
|
| 38 |
-
class
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
confidence: float
|
| 41 |
|
| 42 |
class RecipeResponse(BaseModel):
|
| 43 |
status: str
|
| 44 |
-
recommendations: List[
|
| 45 |
query: RecipeRequest
|
| 46 |
error: Optional[str] = None
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Load model on startup
|
| 49 |
@app.on_event("startup")
|
| 50 |
async def load_model():
|
|
@@ -62,16 +461,25 @@ async def load_model():
|
|
| 62 |
print("π¦ Loading base GPT-2...")
|
| 63 |
base_model = AutoModelForCausalLM.from_pretrained("gpt2")
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
print("π§
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
|
|
|
| 73 |
print(f"β
Model loaded successfully on {device}!")
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
print(f"β Error loading model: {e}")
|
| 77 |
print("π Falling back to base GPT-2...")
|
|
@@ -82,15 +490,23 @@ async def load_model():
|
|
| 82 |
tokenizer.pad_token = tokenizer.eos_token
|
| 83 |
model = AutoModelForCausalLM.from_pretrained("gpt2").to(device)
|
| 84 |
model.eval()
|
|
|
|
| 85 |
|
| 86 |
# Health check endpoint
|
| 87 |
@app.get("/")
|
| 88 |
async def root():
|
|
|
|
|
|
|
|
|
|
| 89 |
return {
|
| 90 |
-
"message": "π³ Recipe AI Assistant API",
|
| 91 |
"status": "healthy",
|
| 92 |
"model_loaded": model is not None,
|
| 93 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
# Health check endpoint
|
|
@@ -99,6 +515,8 @@ async def health_check():
|
|
| 99 |
return {
|
| 100 |
"status": "healthy",
|
| 101 |
"model_status": "loaded" if model is not None else "not_loaded",
|
|
|
|
|
|
|
| 102 |
"device": device
|
| 103 |
}
|
| 104 |
|
|
@@ -106,138 +524,70 @@ async def health_check():
|
|
| 106 |
@app.post("/api/recipe-suggestions", response_model=RecipeResponse)
|
| 107 |
async def get_recipe_suggestions(request: RecipeRequest):
|
| 108 |
try:
|
| 109 |
-
if
|
| 110 |
-
|
| 111 |
-
|
| 112 |
print(f"π₯ Recipe request: {request.ingredients}, prefs: {request.preferences}, time: {request.max_minutes}")
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
|
| 116 |
request.ingredients,
|
| 117 |
request.preferences,
|
| 118 |
request.max_minutes
|
| 119 |
)
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
return RecipeResponse(
|
| 122 |
status="success",
|
| 123 |
recommendations=recommendations,
|
| 124 |
query=request
|
| 125 |
)
|
| 126 |
|
| 127 |
-
except HTTPException:
|
| 128 |
-
raise
|
| 129 |
except Exception as e:
|
| 130 |
print(f"β Error generating recommendations: {e}")
|
| 131 |
raise HTTPException(status_code=500, detail=str(e))
|
| 132 |
|
| 133 |
-
async def generate_recommendations(
|
| 134 |
-
ingredients: str,
|
| 135 |
-
preferences: str,
|
| 136 |
-
max_minutes: int
|
| 137 |
-
) -> List[RecipeRecommendation]:
|
| 138 |
-
"""Generate recipe recommendations using the fine-tuned model"""
|
| 139 |
-
|
| 140 |
-
try:
|
| 141 |
-
recommendations = []
|
| 142 |
-
|
| 143 |
-
# Generate 3 diverse recommendations
|
| 144 |
-
for i in range(3):
|
| 145 |
-
# Build prompt in training format
|
| 146 |
-
user_input = []
|
| 147 |
-
if ingredients:
|
| 148 |
-
user_input.append(f"I have {ingredients}.")
|
| 149 |
-
user_input.append(f"I'm looking for something ready in about {max_minutes} minutes.")
|
| 150 |
-
if preferences:
|
| 151 |
-
user_input.append(f"Preferences: {preferences}.")
|
| 152 |
-
|
| 153 |
-
user_prompt = " ".join(user_input)
|
| 154 |
-
prompt = f"User: {user_prompt}\nAssistant: "
|
| 155 |
-
|
| 156 |
-
# Vary temperature for diversity
|
| 157 |
-
temperature = 0.7 + (i * 0.1)
|
| 158 |
-
|
| 159 |
-
# Generate response
|
| 160 |
-
with torch.no_grad():
|
| 161 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 162 |
-
|
| 163 |
-
outputs = model.generate(
|
| 164 |
-
**inputs,
|
| 165 |
-
max_new_tokens=150,
|
| 166 |
-
temperature=temperature,
|
| 167 |
-
top_p=0.95,
|
| 168 |
-
do_sample=True,
|
| 169 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 170 |
-
repetition_penalty=1.1
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
# Decode response
|
| 174 |
-
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 175 |
-
|
| 176 |
-
# Extract assistant response
|
| 177 |
-
assistant_start = full_response.find("Assistant:")
|
| 178 |
-
if assistant_start != -1:
|
| 179 |
-
suggestion = full_response[assistant_start + len("Assistant:"):].strip()
|
| 180 |
-
else:
|
| 181 |
-
suggestion = full_response.strip()
|
| 182 |
-
|
| 183 |
-
# Calculate confidence (higher for first recommendations)
|
| 184 |
-
confidence = max(0.6, 1.0 - (i * 0.15))
|
| 185 |
-
|
| 186 |
-
recommendations.append(
|
| 187 |
-
RecipeRecommendation(
|
| 188 |
-
suggestion=suggestion,
|
| 189 |
-
confidence=confidence
|
| 190 |
-
)
|
| 191 |
-
)
|
| 192 |
-
|
| 193 |
-
return recommendations
|
| 194 |
-
|
| 195 |
-
except Exception as e:
|
| 196 |
-
print(f"β Error in generate_recommendations: {e}")
|
| 197 |
-
# Return fallback recommendations
|
| 198 |
-
return [
|
| 199 |
-
RecipeRecommendation(
|
| 200 |
-
suggestion="I'm having trouble generating custom recipes right now. Here's a quick suggestion: try a simple stir-fry with your ingredients!",
|
| 201 |
-
confidence=0.5
|
| 202 |
-
)
|
| 203 |
-
]
|
| 204 |
-
|
| 205 |
-
# Ingredient parsing endpoint (bonus feature)
|
| 206 |
-
@app.post("/api/parse-ingredients")
|
| 207 |
-
async def parse_ingredients(text: dict):
|
| 208 |
-
"""Parse ingredients from natural language text"""
|
| 209 |
-
try:
|
| 210 |
-
query = text.get("text", "")
|
| 211 |
-
|
| 212 |
-
# Simple ingredient extraction (you can enhance this)
|
| 213 |
-
common_ingredients = [
|
| 214 |
-
"chicken", "beef", "pork", "fish", "salmon", "shrimp", "tofu",
|
| 215 |
-
"rice", "pasta", "quinoa", "bread", "potatoes",
|
| 216 |
-
"tomatoes", "onion", "garlic", "ginger", "peppers", "broccoli",
|
| 217 |
-
"spinach", "carrots", "cheese", "milk", "eggs", "butter"
|
| 218 |
-
]
|
| 219 |
-
|
| 220 |
-
found_ingredients = [ing for ing in common_ingredients if ing in query.lower()]
|
| 221 |
-
|
| 222 |
-
return {
|
| 223 |
-
"status": "success",
|
| 224 |
-
"ingredients": found_ingredients,
|
| 225 |
-
"original_text": query
|
| 226 |
-
}
|
| 227 |
-
|
| 228 |
-
except Exception as e:
|
| 229 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 230 |
-
|
| 231 |
-
# Recipe details endpoint (for future expansion)
|
| 232 |
-
@app.get("/api/recipe/{recipe_id}")
|
| 233 |
-
async def get_recipe_details(recipe_id: str):
|
| 234 |
-
"""Get detailed recipe information (placeholder for future feature)"""
|
| 235 |
-
return {
|
| 236 |
-
"status": "success",
|
| 237 |
-
"message": "Recipe details endpoint - coming soon!",
|
| 238 |
-
"recipe_id": recipe_id
|
| 239 |
-
}
|
| 240 |
-
|
| 241 |
if __name__ == "__main__":
|
| 242 |
port = int(os.environ.get("PORT", 7860))
|
| 243 |
uvicorn.run(
|
|
|
|
| 7 |
from peft import PeftModel
|
| 8 |
import uvicorn
|
| 9 |
import os
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import ast
|
| 12 |
+
import re
|
| 13 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 14 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 15 |
+
import numpy as np
|
| 16 |
|
| 17 |
# Initialize FastAPI app
|
| 18 |
app = FastAPI(
|
| 19 |
title="π³ Recipe AI Assistant API",
|
| 20 |
+
description="AI-powered recipe recommendations using real recipe database",
|
| 21 |
+
version="2.0.0"
|
| 22 |
)
|
| 23 |
|
| 24 |
# Add CORS middleware for web and mobile access
|
|
|
|
| 30 |
allow_headers=["*"],
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# Global variables
|
| 34 |
tokenizer = None
|
| 35 |
model = None
|
| 36 |
+
recipes_df = None
|
| 37 |
+
vectorizer = None
|
| 38 |
+
recipe_vectors = None
|
| 39 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 40 |
|
| 41 |
# Request/Response Models
|
|
|
|
| 44 |
preferences: Optional[str] = ""
|
| 45 |
max_minutes: int = 30
|
| 46 |
|
| 47 |
+
class DatabaseRecipe(BaseModel):
|
| 48 |
+
id: int
|
| 49 |
+
name: str
|
| 50 |
+
description: str
|
| 51 |
+
ingredients: List[str]
|
| 52 |
+
steps: List[str]
|
| 53 |
+
minutes: int
|
| 54 |
+
servings: Optional[int] = None
|
| 55 |
+
nutrition: Optional[dict] = None
|
| 56 |
+
tags: List[str] = []
|
| 57 |
confidence: float
|
| 58 |
|
| 59 |
class RecipeResponse(BaseModel):
|
| 60 |
status: str
|
| 61 |
+
recommendations: List[DatabaseRecipe]
|
| 62 |
query: RecipeRequest
|
| 63 |
error: Optional[str] = None
|
| 64 |
|
| 65 |
+
def safe_eval_list(x):
|
| 66 |
+
"""Safely parse string representations of lists"""
|
| 67 |
+
if isinstance(x, list):
|
| 68 |
+
return x
|
| 69 |
+
if isinstance(x, str):
|
| 70 |
+
try:
|
| 71 |
+
# Try to evaluate as Python literal
|
| 72 |
+
result = ast.literal_eval(x)
|
| 73 |
+
if isinstance(result, list):
|
| 74 |
+
return [str(item) for item in result]
|
| 75 |
+
except (ValueError, SyntaxError):
|
| 76 |
+
# Fall back to simple string splitting
|
| 77 |
+
return [item.strip() for item in x.split(',') if item.strip()]
|
| 78 |
+
return []
|
| 79 |
+
|
| 80 |
+
def load_recipes():
|
| 81 |
+
"""Load and process the RAW_recipes.csv file from Hugging Face dataset"""
|
| 82 |
+
global recipes_df, vectorizer, recipe_vectors
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
# Try to load from Hugging Face dataset directly
|
| 86 |
+
print("π Attempting to load recipe dataset from Hugging Face...")
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
# Method 1: Try with datasets library
|
| 90 |
+
try:
|
| 91 |
+
from datasets import load_dataset
|
| 92 |
+
print("π Loading from nutrientartcd/recipe-dataset...")
|
| 93 |
+
dataset = load_dataset("nutrientartcd/recipe-dataset")
|
| 94 |
+
# The dataset might not have splits, so try different approaches
|
| 95 |
+
if hasattr(dataset, 'to_pandas'):
|
| 96 |
+
df = dataset.to_pandas()
|
| 97 |
+
elif 'train' in dataset:
|
| 98 |
+
df = dataset['train'].to_pandas()
|
| 99 |
+
else:
|
| 100 |
+
# Get the first available split
|
| 101 |
+
split_name = list(dataset.keys())[0]
|
| 102 |
+
df = dataset[split_name].to_pandas()
|
| 103 |
+
print(f"β
Successfully loaded {len(df)} recipes from Hugging Face datasets!")
|
| 104 |
+
except Exception as datasets_error:
|
| 105 |
+
print(f"β οΈ Datasets library failed: {datasets_error}")
|
| 106 |
+
|
| 107 |
+
# Method 2: Direct CSV download from Hugging Face
|
| 108 |
+
print("π Trying direct CSV download from Hugging Face...")
|
| 109 |
+
import urllib.request
|
| 110 |
+
csv_url = "https://huggingface.co/datasets/nutrientartcd/recipe-dataset/resolve/main/RAW_recipes.csv"
|
| 111 |
+
local_csv = "/tmp/RAW_recipes_downloaded.csv"
|
| 112 |
+
|
| 113 |
+
print(f"Downloading from: {csv_url}")
|
| 114 |
+
urllib.request.urlretrieve(csv_url, local_csv)
|
| 115 |
+
|
| 116 |
+
df = pd.read_csv(local_csv)
|
| 117 |
+
print(f"β
Successfully downloaded and loaded {len(df)} recipes from CSV!")
|
| 118 |
+
except Exception as hf_error:
|
| 119 |
+
print(f"β οΈ Both Hugging Face methods failed: {hf_error}")
|
| 120 |
+
|
| 121 |
+
# Try local paths as fallback
|
| 122 |
+
print("π Trying local CSV files...")
|
| 123 |
+
possible_paths = [
|
| 124 |
+
"RAW_recipes.csv",
|
| 125 |
+
"/tmp/RAW_recipes.csv",
|
| 126 |
+
"./RAW_recipes.csv",
|
| 127 |
+
"../RAW_recipes.csv",
|
| 128 |
+
"/app/RAW_recipes.csv",
|
| 129 |
+
"recipe_data/RAW_recipes.csv"
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
dataset_path = None
|
| 133 |
+
for path in possible_paths:
|
| 134 |
+
if os.path.exists(path):
|
| 135 |
+
dataset_path = path
|
| 136 |
+
break
|
| 137 |
+
|
| 138 |
+
if dataset_path is None:
|
| 139 |
+
print("β No local CSV files found either")
|
| 140 |
+
print("π Current working directory:", os.getcwd())
|
| 141 |
+
print("π Available files:", [f for f in os.listdir('.') if f.endswith('.csv')][:10])
|
| 142 |
+
raise FileNotFoundError("Neither Hugging Face dataset nor local CSV found")
|
| 143 |
+
|
| 144 |
+
print(f"π Loading recipes from local file {dataset_path}...")
|
| 145 |
+
df = pd.read_csv(dataset_path)
|
| 146 |
+
|
| 147 |
+
# Clean and process the dataframe
|
| 148 |
+
required_cols = ['id', 'name', 'minutes', 'ingredients', 'steps']
|
| 149 |
+
missing_cols = [col for col in required_cols if col not in df.columns]
|
| 150 |
+
if missing_cols:
|
| 151 |
+
raise ValueError(f"Missing required columns: {missing_cols}")
|
| 152 |
+
|
| 153 |
+
# Parse string lists
|
| 154 |
+
df['ingredients'] = df['ingredients'].apply(safe_eval_list)
|
| 155 |
+
df['steps'] = df['steps'].apply(safe_eval_list)
|
| 156 |
+
df['tags'] = df.get('tags', '[]').apply(safe_eval_list)
|
| 157 |
+
df['nutrition'] = df.get('nutrition', '[]').apply(safe_eval_list)
|
| 158 |
+
|
| 159 |
+
# Clean data
|
| 160 |
+
df = df[
|
| 161 |
+
(df['name'].str.len() > 1) &
|
| 162 |
+
(df['minutes'] > 0) &
|
| 163 |
+
(df['ingredients'].str.len() > 0) &
|
| 164 |
+
(df['steps'].str.len() > 0)
|
| 165 |
+
].copy()
|
| 166 |
+
|
| 167 |
+
# Create searchable text fields
|
| 168 |
+
df['ingredients_text'] = df['ingredients'].apply(lambda x: ' '.join(x).lower())
|
| 169 |
+
df['steps_text'] = df['steps'].apply(lambda x: ' '.join(x).lower())
|
| 170 |
+
df['tags_text'] = df['tags'].apply(lambda x: ' '.join(x).lower())
|
| 171 |
+
df['search_text'] = (
|
| 172 |
+
df['name'].str.lower() + ' ' +
|
| 173 |
+
df['ingredients_text'] + ' ' +
|
| 174 |
+
df['tags_text'] + ' ' +
|
| 175 |
+
df.get('description', '').fillna('').str.lower()
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
# Create TF-IDF vectors for semantic search
|
| 179 |
+
print("π Building search index...")
|
| 180 |
+
vectorizer = TfidfVectorizer(
|
| 181 |
+
max_features=5000,
|
| 182 |
+
stop_words='english',
|
| 183 |
+
ngram_range=(1, 2),
|
| 184 |
+
min_df=2
|
| 185 |
+
)
|
| 186 |
+
recipe_vectors = vectorizer.fit_transform(df['search_text'])
|
| 187 |
+
|
| 188 |
+
recipes_df = df
|
| 189 |
+
print(f"β
Loaded {len(df)} recipes successfully!")
|
| 190 |
+
|
| 191 |
+
except Exception as e:
|
| 192 |
+
print(f"β Error loading recipes: {e}")
|
| 193 |
+
print(f"π Error details: {type(e).__name__}: {str(e)}")
|
| 194 |
+
|
| 195 |
+
# Create a more comprehensive fallback dataset
|
| 196 |
+
print("π Creating fallback recipe dataset...")
|
| 197 |
+
recipes_df = pd.DataFrame({
|
| 198 |
+
'id': [234567, 458976, 123789, 345678, 567890],
|
| 199 |
+
'name': [
|
| 200 |
+
'15-Minute Pasta Aglio e Olio',
|
| 201 |
+
'Lemon Herb Grilled Chicken',
|
| 202 |
+
'Rainbow Buddha Bowl',
|
| 203 |
+
'Mediterranean Quinoa Salad',
|
| 204 |
+
'Classic Caesar Salad'
|
| 205 |
+
],
|
| 206 |
+
'minutes': [15, 25, 30, 20, 10],
|
| 207 |
+
'ingredients': [
|
| 208 |
+
['1 lb spaghetti', '6 cloves garlic (sliced)', '1/2 cup olive oil', '1/4 cup fresh parsley', 'red pepper flakes'],
|
| 209 |
+
['4 chicken breasts', '2 lemons (juiced)', '2 tbsp olive oil', '2 tsp dried herbs', 'salt and pepper'],
|
| 210 |
+
['1 cup quinoa', '2 cups mixed vegetables', '3 tbsp tahini', '1 lemon (juiced)', '2 tbsp olive oil'],
|
| 211 |
+
['2 cups cooked quinoa', '1 cup cherry tomatoes', '1 cucumber (diced)', '1/2 cup olives', '3 tbsp olive oil'],
|
| 212 |
+
['1 large romaine lettuce', '1/2 cup parmesan cheese', '1/4 cup caesar dressing', '1/2 cup croutons', 'black pepper']
|
| 213 |
+
],
|
| 214 |
+
'steps': [
|
| 215 |
+
['Cook pasta until al dente', 'Heat oil and sautΓ© garlic until golden', 'Toss pasta with oil and garlic', 'Add parsley and pepper flakes'],
|
| 216 |
+
['Marinate chicken in lemon juice and herbs for 30 minutes', 'Heat grill to medium-high heat', 'Grill chicken 6-8 minutes per side', 'Rest for 5 minutes before serving'],
|
| 217 |
+
['Cook quinoa according to package directions', 'Roast vegetables at 400Β°F for 25 minutes', 'Whisk tahini with lemon juice', 'Assemble bowl and drizzle with dressing'],
|
| 218 |
+
['Cool cooked quinoa completely', 'Dice all vegetables', 'Combine quinoa and vegetables', 'Dress with olive oil and lemon'],
|
| 219 |
+
['Wash and chop romaine lettuce', 'Toss with caesar dressing', 'Top with parmesan and croutons', 'Season with black pepper']
|
| 220 |
+
],
|
| 221 |
+
'tags': [['quick', 'italian', 'pasta'], ['healthy', 'protein', 'grilled'], ['vegetarian', 'healthy', 'bowl'], ['vegetarian', 'mediterranean', 'salad'], ['salad', 'classic', 'vegetarian']],
|
| 222 |
+
'nutrition': [[], [], [], [], []],
|
| 223 |
+
'description': [
|
| 224 |
+
'A classic Italian dish that\'s simple yet delicious.',
|
| 225 |
+
'Fresh and flavorful grilled chicken with herbs and bright lemon flavor.',
|
| 226 |
+
'A nutritious and colorful bowl packed with healthy ingredients.',
|
| 227 |
+
'A protein-rich salad with fresh vegetables and herbs.',
|
| 228 |
+
'A classic caesar salad with crisp romaine and parmesan.'
|
| 229 |
+
]
|
| 230 |
+
})
|
| 231 |
+
|
| 232 |
+
# Process the fallback dataset the same way
|
| 233 |
+
recipes_df['ingredients_text'] = recipes_df['ingredients'].apply(lambda x: ' '.join(x).lower())
|
| 234 |
+
recipes_df['steps_text'] = recipes_df['steps'].apply(lambda x: ' '.join(x).lower())
|
| 235 |
+
recipes_df['tags_text'] = recipes_df['tags'].apply(lambda x: ' '.join(x).lower())
|
| 236 |
+
recipes_df['search_text'] = (
|
| 237 |
+
recipes_df['name'].str.lower() + ' ' +
|
| 238 |
+
recipes_df['ingredients_text'] + ' ' +
|
| 239 |
+
recipes_df['tags_text'] + ' ' +
|
| 240 |
+
recipes_df['description'].fillna('').str.lower()
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
# Create simple vectorizer for fallback
|
| 244 |
+
print("π Building fallback search index...")
|
| 245 |
+
vectorizer = TfidfVectorizer(
|
| 246 |
+
max_features=1000,
|
| 247 |
+
stop_words='english',
|
| 248 |
+
ngram_range=(1, 2),
|
| 249 |
+
min_df=1
|
| 250 |
+
)
|
| 251 |
+
recipe_vectors = vectorizer.fit_transform(recipes_df['search_text'])
|
| 252 |
+
|
| 253 |
+
print(f"β
Fallback dataset ready with {len(recipes_df)} recipes!")
|
| 254 |
+
return # Exit early for fallback dataset
|
| 255 |
+
|
| 256 |
+
@torch.inference_mode()
|
| 257 |
+
def extract_query_features_with_gpt2(query_text, preferences="", max_minutes=30):
|
| 258 |
+
"""Use GPT-2 to intelligently extract searchable features from user query"""
|
| 259 |
+
global tokenizer, model
|
| 260 |
+
|
| 261 |
+
if model is None or tokenizer is None:
|
| 262 |
+
# Fallback to simple extraction if model not loaded
|
| 263 |
+
return extract_query_features_simple(query_text, preferences, max_minutes)
|
| 264 |
+
|
| 265 |
+
# Create a structured prompt for GPT-2 to extract features
|
| 266 |
+
full_query = f"{query_text} {preferences}".strip()
|
| 267 |
+
|
| 268 |
+
extraction_prompt = f"""Extract cooking information from this request: "{full_query}"
|
| 269 |
+
|
| 270 |
+
Ingredients mentioned: """
|
| 271 |
+
|
| 272 |
+
try:
|
| 273 |
+
inputs = tokenizer(extraction_prompt, return_tensors="pt").to(device)
|
| 274 |
+
|
| 275 |
+
# Generate a short response to extract ingredients/features
|
| 276 |
+
outputs = model.generate(
|
| 277 |
+
**inputs,
|
| 278 |
+
max_new_tokens=50,
|
| 279 |
+
temperature=0.3, # Lower temperature for more focused extraction
|
| 280 |
+
top_p=0.9,
|
| 281 |
+
do_sample=True,
|
| 282 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 283 |
+
repetition_penalty=1.1
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 287 |
+
gpt2_extraction = response[len(extraction_prompt):].strip()
|
| 288 |
+
|
| 289 |
+
# Parse the GPT-2 response and combine with rule-based extraction
|
| 290 |
+
gpt2_features = parse_gpt2_extraction(gpt2_extraction)
|
| 291 |
+
rule_features = extract_query_features_simple(query_text, preferences, max_minutes)
|
| 292 |
+
|
| 293 |
+
# Combine both approaches
|
| 294 |
+
combined_features = {
|
| 295 |
+
'ingredients': list(set(gpt2_features.get('ingredients', []) + rule_features['ingredients'])),
|
| 296 |
+
'cuisines': list(set(gpt2_features.get('cuisines', []) + rule_features['cuisines'])),
|
| 297 |
+
'diets': list(set(gpt2_features.get('diets', []) + rule_features['diets'])),
|
| 298 |
+
'styles': list(set(gpt2_features.get('styles', []) + rule_features['styles'])),
|
| 299 |
+
'max_minutes': max_minutes,
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
combined_features['search_terms'] = (
|
| 303 |
+
combined_features['ingredients'] +
|
| 304 |
+
combined_features['cuisines'] +
|
| 305 |
+
combined_features['diets'] +
|
| 306 |
+
combined_features['styles']
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
print(f"π§ GPT-2 enhanced extraction: {combined_features['search_terms'][:8]}")
|
| 310 |
+
return combined_features
|
| 311 |
+
|
| 312 |
+
except Exception as e:
|
| 313 |
+
print(f"β οΈ GPT-2 extraction failed, using rule-based: {e}")
|
| 314 |
+
return extract_query_features_simple(query_text, preferences, max_minutes)
|
| 315 |
+
|
| 316 |
+
def parse_gpt2_extraction(gpt2_text):
|
| 317 |
+
"""Parse GPT-2's extraction response into structured features"""
|
| 318 |
+
text_lower = gpt2_text.lower()
|
| 319 |
+
|
| 320 |
+
# Extract ingredients from GPT-2 response
|
| 321 |
+
ingredients = []
|
| 322 |
+
common_ingredients = [
|
| 323 |
+
'chicken', 'beef', 'pork', 'fish', 'salmon', 'shrimp', 'tofu',
|
| 324 |
+
'pasta', 'rice', 'quinoa', 'bread', 'potatoes', 'noodles',
|
| 325 |
+
'tomatoes', 'onion', 'garlic', 'ginger', 'peppers', 'broccoli',
|
| 326 |
+
'spinach', 'carrots', 'mushrooms', 'avocado', 'lemon', 'lime',
|
| 327 |
+
'cheese', 'milk', 'eggs', 'butter', 'oil', 'flour', 'herbs',
|
| 328 |
+
'beans', 'lentils', 'chickpeas'
|
| 329 |
+
]
|
| 330 |
+
|
| 331 |
+
for ing in common_ingredients:
|
| 332 |
+
if ing in text_lower:
|
| 333 |
+
ingredients.append(ing)
|
| 334 |
+
|
| 335 |
+
# Look for cuisine mentions
|
| 336 |
+
cuisines = []
|
| 337 |
+
cuisine_words = ['italian', 'mexican', 'asian', 'chinese', 'thai', 'indian', 'greek', 'french', 'mediterranean']
|
| 338 |
+
for cuisine in cuisine_words:
|
| 339 |
+
if cuisine in text_lower:
|
| 340 |
+
cuisines.append(cuisine)
|
| 341 |
+
|
| 342 |
+
# Look for dietary preferences
|
| 343 |
+
diets = []
|
| 344 |
+
diet_words = ['vegetarian', 'vegan', 'healthy', 'low-carb', 'keto', 'gluten-free']
|
| 345 |
+
for diet in diet_words:
|
| 346 |
+
if diet in text_lower:
|
| 347 |
+
diets.append(diet)
|
| 348 |
+
|
| 349 |
+
# Look for cooking styles
|
| 350 |
+
styles = []
|
| 351 |
+
style_words = ['quick', 'easy', 'fast', 'slow', 'comfort', 'light', 'hearty', 'spicy']
|
| 352 |
+
for style in style_words:
|
| 353 |
+
if style in text_lower:
|
| 354 |
+
styles.append(style)
|
| 355 |
+
|
| 356 |
+
return {
|
| 357 |
+
'ingredients': ingredients,
|
| 358 |
+
'cuisines': cuisines,
|
| 359 |
+
'diets': diets,
|
| 360 |
+
'styles': styles
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
def extract_query_features_simple(query_text, preferences="", max_minutes=30):
|
| 364 |
+
"""Fallback rule-based feature extraction"""
|
| 365 |
+
query_lower = query_text.lower() + " " + preferences.lower()
|
| 366 |
+
|
| 367 |
+
# Extract ingredients mentioned
|
| 368 |
+
common_ingredients = [
|
| 369 |
+
'chicken', 'beef', 'pork', 'fish', 'salmon', 'shrimp', 'tofu',
|
| 370 |
+
'pasta', 'rice', 'quinoa', 'bread', 'potatoes', 'noodles',
|
| 371 |
+
'tomatoes', 'onion', 'garlic', 'ginger', 'peppers', 'broccoli',
|
| 372 |
+
'spinach', 'carrots', 'mushrooms', 'avocado', 'lemon', 'lime',
|
| 373 |
+
'cheese', 'milk', 'eggs', 'butter', 'oil', 'flour', 'herbs',
|
| 374 |
+
'beans', 'lentils', 'chickpeas'
|
| 375 |
+
]
|
| 376 |
+
|
| 377 |
+
mentioned_ingredients = [ing for ing in common_ingredients if ing in query_lower]
|
| 378 |
+
|
| 379 |
+
# Extract cuisine preferences
|
| 380 |
+
cuisines = ['italian', 'mexican', 'asian', 'chinese', 'thai', 'indian', 'greek', 'french']
|
| 381 |
+
mentioned_cuisines = [cuisine for cuisine in cuisines if cuisine in query_lower]
|
| 382 |
+
|
| 383 |
+
# Extract diet preferences
|
| 384 |
+
diets = ['vegetarian', 'vegan', 'healthy', 'low-carb', 'keto', 'gluten-free']
|
| 385 |
+
mentioned_diets = [diet for diet in diets if diet in query_lower]
|
| 386 |
+
|
| 387 |
+
# Extract cooking style
|
| 388 |
+
styles = ['quick', 'easy', 'fast', 'slow', 'comfort', 'light', 'hearty']
|
| 389 |
+
mentioned_styles = [style for style in styles if style in query_lower]
|
| 390 |
+
|
| 391 |
+
return {
|
| 392 |
+
'ingredients': mentioned_ingredients,
|
| 393 |
+
'cuisines': mentioned_cuisines,
|
| 394 |
+
'diets': mentioned_diets,
|
| 395 |
+
'styles': mentioned_styles,
|
| 396 |
+
'max_minutes': max_minutes,
|
| 397 |
+
'search_terms': mentioned_ingredients + mentioned_cuisines + mentioned_diets + mentioned_styles
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
def search_recipes(query_features, top_k=10):
|
| 401 |
+
"""Search for recipes matching the query features"""
|
| 402 |
+
global recipes_df, vectorizer, recipe_vectors
|
| 403 |
+
|
| 404 |
+
if recipes_df is None:
|
| 405 |
+
load_recipes()
|
| 406 |
+
|
| 407 |
+
# Filter by time constraint
|
| 408 |
+
filtered_df = recipes_df[recipes_df['minutes'] <= query_features['max_minutes']].copy()
|
| 409 |
+
|
| 410 |
+
if len(filtered_df) == 0:
|
| 411 |
+
filtered_df = recipes_df.copy() # Fall back to all recipes
|
| 412 |
+
|
| 413 |
+
# Create search query
|
| 414 |
+
search_query = ' '.join(query_features['search_terms'])
|
| 415 |
+
|
| 416 |
+
if search_query and vectorizer is not None:
|
| 417 |
+
# Semantic search using TF-IDF
|
| 418 |
+
query_vector = vectorizer.transform([search_query])
|
| 419 |
+
filtered_vectors = recipe_vectors[filtered_df.index]
|
| 420 |
+
similarities = cosine_similarity(query_vector, filtered_vectors).flatten()
|
| 421 |
+
|
| 422 |
+
# Add similarity scores
|
| 423 |
+
filtered_df = filtered_df.copy()
|
| 424 |
+
filtered_df['similarity'] = similarities
|
| 425 |
+
|
| 426 |
+
# Boost recipes that match specific criteria
|
| 427 |
+
if query_features['ingredients']:
|
| 428 |
+
for ingredient in query_features['ingredients']:
|
| 429 |
+
mask = filtered_df['ingredients_text'].str.contains(ingredient, na=False)
|
| 430 |
+
filtered_df.loc[mask, 'similarity'] *= 1.5
|
| 431 |
+
|
| 432 |
+
if query_features['cuisines']:
|
| 433 |
+
for cuisine in query_features['cuisines']:
|
| 434 |
+
mask = filtered_df['tags_text'].str.contains(cuisine, na=False) | \
|
| 435 |
+
filtered_df['name'].str.lower().str.contains(cuisine, na=False)
|
| 436 |
+
filtered_df.loc[mask, 'similarity'] *= 1.3
|
| 437 |
+
|
| 438 |
+
# Sort by similarity
|
| 439 |
+
filtered_df = filtered_df.sort_values('similarity', ascending=False)
|
| 440 |
+
else:
|
| 441 |
+
# Fallback: random selection
|
| 442 |
+
filtered_df = filtered_df.sample(min(len(filtered_df), top_k*2), random_state=42)
|
| 443 |
+
filtered_df['similarity'] = 0.5
|
| 444 |
+
|
| 445 |
+
return filtered_df.head(top_k)
|
| 446 |
+
|
| 447 |
# Load model on startup
|
| 448 |
@app.on_event("startup")
|
| 449 |
async def load_model():
|
|
|
|
| 461 |
print("π¦ Loading base GPT-2...")
|
| 462 |
base_model = AutoModelForCausalLM.from_pretrained("gpt2")
|
| 463 |
|
| 464 |
+
# Try to load fine-tuned LoRA adapter
|
| 465 |
+
print("π§ Looking for LoRA adapter...")
|
| 466 |
+
try:
|
| 467 |
+
model = PeftModel.from_pretrained(
|
| 468 |
+
base_model,
|
| 469 |
+
"nutrientartcd/recipe-gpt2-lora"
|
| 470 |
+
).to(device)
|
| 471 |
+
print("β
LoRA adapter loaded successfully!")
|
| 472 |
+
except Exception as e:
|
| 473 |
+
print(f"β οΈ Could not load LoRA adapter: {e}")
|
| 474 |
+
print("π Using base GPT-2 model...")
|
| 475 |
+
model = base_model.to(device)
|
| 476 |
|
| 477 |
+
model.eval()
|
| 478 |
print(f"β
Model loaded successfully on {device}!")
|
| 479 |
|
| 480 |
+
# Load recipe database
|
| 481 |
+
load_recipes()
|
| 482 |
+
|
| 483 |
except Exception as e:
|
| 484 |
print(f"β Error loading model: {e}")
|
| 485 |
print("π Falling back to base GPT-2...")
|
|
|
|
| 490 |
tokenizer.pad_token = tokenizer.eos_token
|
| 491 |
model = AutoModelForCausalLM.from_pretrained("gpt2").to(device)
|
| 492 |
model.eval()
|
| 493 |
+
load_recipes()
|
| 494 |
|
| 495 |
# Health check endpoint
|
| 496 |
@app.get("/")
|
| 497 |
async def root():
|
| 498 |
+
if recipes_df is None:
|
| 499 |
+
load_recipes()
|
| 500 |
+
|
| 501 |
return {
|
| 502 |
+
"message": "π³ Recipe AI Assistant API v2.0",
|
| 503 |
"status": "healthy",
|
| 504 |
"model_loaded": model is not None,
|
| 505 |
+
"recipes_loaded": recipes_df is not None,
|
| 506 |
+
"recipe_count": len(recipes_df) if recipes_df is not None else 0,
|
| 507 |
+
"device": device,
|
| 508 |
+
"current_directory": os.getcwd(),
|
| 509 |
+
"available_files": [f for f in os.listdir('.') if f.endswith('.csv')][:5]
|
| 510 |
}
|
| 511 |
|
| 512 |
# Health check endpoint
|
|
|
|
| 515 |
return {
|
| 516 |
"status": "healthy",
|
| 517 |
"model_status": "loaded" if model is not None else "not_loaded",
|
| 518 |
+
"recipes_status": "loaded" if recipes_df is not None else "not_loaded",
|
| 519 |
+
"recipe_count": len(recipes_df) if recipes_df is not None else 0,
|
| 520 |
"device": device
|
| 521 |
}
|
| 522 |
|
|
|
|
| 524 |
@app.post("/api/recipe-suggestions", response_model=RecipeResponse)
|
| 525 |
async def get_recipe_suggestions(request: RecipeRequest):
|
| 526 |
try:
|
| 527 |
+
if recipes_df is None:
|
| 528 |
+
load_recipes()
|
| 529 |
+
|
| 530 |
print(f"π₯ Recipe request: {request.ingredients}, prefs: {request.preferences}, time: {request.max_minutes}")
|
| 531 |
|
| 532 |
+
# Use GPT-2 enhanced feature extraction
|
| 533 |
+
query_features = extract_query_features_with_gpt2(
|
| 534 |
request.ingredients,
|
| 535 |
request.preferences,
|
| 536 |
request.max_minutes
|
| 537 |
)
|
| 538 |
|
| 539 |
+
# Search for matching recipes
|
| 540 |
+
matching_recipes = search_recipes(query_features, top_k=5)
|
| 541 |
+
|
| 542 |
+
# Convert to response format
|
| 543 |
+
recommendations = []
|
| 544 |
+
for _, recipe in matching_recipes.iterrows():
|
| 545 |
+
# Parse nutrition if available
|
| 546 |
+
nutrition = None
|
| 547 |
+
if isinstance(recipe.get('nutrition'), list) and len(recipe['nutrition']) > 0:
|
| 548 |
+
try:
|
| 549 |
+
if isinstance(recipe['nutrition'][0], str):
|
| 550 |
+
nutrition_list = ast.literal_eval(recipe['nutrition'][0])
|
| 551 |
+
else:
|
| 552 |
+
nutrition_list = recipe['nutrition']
|
| 553 |
+
|
| 554 |
+
if len(nutrition_list) >= 7: # Ensure we have enough nutrition values
|
| 555 |
+
nutrition = {
|
| 556 |
+
"calories": float(nutrition_list[0]) if nutrition_list[0] else 0,
|
| 557 |
+
"fat": float(nutrition_list[1]) if nutrition_list[1] else 0,
|
| 558 |
+
"sugar": float(nutrition_list[2]) if nutrition_list[2] else 0,
|
| 559 |
+
"sodium": float(nutrition_list[3]) if nutrition_list[3] else 0,
|
| 560 |
+
"protein": float(nutrition_list[4]) if nutrition_list[4] else 0,
|
| 561 |
+
"saturated_fat": float(nutrition_list[5]) if nutrition_list[5] else 0,
|
| 562 |
+
"carbs": float(nutrition_list[6]) if nutrition_list[6] else 0
|
| 563 |
+
}
|
| 564 |
+
except:
|
| 565 |
+
nutrition = None
|
| 566 |
+
|
| 567 |
+
db_recipe = DatabaseRecipe(
|
| 568 |
+
id=int(recipe['id']),
|
| 569 |
+
name=recipe['name'],
|
| 570 |
+
description=recipe.get('description', ''),
|
| 571 |
+
ingredients=recipe['ingredients'],
|
| 572 |
+
steps=recipe['steps'],
|
| 573 |
+
minutes=int(recipe['minutes']),
|
| 574 |
+
servings=recipe.get('n_steps', 4), # Use n_steps as proxy for servings if not available
|
| 575 |
+
nutrition=nutrition,
|
| 576 |
+
tags=recipe['tags'],
|
| 577 |
+
confidence=float(recipe.get('similarity', 0.5))
|
| 578 |
+
)
|
| 579 |
+
recommendations.append(db_recipe)
|
| 580 |
+
|
| 581 |
return RecipeResponse(
|
| 582 |
status="success",
|
| 583 |
recommendations=recommendations,
|
| 584 |
query=request
|
| 585 |
)
|
| 586 |
|
|
|
|
|
|
|
| 587 |
except Exception as e:
|
| 588 |
print(f"β Error generating recommendations: {e}")
|
| 589 |
raise HTTPException(status_code=500, detail=str(e))
|
| 590 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
if __name__ == "__main__":
|
| 592 |
port = int(os.environ.get("PORT", 7860))
|
| 593 |
uvicorn.run(
|
requirements.txt
CHANGED
|
@@ -7,4 +7,8 @@ pydantic>=2.0.0
|
|
| 7 |
python-multipart==0.0.6
|
| 8 |
huggingface_hub>=0.19.0
|
| 9 |
accelerate>=0.24.0
|
| 10 |
-
safetensors>=0.4.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
python-multipart==0.0.6
|
| 8 |
huggingface_hub>=0.19.0
|
| 9 |
accelerate>=0.24.0
|
| 10 |
+
safetensors>=0.4.0
|
| 11 |
+
pandas>=2.0.0
|
| 12 |
+
scikit-learn>=1.3.0
|
| 13 |
+
numpy>=1.24.0
|
| 14 |
+
datasets>=2.19.0
|
test_api.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Simple test script to verify the FastAPI recipe service is working
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import requests
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# Test the API endpoints
|
| 10 |
+
BASE_URL = "https://nutrientartcd-recipe-ai-fastapi.hf.space" # Update this to your actual URL
|
| 11 |
+
|
| 12 |
+
def test_health_check():
|
| 13 |
+
"""Test the health check endpoint"""
|
| 14 |
+
try:
|
| 15 |
+
response = requests.get(f"{BASE_URL}/")
|
| 16 |
+
print("π₯ Health Check:")
|
| 17 |
+
print(f"Status: {response.status_code}")
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
data = response.json()
|
| 20 |
+
print(f"Recipe count: {data.get('recipe_count', 'N/A')}")
|
| 21 |
+
print(f"Recipes loaded: {data.get('recipes_loaded', False)}")
|
| 22 |
+
return True
|
| 23 |
+
else:
|
| 24 |
+
print(f"Error: {response.text}")
|
| 25 |
+
return False
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"β Health check failed: {e}")
|
| 28 |
+
return False
|
| 29 |
+
|
| 30 |
+
def test_recipe_suggestions():
|
| 31 |
+
"""Test the recipe suggestions endpoint"""
|
| 32 |
+
try:
|
| 33 |
+
payload = {
|
| 34 |
+
"ingredients": "pasta, garlic, olive oil",
|
| 35 |
+
"preferences": "quick italian",
|
| 36 |
+
"max_minutes": 30
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
response = requests.post(
|
| 40 |
+
f"{BASE_URL}/api/recipe-suggestions",
|
| 41 |
+
json=payload,
|
| 42 |
+
headers={"Content-Type": "application/json"}
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
print("\nπ Recipe Suggestions Test:")
|
| 46 |
+
print(f"Status: {response.status_code}")
|
| 47 |
+
|
| 48 |
+
if response.status_code == 200:
|
| 49 |
+
data = response.json()
|
| 50 |
+
print(f"Status: {data.get('status')}")
|
| 51 |
+
recipes = data.get('recommendations', [])
|
| 52 |
+
print(f"Found {len(recipes)} recipes")
|
| 53 |
+
|
| 54 |
+
for i, recipe in enumerate(recipes[:2]): # Show first 2
|
| 55 |
+
print(f"\nRecipe {i+1}:")
|
| 56 |
+
print(f" ID: {recipe.get('id')}")
|
| 57 |
+
print(f" Name: {recipe.get('name')}")
|
| 58 |
+
print(f" Minutes: {recipe.get('minutes')}")
|
| 59 |
+
print(f" Ingredients: {len(recipe.get('ingredients', []))} items")
|
| 60 |
+
print(f" Steps: {len(recipe.get('steps', []))} steps")
|
| 61 |
+
|
| 62 |
+
return len(recipes) > 0
|
| 63 |
+
else:
|
| 64 |
+
print(f"Error: {response.text}")
|
| 65 |
+
return False
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"β Recipe suggestions failed: {e}")
|
| 69 |
+
return False
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
print("π§ͺ Testing FastAPI Recipe Service")
|
| 73 |
+
print(f"Base URL: {BASE_URL}")
|
| 74 |
+
print("-" * 50)
|
| 75 |
+
|
| 76 |
+
health_ok = test_health_check()
|
| 77 |
+
|
| 78 |
+
if health_ok:
|
| 79 |
+
recipes_ok = test_recipe_suggestions()
|
| 80 |
+
|
| 81 |
+
if recipes_ok:
|
| 82 |
+
print("\nβ
All tests passed! The API is working correctly.")
|
| 83 |
+
else:
|
| 84 |
+
print("\nβ Recipe suggestions test failed.")
|
| 85 |
+
else:
|
| 86 |
+
print("\nβ Health check failed - service may not be running.")
|