Spaces:
Runtime error
Runtime error
File size: 2,957 Bytes
0868810 9ee36a2 0868810 bc2e290 0868810 df028d2 0868810 9ee36a2 0868810 9ee36a2 0868810 | 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | # Import required libraries
from fastapi import FastAPI, HTTPException, Form, Depends
from pydantic import BaseModel
import uvicorn
import joblib
import pandas as pd
# Load the pre-trained pipeline
model = joblib.load('pipelin.pkl')
# Create a FASTAPI app
app = FastAPI(
title="Sepsis Prediction API"
)
@app.get("/")
async def root():
return {
"info": "Welcome to the Sepsis Prediction API! This API predicts the probability of a patient having sepsis based on their vitals."
}
# Define a Pydantic model for input data
class Sepsis(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: int
Insurance: int
@classmethod
def as_form(
cls,
plasma_glucose: float = Form(...),
blood_work_result_1: float = Form(...),
blood_pressure: float = Form(...),
blood_work_result_2: float = Form(...),
blood_work_result_3: float = Form(...),
body_mass_index: float = Form(...),
blood_work_result_4: float = Form(...),
Age: int = Form(...),
Insurance: int = Form(...)
):
return cls(
plasma_glucose=plasma_glucose,
blood_work_result_1=blood_work_result_1,
blood_pressure=blood_pressure,
blood_work_result_2=blood_work_result_2,
blood_work_result_3=blood_work_result_3,
body_mass_index=body_mass_index,
blood_work_result_4=blood_work_result_4,
Age=Age,
Insurance=Insurance
)
# Define a route for prediction
@app.post("/predict/")
async def create_dataframe(form_data: Sepsis = Depends(Sepsis.as_form)):
try:
# Convert the form data to a data frame
df = pd.DataFrame(form_data.dict(), index=[0])
# Predicting
output = model.predict_proba(df)
df["predicted_label"] = output.argmax(axis=-1)
mapping = {0: "Sepsis Negative", 1: "Sepsis Positive"}
df["predicted_label"] = [mapping[x] for x in df["predicted_label"]]
# Calculating confidence score
confidence_score = output.max(axis=-1)
df["confidence_score"] = f"{round((confidence_score[0] * 100), 2)}%"
# Creating a display output
msg = "Execution Successful!"
code = 1
pred = df.to_dict("records")
result = {"Execution Message": msg, "Execution Code": code, "Prediction": pred}
except Exception as e:
# If there is an error...
msg = "Execution failed!"
code = 0
pred = None
result = {"Error": str(e), "Execution Message": msg, "Execution Code": code, "Prediction": pred }
return result
# Run the FASTAPI application
if __name__ == "__main":
uvicorn.run(app, host="127.0.0.1", port=8000, reload=True)
|