Upload 4 files
Browse files- gbc.pkl +3 -0
- main.py +83 -0
- requirements.txt +0 -0
- scaler.pkl +3 -0
gbc.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b0cf7da0343fc4dec64684c629381546076e2b651145ba3c7b0d9c7ab939bc04
|
| 3 |
+
size 346540
|
main.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Form
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import pickle
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import uvicorn
|
| 7 |
+
import os
|
| 8 |
+
from sklearn.preprocessing import StandardScaler
|
| 9 |
+
import joblib
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
""" Creating the FastAPI Instance. i.e. foundation for our API,
|
| 13 |
+
which will be the main part of our project"""
|
| 14 |
+
|
| 15 |
+
app = FastAPI(title="API",description="API for sepsis prediction")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
"""We load a machine learning model and a scaler that help us make predictions based on data."""
|
| 19 |
+
model = joblib.load('gbc.pkl',mmap_mode='r')
|
| 20 |
+
scaler = joblib.load('scaler.pkl',mmap_mode='r')
|
| 21 |
+
|
| 22 |
+
"""We define a function that will make predictions using our model and scaler."""
|
| 23 |
+
def predict(df, endpoint='simple'):
|
| 24 |
+
# Scaling
|
| 25 |
+
scaled_df = scaler.transform(df)
|
| 26 |
+
|
| 27 |
+
# Prediction
|
| 28 |
+
prediction = model.predict_proba(scaled_df)
|
| 29 |
+
highest_proba = prediction.max(axis=1)
|
| 30 |
+
|
| 31 |
+
predicted_labels = ["Patient does not have sepsis" if i == 0 else "Patient has Sepsis" for i in highest_proba]
|
| 32 |
+
response = []
|
| 33 |
+
for label, proba in zip(predicted_labels, highest_proba):
|
| 34 |
+
output = {
|
| 35 |
+
"prediction": label,
|
| 36 |
+
"probability of prediction": str(round(proba * 100)) + '%'
|
| 37 |
+
}
|
| 38 |
+
response.append(output)
|
| 39 |
+
return response
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
"""We create models for the data that our API will work with.
|
| 43 |
+
We define what kind of information the data will have.
|
| 44 |
+
It's like deciding what information we need to collect and how it should be organized."""
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
"""These classes define the data models used for API endpoints.
|
| 48 |
+
The 'Patient' class represents a single patient's data,
|
| 49 |
+
and the 'Patients' class represents a list of patients' data.
|
| 50 |
+
The Patients class also includes a class method return_list_of_dict()
|
| 51 |
+
that converts the Patients object into a list of dictionaries"""
|
| 52 |
+
|
| 53 |
+
class Patient(BaseModel):
|
| 54 |
+
Blood_Work_R1: float = Form(...)
|
| 55 |
+
Blood_Pressure: float = Form(...)
|
| 56 |
+
Blood_Work_R3: float = Form(...)
|
| 57 |
+
BMI: float = Form(...)
|
| 58 |
+
Blood_Work_R4: float = Form(...)
|
| 59 |
+
Patient_age: int = Form(...)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
"""Next block of code defines different parts of our API and how it responds to different requests.
|
| 63 |
+
It sets up a main page with a specific message, provides a checkup endpoint to receive
|
| 64 |
+
optional parameters, and sets up prediction endpoints to receive medical data for making predictions,
|
| 65 |
+
either for a single patient or multiple patients."""
|
| 66 |
+
|
| 67 |
+
@app.get("/")
|
| 68 |
+
def root():
|
| 69 |
+
return {"API": "This is an API for sepsis prediction."}
|
| 70 |
+
|
| 71 |
+
# Prediction endpoint (Where we will input our features)
|
| 72 |
+
@app.post("/predict")
|
| 73 |
+
def predict_sepsis(patient: Patient):
|
| 74 |
+
|
| 75 |
+
# Make prediction
|
| 76 |
+
data = pd.DataFrame(patient.dict(), index=[0])
|
| 77 |
+
scaled_data = scaler.transform(data)
|
| 78 |
+
parsed = predict(df=scaled_data)
|
| 79 |
+
return {"output": parsed}
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
os.environ["DEBUG"] = "True" # Enable debug mode
|
| 83 |
+
uvicorn.run("main:app", reload=True)
|
requirements.txt
ADDED
|
Binary file (1.74 kB). View file
|
|
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9cfc028874b4c2aa6d9806f11a4a14c338bffcf00d34fac767a8a2232a498e9b
|
| 3 |
+
size 1159
|