Spaces:
Running
Running
Update app/api/routes.py
Browse files- app/api/routes.py +38 -16
app/api/routes.py
CHANGED
|
@@ -151,54 +151,76 @@ async def handle_analyze_food_image():
|
|
| 151 |
try:
|
| 152 |
if 'image' not in request.files:
|
| 153 |
return jsonify({"error": "No image file provided"}), 400
|
| 154 |
-
|
| 155 |
-
file = request.files['image']
|
| 156 |
if file.filename == '':
|
| 157 |
return jsonify({"error": "No selected file"}), 400
|
| 158 |
|
| 159 |
print("Analyzing Food Image...", file=sys.stderr)
|
| 160 |
description = image_query.analyze_food_image(file)
|
| 161 |
-
|
| 162 |
print(f"Image Description: {description[:50]}...", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
extracted_info = extraction.extract_recipe_attributes(description)
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
if 'error' in extracted_info:
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
feature_weights_extract = {
|
| 169 |
-
'ingredients': 0.50,
|
| 170 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
}
|
| 172 |
-
|
| 173 |
category = extracted_info.get('category', '')
|
| 174 |
calories = extracted_info.get('calories', None)
|
| 175 |
-
time = extracted_info.get('time', None)
|
| 176 |
keywords = extracted_info.get('keywords', [])
|
| 177 |
keywords_name = extracted_info.get('keywords_name', [])
|
| 178 |
ingredients = extracted_info.get('ingredients', [])
|
| 179 |
-
|
| 180 |
try:
|
| 181 |
calories = int(calories) if calories else None
|
| 182 |
time = int(time) if time else None
|
| 183 |
except (ValueError, TypeError):
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
| 186 |
recommendations = await current_app.recommendation_system.get_recommendations(
|
| 187 |
category=category,
|
| 188 |
-
ingredients=ingredients,
|
| 189 |
calories=calories,
|
| 190 |
time=time,
|
| 191 |
keywords=keywords,
|
| 192 |
keywords_name=keywords_name,
|
| 193 |
feature_weights=feature_weights_extract
|
| 194 |
)
|
| 195 |
-
|
| 196 |
log_memory("End Image")
|
| 197 |
recipe_list = [vars(recipe) for recipe in recommendations]
|
| 198 |
return jsonify(recipe_list)
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
-
print("!!!
|
| 202 |
traceback.print_exc(file=sys.stderr)
|
| 203 |
return jsonify({"error": str(e)}), 500
|
| 204 |
|
|
|
|
| 151 |
try:
|
| 152 |
if 'image' not in request.files:
|
| 153 |
return jsonify({"error": "No image file provided"}), 400
|
| 154 |
+
|
| 155 |
+
file = request. files['image']
|
| 156 |
if file.filename == '':
|
| 157 |
return jsonify({"error": "No selected file"}), 400
|
| 158 |
|
| 159 |
print("Analyzing Food Image...", file=sys.stderr)
|
| 160 |
description = image_query.analyze_food_image(file)
|
|
|
|
| 161 |
print(f"Image Description: {description[:50]}...", file=sys.stderr)
|
| 162 |
+
|
| 163 |
+
# 🔥 ADD MORE LOGGING HERE
|
| 164 |
+
print(f"Full Image Description: {description}", file=sys.stderr)
|
| 165 |
+
|
| 166 |
extracted_info = extraction.extract_recipe_attributes(description)
|
| 167 |
+
|
| 168 |
+
# 🔥 LOG THE EXTRACTION RESULT
|
| 169 |
+
print(f"Extraction Result: {extracted_info}", file=sys.stderr)
|
| 170 |
+
|
| 171 |
if 'error' in extracted_info:
|
| 172 |
+
# 🔥 DON'T STOP - CONTINUE WITH EMPTY VALUES
|
| 173 |
+
print(f"Extraction warning (continuing anyway): {extracted_info}", file=sys.stderr)
|
| 174 |
+
extracted_info = {
|
| 175 |
+
"category": "",
|
| 176 |
+
"calories": None,
|
| 177 |
+
"time": None,
|
| 178 |
+
"keywords": description.split(', ')[: 5], # Use first 5 words from description
|
| 179 |
+
"keywords_name": description.split(', ')[:3],
|
| 180 |
+
"ingredients": description.split(', ')
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
feature_weights_extract = {
|
| 184 |
+
'ingredients': 0.50,
|
| 185 |
+
'category': 0.0,
|
| 186 |
+
'dietary': 0.0,
|
| 187 |
+
'calories': 0.0,
|
| 188 |
+
'time': 0.0,
|
| 189 |
+
'keywords': 0.40,
|
| 190 |
+
'keywords_name': 0.10
|
| 191 |
}
|
| 192 |
+
|
| 193 |
category = extracted_info.get('category', '')
|
| 194 |
calories = extracted_info.get('calories', None)
|
| 195 |
+
time = extracted_info. get('time', None)
|
| 196 |
keywords = extracted_info.get('keywords', [])
|
| 197 |
keywords_name = extracted_info.get('keywords_name', [])
|
| 198 |
ingredients = extracted_info.get('ingredients', [])
|
| 199 |
+
|
| 200 |
try:
|
| 201 |
calories = int(calories) if calories else None
|
| 202 |
time = int(time) if time else None
|
| 203 |
except (ValueError, TypeError):
|
| 204 |
+
calories = None
|
| 205 |
+
time = None
|
| 206 |
+
|
| 207 |
+
print("Calling Recommendation System...", file=sys.stderr)
|
| 208 |
recommendations = await current_app.recommendation_system.get_recommendations(
|
| 209 |
category=category,
|
| 210 |
+
ingredients=ingredients,
|
| 211 |
calories=calories,
|
| 212 |
time=time,
|
| 213 |
keywords=keywords,
|
| 214 |
keywords_name=keywords_name,
|
| 215 |
feature_weights=feature_weights_extract
|
| 216 |
)
|
| 217 |
+
|
| 218 |
log_memory("End Image")
|
| 219 |
recipe_list = [vars(recipe) for recipe in recommendations]
|
| 220 |
return jsonify(recipe_list)
|
| 221 |
|
| 222 |
except Exception as e:
|
| 223 |
+
print("! !! CRITICAL ERROR IN IMAGE ANALYSIS !! !", file=sys.stderr)
|
| 224 |
traceback.print_exc(file=sys.stderr)
|
| 225 |
return jsonify({"error": str(e)}), 500
|
| 226 |
|