import gradio as gr import pandas as pd import joblib import matplotlib matplotlib.use('TkAgg') 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 pred_data = { "id": 0, "rcount": train_data['rcount'].mean(), "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": train_data['sodium'].mean(), "glucose": glucose, "bloodureanitro": train_data['bloodureanitro'].mean(), "creatinine": train_data['creatinine'].mean(), "bmi": bmi, "pulse": pulse, "respiration": train_data['respiration'].mean(), "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()