import os import gradio as gr import pandas as pd from KNN.KNNModel import KNNModel class KNN_UI: def __init__(self): self.knn_model = KNNModel() try: # Load and preprocess data self.knn_model.load_and_preprocess_data() # Find optimal k and train the model X_train, X_test, y_train, y_test = self.knn_model.load_and_preprocess_data() self.knn_model.find_optimal_k(X_train, y_train) self.knn_model.train_model(X_train, y_train) except Exception as e: print(f"Error during initialization: {e}") def get_interface(self) -> gr.Blocks: with gr.Blocks() as interface: #self.__get_inputs_ui() gr.Markdown("## Fat Percentage Prediction through kNN Regressor" ) gr.Markdown("") gr.Markdown( "Welcome to the **Fat Percentage Prediction** section. Here, you can determine an individual's Fat Percentage based on a set of input parameters." ) gr.Markdown( "You are to input details such as workout frequency, session duration, water intake, calories burned, and experience level to make a prediction. " "The Experience Level is grouped into three Levels: 1- Beginner, Professional, and 3- Expert. " ) self.__get_inputs_ui() return interface def __get_inputs_ui(self): def predict(workout_frequency,session_duration, water_intake, calories_burned, experience_level): try: # Encode categorical features if 'Experience_Level' not in self.knn_model.label_encoders: return f"Error: 'Experience_Level' encoder not found. Ensure the column exists in the dataset." experience_level_encoded = self.knn_model.label_encoders['Experience_Level'].transform([experience_level])[0] # Create input DataFrame input_data = pd.DataFrame({ 'Workout_Frequency (days/week)': [workout_frequency], 'Session_Duration (hours)': [session_duration], 'Water_Intake (liters)': [water_intake], 'Calories_Burned': [calories_burned], 'Experience_Level': [experience_level_encoded] }) # Predict fat percentage predicted_fat_percentage = self.knn_model.predict(input_data) return f"Predicted Fat Percentage: {predicted_fat_percentage:.2f}%" except Exception as e: return f"Error: {str(e)}" with gr.Column() as inputs_ui: gr.Markdown("# Input your record") workout_frequency = gr.Number(label="Workout Frequency (days/week)", minimum=1, maximum=7, step=1, value=3) session_duration = gr.Number(label="Session Duration (hours)", minimum=0.0, maximum=24.0, step=0.1, value=1.25) water_intake = gr.Number(label="Water Intake (liters/day)", minimum=0.0, maximum=10.0, step=0.1, value=2.5) calories_burned = gr.Number(label="Calories Burned", minimum=0.0, maximum=2000.0, step=0.1, value=905.5) experience_level = gr.Dropdown( label="Experience Level", choices=self.knn_model.label_encoders['Experience_Level'].classes_.tolist() ) predict_btn = gr.Button("Calculate") gr.Markdown("# Calculated Fat Percentage") res = gr.Markdown("") predict_btn.click(predict, inputs=[calories_burned, session_duration, workout_frequency, water_intake, experience_level], outputs=res) return inputs_ui # Initialize the UI and launch the Gradio interface if __name__ == "__main__": knn_ui = KNN_UI() interface = knn_ui.get_interface() interface.launch()