File size: 1,212 Bytes
eb3491c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
import joblib
import numpy as np
from pydantic import BaseModel

app = FastAPI()

# Load the model (ensure this path is correct)
model = joblib.load("model.pkl")

# Pydantic model for input validation
class InputData(BaseModel):
    BMI: float
    Parity: int
    Gravida: int
    Glucoselevel0minblood: float
    Glucoselevel120minblood: float
    SystolicBloodPressureCuff: float
    DiastolicBloodPressure: float

@app.get("/")
def home():
    return {"message": "ML API Running"}

@app.post("/predict")
def predict(data: InputData):
    # Convert the data to a numpy array for prediction
    input_array = np.array([[data.BMI, data.Parity, data.Gravida, data.Glucoselevel0minblood, 
                             data.Glucoselevel120minblood, data.SystolicBloodPressureCuff, 
                             data.DiastolicBloodPressure]])
    
    # Perform prediction and get probabilities
    prediction = model.predict(input_array)
    prob = model.predict_proba(input_array)[:, 1]  # Probability for the positive class (1)

    # Return both prediction and probability
    return {"prediction": int(prediction[0]), "probability": prob[0] * 100}