Spaces:
Sleeping
Sleeping
har1zarD
commited on
Commit
Β·
fb3e2c6
1
Parent(s):
18660c1
app
Browse files
app.py
CHANGED
|
@@ -288,6 +288,18 @@ def health():
|
|
| 288 |
"device": device.upper()
|
| 289 |
}
|
| 290 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
async def _analyze_food_internal(file: UploadFile) -> Dict[str, Any]:
|
| 292 |
"""Internal food analysis function (shared logic)."""
|
| 293 |
# Validate file type
|
|
@@ -325,18 +337,28 @@ async def analyze_food(file: UploadFile = File(...)):
|
|
| 325 |
Returns:
|
| 326 |
JSON with food recognition results in format expected by frontend
|
| 327 |
"""
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
# Transform to frontend-expected format
|
| 331 |
-
transformed = {
|
| 332 |
-
"label": results["primary_prediction"]["name"], # Use readable name
|
| 333 |
-
"confidence": results["primary_prediction"]["confidence"],
|
| 334 |
-
"nutrition": results["nutrition"],
|
| 335 |
-
"source": "AI Food Recognition",
|
| 336 |
-
"alternatives": results["top_predictions"]
|
| 337 |
-
}
|
| 338 |
|
| 339 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
|
| 341 |
@app.post("/analyze")
|
| 342 |
async def analyze(file: UploadFile = File(...)):
|
|
|
|
| 288 |
"device": device.upper()
|
| 289 |
}
|
| 290 |
|
| 291 |
+
@app.get("/api/nutrition/test")
|
| 292 |
+
def test_nutrition_route():
|
| 293 |
+
"""Test endpoint to verify routing."""
|
| 294 |
+
logger.info("π§ͺ TEST: /api/nutrition/test endpoint called!")
|
| 295 |
+
return {"message": "Nutrition API route is working!", "timestamp": "2025-10-31"}
|
| 296 |
+
|
| 297 |
+
@app.post("/api/nutrition/test-post")
|
| 298 |
+
async def test_nutrition_post():
|
| 299 |
+
"""Test POST endpoint to verify routing."""
|
| 300 |
+
logger.info("π§ͺ TEST: /api/nutrition/test-post endpoint called!")
|
| 301 |
+
return {"message": "Nutrition API POST route is working!", "timestamp": "2025-10-31"}
|
| 302 |
+
|
| 303 |
async def _analyze_food_internal(file: UploadFile) -> Dict[str, Any]:
|
| 304 |
"""Internal food analysis function (shared logic)."""
|
| 305 |
# Validate file type
|
|
|
|
| 337 |
Returns:
|
| 338 |
JSON with food recognition results in format expected by frontend
|
| 339 |
"""
|
| 340 |
+
logger.info("π₯ /api/nutrition/analyze-food endpoint called!")
|
| 341 |
+
logger.info(f"π File received: {file.filename}, Content-Type: {file.content_type}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
+
try:
|
| 344 |
+
results = await _analyze_food_internal(file)
|
| 345 |
+
logger.info(f"π Internal analysis successful: {results['primary_prediction']['name']}")
|
| 346 |
+
|
| 347 |
+
# Transform to frontend-expected format
|
| 348 |
+
transformed = {
|
| 349 |
+
"label": results["primary_prediction"]["name"], # Use readable name
|
| 350 |
+
"confidence": results["primary_prediction"]["confidence"],
|
| 351 |
+
"nutrition": results["nutrition"],
|
| 352 |
+
"source": "AI Food Recognition",
|
| 353 |
+
"alternatives": results["top_predictions"]
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
logger.info(f"β
Returning transformed response: {transformed['label']} ({transformed['confidence']:.2%})")
|
| 357 |
+
return JSONResponse(content=transformed)
|
| 358 |
+
|
| 359 |
+
except Exception as e:
|
| 360 |
+
logger.error(f"β Error in /api/nutrition/analyze-food: {e}")
|
| 361 |
+
raise
|
| 362 |
|
| 363 |
@app.post("/analyze")
|
| 364 |
async def analyze(file: UploadFile = File(...)):
|