|
|
from fastapi import FastAPI |
|
|
from pydantic import BaseModel |
|
|
import pandas as pd |
|
|
import joblib |
|
|
import os |
|
|
import requests |
|
|
|
|
|
MODEL_URL = "https://huggingface.co/adeshjain/model1/resolve/main/model_j.joblib" |
|
|
SCALER_URL = "https://huggingface.co/adeshjain/model1/resolve/main/scaler_j.joblib" |
|
|
|
|
|
def download_file(url, filename): |
|
|
path = os.path.join("/tmp", filename) |
|
|
if not os.path.exists(path): |
|
|
r = requests.get(url) |
|
|
r.raise_for_status() |
|
|
with open(path, "wb") as f: |
|
|
f.write(r.content) |
|
|
return path |
|
|
|
|
|
model = joblib.load(download_file(MODEL_URL, "model_j.joblib")) |
|
|
scaler = joblib.load(download_file(SCALER_URL, "scaler_j.joblib")) |
|
|
|
|
|
class ClaimData(BaseModel): |
|
|
OPAnnualReimbursementAmt: float |
|
|
OPAnnualDeductibleAmt: float |
|
|
DeductibleAmtPaid: float |
|
|
claim: float |
|
|
period: float |
|
|
phy_same: float |
|
|
Gender_1: float |
|
|
Gender_2: float |
|
|
RenalDiseaseIndicator: float |
|
|
age: float |
|
|
alife: float |
|
|
Provider: float |
|
|
NoOfMonths_PartACov: float |
|
|
NoOfMonths_PartBCov: float |
|
|
ChronicCond_Alzheimer: float |
|
|
ChronicCond_KidneyDisease: float |
|
|
ChronicCond_Cancer: float |
|
|
ChronicCond_ObstrPulmonary: float |
|
|
ChronicCond_Depression: float |
|
|
ChronicCond_Diabetes: float |
|
|
ChronicCond_IschemicHeart: float |
|
|
ChronicCond_stroke: float |
|
|
IPAnnualReimbursementAmt: float |
|
|
|
|
|
app = FastAPI(title="Medicare Fraud Prediction API") |
|
|
|
|
|
@app.post("/predict") |
|
|
def predict_fraud(data: ClaimData): |
|
|
|
|
|
df = pd.DataFrame([data.dict()]) |
|
|
|
|
|
|
|
|
columns_to_scale = ['OPAnnualReimbursementAmt','OPAnnualDeductibleAmt','DeductibleAmtPaid'] |
|
|
df[columns_to_scale] = scaler.transform(df[columns_to_scale]) |
|
|
|
|
|
|
|
|
pred = model.predict(df)[0] |
|
|
result = "Fraud" if pred == 1 else "Not Fraud" |
|
|
|
|
|
return {"prediction": result} |
|
|
|
|
|
@app.get("/") |
|
|
def root(): |
|
|
return {"message": "working"} |
|
|
|