imalexianne commited on
Commit
91582e7
·
1 Parent(s): fcd66fa

add main.py file

Browse files
Files changed (1) hide show
  1. main.py +103 -0
main.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ from fastapi import FastAPI
4
+ from pydantic import BaseModel
5
+ import joblib
6
+ import pandas as pd
7
+ import numpy as np
8
+ from sklearn.preprocessing import StandardScaler
9
+ from sklearn.impute import SimpleImputer
10
+ from sklearn.compose import ColumnTransformer
11
+ from sklearn.pipeline import Pipeline
12
+ from sklearn.linear_model import LogisticRegression
13
+
14
+ app = FastAPI()
15
+
16
+ # Load the entire pipeline
17
+ # pipeline_filepath = "pipeline.joblib"
18
+ # pipeline = joblib.load(pipeline_filepath)
19
+
20
+ # import joblib
21
+
22
+ # Reload the model
23
+ pipeline_filepath = "pipeline.joblib"
24
+ pipeline = joblib.load(pipeline_filepath)
25
+
26
+ # Save the model again
27
+ joblib.dump(pipeline, pipeline_filepath)
28
+
29
+
30
+ class PatientData(BaseModel):
31
+ Plasma_glucose : float
32
+ Blood_Work_Result_1: float
33
+ Blood_Pressure : float
34
+ Blood_Work_Result_2 : float
35
+ Blood_Work_Result_3 : float
36
+ Body_mass_index : float
37
+ Blood_Work_Result_4: float
38
+ Age: float
39
+ Insurance: int
40
+
41
+ @app.get("/")
42
+ def read_root():
43
+ explanation = {
44
+ 'message': "Welcome to the Sepsis Prediction App",
45
+ 'description': "This API allows you to predict sepsis based on patient data.",
46
+ 'usage': "Submit a POST request to /predict with patient data to make predictions.",
47
+
48
+ }
49
+ return explanation
50
+
51
+ # @app.post("/predict")
52
+ # def get_data_from_user(data: PatientData):
53
+ # user_input = data.dict()
54
+
55
+ # input_df = pd.DataFrame([user_input])
56
+
57
+ # Make predictions using the loaded pipeline
58
+ # prediction = pipeline.predict(input_df)
59
+ # probabilities = pipeline.predict_proba(input_df)
60
+
61
+
62
+ # probability_of_positive_class = probabilities[0][1]
63
+
64
+ # Calculate the prediction
65
+ # sepsis_status = "Positive" if prediction[0] == 1 else "Negative"
66
+ # 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."
67
+
68
+ # result = {
69
+ # 'predicted_sepsis': sepsis_status,
70
+ # 'probability': probability_of_positive_class,
71
+ # 'sepsis_explanation': sepsis_explanation
72
+ # }
73
+ # return result
74
+
75
+ import logging
76
+
77
+ # Configure logging
78
+ logging.basicConfig(level=logging.INFO) # Set the desired logging level
79
+
80
+ @app.post("/predict")
81
+ def get_data_from_user(data: PatientData):
82
+ try:
83
+ logging.info("Received data: %s", data.dict())
84
+ user_input = data.dict()
85
+ input_df = pd.DataFrame([user_input])
86
+
87
+ # Make predictions using the loaded pipeline
88
+ prediction = pipeline.predict(input_df)
89
+ probabilities = pipeline.predict_proba(input_df)
90
+
91
+ probability_of_positive_class = probabilities[0][1]
92
+
93
+ # Calculate the prediction
94
+ sepsis_status = "Positive" if prediction[0] == 1 else "Negative"
95
+
96
+ result = {
97
+ 'predicted_sepsis': sepsis_status,
98
+ 'probability': probability_of_positive_class,
99
+ }
100
+ return result
101
+ except Exception as e:
102
+ logging.error("Error: %s", e)
103
+ return {"error": "An error occurred during prediction."}