import numpy as np import pickle import gradio as gr # Load the saved pickle model with open('model-r.pkl', 'rb') as f: model = pickle.load(f) # Define action mapping action_map = { 0: "CLASS0ACTION", 1: "Hand at rest", 2: "Hand clenched in a fist", 3: "Wrist flexion", 4: "Wrist extension", 5: "Radial deviations", 6: "Ulnar deviations", } # Function to process inputs and get a prediction def action(e1, e2, e3, e4, e5, e6, e7, e8): # Duplicate each value 3 times to create a 24-length input input_data = np.array([e1, e2, e3, e4, e5, e6, e7, e8]) input_data_reshaped = input_data.reshape(1, -1) predicted_label = model.predict(input_data_reshaped)[0] return action_map.get(predicted_label, "Unknown action") # Define Gradio UI with improved styling with gr.Blocks(theme=gr.themes.Soft()) as iface: gr.Markdown(""" # 🤖 ML Model Predictor ### Enter the 8 feature values below to get a prediction """) with gr.Row(): inputs = [gr.Number(label=f"Feature {i+1}", interactive=True) for i in range(8)] output = gr.Textbox(label="Prediction", interactive=False) submit_btn = gr.Button("🔍 Predict") submit_btn.click(action, inputs=inputs, outputs=output) gr.Examples( examples=[ [-2.00e-05, 1.00e-05, 2.20e-04, 1.80e-04, -1.50e-04, -5.00e-05, 1.00e-05, 0], [1.60e-04, -1.00e-04, -2.40e-04, 2.00e-04, 1.00e-04, -9.00e-05, -5.00e-05, -5.00e-05], [-1.00e-05, 1.00e-05, 1.00e-05, 0, -2.00e-05, 0, -3.00e-05, -3.00e-05], ], inputs=inputs, label="Try with Example Inputs" ) gr.Markdown(""" ### 🔍 How it Works: - Enter values for the 8 features. - Click the **Predict** button. - The model will analyze the input and classify the hand motion. """) # Launch Gradio UI iface.launch(share=True, debug=True)