File size: 2,348 Bytes
38493c8
df9828e
64c643e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38493c8
 
 
 
 
 
64c643e
38493c8
 
 
 
 
 
 
9381e32
 
 
 
 
64c643e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
from fastapi import FastAPI, Request, HTTPException, APIRouter, Depends
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression

app = FastAPI()

# Load the entire pipeline
pipeline_filepath = "pipeline.joblib"
pipeline = joblib.load(pipeline_filepath)

class PatientData(BaseModel):
    Plasma_glucose : float
    Blood_Work_Result_1: float
    Blood_Pressure : float
    Blood_Work_Result_2 : float
    Blood_Work_Result_3 : float
    Body_mass_index  : float
    Blood_Work_Result_4: float
    Age: float
    Insurance: int

# @app.get("/")
# def read_root():
#     explanation = {
#         'message': "Welcome to the Sepsis Prediction App",
#         'description': "This API allows you to predict sepsis based on patient data.",
#         'usage': "Submit a POST request to /predict with patient data to make predictions.",
        
#     }
#     return explanation
    
@app.get("/")
async def root():
    return RedirectResponse(url="/docs")
    
    #swagger ui
@app.get("/docs")
async def get_swagger_ui_html():
    return get_swagger_ui_html(openapi_url="/openapi.json", title="API docs")
    
@app.post("/predict")
def get_data_from_user(data: PatientData):
    user_input = data.dict()

    input_df = pd.DataFrame([user_input])

    # Make predictions using the loaded pipeline
    prediction = pipeline.predict(input_df)
    probabilities = pipeline.predict_proba(input_df)

    
    probability_of_positive_class = probabilities[0][1]

    # Calculate the prediction
    sepsis_status = "Positive" if prediction[0] == 1 else "Negative"
    sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention." if prediction[0] == 1 else "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."

    result = {
        'predicted_sepsis': sepsis_status,
        'probability': probability_of_positive_class,
        'sepsis_explanation': sepsis_explanation
    }
    return result