Spaces:
Runtime error
Runtime error
Commit
·
9381e32
1
Parent(s):
5c4ce0e
Update main.py
Browse files
main.py
CHANGED
@@ -1,147 +1,11 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
# app = FastAPI()
|
12 |
-
|
13 |
-
# # Load the numerical imputer, scaler, and model
|
14 |
-
# num_imputer_filepath = "joblib_files/numerical_imputer.joblib"
|
15 |
-
# scaler_filepath = "joblib_files/scaler.joblib"
|
16 |
-
# model_filepath = "joblib_files/lr_model.joblib"
|
17 |
-
|
18 |
-
# num_imputer = joblib.load(num_imputer_filepath)
|
19 |
-
# scaler = joblib.load(scaler_filepath)
|
20 |
-
# model = joblib.load(model_filepath)
|
21 |
-
|
22 |
-
# class PatientData(BaseModel):
|
23 |
-
# PRG: float
|
24 |
-
# PL: float
|
25 |
-
# PR: float
|
26 |
-
# SK: float
|
27 |
-
# TS: float
|
28 |
-
# M11: float
|
29 |
-
# BD2: float
|
30 |
-
# Age: float
|
31 |
-
# Insurance: int
|
32 |
-
|
33 |
-
# def preprocess_input_data(user_input):
|
34 |
-
# input_data_df = pd.DataFrame([user_input])
|
35 |
-
# num_columns = [col for col in input_data_df.columns if input_data_df[col].dtype != 'object']
|
36 |
-
# input_data_imputed_num = num_imputer.transform(input_data_df[num_columns])
|
37 |
-
# input_scaled_df = pd.DataFrame(scaler.transform(input_data_imputed_num), columns=num_columns)
|
38 |
-
# return input_scaled_df
|
39 |
-
|
40 |
-
# @app.get("/")
|
41 |
-
# def read_root():
|
42 |
-
# return "Sepsis Prediction App"
|
43 |
-
# @app.post("/sepsis/predict")
|
44 |
-
# def get_data_from_user(data:PatientData):
|
45 |
-
# user_input = data.dict()
|
46 |
-
# input_scaled_df = preprocess_input_data(user_input)
|
47 |
-
# probabilities = model.predict_proba(input_scaled_df)[0]
|
48 |
-
# prediction = np.argmax(probabilities)
|
49 |
-
|
50 |
-
# sepsis_status = "Positive" if prediction == 1 else "Negative"
|
51 |
-
# probability = probabilities[1] if prediction == 1 else probabilities[0]
|
52 |
-
|
53 |
-
# if prediction == 1:
|
54 |
-
# sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention."
|
55 |
-
# else:
|
56 |
-
# sepsis_explanation = "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."
|
57 |
-
|
58 |
-
# statement = f"The patient's sepsis status is {sepsis_status} with a probability of {probability:.2f}. {sepsis_explanation}"
|
59 |
-
|
60 |
-
# user_input_statement = "user-inputted data: "
|
61 |
-
# output_df = pd.DataFrame([user_input])
|
62 |
-
|
63 |
-
# result = {'predicted_sepsis': sepsis_status, 'statement': statement, 'user_input_statement': user_input_statement, 'input_data_df': output_df.to_dict('records')}
|
64 |
-
# return result
|
65 |
-
|
66 |
-
# from fastapi import FastAPI, Form
|
67 |
-
# from pydantic import BaseModel
|
68 |
-
# import joblib
|
69 |
-
# import pandas as pd
|
70 |
-
# import numpy as np
|
71 |
-
# import uvicorn
|
72 |
-
# from fastapi.responses import JSONResponse
|
73 |
-
|
74 |
-
# app = FastAPI()
|
75 |
-
|
76 |
-
# # Load the entire pipeline
|
77 |
-
# pipeline_filepath = "pipeline.joblib"
|
78 |
-
# pipeline = joblib.load(pipeline_filepath)
|
79 |
-
|
80 |
-
# class PatientData(BaseModel):
|
81 |
-
# PRG: float
|
82 |
-
# PL: float
|
83 |
-
# PR: float
|
84 |
-
# SK: float
|
85 |
-
# TS: float
|
86 |
-
# M11: float
|
87 |
-
# BD2: float
|
88 |
-
# Age: float
|
89 |
-
# Insurance: int
|
90 |
-
|
91 |
-
# @app.get("/")
|
92 |
-
# def read_root():
|
93 |
-
# explanation = {
|
94 |
-
# 'message': "Welcome to the Sepsis Prediction App",
|
95 |
-
# 'description': "This API allows you to predict sepsis based on patient data.",
|
96 |
-
# 'usage': "Submit a POST request to /predict with patient data to make predictions.",
|
97 |
-
# 'input_fields': {
|
98 |
-
# 'PRG': 'Plasma_glucose',
|
99 |
-
# 'PL': 'Blood_Work_Result_1',
|
100 |
-
# 'PR': 'Blood_Pressure',
|
101 |
-
# 'SK': 'Blood_Work_Result_2',
|
102 |
-
# 'TS': 'Blood_Work_Result_3',
|
103 |
-
# 'M11': 'Body_mass_index',
|
104 |
-
# 'BD2': 'Blood_Work_Result_4',
|
105 |
-
# 'Insurance': 'Sepsis (Positive = 1, Negative = 0)'
|
106 |
-
# }
|
107 |
-
# }
|
108 |
-
# return explanation
|
109 |
-
|
110 |
-
|
111 |
-
# @app.post("/predict")
|
112 |
-
# def get_data_from_user(data: PatientData):
|
113 |
-
# user_input = data.model_dump()
|
114 |
-
|
115 |
-
|
116 |
-
# input_df = pd.DataFrame([user_input])
|
117 |
-
# # Make predictions using the loaded pipeline
|
118 |
-
# # Make predictions using the loaded pipeline
|
119 |
-
# predictions = pipeline.predict(user_input)
|
120 |
-
# probabilities = pipeline.decision_function(user_input)
|
121 |
-
|
122 |
-
# # Assuming the pipeline uses a Logistic Regression model
|
123 |
-
# probability_of_positive_class = probabilities[0]
|
124 |
-
|
125 |
-
# # Calculate the prediction
|
126 |
-
# prediction = 1 if probability_of_positive_class >= 0.5 else 0
|
127 |
-
|
128 |
-
# sepsis_status = "Positive" if prediction == 1 else "Negative"
|
129 |
-
# sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention." if prediction == 1 else "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."
|
130 |
-
|
131 |
-
|
132 |
-
# if prediction == 1:
|
133 |
-
# sepsis_status = "Positive"
|
134 |
-
# sepsis_explanation = "A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention."
|
135 |
-
# else:
|
136 |
-
# sepsis_status = "Negative"
|
137 |
-
# sepsis_explanation = "A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."
|
138 |
-
|
139 |
-
# result = {
|
140 |
-
# 'predicted_sepsis': sepsis_status,
|
141 |
-
# 'sepsis_explanation': sepsis_explanation
|
142 |
-
# }
|
143 |
-
# return result
|
144 |
-
|
145 |
from fastapi import FastAPI
|
146 |
from pydantic import BaseModel
|
147 |
import joblib
|
@@ -179,7 +43,11 @@ def read_root():
|
|
179 |
|
180 |
}
|
181 |
return explanation
|
182 |
-
|
|
|
|
|
|
|
|
|
183 |
@app.post("/predict")
|
184 |
def get_data_from_user(data: PatientData):
|
185 |
user_input = data.dict()
|
|
|
1 |
+
from fastapi.openapi.models import APIKey, OAuthFlows as OAuthFlowsModel
|
2 |
+
from fastapi.openapi.models import OAuthFlowAuthorizationCode as OAuthFlowAuthorizationCodeModel
|
3 |
+
from fastapi.openapi.models import OAuthFlowAuthorizationCode as OAuthFlowAuthorizationCodeModel
|
4 |
+
from fastapi.openapi.models import OAuthFlowsAuthorizationCode
|
5 |
+
from fastapi.openapi.models import OAuthFlowAuthorizationCode as OAuthFlowAuthorizationCodeModel
|
6 |
+
from fastapi.openapi.models import OAuthFlowAuthorizationCode as OAuthFlowAuthorizationCodeModel
|
7 |
+
from fastapi.openapi.models import OAuthFlowAuthorizationCode
|
8 |
+
from fastapi.openapi.models import OAuthFlowsAuthorizationCode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
from fastapi import FastAPI
|
10 |
from pydantic import BaseModel
|
11 |
import joblib
|
|
|
43 |
|
44 |
}
|
45 |
return explanation
|
46 |
+
#swagger ui
|
47 |
+
@app.get("/docs")
|
48 |
+
async def get_swagger_ui_html():
|
49 |
+
return get_swagger_ui_html(openapi_url="/openapi.json", title="API docs")
|
50 |
+
|
51 |
@app.post("/predict")
|
52 |
def get_data_from_user(data: PatientData):
|
53 |
user_input = data.dict()
|