Upload 4 files
Browse files- KNN_UI.py +8 -0
- NeuralNetwork_UI.py +115 -0
- RandomForest_UI.py +8 -0
- SVM_UI.py +104 -0
KNN_UI.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
class KNN_UI():
|
4 |
+
def get_interface(self) -> gr.Blocks:
|
5 |
+
with gr.Blocks() as interface:
|
6 |
+
gr.Markdown("hello world!")
|
7 |
+
|
8 |
+
return interface
|
NeuralNetwork_UI.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
+
from NeuralNetwork.NeuralNetwork import My_NeuralNetwork
|
5 |
+
|
6 |
+
class NeuralNetwork_UI():
|
7 |
+
def __init__(self):
|
8 |
+
self.nn = My_NeuralNetwork()
|
9 |
+
|
10 |
+
|
11 |
+
def get_interface(self) -> gr.Blocks:
|
12 |
+
with gr.Blocks() as interface:
|
13 |
+
self.__get_network_parameters_ui()
|
14 |
+
self.__get_inputs_ui()
|
15 |
+
return interface
|
16 |
+
|
17 |
+
def __get_network_parameters_ui(self):
|
18 |
+
def get_model_info(num_layer, epochs, correlation_treshold, *neurons):
|
19 |
+
self.nn.num_layer = num_layer
|
20 |
+
self.nn.epochs = epochs
|
21 |
+
self.nn.correlation_treshold = correlation_treshold
|
22 |
+
dimension = []
|
23 |
+
for i in range(self.nn.num_layer):
|
24 |
+
dimension.append(neurons[i])
|
25 |
+
self.nn.dimension = tuple(dimension)
|
26 |
+
return f"""Dimension: {self.nn.dimension}, Correlation treshold: {self.nn.correlation_treshold}, Epochs: {self.nn.epochs}"""
|
27 |
+
|
28 |
+
def train_model():
|
29 |
+
self.nn.train_model()
|
30 |
+
image_path = os.path.abspath(os.path.join('app', 'NeuralNetwork', 'graph.png'))
|
31 |
+
return f"Here are the features that will be necessary for the inputs : {self.nn.numeric_columns}", f"<img src='{image_path}'>"
|
32 |
+
|
33 |
+
with gr.Column() as network_parameters_ui:
|
34 |
+
gr.Markdown("# Network Parameters")
|
35 |
+
slider = gr.Slider(1, self.nn.MAX_LAYERS, value=self.nn.num_layer, step=1.0, label="Number of Hidden Layers")
|
36 |
+
gr.Markdown("Number of Neurons per Layer:")
|
37 |
+
with gr.Row():
|
38 |
+
nums = [gr.Number(label=f"Layer {i+1}", value=32) for i in range(self.nn.MAX_LAYERS)]
|
39 |
+
correlation_treshold = gr.Number(label="Correlation treshold", value=self.nn.correlation_treshold, step=0.01, minimum=0.01, maximum=1.00)
|
40 |
+
epochs = gr.Number(label="Epochs", value=self.nn.epochs, step=1, minimum=1, maximum=1000)
|
41 |
+
|
42 |
+
with gr.Group():
|
43 |
+
gr.Markdown("Model info : ")
|
44 |
+
model_info = gr.Markdown(f"""Dimension: {self.nn.dimension}, Correlation treshold: {self.nn.correlation_treshold}, Epochs: {self.nn.epochs}""")
|
45 |
+
|
46 |
+
train_btn = gr.Button("Train model")
|
47 |
+
res = gr.Markdown("")
|
48 |
+
img_res = gr.Markdown("")
|
49 |
+
|
50 |
+
slider.release(get_model_info, inputs=[slider, epochs, correlation_treshold] + nums, outputs=model_info)
|
51 |
+
correlation_treshold.change(get_model_info, inputs=[slider, epochs, correlation_treshold] + nums, outputs=model_info)
|
52 |
+
epochs.change(get_model_info, inputs=[slider, epochs, correlation_treshold] + nums, outputs=model_info)
|
53 |
+
for num in nums:
|
54 |
+
num.change(get_model_info, inputs=[slider, epochs, correlation_treshold] + nums, outputs=model_info)
|
55 |
+
|
56 |
+
train_btn.click(train_model, outputs=[res, img_res])
|
57 |
+
|
58 |
+
return network_parameters_ui
|
59 |
+
|
60 |
+
|
61 |
+
def __get_inputs_ui(self):
|
62 |
+
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):
|
63 |
+
data = {
|
64 |
+
'Age': [age],
|
65 |
+
'Gender_Male': [1.0 if gender=="Male" else 0.0],
|
66 |
+
'Gender_Female': [1.0 if gender=="Female" else 0.0],
|
67 |
+
'Weight (kg)': [weight],
|
68 |
+
'Height (m)': [height],
|
69 |
+
'Max_BPM': [max_bpm],
|
70 |
+
'Avg_BPM': [avg_bpm],
|
71 |
+
'Resting_BPM': [resting_bpm],
|
72 |
+
'Session_Duration (hours)': [session_duration],
|
73 |
+
'Calories_Burned': [calories_burned],
|
74 |
+
'Workout_Type_Strength': [1.0 if workout_type=="Strength" else 0.0],
|
75 |
+
'Workout_Type_Yoga': [1.0 if workout_type=="Yoga" else 0.0],
|
76 |
+
'Workout_Type_Cardio': [1.0 if workout_type=="Cardio" else 0.0],
|
77 |
+
'Workout_Type_HIIT': [1.0 if workout_type=="HIIT" else 0.0],
|
78 |
+
'Fat_Percentage': [fat_percentage],
|
79 |
+
# 'Water_Intake (liters)': [None], # Column that the model will try to predict
|
80 |
+
'Workout_Frequency (days/week)': [workout_frequency],
|
81 |
+
'Experience_Level': [experience_level],
|
82 |
+
'BMI': [bmi]
|
83 |
+
}
|
84 |
+
|
85 |
+
new_input = pd.DataFrame(data)
|
86 |
+
water_intake = self.nn.predict(new_input)
|
87 |
+
return f"Predicted Water_Intake (liters) : {water_intake}"
|
88 |
+
|
89 |
+
|
90 |
+
with gr.Column() as inputs_ui:
|
91 |
+
gr.Markdown("# Inputs")
|
92 |
+
with gr.Row():
|
93 |
+
age = gr.Number(label="Age", minimum=1, maximum=100, step=1, value=38)
|
94 |
+
gender = gr.Dropdown(["Male", "Female"], label="Gender", value="Male")
|
95 |
+
weight = gr.Number(label="Weight (kg)", minimum=0.0, maximum=1000.0, step=0.1, value=74.0)
|
96 |
+
height = gr.Number(label="Height (m)", minimum=0.00, maximum=3.00, step=0.01, value=1.72)
|
97 |
+
max_bpm = gr.Number(label="Max_BPM", minimum=0, maximum=1000, step=1, value=180)
|
98 |
+
avg_bpm = gr.Number(label="Avg_BPM", minimum=0, maximum=1000, step=1, value=144)
|
99 |
+
resting_bpm = gr.Number(label="Resting_BPM", minimum=0, maximum=1000, step=1, value=62)
|
100 |
+
session_duration = gr.Number(label="Session_Duration (hours)", minimum=0.00, maximum=24.00, step=0.01, value=1.25)
|
101 |
+
calories_burned = gr.Number(label="Calories_Burned", minimum=0.0, maximum=2000.0, step=0.1, value=905.5)
|
102 |
+
workout_type = gr.Dropdown(["Yoga", "HIIT", "Cardio", "Strength"], label="Workout_Type", value="Strength")
|
103 |
+
fat_percentage = gr.Number(label="Fat_Percentage", minimum=0.0, maximum=50.0, step=0.1, value=25.0)
|
104 |
+
workout_frequency = gr.Number(label="Workout_Frequency (days/week)", minimum=1, maximum=5, step=1, value=3)
|
105 |
+
experience_level = gr.Number(label="Experience_Level", minimum=1, maximum=3, step=1, value=2)
|
106 |
+
bmi = gr.Number(label="BMI", minimum=0.0, maximum=100.0, step=0.1, value=25.0)
|
107 |
+
|
108 |
+
predict_btn = gr.Button("Predict")
|
109 |
+
gr.Markdown("# Outputs")
|
110 |
+
res = gr.Markdown("")
|
111 |
+
|
112 |
+
|
113 |
+
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)
|
114 |
+
return inputs_ui
|
115 |
+
|
RandomForest_UI.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
class RandomForest_UI():
|
4 |
+
def get_interface(self) -> gr.Blocks:
|
5 |
+
with gr.Blocks() as interface:
|
6 |
+
gr.Markdown("hello world!")
|
7 |
+
|
8 |
+
return interface
|
SVM_UI.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from SVM.SVM_C import SVM_Classifier
|
3 |
+
from SVM.SVM_R import SVM_Regressor
|
4 |
+
|
5 |
+
class SVM_UI():
|
6 |
+
def __init__(self):
|
7 |
+
#self.svm = SVM_Classifier()
|
8 |
+
self.svm = SVM_Regressor()
|
9 |
+
|
10 |
+
def predict(self, weight, height, gender, fat, freq, experience, duration, workout):
|
11 |
+
# Update the SVM model with current values
|
12 |
+
self.svm.weight = weight
|
13 |
+
self.svm.height = height
|
14 |
+
self.svm.gender = gender
|
15 |
+
self.svm.fat = fat
|
16 |
+
self.svm.freq = freq
|
17 |
+
self.svm.experience = experience
|
18 |
+
self.svm.duration = duration
|
19 |
+
self.svm.workout = workout
|
20 |
+
|
21 |
+
# Get prediction and debug info
|
22 |
+
prediction = self.svm.make_prediction()
|
23 |
+
debug_info = self.svm.get_debug_info()
|
24 |
+
|
25 |
+
return prediction, debug_info
|
26 |
+
|
27 |
+
def get_interface(self) -> gr.Blocks:
|
28 |
+
|
29 |
+
result_text = gr.Textbox(label="Modify the settings to check how much water should you take:",interactive=False)
|
30 |
+
|
31 |
+
debug_text = gr.Textbox(label="Debug Information", interactive=False, lines=20, info="This is for the one who made this site... do not worry about this ;)") # Make it bigger for debug info
|
32 |
+
|
33 |
+
with gr.Blocks() as interface:
|
34 |
+
gr.Markdown("hello world!")
|
35 |
+
gr.Markdown("In this model we are using a Support Vector Machine tuned for a regression target.")
|
36 |
+
gr.Markdown("Based on the information you provide, the model will recommend the amount of water (in litres) you need to consume for your intended workout session.")
|
37 |
+
|
38 |
+
inputs = []
|
39 |
+
|
40 |
+
with gr.Column():
|
41 |
+
gr.HTML("<hr style='border: 1px solid #ccc; width: 100%;'>")
|
42 |
+
|
43 |
+
with gr.Column():
|
44 |
+
gr.Markdown("# Water consumption for workout session")
|
45 |
+
gr.Markdown("How much water should you take with you for your next workout session:")
|
46 |
+
result_text.render()
|
47 |
+
|
48 |
+
with gr.Row():
|
49 |
+
with gr.Column():# as network_parameters_ui:
|
50 |
+
gr.Markdown("# About you")
|
51 |
+
|
52 |
+
slider1 = gr.Slider(40, 140, value=self.svm.weight, step=1.0, label="Weight (kg)", interactive=True)
|
53 |
+
inputs.append(slider1)
|
54 |
+
|
55 |
+
slider2 = gr.Slider(1.5, 2.2, value=self.svm.height, step=0.01, label="Height (m)", interactive=True)
|
56 |
+
inputs.append(slider2)
|
57 |
+
|
58 |
+
radio1 = gr.Radio(["Male", "Female"], value=self.svm.gender, label="Gender", interactive=True)
|
59 |
+
inputs.append(radio1)
|
60 |
+
|
61 |
+
with gr.Column():# as network_parameters_ui:
|
62 |
+
gr.Markdown("# Your fitness level")
|
63 |
+
|
64 |
+
slider4 = gr.Slider(20, 70, value=self.svm.fat, step=1.0, label="Fat percentage (%)", info="What is the percentage fat in your body?",interactive=True)
|
65 |
+
inputs.append(slider4)
|
66 |
+
|
67 |
+
slider5 = gr.Slider(1, 5, value=self.svm.freq, step=1.0, label="Workouts a week", info="How many times a week do you go to the gym?",interactive=True)
|
68 |
+
inputs.append(slider5)
|
69 |
+
|
70 |
+
slider6 = gr.Slider(1, 3, value=self.svm.experience, step=1.0, label="Experience level", info="1 = Beginner; 3 = Very experienced?",interactive=True)
|
71 |
+
inputs.append(slider6)
|
72 |
+
|
73 |
+
with gr.Column():
|
74 |
+
gr.Markdown("# Your session today")
|
75 |
+
|
76 |
+
slider3 = gr.Slider(0.1, 2.0, value=self.svm.duration, step=0.1, label="Session duration (hours)", info="How long are you planning to exercise today?",interactive=True)
|
77 |
+
inputs.append(slider3)
|
78 |
+
|
79 |
+
radio2 = gr.Radio(["Cardio", "HIIT", "Strength","Yoga"], value=self.svm.workout, label="Workout type", info="What are you planning to do today?", interactive=True)
|
80 |
+
inputs.append(radio2)
|
81 |
+
|
82 |
+
with gr.Column():
|
83 |
+
gr.HTML("<hr style='border: 1px solid #ccc; width: 100%;'>")
|
84 |
+
|
85 |
+
with gr.Column():
|
86 |
+
debug_text.render()
|
87 |
+
|
88 |
+
#predict_btn = gr.Button("Get Water recommendation", variant="primary")
|
89 |
+
|
90 |
+
#result_text = gr.Textbox(label="Recommendation",interactive=False)
|
91 |
+
|
92 |
+
#debug_text = gr.Textbox(label="Debug Information", interactive=False, lines=20) # Make it bigger for debug info
|
93 |
+
|
94 |
+
#predict_btn.click(fn=self.predict, inputs=inputs, outputs=[result_text, debug_text])
|
95 |
+
|
96 |
+
# Add debounced change event handlers to all inputs
|
97 |
+
for input_component in inputs:
|
98 |
+
input_component.change(
|
99 |
+
fn=self.predict,
|
100 |
+
inputs=inputs,
|
101 |
+
outputs=[result_text, debug_text]
|
102 |
+
)
|
103 |
+
|
104 |
+
return interface
|