File size: 1,918 Bytes
33bdf17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import FastAPI, Query, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
import joblib 

app = FastAPI()
templates = Jinja2Templates(directory="templates")

# Load the pickled XGBoost model
xgb_model = joblib.load("xgb_model.joblib")

@app.get("/display")
def display_params(
    request: Request,
    prg: float = Query(..., description="Plasma glucose"),
    pl: float = Query(..., description="Blood Work Result-1 (mu U/ml)"),
    pr: float = Query(..., description="Blood Pressure (mm Hg)"),
    sk: float = Query(..., description="Blood Work Result-2 (mm)"),
    ts: float = Query(..., description="Blood Work Result-3 (mu U/ml)"),
    m11: float = Query(..., description="Body mass index (weight in kg/(height in m)^2"),
    bd2: float = Query(..., description="Blood Work Result-4 (mu U/ml)"),
    age: int = Query(..., description="Patient's age (years)")
):
    # Prepare input features for prediction
    input_features = [prg, pl, pr, sk, ts, m11, bd2, age]

    # Make predictions using the loaded model
    prediction = xgb_model.predict_proba([input_features])[0]

    # Create a JSON response 
    response = {
        "request": {
            "prg": prg,
            "pl": pl,
            "pr": pr,
            "sk": sk,
            "ts": ts,
            "m11": m11,
            "bd2": bd2,
            "age": age
        },
        "prediction": {
            "class_0_probability": prediction[0],
            "class_1_probability": prediction[1]               
        }
    }

    return templates.TemplateResponse(
        "display_params.html",
        {
            "request": request,
            "prg": prg,
            "pl": pl,
            "pr": pr,
            "sk": sk,
            "ts": ts,
            "m11": m11,
            "bd2": bd2,
            "age": age,
            "prediction": response["prediction"]
        }
    )