import os import gradio as gr import pandas as pd from NeuralNetwork.NeuralNetwork import My_NeuralNetwork class NeuralNetwork_UI(): def __init__(self): self.nn = My_NeuralNetwork() def get_interface(self) -> gr.Blocks: with gr.Blocks() as interface: self.__get_network_parameters_ui() self.__get_inputs_ui() return interface def __get_network_parameters_ui(self): def get_model_info(num_layer, epochs, correlation_treshold, *neurons): self.nn.num_layer = num_layer self.nn.epochs = epochs self.nn.correlation_treshold = correlation_treshold dimension = [] for i in range(self.nn.num_layer): dimension.append(neurons[i]) self.nn.dimension = tuple(dimension) return f"""Dimension: {self.nn.dimension}, Correlation treshold: {self.nn.correlation_treshold}, Epochs: {self.nn.epochs}""" def train_model(): self.nn.train_model() image_path = os.path.abspath(os.path.join('app', 'NeuralNetwork', 'graph.png')) return f"Here are the features that will be necessary for the inputs : {self.nn.numeric_columns}", f"" with gr.Column() as network_parameters_ui: gr.Markdown("# Network Parameters") slider = gr.Slider(1, self.nn.MAX_LAYERS, value=self.nn.num_layer, step=1.0, label="Number of Hidden Layers") gr.Markdown("Number of Neurons per Layer:") with gr.Row(): nums = [gr.Number(label=f"Layer {i+1}", value=32) for i in range(self.nn.MAX_LAYERS)] correlation_treshold = gr.Number(label="Correlation treshold", value=self.nn.correlation_treshold, step=0.01, minimum=0.01, maximum=1.00) epochs = gr.Number(label="Epochs", value=self.nn.epochs, step=1, minimum=1, maximum=1000) with gr.Group(): gr.Markdown("Model info : ") model_info = gr.Markdown(f"""Dimension: {self.nn.dimension}, Correlation treshold: {self.nn.correlation_treshold}, Epochs: {self.nn.epochs}""") train_btn = gr.Button("Train model") res = gr.Markdown("") img_res = gr.Markdown("") slider.release(get_model_info, inputs=[slider, epochs, correlation_treshold] + nums, outputs=model_info) correlation_treshold.change(get_model_info, inputs=[slider, epochs, correlation_treshold] + nums, outputs=model_info) epochs.change(get_model_info, inputs=[slider, epochs, correlation_treshold] + nums, outputs=model_info) for num in nums: num.change(get_model_info, inputs=[slider, epochs, correlation_treshold] + nums, outputs=model_info) train_btn.click(train_model, outputs=[res, img_res]) return network_parameters_ui def __get_inputs_ui(self): def predict(age, gender, weight, height, max_bpm, avg_bpm, resting_bpm, session_duration, calories_burned, workout_type, fat_percentage, workout_frequency, experience_level, bmi): data = { 'Age': [age], 'Gender_Male': [1.0 if gender=="Male" else 0.0], 'Gender_Female': [1.0 if gender=="Female" else 0.0], 'Weight (kg)': [weight], 'Height (m)': [height], 'Max_BPM': [max_bpm], 'Avg_BPM': [avg_bpm], 'Resting_BPM': [resting_bpm], 'Session_Duration (hours)': [session_duration], 'Calories_Burned': [calories_burned], 'Workout_Type_Strength': [1.0 if workout_type=="Strength" else 0.0], 'Workout_Type_Yoga': [1.0 if workout_type=="Yoga" else 0.0], 'Workout_Type_Cardio': [1.0 if workout_type=="Cardio" else 0.0], 'Workout_Type_HIIT': [1.0 if workout_type=="HIIT" else 0.0], 'Fat_Percentage': [fat_percentage], # 'Water_Intake (liters)': [None], # Column that the model will try to predict 'Workout_Frequency (days/week)': [workout_frequency], 'Experience_Level': [experience_level], 'BMI': [bmi] } new_input = pd.DataFrame(data) water_intake = self.nn.predict(new_input) return f"Predicted Water_Intake (liters) : {water_intake}" with gr.Column() as inputs_ui: gr.Markdown("# Inputs") with gr.Row(): age = gr.Number(label="Age", minimum=1, maximum=100, step=1, value=38) gender = gr.Dropdown(["Male", "Female"], label="Gender", value="Male") weight = gr.Number(label="Weight (kg)", minimum=0.0, maximum=1000.0, step=0.1, value=74.0) height = gr.Number(label="Height (m)", minimum=0.00, maximum=3.00, step=0.01, value=1.72) max_bpm = gr.Number(label="Max_BPM", minimum=0, maximum=1000, step=1, value=180) avg_bpm = gr.Number(label="Avg_BPM", minimum=0, maximum=1000, step=1, value=144) resting_bpm = gr.Number(label="Resting_BPM", minimum=0, maximum=1000, step=1, value=62) session_duration = gr.Number(label="Session_Duration (hours)", minimum=0.00, maximum=24.00, step=0.01, value=1.25) calories_burned = gr.Number(label="Calories_Burned", minimum=0.0, maximum=2000.0, step=0.1, value=905.5) workout_type = gr.Dropdown(["Yoga", "HIIT", "Cardio", "Strength"], label="Workout_Type", value="Strength") fat_percentage = gr.Number(label="Fat_Percentage", minimum=0.0, maximum=50.0, step=0.1, value=25.0) workout_frequency = gr.Number(label="Workout_Frequency (days/week)", minimum=1, maximum=5, step=1, value=3) experience_level = gr.Number(label="Experience_Level", minimum=1, maximum=3, step=1, value=2) bmi = gr.Number(label="BMI", minimum=0.0, maximum=100.0, step=0.1, value=25.0) predict_btn = gr.Button("Predict") gr.Markdown("# Outputs") res = gr.Markdown("") predict_btn.click(predict, inputs=[age, gender, weight, height, max_bpm, avg_bpm, resting_bpm, session_duration, calories_burned, workout_type, fat_percentage, workout_frequency, experience_level, bmi], outputs=res) return inputs_ui