Spaces:
Sleeping
Sleeping
Commit
·
643ab60
1
Parent(s):
23a5052
Create test_app.py with inline endpoints to isolate router import issue
Browse files- main.py +3 -3
- test_app.py +76 -0
main.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
# Import the FastAPI app from
|
2 |
-
from
|
3 |
|
4 |
# This file exists to satisfy Hugging Face Spaces' Dockerfile
|
5 |
# which expects: uvicorn main:app
|
6 |
-
#
|
|
|
1 |
+
# Import the FastAPI app from test_app.py
|
2 |
+
from test_app import app
|
3 |
|
4 |
# This file exists to satisfy Hugging Face Spaces' Dockerfile
|
5 |
# which expects: uvicorn main:app
|
6 |
+
# Using test_app.py to isolate the router import issue
|
test_app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from typing import Dict, Any, List, Optional
|
5 |
+
|
6 |
+
app = FastAPI(title="Health Assistant API - Test Version")
|
7 |
+
|
8 |
+
# 配置 CORS
|
9 |
+
app.add_middleware(
|
10 |
+
CORSMiddleware,
|
11 |
+
allow_origins=["*"],
|
12 |
+
allow_credentials=True,
|
13 |
+
allow_methods=["*"],
|
14 |
+
allow_headers=["*"],
|
15 |
+
)
|
16 |
+
|
17 |
+
# 直接在這個檔案中定義 router 和端點
|
18 |
+
class WeightEstimationResponse(BaseModel):
|
19 |
+
food_type: str
|
20 |
+
estimated_weight: float
|
21 |
+
weight_confidence: float
|
22 |
+
weight_error_range: List[float]
|
23 |
+
nutrition: Dict[str, Any]
|
24 |
+
reference_object: Optional[str] = None
|
25 |
+
note: str
|
26 |
+
|
27 |
+
@app.get("/")
|
28 |
+
async def root():
|
29 |
+
return {"message": "Health Assistant API - Test Version is running"}
|
30 |
+
|
31 |
+
@app.get("/health")
|
32 |
+
async def health_check():
|
33 |
+
return {
|
34 |
+
"status": "healthy",
|
35 |
+
"version": "test",
|
36 |
+
"endpoints": [
|
37 |
+
"/ai/analyze-food-image/",
|
38 |
+
"/ai/analyze-food-image-with-weight/",
|
39 |
+
"/ai/health"
|
40 |
+
]
|
41 |
+
}
|
42 |
+
|
43 |
+
@app.post("/ai/analyze-food-image/")
|
44 |
+
async def analyze_food_image_endpoint(file: UploadFile = File(...)):
|
45 |
+
"""測試食物圖片分析端點"""
|
46 |
+
if not file.content_type or not file.content_type.startswith("image/"):
|
47 |
+
raise HTTPException(status_code=400, detail="上傳的檔案不是圖片格式。")
|
48 |
+
|
49 |
+
return {"food_name": "測試食物", "nutrition_info": {"calories": 100}}
|
50 |
+
|
51 |
+
@app.post("/ai/analyze-food-image-with-weight/", response_model=WeightEstimationResponse)
|
52 |
+
async def analyze_food_image_with_weight_endpoint(file: UploadFile = File(...)):
|
53 |
+
"""測試重量估算端點"""
|
54 |
+
if not file.content_type or not file.content_type.startswith("image/"):
|
55 |
+
raise HTTPException(status_code=400, detail="上傳的檔案不是圖片格式。")
|
56 |
+
|
57 |
+
return WeightEstimationResponse(
|
58 |
+
food_type="測試食物",
|
59 |
+
estimated_weight=150.0,
|
60 |
+
weight_confidence=0.85,
|
61 |
+
weight_error_range=[130.0, 170.0],
|
62 |
+
nutrition={"calories": 100, "protein": 5, "fat": 2, "carbs": 15},
|
63 |
+
reference_object="硬幣",
|
64 |
+
note="測試重量估算結果"
|
65 |
+
)
|
66 |
+
|
67 |
+
@app.get("/ai/health")
|
68 |
+
async def ai_health_check():
|
69 |
+
"""AI 服務健康檢查"""
|
70 |
+
return {
|
71 |
+
"status": "healthy",
|
72 |
+
"services": {
|
73 |
+
"food_classification": "available",
|
74 |
+
"weight_estimation": "available"
|
75 |
+
}
|
76 |
+
}
|