import gradio as gr import numpy as np import pandas as pd import subprocess import sys subprocess.check_call([sys.executable,'-m','pip','install','tensorflow']) subprocess.check_call([sys.executable,'-m','pip','install','scikit-learn']) from sklearn.preprocessing import LabelEncoder, LabelBinarizer from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn import KerasClassifier from sklearn.model_selection import KFold from sklearn.ensemble import RandomForestClassifier # Load data df = pd.read_csv('ExperimentalMigraneData.csv') X = df[['Age','Duration','Frequency','Location','Character','Intensity','Nausea','Vomit','Phonophobia','Photophobia','Visual','Sensory','Dysphasia','Dysarthria','Vertigo','Tinnitus','Hypoacusis','Diplopia','Visual_defect','Ataxia','Conscience','Paresthesia','DPF', 'On Periods']].values #selección de variables de entrada Y = df['Types'] #select target # Define the base Keras model def baseline_model(): model = Sequential() model.add(Dense(14, input_dim = 24, activation = 'relu')) # Rectified Linear Unit Activation Function model.add(Dense(14, activation = 'relu')) model.add(Dense(3, activation = 'softmax')) # Softmax for multi-class classification model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) return model # Define the Keras Classifier to use the base model estimator = KerasClassifier(build_fn = baseline_model, epochs = 100, batch_size = 10, verbose = 0) # Train the model estimator.fit(X, Y) # Define the input component with 24 number inputs inputs = [] for i in range(24): inputs.append(gr.inputs.Number(label=df.columns[i])) # Define the output component to show the predicted output output = gr.outputs.Label(label="Output") # Define the migraine type mapping dictionary migraine_types = {0: 'Non-Menstrual Migraine', 1: 'Menstrual Migraine', 2: 'Others'} # Define the Gradio interface function def predict(*args): # Convert the inputs into a numpy array input_array = np.array(args).reshape(1, -1) # Use the pre-trained estimator to predict the output based on the input array y_pred = estimator.predict(input_array) # Map the integer prediction to corresponding migraine type predicted_type = migraine_types[int(y_pred[0])] # Return the predicted output as text return predicted_type # Run the Gradio interface interface = gr.Interface(fn=predict, inputs=inputs, outputs=output) interface.launch()