Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
|
| 9 |
+
# --- KONFIGURASI HF HUB ---
|
| 10 |
+
HF_REPO_ID = "aephidayatuloh/bank-model"
|
| 11 |
+
HF_MODEL_FILENAME = "random_forest_bank_marketing_pipeline.joblib"
|
| 12 |
+
|
| 13 |
+
# --- DEFINISI DATA INPUT (Pydantic) ---
|
| 14 |
+
# [Pastikan ini sama persis dengan yang Anda gunakan sebelumnya]
|
| 15 |
+
class PredictionInput(BaseModel):
|
| 16 |
+
age: int
|
| 17 |
+
balance: int
|
| 18 |
+
day: int
|
| 19 |
+
campaign: int
|
| 20 |
+
pdays: int
|
| 21 |
+
previous: int
|
| 22 |
+
job: str
|
| 23 |
+
marital: str
|
| 24 |
+
education: str
|
| 25 |
+
default: str
|
| 26 |
+
housing: str
|
| 27 |
+
loan: str
|
| 28 |
+
contact: str
|
| 29 |
+
month: str
|
| 30 |
+
poutcome: str
|
| 31 |
+
|
| 32 |
+
# --- SETUP MODEL (DIJALANKAN SEKALI SAAT STARTUP) ---
|
| 33 |
+
app = FastAPI(title="Bank Deposit Prediction (Docker)")
|
| 34 |
+
|
| 35 |
+
@app.on_event("startup")
|
| 36 |
+
def load_model():
|
| 37 |
+
global MODEL_PIPELINE
|
| 38 |
+
try:
|
| 39 |
+
# Download model dari HF Hub (direkomendasikan)
|
| 40 |
+
downloaded_model_path = hf_hub_download(
|
| 41 |
+
repo_id=HF_REPO_ID,
|
| 42 |
+
filename=HF_MODEL_FILENAME
|
| 43 |
+
)
|
| 44 |
+
MODEL_PIPELINE = joblib.load(downloaded_model_path)
|
| 45 |
+
print("✅ Model berhasil dimuat dari Hugging Face Hub.")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"❌ Gagal memuat model: {e}")
|
| 48 |
+
MODEL_PIPELINE = None
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# --- ENDPOINT PREDIKSI ---
|
| 52 |
+
|
| 53 |
+
@app.get("/")
|
| 54 |
+
def home():
|
| 55 |
+
return {"status": "ok", "message": "FastAPI is running inside Docker on HF Spaces."}
|
| 56 |
+
|
| 57 |
+
@app.post("/predict")
|
| 58 |
+
def predict(data: PredictionInput):
|
| 59 |
+
if MODEL_PIPELINE is None:
|
| 60 |
+
raise HTTPException(status_code=500, detail="Model gagal dimuat.")
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
input_df = pd.DataFrame([data.dict()])
|
| 64 |
+
|
| 65 |
+
prediction = MODEL_PIPELINE.predict(input_df)[0]
|
| 66 |
+
prediction_proba = MODEL_PIPELINE.predict_proba(input_df)[0].tolist()
|
| 67 |
+
|
| 68 |
+
return {
|
| 69 |
+
"prediction_class": int(prediction),
|
| 70 |
+
"probability": prediction_proba
|
| 71 |
+
}
|
| 72 |
+
except Exception as e:
|
| 73 |
+
raise HTTPException(status_code=500, detail=f"Prediction error: {e}")
|