Spaces:
Runtime error
Runtime error
Download main.py from Thiophai/ImageGenerator: direct link, hf CLI and curl.
- Browser
- Download file 1.74 kB
-
https://huggingface.co/spaces/Thiophai/ImageGenerator/resolve/main/main.py
- Command line
-
hf download hf://spaces/Thiophai/ImageGenerator/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Thiophai/ImageGenerator/resolve/main/main.py
1.74 kB
| import requests | |
| from fastapi import FastAPI, File, UploadFile | |
| from fastapi.responses import JSONResponse | |
| # ตั้งค่า API URLs | |
| PREDICTION_API_URL = "https://thiophai-foodai-llmmodel.hf.space/predict" | |
| FOOD_GENERATOR_API_URL = "https://thiophai-foodai-llmmodel.hf.space/predict" | |
| app = FastAPI() | |
| async def predict_food(file: UploadFile = File(...)): | |
| """ | |
| API ตัวกลางสำหรับรับไฟล์ภาพ ส่งไปยัง Prediction API เพื่อทำนายชื่อเมนู | |
| และส่งชื่อเมนูไปยัง Food Generator API เพื่อดึงข้อมูลโภชนาการ | |
| """ | |
| # ส่งภาพไปที่ Prediction API | |
| files = {"file": (file.filename, await file.read(), file.content_type)} | |
| response = requests.post(PREDICTION_API_URL, files=files) | |
| if response.status_code != 200: | |
| return JSONResponse(content={"error": "Failed to predict food"}, status_code=500) | |
| prediction_result = response.json() | |
| # ตรวจสอบผลลัพธ์จาก Prediction API | |
| if "prediction" not in prediction_result or prediction_result["prediction"] == "Not food": | |
| return JSONResponse(content={"error": "The uploaded image does not contain food"}, status_code=400) | |
| food_name = prediction_result["prediction"] | |
| # ส่งชื่อเมนูไปยัง Food Generator API | |
| response = requests.post(FOOD_GENERATOR_API_URL, params={"menu_name": food_name}) | |
| if response.status_code != 200: | |
| return JSONResponse(content={"error": "Failed to generate food details"}, status_code=500) | |
| return response.json() | |