mlmodels / backend /main.py
sathishleo's picture
Restructure for HF Spaces: app.py at root, backend moved
3724af6
raw
history blame contribute delete
615 Bytes
import joblib
from fastapi import FastAPI
import pickle
import pandas as pd
import os
# main.py
# relative import
# optional if using as module
app = FastAPI()
MODEL_PATH = "models/best_model.pkl"
# Train model if it doesn't exist
if os.path.exists(MODEL_PATH):
model = joblib.load(MODEL_PATH)
# Load model
@app.get("/")
def read_root():
return {"message": "Hello World"}
@app.get("/predict")
def predict(preg: int, glu: int, bp: int):
df = pd.DataFrame([[preg, glu, bp]], columns=["Pregnancies","Glucose","BloodPressure"])
pred = model.predict(df)[0]
return {"prediction": int(pred)}