Spaces:
Sleeping
Sleeping
File size: 2,447 Bytes
643ab60 |
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 |
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Dict, Any, List, Optional
app = FastAPI(title="Health Assistant API - Test Version")
# 配置 CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 直接在這個檔案中定義 router 和端點
class WeightEstimationResponse(BaseModel):
food_type: str
estimated_weight: float
weight_confidence: float
weight_error_range: List[float]
nutrition: Dict[str, Any]
reference_object: Optional[str] = None
note: str
@app.get("/")
async def root():
return {"message": "Health Assistant API - Test Version is running"}
@app.get("/health")
async def health_check():
return {
"status": "healthy",
"version": "test",
"endpoints": [
"/ai/analyze-food-image/",
"/ai/analyze-food-image-with-weight/",
"/ai/health"
]
}
@app.post("/ai/analyze-food-image/")
async def analyze_food_image_endpoint(file: UploadFile = File(...)):
"""測試食物圖片分析端點"""
if not file.content_type or not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="上傳的檔案不是圖片格式。")
return {"food_name": "測試食物", "nutrition_info": {"calories": 100}}
@app.post("/ai/analyze-food-image-with-weight/", response_model=WeightEstimationResponse)
async def analyze_food_image_with_weight_endpoint(file: UploadFile = File(...)):
"""測試重量估算端點"""
if not file.content_type or not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="上傳的檔案不是圖片格式。")
return WeightEstimationResponse(
food_type="測試食物",
estimated_weight=150.0,
weight_confidence=0.85,
weight_error_range=[130.0, 170.0],
nutrition={"calories": 100, "protein": 5, "fat": 2, "carbs": 15},
reference_object="硬幣",
note="測試重量估算結果"
)
@app.get("/ai/health")
async def ai_health_check():
"""AI 服務健康檢查"""
return {
"status": "healthy",
"services": {
"food_classification": "available",
"weight_estimation": "available"
}
} |