Spaces:
Sleeping
Sleeping
Update FastAPI_app.py
Browse files- FastAPI_app.py +30 -19
FastAPI_app.py
CHANGED
|
@@ -163,23 +163,34 @@ def home(request: Request):
|
|
| 163 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 164 |
|
| 165 |
|
| 166 |
-
#
|
| 167 |
-
@app.post("/
|
| 168 |
-
async def
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
| 180 |
|
| 181 |
-
ingredient_names = [i["name"] for i in ingredients]
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
recipe_text = None
|
| 184 |
api_key = (user_api_key or "").strip()
|
| 185 |
|
|
@@ -203,8 +214,9 @@ async def upload_image(file: UploadFile = File(...), user_api_key: str = Form(al
|
|
| 203 |
response = model.generate_content(prompt)
|
| 204 |
recipe_text = response.text.strip()
|
| 205 |
print("\n🟢 Gemini succeeded.")
|
|
|
|
| 206 |
end = time.time()
|
| 207 |
-
print(f"Time taken: {end-start:.2f}s")
|
| 208 |
|
| 209 |
except Exception as e_gemini:
|
| 210 |
print("Gemini failed:", e_gemini)
|
|
@@ -219,21 +231,20 @@ async def upload_image(file: UploadFile = File(...), user_api_key: str = Form(al
|
|
| 219 |
print("\n🟡 No API key → Using Qwen fallback.")
|
| 220 |
recipe_text = generate_recipe_qwen(ingredient_names)
|
| 221 |
print("\n🟢 Qwen succeeded.")
|
| 222 |
-
end = time.time()
|
| 223 |
-
print(f"Time taken: {end-start:.2f}s")
|
| 224 |
|
| 225 |
except Exception as e_local2:
|
| 226 |
print("\n🔴 Qwen local failed:", e_local2)
|
| 227 |
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!"
|
| 228 |
raise e_local2
|
| 229 |
|
| 230 |
-
return {"
|
| 231 |
|
| 232 |
except HTTPException:
|
| 233 |
raise
|
| 234 |
except Exception as e:
|
| 235 |
traceback.print_exc()
|
| 236 |
-
|
|
|
|
| 237 |
|
| 238 |
# Health check
|
| 239 |
@app.get("/health")
|
|
|
|
| 163 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 164 |
|
| 165 |
|
| 166 |
+
# Ingredient detection route
|
| 167 |
+
@app.post("/detect-ingredients/")
|
| 168 |
+
async def detect_ingredients(file: UploadFile = File(...)):
|
| 169 |
+
if not file.filename.lower().endswith((".jpg", ".jpeg", ".png", ".webp", ".gif")):
|
| 170 |
+
raise HTTPException(status_code=400, detail="Invalid image format.")
|
| 171 |
+
|
| 172 |
+
start = time.time()
|
| 173 |
+
img_bytes = await file.read()
|
| 174 |
+
pil_img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 175 |
|
| 176 |
+
ingredients = infer_image(pil_img)
|
| 177 |
+
end = time.time()
|
| 178 |
+
print(f"Detected ingredients: {ingredients} (⌛ Took {end-start:.2f}s)")
|
| 179 |
+
|
| 180 |
+
return {"ingredients": ingredients}
|
| 181 |
|
|
|
|
| 182 |
|
| 183 |
+
# Generate recipe route
|
| 184 |
+
@app.post("/generate-recipe/")
|
| 185 |
+
async def generate_recipe(ingredients: str = Form(...), user_api_key: str = Form(alias="api_key", default="")):
|
| 186 |
+
try:
|
| 187 |
+
ingredient_names = [ing.strip() for ing in ingredients.split(",") if ing.strip()]
|
| 188 |
+
print(f"\nGenerating recipe for ingredients: {ingredient_names} in `generate-recipe route`")
|
| 189 |
+
if not ingredient_names:
|
| 190 |
+
raise HTTPException(status_code=400, detail="No ingredients provided.")
|
| 191 |
+
|
| 192 |
+
start = time.time()
|
| 193 |
+
|
| 194 |
recipe_text = None
|
| 195 |
api_key = (user_api_key or "").strip()
|
| 196 |
|
|
|
|
| 214 |
response = model.generate_content(prompt)
|
| 215 |
recipe_text = response.text.strip()
|
| 216 |
print("\n🟢 Gemini succeeded.")
|
| 217 |
+
|
| 218 |
end = time.time()
|
| 219 |
+
print(f"\n⌛ Time taken: {end-start:.2f}s")
|
| 220 |
|
| 221 |
except Exception as e_gemini:
|
| 222 |
print("Gemini failed:", e_gemini)
|
|
|
|
| 231 |
print("\n🟡 No API key → Using Qwen fallback.")
|
| 232 |
recipe_text = generate_recipe_qwen(ingredient_names)
|
| 233 |
print("\n🟢 Qwen succeeded.")
|
|
|
|
|
|
|
| 234 |
|
| 235 |
except Exception as e_local2:
|
| 236 |
print("\n🔴 Qwen local failed:", e_local2)
|
| 237 |
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!"
|
| 238 |
raise e_local2
|
| 239 |
|
| 240 |
+
return {"recipe": recipe_text}
|
| 241 |
|
| 242 |
except HTTPException:
|
| 243 |
raise
|
| 244 |
except Exception as e:
|
| 245 |
traceback.print_exc()
|
| 246 |
+
|
| 247 |
+
|
| 248 |
|
| 249 |
# Health check
|
| 250 |
@app.get("/health")
|