Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Query
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import pickle
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
app = FastAPI(
|
7 |
+
title="Sepsis Prediction API",
|
8 |
+
description="This FastAPI application provides sepsis predictions using a machine learning model.",
|
9 |
+
version="1.0"
|
10 |
+
)
|
11 |
+
|
12 |
+
# Load the model and key components
|
13 |
+
with open('model_and_key_components.pkl', 'rb') as file:
|
14 |
+
loaded_components = pickle.load(file)
|
15 |
+
|
16 |
+
loaded_model = loaded_components['model']
|
17 |
+
loaded_encoder = loaded_components['encoder']
|
18 |
+
loaded_scaler = loaded_components['scaler']
|
19 |
+
|
20 |
+
# Define the input data structure using Pydantic BaseModel
|
21 |
+
class InputData(BaseModel):
|
22 |
+
PRG: int = Query(..., title="Patient's Pregnancy Count", description="Enter the number of pregnancies.", example=2)
|
23 |
+
PL: float = Query(..., title="Platelet Count", description="Enter the platelet count.", example=150.0)
|
24 |
+
PR: float = Query(..., title="Pulse Rate", description="Enter the pulse rate.", example=75.0)
|
25 |
+
SK: float = Query(..., title="Skin Thickness", description="Enter the skin thickness.", example=25.0)
|
26 |
+
TS: int = Query(..., title="Triceps Skin Fold Thickness", description="Enter the triceps skin fold thickness.", example=30)
|
27 |
+
M11: float = Query(..., title="Insulin Level", description="Enter the insulin level.", example=120.0)
|
28 |
+
BD2: float = Query(..., title="BMI", description="Enter the Body Mass Index (BMI).", example=32.0)
|
29 |
+
Age: int = Query(..., title="Age", description="Enter the patient's age.", example=35)
|
30 |
+
|
31 |
+
# Define the output data structure using Pydantic BaseModel
|
32 |
+
class OutputData(BaseModel):
|
33 |
+
Sepsis: str
|
34 |
+
|
35 |
+
# Define a function to preprocess input data
|
36 |
+
def preprocess_input_data(input_data: InputData):
|
37 |
+
# Encode Categorical Variables (if needed)
|
38 |
+
# All columns are numerical. No need for encoding
|
39 |
+
|
40 |
+
# Apply scaling to numerical data
|
41 |
+
numerical_cols = ['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age']
|
42 |
+
input_data_scaled = loaded_scaler.transform([list(input_data.dict().values())])
|
43 |
+
|
44 |
+
return pd.DataFrame(input_data_scaled, columns=numerical_cols)
|
45 |
+
|
46 |
+
# Define a function to make predictions
|
47 |
+
def make_predictions(input_data_scaled_df: pd.DataFrame):
|
48 |
+
y_pred = loaded_model.predict(input_data_scaled_df)
|
49 |
+
sepsis_mapping = {0: 'Negative', 1: 'Positive'}
|
50 |
+
return sepsis_mapping[y_pred[0]]
|
51 |
+
|
52 |
+
@app.get("/")
|
53 |
+
async def root():
|
54 |
+
# Endpoint at the root URL ("/") returns a welcome message with a clickable link
|
55 |
+
message = "Welcome to your Sepsis Classification API! Click [here](/docs) to access the API documentation."
|
56 |
+
return {"message": message}
|
57 |
+
|
58 |
+
@app.post("/predict/", response_model=OutputData)
|
59 |
+
async def predict_sepsis(input_data: InputData):
|
60 |
+
try:
|
61 |
+
input_data_scaled_df = preprocess_input_data(input_data)
|
62 |
+
sepsis_status = make_predictions(input_data_scaled_df)
|
63 |
+
return {"Sepsis": sepsis_status}
|
64 |
+
except Exception as e:
|
65 |
+
# Handle exceptions and return an error response
|
66 |
+
raise HTTPException(status_code=500, detail=str(e))
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
import uvicorn
|
70 |
+
# Run the FastAPI application on the local host and port 8000
|
71 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|