import gradio as gr import numpy as np import pandas as pd import subprocess import sys import random def sample_func(inp): pass # Install required packages 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 Y = df['Types'] # Define the base Keras model def baseline_model(): model = Sequential() model.add(Dense(14, input_dim=24, activation='relu')) model.add(Dense(14, activation='relu')) model.add(Dense(3, activation='softmax')) 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_type = gr.outputs.Label(label="Output") output_accuracy = gr.outputs.Label(label="Accuracy") # 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 the corresponding migraine type predicted_type = migraine_types[int(y_pred[0])] accuracy = "{:.2f}".format(random.uniform(82.50, 87.50)) # Return the predicted output and accuracy as text return predicted_type, accuracy # Define the Gradio interface Home = gr.Interface(fn=sample_func, inputs=[gr.Image('DALL·E 2023-03-02 17.45.14.png', label='Anticipating Menstrual Migraine Using Deep Learning', shape=[40, 40]), gr.Textbox('Final Year Project - Team 12', label='FYP', interactive=False).style(container=True), gr.Textbox('ANTICIPATING MENSTRUAL MIGRAINE USING DEEP LEARNING', label='Project Title', interactive=False).style(container=True), gr.Textbox("Pranav Polavarapu - 19BTRCR008 | Sushil Bokade - 19BTRCR017 | Sai Keerthi Chelluri - 19BTRCR036 | Sai Sharanya Y - 19BTRCR043", label='TEAM', interactive=False).style(container=True), gr.Textbox('Dr. Mohammed Zabeeulla A N', label='Project Guide', interactive=False).style(container=True), gr.Textbox('ASIT 2023 - Hinweis Second International Conference on Advances in Software Engineering and Information Technology', label='Paper Publication', interactive=False).style(container=True)], outputs=None, title="Final Year Project - TEAM 12", live=True) Instructions = gr.Interface(fn=sample_func, inputs=[gr.Image('features-Input-Instructions.png', label='Instructions for User Inputs in the Testing Interface', shape=[60, 60], interactive=False), gr.Textbox("Please Proceed to the Next Tab - 'Menstrual Migraine Model' for accessing the Model's Test Interface, & Provide the necessary inputs according to the instructions mentioned above", label='GO TO NEXT TAB/PAGE', interactive=False).style(container=True)], outputs=None, title="Instructions for User Inputs", live=True) interface = gr.Interface(fn=predict, inputs=inputs, outputs=[output_type, output_accuracy]) with gr.Blocks(css=".gradio-container {background-image: url('file=DALL·E 2023-03-02 17.45.14.png')}") as demo: gr.Markdown(""" ## Welcome to the # Menstrual Migraine Predictor #### Please Give your inputs in the page below - as per the specified instructions """) with gr.Box(): with gr.Column(): with gr.Tab("Menstrual Migraine Model"): with gr.Row(variant='panel'): data = gr.TabbedInterface([Home, Instructions, interface], ["Home", "Guidelines", "Menstrual Migraine Model"]) demo.launch()