-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
44 lines (36 loc) · 997 Bytes
/
api.py
File metadata and controls
44 lines (36 loc) · 997 Bytes
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
"""FastAPI inference server for churn prediction"""
from fastapi import FastAPI
from pydantic import BaseModel
import pickle
import numpy as np
app = FastAPI()
# Load model
with open('models/churn_model.pkl', 'rb') as f:
model = pickle.load(f)
class CustomerData(BaseModel):
age: int
tenure_months: int
monthly_charges: float
total_charges: float
num_support_calls: int
@app.get("/health")
def health():
return {"status": "healthy"}
@app.post("/predict")
def predict(data: CustomerData):
features = np.array([[
data.age,
data.tenure_months,
data.monthly_charges,
data.total_charges,
data.num_support_calls
]])
prediction = model.predict(features)[0]
probability = model.predict_proba(features)[0][1]
return {
"churn": int(prediction),
"churn_probability": float(probability)
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)