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