Spaces:
Runtime error
Runtime error
File size: 2,988 Bytes
91582e7 f8b50d2 33e8323 91582e7 4dd528a 91582e7 33e8323 91582e7 |
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 |
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
pipeline_filepath = "tmp/pipeline.joblib"
pipeline = joblib.load(pipeline_filepath)
app = FastAPI()
# Save the model again
# joblib.dump(pipeline, 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.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
import logging
# Configure logging
logging.basicConfig(level=logging.INFO) # Set the desired logging level
@app.post("/predict")
def get_data_from_user(data: PatientData):
try:
logging.info("Received data: %s", data.dict())
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"
result = {
'predicted_sepsis': sepsis_status,
'probability': probability_of_positive_class,
}
return result
except Exception as e:
logging.error("Error: %s", e)
return {"error": "An error occurred during prediction."}
|