MachineLearning / app.py
Sebastian Nausester
app.py updated
fc251a8
import gradio as gr
import pandas as pd
import joblib
def predict(gender, bmi, asthma, glucose, pulse):
gender_char = 1
if gender == "Female":
gender_char = 0
asthma_val = False
if asthma is True:
asthma_val = 1
# Hardcoded Values are the mean of the training_data.csv
pred_data = {
"id": 0,
"rcount": 1.1178,
"gender": gender_char,
"dialysisrenalendstage": False,
"asthma": asthma_val,
"irondef": False,
"pneum": False,
"substancedependence": False,
"psychologicaldisordermajor": False,
"depress": False,
"psychother": False,
"fibrosisandother": False,
"malnutrition": False,
"hemo": False,
"hematocrit": False,
"neutrophils": False,
"sodium": 137.89369109187714,
"glucose": glucose,
"bloodureanitro": 14.101120748294186,
"creatinine": 1.0994443932147715,
"bmi": bmi,
"pulse": pulse,
"respiration": 6.491511666666584,
"secondarydiagnosisnonicd9": 0,
"facid": 0
}
features = ['id', 'rcount', 'gender', 'dialysisrenalendstage', 'asthma', 'irondef', 'pneum', 'substancedependence',
'psychologicaldisordermajor', 'depress', 'psychother', 'fibrosisandother', 'malnutrition', 'hemo',
'hematocrit', 'neutrophils', 'sodium', 'glucose', 'bloodureanitro', 'creatinine', 'bmi', 'pulse',
'respiration', 'secondarydiagnosisnonicd9', 'facid']
pred_df = pd.DataFrame([pred_data], columns=features)
result = "Error in prediction"
try:
model = joblib.load('data/model.pkl')
result = model.predict(pred_df)
except Exception as e:
print(f"Fehler beim Laden des Modells: {str(e)}")
print(result)
return result
gender = 0
bmi_min, bmi_max, bmi_default = 15, 50, 25
asthma = 0
glucose_min, glucose_max, glucose_default = 30, 190, 85
pulse_min, pulse_max, pulse_default = 40, 70, 180
# Create the interface
iface = gr.Interface(
fn=predict,
inputs=[
gr.components.Radio(["Female", "Male"], label="Gender"),
gr.components.Slider(minimum=bmi_min, maximum=bmi_max, value=bmi_default, label="BMI"),
gr.components.Checkbox(label="Asthma"),
gr.components.Slider(minimum=glucose_min, maximum=glucose_max, value=glucose_default, label="Glucose Level"),
gr.components.Slider(minimum=pulse_min, maximum=glucose_max, value=pulse_default, label="Pulse")
],
outputs=gr.components.Textbox(label="Prediction of stay length"),
title="Length of Stay - Predictor",
description="""Enter the values to get a prediction of your length of stay""",
)
# Launch the interface
iface.launch()