Anthony Ndung'u commited on
Commit
af50b58
1 Parent(s): 27b7c82

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +66 -0
main.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import libraries
2
+ from pydantic import BaseModel
3
+ import pandas as pd
4
+ import joblib
5
+ import uvicorn
6
+ import numpy as np
7
+ from fastapi import FastAPI, HTTPException,Query
8
+
9
+ app = FastAPI()
10
+
11
+ ###create home
12
+ @app.get('/')
13
+ def home():
14
+ return{'message':'Welcome to Sepsis Prediction Using Fastapi'}
15
+
16
+ ## Load the model
17
+ model = joblib.load("src/rf_pipeline.joblib")
18
+
19
+ # Endpoint for predicting sepsis using a GET request
20
+ @app.post("/predict")
21
+ def predict_sepsis(
22
+ PRG: int = Query(..., description="Plasma_glucose"),
23
+ PL: int = Query(..., description="Blood_Work_R1"),
24
+ PR: int = Query(..., description="Blood_Pressure"),
25
+ SK: int = Query(..., description="Blood_Work_R2"),
26
+ TS: int = Query(..., description="Blood_Work_R3"),
27
+ M11: float = Query(..., description="BMI"),
28
+ BD2: float = Query(..., description="Blood_Work_R4"),
29
+ Age: int = Query(..., description="Age")
30
+ ):
31
+ try:
32
+ # Convert input data to a dictionary
33
+ input_data = {
34
+ 'PRG': PRG,
35
+ 'PL': PL,
36
+ 'PR': PR,
37
+ 'SK': SK,
38
+ 'TS': TS,
39
+ 'M11': M11,
40
+ 'BD2': BD2,
41
+ 'Age': Age,
42
+ }
43
+
44
+
45
+ # Convert input_data to DataFrame
46
+ input_data_df = pd.DataFrame([input_data])
47
+
48
+ # Use the loaded model to make predictions
49
+
50
+ prediction= model.predict(input_data_df)[0]
51
+
52
+ sepsis_status = "patient has sepsis" if prediction == 1 else "Patient does not have sepsis"
53
+
54
+ # Return the prediction
55
+ return {"prediction": sepsis_status}
56
+
57
+ except Exception as e:
58
+ raise HTTPException(status_code=500, detail=str(e))
59
+
60
+ if __name__ == "__main__":
61
+ import uvicorn
62
+ import nest_asyncio
63
+
64
+ nest_asyncio.apply()
65
+
66
+ uvicorn.run(app, host="127.0.0.1", port=8003, log_level="info")