Sonny4Sonnix commited on
Commit
33bdf17
·
1 Parent(s): d6e9c1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py CHANGED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query, Request
2
+ from fastapi.responses import HTMLResponse
3
+ from fastapi.templating import Jinja2Templates
4
+ import joblib
5
+
6
+ app = FastAPI()
7
+ templates = Jinja2Templates(directory="templates")
8
+
9
+ # Load the pickled XGBoost model
10
+ xgb_model = joblib.load("xgb_model.joblib")
11
+
12
+ @app.get("/display")
13
+ def display_params(
14
+ request: Request,
15
+ prg: float = Query(..., description="Plasma glucose"),
16
+ pl: float = Query(..., description="Blood Work Result-1 (mu U/ml)"),
17
+ pr: float = Query(..., description="Blood Pressure (mm Hg)"),
18
+ sk: float = Query(..., description="Blood Work Result-2 (mm)"),
19
+ ts: float = Query(..., description="Blood Work Result-3 (mu U/ml)"),
20
+ m11: float = Query(..., description="Body mass index (weight in kg/(height in m)^2"),
21
+ bd2: float = Query(..., description="Blood Work Result-4 (mu U/ml)"),
22
+ age: int = Query(..., description="Patient's age (years)")
23
+ ):
24
+ # Prepare input features for prediction
25
+ input_features = [prg, pl, pr, sk, ts, m11, bd2, age]
26
+
27
+ # Make predictions using the loaded model
28
+ prediction = xgb_model.predict_proba([input_features])[0]
29
+
30
+ # Create a JSON response
31
+ response = {
32
+ "request": {
33
+ "prg": prg,
34
+ "pl": pl,
35
+ "pr": pr,
36
+ "sk": sk,
37
+ "ts": ts,
38
+ "m11": m11,
39
+ "bd2": bd2,
40
+ "age": age
41
+ },
42
+ "prediction": {
43
+ "class_0_probability": prediction[0],
44
+ "class_1_probability": prediction[1]
45
+ }
46
+ }
47
+
48
+ return templates.TemplateResponse(
49
+ "display_params.html",
50
+ {
51
+ "request": request,
52
+ "prg": prg,
53
+ "pl": pl,
54
+ "pr": pr,
55
+ "sk": sk,
56
+ "ts": ts,
57
+ "m11": m11,
58
+ "bd2": bd2,
59
+ "age": age,
60
+ "prediction": response["prediction"]
61
+ }
62
+ )