Spaces:
Running
Running
File size: 18,946 Bytes
a0657cc ec179db a0657cc 613c8a3 ec179db c44131b 08f1f4f a0657cc 613c8a3 f3b4f95 a0657cc 613c8a3 196f770 08f1f4f a0657cc c44131b 08f1f4f c44131b 08f1f4f c44131b 6671987 c44131b 0cd8d56 6671987 08f1f4f 6671987 c44131b 196f770 08f1f4f d9084d2 08f1f4f d9084d2 08f1f4f d9084d2 08f1f4f 196f770 5b0a781 c44131b 5b0a781 c1d85a4 68d6453 196f770 344d8c3 196f770 344d8c3 68d6453 34d486d 68d6453 344d8c3 c1d85a4 196f770 08f1f4f 196f770 08f1f4f 08225fa d9084d2 08225fa 08f1f4f 68d6453 5b0a781 68d6453 b480d52 6671987 d9084d2 6671987 d9084d2 6671987 b480d52 68d6453 b480d52 4e4a6be dfb2c6f 4e4a6be b480d52 4e4a6be 196f770 f6e3767 68d6453 4e4a6be 196f770 4e4a6be 196f770 68d6453 196f770 4e4a6be b480d52 4e4a6be 5b0a781 4e4a6be ec179db a0657cc 5b0a781 0bc84b2 5b0a781 c1d85a4 5b0a781 a0657cc 043e2e5 ec179db c44131b a0657cc 196f770 a0657cc ec179db a0657cc 196f770 a0657cc 5b0a781 e65eb2a c44131b 0c43167 e65eb2a 5b0a781 c44131b 5b0a781 c44131b 5b0a781 c44131b 5b0a781 c44131b 5b0a781 c44131b 5b0a781 c44131b 5b0a781 c44131b 5b0a781 e65eb2a 5b0a781 e65eb2a ec179db 5b0a781 08f1f4f 5b0a781 e65eb2a c44131b 5b0a781 e65eb2a 613c8a3 ec179db e65eb2a c44131b 5b0a781 c44131b 5b0a781 c44131b 5b0a781 c44131b 5b0a781 c44131b 0bc84b2 e65eb2a 5b0a781 e65eb2a ec179db d148f3b ec179db 5b0a781 ec179db a0657cc 5b0a781 ec179db d9084d2 196f770 a0657cc d9084d2 6671987 d9084d2 6671987 a0657cc 196f770 344d8c3 5b0a781 344d8c3 5b0a781 02990d5 c44131b 5b0a781 d148f3b 5b0a781 0c43167 5b0a781 613c8a3 a0657cc 5b0a781 d148f3b 68d6453 5b0a781 02990d5 0c43167 c44131b 5b0a781 08f1f4f 5b0a781 196f770 0c43167 68d6453 196f770 613c8a3 e65eb2a 613c8a3 ec179db 5b0a781 a0657cc 5b0a781 e65eb2a 196f770 a0657cc 7f54c7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# FastAPI application for Fridge2Dish
# import libraries
import os
import io
import time
import traceback
import threading
import asyncio
from typing import Optional, List, Dict
import uvicorn
import numpy as np
import cv2 as cv
from PIL import Image
from fastapi import FastAPI, Form, UploadFile, File, Request, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
# import ML libraries
import torch
import tensorflow as tf
import google.generativeai as genai
from ultralytics import YOLO
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load model and class for YOLO
yolo_model = None
def load_yolo_model():
global yolo_model
if yolo_model is not None:
return yolo_model
print("\nπ΅ Loading YOLOv8 model...")
try:
yolo_model = YOLO("yolov8l.pt")
print("\nπ’ YOLOv8 model loaded.")
except Exception as e:
print(f"\nπ΄ Failed to load YOLOv8 model: {e}")
yolo_model = None
return yolo_model
# Might update later on...
yolo_CLASS_NAMES = {
# Fruits
"banana": True, "apple": True, "orange": True, "lemon": True, "watermelon": True,
"grapes": True, "strawberry": True, "blueberry": True, "kiwi": True,
# Vegetables
"carrot": True, "broccoli": True, "cauliflower": True, "cucumber": True,
"tomato": True, "bell pepper": True, "hot pepper": True, "onion": True,
"garlic": True, "lettuce": True, "cabbage": True, "eggplant": True,
"avocado": True, "zucchini": True, "corn": True, "mushroom": True,
# Dairy & Eggs
"cheese": True, "milk": True, "yogurt": True, "butter": True,
# Proteins & Prepared
"egg": True, "sandwich": True, "hot dog": True, "cake": True,
"donut": True,
# Food related items but not food ingredients per se
"bottle": False,
"wine glass": False,
"cup": False,
"bowl": False,
"spoon": False,
"fork": False,
"knife": False,
# Block some ambiguous ones
"pizza": False,
# Explicitly block non-food
"person": False, "chair": False, "tv": False, "laptop": False, "cell phone": False,
"book": False, "teddy bear": False, "potted plant": False, "vase": False,
"refrigerator": False, "oven": False, "microwave": False, "sink": False,
"clock": False, "suitcase": False, "backpack": False, "handbag": False,
}
# load model and class for custom CNN model
custom_tf_model = None
cnn_CLASS_NAMES = [
'apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum', 'carrot', 'cauliflower',
'chilli pepper', 'corn', 'cucumber', 'eggplant', 'garlic', 'ginger', 'grapes', 'jalepeno',
'kiwi', 'lemon', 'lettuce', 'mango', 'onion', 'orange', 'paprika', 'pear', 'peas',
'pineapple', 'pomegranate', 'potato', 'raddish', 'soy beans', 'spinach', 'sweetcorn',
'sweetpotato', 'tomato', 'turnip', 'watermelon'
]
# Load custom CNN model
def load_cnn_model():
global custom_tf_model
if custom_tf_model is not None:
return custom_tf_model
print("\nπ΅ Loading ingredient model")
try:
custom_tf_model = tf.keras.models.load_model("models/ingredient_model.keras")
print("\nπ’ Ingredient model loaded successfully!")
except Exception as e:
print(f"\nπ΄ Failed to load model: {e}")
custom_tf_model = None
return custom_tf_model
# Thread-safe lazy loading
_lock = threading.Lock()
_tokenizer = None
_model = None
# Global task tracker
current_task: Optional[asyncio.Task] = None
task_lock = threading.Lock()
cancel_event = threading.Event()
# Qwen fallback first time function
def load_Qwen():
global _tokenizer, _model
if _model is not None:
return _tokenizer, _model
with _lock:
if _model is not None:
return _tokenizer, _model
try:
print("\nπ΅ [Fallback] Loading Qwen2.5-1.5B-Instruct")
_tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct", trust_remote_code=True)
_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct", device_map="auto", torch_dtype=torch.float16)
print("\nπ’ [Fallback] Qwen ready!")
return _tokenizer, _model
except TimeoutError:
raise RuntimeError("\nπ΄ [Fallback] Qwen load timed out.")
# Preprocessing for custom model
def preprocess_for_cnn(pil_img: Image.Image) -> np.ndarray:
img = pil_img.resize((224, 224))
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
return img_array.astype(np.float32)
async def infer_cnn(pil_img: Image.Image) -> List[Dict]:
if cancel_event.is_set():
raise asyncio.CancelledError()
cnn_model = load_cnn_model()
if cnn_model is None:
return []
try:
img_array = await asyncio.to_thread(preprocess_for_cnn, pil_img)
if cancel_event.is_set():
raise asyncio.CancelledError()
preds = await asyncio.to_thread(cnn_model.predict, img_array)
conf = float(np.max(preds))
pred_idx = int(np.argmax(preds))
if conf > 0.3:
name = cnn_CLASS_NAMES[pred_idx].replace("_", " ").title()
return [{"name": name, "confidence": round(conf, 3)}]
except Exception as e:
print("\nπ΄ Custom model inference failed:", e)
return []
# Original YOLO inference
def infer_yolo(pil_image: Image.Image) -> List[Dict]:
yolo_model = load_yolo_model()
open_cv_image = np.array(pil_image)
open_cv_image = open_cv_image[:, :, ::-1].copy()
img = cv.resize(open_cv_image, (640, 640))
results = yolo_model(img, conf=0.2, iou=0.45, verbose=False)[0]
detected = []
if results.boxes is not None and len(results.boxes) > 0:
for box in results.boxes:
cls_name = results.names[int(box.cls[0])]
conf = float(box.conf[0])
if yolo_CLASS_NAMES.get(cls_name, False):
detected.append({
"name": cls_name.capitalize(),
"confidence": round(conf, 3)
})
seen = set()
final = []
for detect in detected:
if detect["name"] not in seen:
final.append(detect)
seen.add(detect["name"])
return final
async def run_yolo_threadsafe(pil_img):
if cancel_event.is_set():
raise asyncio.CancelledError()
return await asyncio.to_thread(infer_yolo, pil_img)
# run both models and merge results
async def detect_ingredients_hybrid(pil_image: Image.Image) -> List[Dict]:
# Run both models in parallel
yolo_task = run_yolo_threadsafe(pil_image)
cnn_task = infer_cnn(pil_image)
yolo_results, cnn_results = await asyncio.gather(yolo_task, cnn_task, return_exceptions=True)
yolo_detections = yolo_results if isinstance(yolo_results, list) else []
cnn_detections = cnn_results if isinstance(cnn_results, list) else []
all_detections = yolo_detections + cnn_detections
# merge and prefer highest confidence per item
merged = {}
for detect in all_detections:
name = detect["name"].lower()
if name not in merged or detect["confidence"] > merged[name]["confidence"]:
merged[name] = detect
final_detections = list(merged.values())
# sort by confidence
final_detections.sort(key=lambda x: x["confidence"], reverse=True)
return final_detections or [{"name": "No clear ingredients", "confidence": 0.0}]
# Generate recipe with Qwen
def generate_recipe_qwen(ingredient_names):
tokenizer, model = load_Qwen()
messages = [
{"role": "system", "content": "You are a helpful 5-star chef. Always respond ONLY with clean markdown, no extra text, no greetings, no explanations."},
{"role": "user", "content":
f"""You are a 5-star human chef. Create a short recipe using ONLY: {', '.join(ingredient_names)}.
Include:
- Recipe name (# Title)
- One-sentence description
- Ingredients list (add realistic quantities where applicable)
- 6-10 concise cooking steps
- Optional tips
After generating the main recipe, add a final section:
Include:
- Other Possible Dishes (##)
Suggest other 2-4 additional dishes that could be made from one, two or more of the ingredients.
Rules:
- List dish names (short descriptions).
- Keep them plausible and not duplicates of the main dish.
RETURN RESULT IN MARKDOWN FORMAT ONLY.
"""}
]
# Use Qwen chat template
input_text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True)
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
output = model.generate(
inputs.input_ids,
max_new_tokens=500,
temperature=0.7,
do_sample=True,
top_p=0.9,
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.eos_token_id
)
# Strip the prompt part
response = tokenizer.decode(output[0], skip_special_tokens=True)
recipe_text = response.split("assistant")[-1].strip()
# Final cleanup
if "<|" in recipe_text:
recipe_text = recipe_text.split("<|")[0].strip()
# final cancellation check
if cancel_event.is_set():
raise asyncio.CancelledError()
return recipe_text
# Async helper wraps
async def run_qwen_threadsafe(ingredient_names):
# run blocking Qwen genearation in thread
if cancel_event.is_set():
raise asyncio.CancelledError()
return await asyncio.to_thread(generate_recipe_qwen, ingredient_names)
async def run_gemini_threadsafe(gen_model, prompt):
# run Gemini's blocking call in a background thread
if cancel_event.is_set():
raise asyncio.CancelledError()
return await asyncio.to_thread(gen_model.generate_content, prompt)
# FastAPI app setup
app = FastAPI(
title="Fridge2Dish",
description="Upload an image β Detect ingredients β Generate recipes",
version="5.0.0"
)
# static and templates
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Home route
@app.get("/", response_class=HTMLResponse)
def home(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
# Cancel endpoint
@app.post("/cancel")
def cancel_current():
"""
Mark the cancellation flag and cancel the running asyncio task (if any).
Client should still abort the fetch (AbortController) to fully free resources.
"""
cancel_event.set()
with task_lock:
global current_task
if current_task and not current_task.done():
try:
current_task.cancel()
except Exception:
pass
return {"status": "cancelling"}
# Ingredient detection route
@app.post("/detect-ingredients/")
async def detect_ingredients(file: UploadFile = File(...)):
global current_task
if not file.filename.lower().endswith((".jpg", ".jpeg", ".png", ".webp")):
raise HTTPException(status_code=400, detail="Invalid image format.")
# Reset cancellation signal and schedule new task
cancel_event.clear()
with task_lock:
if current_task and not current_task.done():
# signal cancel to background work and cancel the asyncio task
cancel_event.set()
try:
current_task.cancel()
except Exception:
pass
loop = asyncio.get_event_loop()
current_task = loop.create_task(_detect_ingredients_task(file))
try:
result = await current_task
return result
except asyncio.CancelledError:
# return 499 to indicate client cancelled
print("\nπ΄ Ingredient detection cancelled by user.")
raise HTTPException(status_code=499, detail="Cancelled by client")
except Exception as exc:
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(exc))
finally:
with task_lock:
if current_task is not None and current_task.done():
current_task = None
# clear cancel flag after done
cancel_event.clear()
async def _detect_ingredients_task(file: UploadFile):
"""
This task runs in asyncio and uses threads for blocking calls.
It also checks cancel_event.
"""
if cancel_event.is_set():
raise asyncio.CancelledError()
start = time.time()
img_bytes = await file.read()
if cancel_event.is_set():
raise asyncio.CancelledError()
pil_img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
if cancel_event.is_set():
raise asyncio.CancelledError()
# YOLO inference in thread
ingredients = await detect_ingredients_hybrid(pil_img)
if cancel_event.is_set():
raise asyncio.CancelledError()
end = time.time()
print(f"\nDetected ingredients: {ingredients} (β Took {end-start:.2f}s)\n")
return {"ingredients": ingredients}
# Generate recipe route
@app.post("/generate-recipe/")
async def generate_recipe(ingredients: str = Form(...), user_api_key: str = Form(alias="api_key", default="")):
global current_task
with task_lock:
if current_task and not current_task.done():
cancel_event.set()
try:
current_task.cancel()
except Exception:
pass
loop = asyncio.get_event_loop()
current_task = loop.create_task(_generate_recipe_task(ingredients, user_api_key))
try:
result = await current_task
return result
except asyncio.CancelledError:
print("\nπ΄ Recipe generation cancelled by user.")
raise HTTPException(status_code=499, detail="Cancelled by client")
except HTTPException:
raise
except Exception as exc:
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(exc))
finally:
with task_lock:
if current_task is not None and current_task.done():
current_task = None
cancel_event.clear()
async def _generate_recipe_task(ingredients: str, user_api_key: str):
await asyncio.sleep(0.01)
try:
ingredient_names = [ing.strip() for ing in ingredients.split(",") if ing.strip()]
if not ingredient_names:
raise HTTPException(status_code=400, detail="No ingredients provided.")
start = time.time()
recipe_text = None
api_key = (user_api_key or "").strip()
# First try Gemini if API key provided; else fall back to Qwen
if api_key:
try:
# check cancellation before heavy work
if cancel_event.is_set():
raise asyncio.CancelledError()
genai.configure(api_key=api_key)
gen_model = genai.GenerativeModel("gemini-2.5-flash")
prompt = f"""
You are a 5-star human chef. Create a short recipe using only: {', '.join(ingredient_names)}.
Include:
- Recipe name (# Title)
- One-sentence description
- Ingredients list (add realistic quantities where applicable)
- 6-10 concise cooking steps
- Optional tips
After generating the main recipe, add a final section:
Include:
- Other Possible Dishes (##)
Suggest other 2-4 additional dishes that could be made from one, two or more of the ingredients.
Rules:
- List dish names (short descriptions).
- Keep them plausible and not duplicates of the main dish.
RETURN RESULT IN MARKDOWN FORMAT ONLY.
"""
print("\nπ‘ Trying Gemini...")
# run Gemini blocking call in thread and get response object
response = await run_gemini_threadsafe(gen_model, prompt)
if cancel_event.is_set():
raise asyncio.CancelledError()
recipe_text = (response.text or "").strip()
print("\nπ’ Gemini succeeded.")
end = time.time()
print(f"β Time taken: {end-start:.2f}s\n")
except asyncio.CancelledError:
print("\nπ΄ Generation cancelled during Gemini stage.")
raise
except Exception as e_gemini:
print("\nπ΄ Gemini failed:", e_gemini)
print("\nπ‘ Trying Qwen fallback...")
try:
recipe_text = await run_qwen_threadsafe(ingredient_names)
print("\nπ’ Qwen succeeded.")
except asyncio.CancelledError:
print("\nπ΄ Generation cancelled during Qwen fallback.")
raise
except Exception as e_qwen:
print("\nπ΄ Qwen also failed:", e_qwen)
raise e_qwen
else:
# no API key β use Qwen fallback
try:
print("\nπ‘ No API key β Using Qwen fallback.")
recipe_text = await run_qwen_threadsafe(ingredient_names)
print("\nπ’ Qwen succeeded.")
end = time.time()
print(f"β Time taken: {end-start:.2f}s\n")
except asyncio.CancelledError:
print("\nπ΄ Generation cancelled at Qwen stage.")
raise
except Exception as e_local2:
print("\nπ΄ Qwen failed:", e_local2)
recipe_text = "# Sorry!\n\nThe free AI model is taking too long to load right now.\n\nPlease consider adding your Gemini API key for instant recipes.\n\n### Thank you for understanding!"
raise e_local2
return {"recipe": recipe_text}
except HTTPException:
raise
except asyncio.CancelledError:
raise
except Exception:
traceback.print_exc()
raise
# Health check
@app.get("/health")
def health():
return {"status": "ok"}
# Run app
if __name__ == "__main__":
uvicorn.run("FastAPI_app:app", host="0.0.0.0", port=7860)
|