Spaces:
Sleeping
Sleeping
File size: 4,819 Bytes
750ab19 60928b7 750ab19 522bbb2 20cbd6b 750ab19 d59a622 750ab19 d59a622 750ab19 d59a622 750ab19 d59a622 750ab19 d59a622 750ab19 6c54e72 d59a622 6c54e72 93c04c4 20cbd6b d59a622 20cbd6b d59a622 522bbb2 d59a622 93c04c4 6ecec3f d59a622 70fb558 d59a622 522bbb2 d59a622 522bbb2 d59a622 6c54e72 d59a622 6c54e72 d59a622 6c54e72 d59a622 6c54e72 d59a622 6c54e72 750ab19 93c04c4 |
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import pickle
import gradio as gr
with open('models/risk-model.pck', 'rb') as f:
dv, model = pickle.load(f)
def predict_single(customer, dv, model):
x = dv.transform([customer])
y_pred = model.predict_proba(x)[:, 1]
return (y_pred[0] >= 0.5, y_pred[0])
def predict_risk(
sex: int, age: int, classification: int, patient_type: int,
pneumonia: bool, pregnancy: bool, diabetes: bool, copd: bool,
asthma: bool, inmsupr: bool, hypertension: bool, cardiovascular: bool,
renal_chronic: bool, other_disease: bool, obesity: bool, tobacco: bool,
usmer: int, medical_unit: int):
sex_value = 1 if sex == "Femenino" else 2
customer = {
"sex": sex,
"age": age,
"clasiffication_final": classification,
"patient_type": patient_type,
"pneumonia": int(pneumonia),
"pregnant": int(pregnancy),
"diabetes": int(diabetes),
"copd": int(copd),
"asthma": int(asthma),
"inmsupr": int(inmsupr),
"hipertension": int(hypertension),
"cardiovascular": int(cardiovascular),
"renal_chronic": int(renal_chronic),
"other_disease": int(other_disease),
"obesity": int(obesity),
"tobacco": int(tobacco),
"usmer": usmer,
"medical_unit": medical_unit,
}
risk, prediction = predict_single(customer, dv, model)
return {
"Risk": bool(risk),
"Risk Probability": round(float(prediction), 4)
}
with gr.Blocks() as interface:
gr.Markdown("## COVID-19 ICU Risk Predictor")
gr.Markdown(
"Fill in the patient's details below. Fields have validations to ensure correct inputs."
)
with gr.Row():
with gr.Column():
gr.Markdown("### Inputs")
sex = gr.Dropdown(
choices=["Femenino", "Masculino"],
label="Sexo",
value="Masculino",
interactive=True,
info="Seleccione el sexo del paciente (Femenino o Masculino).",
)
age = gr.Number(
label="Age",
interactive=True,
minimum=0,
maximum=120,
info="Enter the patient's age (0-120).",
)
classification = gr.Number(
label="Classification",
interactive=True,
value=1,
minimum=1,
maximum=3,
info="1-3 means the patient was diagnosed with COVID; 4+ means not diagnosed.",
)
patient_type = gr.Dropdown(
choices=["Returned Home", "Hospitalization"],
label="Patient Type",
value="Returned Home",
interactive=True,
info="1 for Returned Home, 2 for Hospitalization",
)
usmer = gr.Number(
label="USMER",
interactive=True,
value=1,
minimum=1,
maximum=3,
info="Medical units: 1 for First Level, 2 for Second Level, 3 for Third Level",
)
medical_unit = gr.Number(
label="Medical Unit",
interactive=True,
value=1,
minimum=1,
info="Type of institution of the National Health System.",
)
with gr.Column():
gr.Markdown("### Binary Inputs")
with gr.Row():
with gr.Column():
pneumonia = gr.Checkbox(label="Pneumonia")
pregnancy = gr.Checkbox(label="Pregnancy")
diabetes = gr.Checkbox(label="Diabetes")
copd = gr.Checkbox(label="COPD")
asthma = gr.Checkbox(label="Asthma")
inmsupr = gr.Checkbox(label="Immunosuppression")
with gr.Column():
hypertension = gr.Checkbox(label="Hypertension")
cardiovascular = gr.Checkbox(label="Cardiovascular Disease")
renal_chronic = gr.Checkbox(label="Chronic Renal Disease")
other_disease = gr.Checkbox(label="Other Disease")
obesity = gr.Checkbox(label="Obesity")
tobacco = gr.Checkbox(label="Tobacco Use")
predict_btn = gr.Button("Predict Risk")
output = gr.JSON(label="Prediction Result")
predict_btn.click(
predict_risk,
inputs=[
sex, age, classification, patient_type, pneumonia, pregnancy,
diabetes, copd, asthma, inmsupr, hypertension, cardiovascular,
renal_chronic, other_disease, obesity, tobacco, usmer, medical_unit,
],
outputs=output,
)
if __name__ == "__main__":
interface.launch()
|