cipherunhsiv's picture
Update app.py
36039f6 verified
raw
history blame
1.87 kB
import numpy as np
from tensorflow.keras.models import load_model
import gradio as gr
# Load the saved .h5 model
model = load_model('model8346.h5')
# Define action mapping
action_map = {
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, e1, e1, e2, e2, e2, e3, e3, e3,
e4, e4, e4, e5, e5, e5, e6, e6, e6,
e7, e7, e7, e8, e8, e8]])
# Get model prediction
prediction = model.predict(input_data)
# Get predicted class
predicted_class = np.argmax(prediction, axis=-1)
return action_map.get(predicted_class[0] + 1, "Unknown action")
# Define Gradio inputs
inputs = [
gr.Number(label="e1"),
gr.Number(label="e2"),
gr.Number(label="e3"),
gr.Number(label="e4"),
gr.Number(label="e5"),
gr.Number(label="e6"),
gr.Number(label="e7"),
gr.Number(label="e8"),
]
# Define Gradio output
output = gr.Textbox(label="Prediction")
# Example inputs (same as before)
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],
]
# Gradio interface
iface = gr.Interface(
fn=action,
inputs=inputs,
outputs=output,
title="ML Model Predictor",
examples=examples,
flagging_options=["Working", "Not Working"],
description="Enter the 8 feature values to get a prediction."
)
# Launch Gradio UI
iface.launch(share=True, debug=True)