Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
python <br># ------------------------------------------------------------ <br># FastAPI service exposing BinhQuocNguyen/food‑recognition‑model <br># ------------------------------------------------------------ <br>from fastapi import FastAPI, HTTPException <br>from pydantic import BaseModel <br>import base64, io <br>from PIL import Image <br>from transformers import pipeline <br> <br># ------------------------------------------------------------------- <br># 1️⃣ Load the model once at startup (fast, no re‑load per request) <br># ------------------------------------------------------------ <br>classifier = pipeline( <br> \"image-classification\", <br> model=\"BinhQuocNguyen/food-recognition-model\" <br>) <br> <br># ------------------------------------------------------------------- <br># 2️⃣ Simple in‑memory “nutritional DB” – you can extend it with all 101 classes <br># ------------------------------------------------------------------- <br># (calories per 100 g, avg portion weight in grams) <br>nutrient_db = { <br> \"Apple\": {\"calories_per_100g\": 52, \"portion_g\": 182}, <br> \"Banana\": {\"calories_per_100g\": 89, \"portion_g\": 118}, <br> \"Orange\": {\"calories_per_100g\": 43, \"portion_g\": 131}, <br> \"Pizza\": {\"calories_per_100g\": 266, \"portion_g\": 200}, <br> \"Bread\": {\"calories_per_100g\": 265, \"portion_g\": 30}, <br> # ← add the rest of your favourite foods here <br>} <br> <br># ------------------------------------------------------------------- <br># 3️⃣ Pydantic model for the incoming JSON payload <br># ------------------------------------------------------------------- <br>class ImageRequest(BaseModel): <br> image: str <br> <br>app = FastAPI() <br> <br># ------------------------------------------------------------ <br># Health‑check endpoint (optional) <br># ------------------------------------------------------------ <br>@app.get(\"/\") <br>def root(): <br> return {\"message\": \"Food‑Recognition API is up\"} <br> <br># ------------------------------------------------------------ <br># 4️⃣ Main inference endpoint <br># ------------------------------------------------------------ <br>@app.post(\"/analyze\") <br>def analyze(request: ImageRequest): <br> # ---- decode the base64 image -------------------------------- <br> try: <br> raw_bytes = base64.b64decode(request.image) <br> pil_img = Image.open(io.BytesIO(raw_bytes)).convert(\"RGB\") <br> except Exception as e: <br> raise HTTPException(status_code=400, detail=\"Invalid base64 image\") <br> <br> # ---- run the classifier -------------------------------------- <br> results = classifier(pil_img) <br> if not results: <br> raise HTTPException(status_code=500, detail=\"Model returned no results\") <br> top = results[0] <br> label = top.get(\"label\") <br> confidence = top.get(\"score\") <br> <br> # ---- look up nutrition data ----------------------------------- <br> # The model’s own DB is not directly exposed, so we use the inline dict above <br> nutrition = nutrient_db.get(label, {\"calories_per_100g\": 0, \"portion_g\": 100}) <br> calories_per_100g = nutrition[\"calories_per_100g\"] <br> portion_g = nutrition[\"portion_g\"] <br> # Estimated calories for the (default) portion size <br> est_calories = calories_per_100g * (portion_g / 100.0) <br> <br> # ---- build the JSON response -------------------------------- <br> return { <br> \"label\": label, <br> \"confidence\": confidence, <br> \"estimated_portion_g\": portion_g, <br> \"calories_per_100g\": calories_per_100g, <br> \"estimated_calories\": round(est_calories, 2) <br> } <br>
|