Spaces:
Running
Running
Commit
ยท
aab0ecf
1
Parent(s):
ae02f3c
- Dockerfile +26 -0
- app.py +7 -65
- router/hospital_router.py +100 -0
Dockerfile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python 3.9 ์ด๋ฏธ์ง ์ฌ์ฉ
|
| 2 |
+
FROM python:3.13-slim
|
| 3 |
+
|
| 4 |
+
# ์์
๋๋ ํ ๋ฆฌ ์ค์
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# ํ์ํ ํ์ผ ๋ณต์ฌ
|
| 8 |
+
# requirements.txt ๋จผ์ ๋ณต์ฌํ์ฌ ์บ์ ํ์ฉ
|
| 9 |
+
COPY requirements.txt .
|
| 10 |
+
|
| 11 |
+
# ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# ๋๋จธ์ง ์์ค ์ฝ๋ ๋ฐ ๋ชจ๋ธ ํ์ผ ๋ณต์ฌ
|
| 15 |
+
COPY . .
|
| 16 |
+
|
| 17 |
+
# Hugging Face Spaces๋ 7860 ํฌํธ๋ฅผ ์ฌ์ฉํจ
|
| 18 |
+
EXPOSE 7860
|
| 19 |
+
|
| 20 |
+
# ๊ถํ ๋ฌธ์ ๋ฐฉ์ง๋ฅผ ์ํด ์ฌ์ฉ์ ์์ฑ ๋ฐ ์ ํ (์ ํ์ฌํญ์ด๋ ๊ถ์ฅ๋จ)
|
| 21 |
+
RUN useradd -m -u 1000 user
|
| 22 |
+
USER user
|
| 23 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 24 |
+
|
| 25 |
+
# FastAPI ์๋ฒ ์คํ (0.0.0.0์ผ๋ก ์ด์ด์ผ ์ธ๋ถ ์ ์ ๊ฐ๋ฅ)
|
| 26 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -1,69 +1,11 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from catboost import CatBoostRegressor
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
app = FastAPI(title="Hospital Recommendation
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
model = CatBoostRegressor()
|
| 12 |
|
| 13 |
-
# ์ ํ๋ฆฌ์ผ์ด์
์์ ์ ๋ชจ๋ธ ๋ก๋
|
| 14 |
-
if os.path.exists(MODEL_PATH):
|
| 15 |
-
try:
|
| 16 |
-
model.load_model(MODEL_PATH)
|
| 17 |
-
print(f"โ
CatBoost ๋ชจ๋ธ์ '{MODEL_PATH}'์์ ์ฑ๊ณต์ ์ผ๋ก ๋ก๋ํ์ต๋๋ค.")
|
| 18 |
-
except Exception as e:
|
| 19 |
-
print(f"โ ๋ชจ๋ธ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 20 |
-
# ์ค์ ๋ฐฐํฌ์์๋ ๋ชจ๋ธ์ด ์์ผ๋ฉด ์๋ฒ๊ฐ ์์๋์ง ์๊ฒ ์์ธ ์ฒ๋ฆฌ ๊ฐ๋ฅ
|
| 21 |
-
else:
|
| 22 |
-
print(f"โ ๏ธ ๊ฒฝ๊ณ : '{MODEL_PATH}' ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค. ๋ชจ๋ธ ํ์ผ์ ์
๋ก๋ํด์ฃผ์ธ์.")
|
| 23 |
-
|
| 24 |
-
# 2. ์
๋ ฅ ๋ฐ์ดํฐ ๊ฒ์ฆ์ ์ํ Pydantic ๋ชจ๋ธ ์ ์
|
| 25 |
-
class HospitalInput(BaseModel):
|
| 26 |
-
distance_m: float # ๊ฑฐ๋ฆฌ (๋ฏธํฐ)
|
| 27 |
-
rating: float # ํ์
|
| 28 |
-
congestion_level: int # ํผ์ก๋ (1~5 ๋ฑ)
|
| 29 |
-
|
| 30 |
-
class Config:
|
| 31 |
-
json_schema_extra = {
|
| 32 |
-
"example": {
|
| 33 |
-
"distance_m": 500,
|
| 34 |
-
"rating": 4.5,
|
| 35 |
-
"congestion_level": 2
|
| 36 |
-
}
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
# 3. ์์ธก ์๋ํฌ์ธํธ
|
| 40 |
-
@app.post("/predict")
|
| 41 |
-
def predict_recommendation(input_data: HospitalInput):
|
| 42 |
-
# ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ ๊ฒฝ์ฐ ์๋ฌ ์ฒ๋ฆฌ
|
| 43 |
-
if model.tree_count_ is None:
|
| 44 |
-
raise HTTPException(status_code=500, detail="Model is not loaded.")
|
| 45 |
-
|
| 46 |
-
try:
|
| 47 |
-
# ์
๋ ฅ ๋ฐ์ดํฐ๋ฅผ DataFrame์ผ๋ก ๋ณํ (ํน์ฑ ์ด๋ฆ๊ณผ ์์๋ฅผ ๋ง์ถค)
|
| 48 |
-
# ํ์ต ์ ์์: ['distance_m', 'rating', 'congestion_level']
|
| 49 |
-
data_df = pd.DataFrame([{
|
| 50 |
-
'distance_m': input_data.distance_m,
|
| 51 |
-
'rating': input_data.rating,
|
| 52 |
-
'congestion_level': input_data.congestion_level
|
| 53 |
-
}])
|
| 54 |
-
|
| 55 |
-
# ์์ธก ์ํ
|
| 56 |
-
prediction = model.predict(data_df)
|
| 57 |
-
|
| 58 |
-
# ๊ฒฐ๊ณผ ๋ฐํ (float ํํ๋ก ๋ณํ)
|
| 59 |
-
return {
|
| 60 |
-
"status": "success",
|
| 61 |
-
"predicted_score": float(prediction[0])
|
| 62 |
-
}
|
| 63 |
-
except Exception as e:
|
| 64 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 65 |
-
|
| 66 |
-
# 4. ํฌ์ค ์ฒดํฌ (์๋ฒ ์ํ ํ์ธ์ฉ)
|
| 67 |
@app.get("/")
|
| 68 |
-
def
|
| 69 |
-
return {"message": "
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from router import hospital_router
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
app = FastAPI(title="Hospital Recommendation API")
|
| 5 |
|
| 6 |
+
# ๋ผ์ฐํฐ ๋ฑ๋ก
|
| 7 |
+
app.include_router(hospital_router.router)
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
@app.get("/")
|
| 10 |
+
def health_check():
|
| 11 |
+
return {"message": "API Server is running. Go to /docs for testing."}
|
router/hospital_router.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from catboost import CatBoostRegressor
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
router = APIRouter(prefix="/hospital", tags=["Hospital Prediction"])
|
| 9 |
+
|
| 10 |
+
# --- ๋ชจ๋ธ ๋ก๋ ๋ก์ง (๊ธฐ์กด๊ณผ ๋์ผ) ---
|
| 11 |
+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 12 |
+
MODEL_PATH = os.path.join(BASE_DIR, 'model', 'catboost_model.bin')
|
| 13 |
+
|
| 14 |
+
model = CatBoostRegressor()
|
| 15 |
+
if os.path.exists(MODEL_PATH):
|
| 16 |
+
try:
|
| 17 |
+
model.load_model(MODEL_PATH)
|
| 18 |
+
print(f"โ
[Router] ๋ชจ๋ธ ๋ก๋ ์๋ฃ: {MODEL_PATH}")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"โ [Router] ๋ชจ๋ธ ๋ก๋ ์คํจ: {e}")
|
| 21 |
+
else:
|
| 22 |
+
print(f"โ ๏ธ [Router] ๊ฒฝ๊ณ : ๋ชจ๋ธ ํ์ผ ์์")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# 1. ์
๋ ฅ ๋ฐ์ดํฐ ๋ชจ๋ธ ์ ์ (๋จ์ผ ๋ณ์ ๊ฐ์ฒด)
|
| 26 |
+
class HospitalInput(BaseModel):
|
| 27 |
+
id: int # ๋ณ์ ID (ํ์)
|
| 28 |
+
distance_m: float
|
| 29 |
+
rating: float
|
| 30 |
+
congestion_level: int
|
| 31 |
+
|
| 32 |
+
class Config:
|
| 33 |
+
json_schema_extra = {
|
| 34 |
+
"example": {
|
| 35 |
+
"id": 101,
|
| 36 |
+
"distance_m": 500,
|
| 37 |
+
"rating": 4.5,
|
| 38 |
+
"congestion_level": 2
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# 2. ์์ธก ์๋ํฌ์ธํธ ์ ์
|
| 43 |
+
# List[HospitalInput]์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ๋ณ์ ๋ฐ์ดํฐ๋ฅผ ๋ฆฌ์คํธ๋ก ๋ฐ์ต๋๋ค.
|
| 44 |
+
@router.post("/predict")
|
| 45 |
+
async def predict_score(hospital_list: List[HospitalInput]):
|
| 46 |
+
# ๋ชจ๋ธ ๋ก๋ ์ํ ํ์ธ
|
| 47 |
+
if model.tree_count_ is None:
|
| 48 |
+
return {
|
| 49 |
+
"success": False,
|
| 50 |
+
"data": None,
|
| 51 |
+
"msg": "AI Server Error: Model is not loaded."
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
# ์
๋ ฅ ๋ฐ์ดํฐ๊ฐ ๋น์ด์๋์ง ํ์ธ
|
| 56 |
+
if not hospital_list:
|
| 57 |
+
return {
|
| 58 |
+
"success": True,
|
| 59 |
+
"data": [],
|
| 60 |
+
"msg": "No hospital data provided."
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
# 1. ๋ฆฌ์คํธ๋ฅผ DataFrame์ผ๋ก ๋ณํ (ํ ๋ฒ์ ์์ธกํ๊ธฐ ์ํจ)
|
| 64 |
+
# Pydantic ๋ชจ๋ธ ๋ฆฌ์คํธ๋ฅผ dict ๋ฆฌ์คํธ๋ก ๋ณํ
|
| 65 |
+
input_data_list = [h.dict() for h in hospital_list]
|
| 66 |
+
df = pd.DataFrame(input_data_list)
|
| 67 |
+
|
| 68 |
+
# 2. ์์ธก์ ํ์ํ ํน์ฑ(Feature)๋ง ์ ํ
|
| 69 |
+
# ํ์ตํ ๋ ์ฌ์ฉํ ํน์ฑ ์์๋ฅผ ๋ง์ถฐ์ค๋๋ค.
|
| 70 |
+
X_features = df[['distance_m', 'rating', 'congestion_level']]
|
| 71 |
+
|
| 72 |
+
# 3. ์ผ๊ด ์์ธก ์ํ (Batch Prediction)
|
| 73 |
+
predictions = model.predict(X_features)
|
| 74 |
+
|
| 75 |
+
# 4. ๊ฒฐ๊ณผ ๋งคํ ๋ฐ ๊ตฌ์กฐํ
|
| 76 |
+
hospital_rank_data = []
|
| 77 |
+
|
| 78 |
+
# DataFrame์ ๊ฐ ํ๊ณผ ์์ธก ๊ฒฐ๊ณผ๋ฅผ ํฉ์นฉ๋๋ค.
|
| 79 |
+
for idx, row in df.iterrows():
|
| 80 |
+
hospital_rank_data.append({
|
| 81 |
+
"id": int(row['id']),
|
| 82 |
+
"predicted_recommendation_score": float(predictions[idx])
|
| 83 |
+
})
|
| 84 |
+
|
| 85 |
+
# 5. ์ ์ ๊ธฐ์ค ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ (Rank)
|
| 86 |
+
hospital_rank_data.sort(key=lambda x: x['predicted_recommendation_score'], reverse=True)
|
| 87 |
+
|
| 88 |
+
# 6. ์ต์ข
๊ฒฐ๊ณผ ๋ฐํ
|
| 89 |
+
return {
|
| 90 |
+
"success": True,
|
| 91 |
+
"data": hospital_rank_data,
|
| 92 |
+
"msg": ""
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
return {
|
| 97 |
+
"success": False,
|
| 98 |
+
"data": None,
|
| 99 |
+
"msg": f"AI Server Error: Prediction Error: {str(e)}"
|
| 100 |
+
}
|