Upload KNN_UI.py
Browse files
KNN_UI.py
CHANGED
@@ -1,8 +1,87 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
+
from KNN.KNNModel import KNNModel
|
5 |
+
|
6 |
+
class KNN_UI:
|
7 |
+
def __init__(self):
|
8 |
+
self.knn_model = KNNModel()
|
9 |
+
try:
|
10 |
+
# Load and preprocess data
|
11 |
+
self.knn_model.load_and_preprocess_data()
|
12 |
+
# Find optimal k and train the model
|
13 |
+
X_train, X_test, y_train, y_test = self.knn_model.load_and_preprocess_data()
|
14 |
+
self.knn_model.find_optimal_k(X_train, y_train)
|
15 |
+
self.knn_model.train_model(X_train, y_train)
|
16 |
+
except Exception as e:
|
17 |
+
print(f"Error during initialization: {e}")
|
18 |
+
|
19 |
+
def get_interface(self) -> gr.Blocks:
|
20 |
+
with gr.Blocks() as interface:
|
21 |
+
#self.__get_inputs_ui()
|
22 |
+
gr.Markdown("## Fat Percentage Prediction through kNN Regressor" )
|
23 |
+
gr.Markdown("")
|
24 |
+
gr.Markdown(
|
25 |
+
"Welcome to the **Fat Percentage Prediction** section. Here, you can determine an individual's Fat Percentage based on a set of input parameters."
|
26 |
+
)
|
27 |
+
|
28 |
+
|
29 |
+
gr.Markdown(
|
30 |
+
"You are to input details such as workout frequency, session duration, water intake, calories burned, and experience level to make a prediction. "
|
31 |
+
"The Experience Level is grouped into three Levels: 1- Beginner, Professional, and 3- Expert. "
|
32 |
+
|
33 |
+
|
34 |
+
)
|
35 |
+
self.__get_inputs_ui()
|
36 |
+
|
37 |
+
return interface
|
38 |
+
|
39 |
+
def __get_inputs_ui(self):
|
40 |
+
def predict(workout_frequency,session_duration, water_intake, calories_burned, experience_level):
|
41 |
+
try:
|
42 |
+
# Encode categorical features
|
43 |
+
if 'Experience_Level' not in self.knn_model.label_encoders:
|
44 |
+
return f"Error: 'Experience_Level' encoder not found. Ensure the column exists in the dataset."
|
45 |
+
|
46 |
+
experience_level_encoded = self.knn_model.label_encoders['Experience_Level'].transform([experience_level])[0]
|
47 |
+
|
48 |
+
# Create input DataFrame
|
49 |
+
input_data = pd.DataFrame({
|
50 |
+
'Workout_Frequency (days/week)': [workout_frequency],
|
51 |
+
'Session_Duration (hours)': [session_duration],
|
52 |
+
'Water_Intake (liters)': [water_intake],
|
53 |
+
'Calories_Burned': [calories_burned],
|
54 |
+
'Experience_Level': [experience_level_encoded]
|
55 |
+
})
|
56 |
+
|
57 |
+
# Predict fat percentage
|
58 |
+
predicted_fat_percentage = self.knn_model.predict(input_data)
|
59 |
+
return f"Predicted Fat Percentage: {predicted_fat_percentage:.2f}%"
|
60 |
+
except Exception as e:
|
61 |
+
return f"Error: {str(e)}"
|
62 |
+
|
63 |
+
with gr.Column() as inputs_ui:
|
64 |
+
gr.Markdown("# Input your record")
|
65 |
+
workout_frequency = gr.Number(label="Workout Frequency (days/week)", minimum=1, maximum=7, step=1, value=3)
|
66 |
+
session_duration = gr.Number(label="Session Duration (hours)", minimum=0.0, maximum=24.0, step=0.1, value=1.25)
|
67 |
+
water_intake = gr.Number(label="Water Intake (liters/day)", minimum=0.0, maximum=10.0, step=0.1, value=2.5)
|
68 |
+
calories_burned = gr.Number(label="Calories Burned", minimum=0.0, maximum=2000.0, step=0.1, value=905.5)
|
69 |
+
experience_level = gr.Dropdown(
|
70 |
+
label="Experience Level",
|
71 |
+
choices=self.knn_model.label_encoders['Experience_Level'].classes_.tolist()
|
72 |
+
|
73 |
+
)
|
74 |
+
|
75 |
+
predict_btn = gr.Button("Calculate")
|
76 |
+
gr.Markdown("# Calculated Fat Percentage")
|
77 |
+
res = gr.Markdown("")
|
78 |
+
|
79 |
+
predict_btn.click(predict, inputs=[calories_burned, session_duration, workout_frequency, water_intake, experience_level], outputs=res)
|
80 |
+
|
81 |
+
return inputs_ui
|
82 |
+
|
83 |
+
# Initialize the UI and launch the Gradio interface
|
84 |
+
if __name__ == "__main__":
|
85 |
+
knn_ui = KNN_UI()
|
86 |
+
interface = knn_ui.get_interface()
|
87 |
+
interface.launch()
|