cipherunhsiv commited on
Commit
a86506c
Β·
verified Β·
1 Parent(s): 85ccb3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -44
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import numpy as np
2
- from tensorflow.keras.models import load_model
3
  import gradio as gr
4
 
5
- # Load the saved .h5 model
6
- model = load_model('model8346.h5')
 
7
 
8
  # Define action mapping
9
  action_map = {
@@ -18,50 +19,41 @@ action_map = {
18
  # Function to process inputs and get a prediction
19
  def action(e1, e2, e3, e4, e5, e6, e7, e8):
20
  # Duplicate each value 3 times to create a 24-length input
21
- input_data = np.array([[e1, e1, e1, e2, e2, e2, e3, e3, e3,
22
- e4, e4, e4, e5, e5, e5, e6, e6, e6,
23
- e7, e7, e7, e8, e8, e8]])
 
 
 
 
 
 
 
24
 
25
- # Get model prediction
26
- prediction = model.predict(input_data)
27
 
28
- # Get predicted class
29
- predicted_class = np.argmax(prediction, axis=-1)
30
 
31
- return action_map.get(predicted_class[0] + 1, "Unknown action")
32
-
33
- # Define Gradio inputs
34
- inputs = [
35
- gr.Number(label="e1"),
36
- gr.Number(label="e2"),
37
- gr.Number(label="e3"),
38
- gr.Number(label="e4"),
39
- gr.Number(label="e5"),
40
- gr.Number(label="e6"),
41
- gr.Number(label="e7"),
42
- gr.Number(label="e8"),
43
- ]
44
-
45
- # Define Gradio output
46
- output = gr.Textbox(label="Prediction")
47
-
48
- # Example inputs (same as before)
49
- examples = [
50
- [-2.00e-05, 1.00e-05, 2.20e-04, 1.80e-04, -1.50e-04, -5.00e-05, 1.00e-05, 0],
51
- [1.60e-04, -1.00e-04, -2.40e-04, 2.00e-04, 1.00e-04, -9.00e-05, -5.00e-05, -5.00e-05],
52
- [-1.00e-05, 1.00e-05, 1.00e-05, 0, -2.00e-05, 0, -3.00e-05, -3.00e-05],
53
- ]
54
-
55
- # Gradio interface
56
- iface = gr.Interface(
57
- fn=action,
58
- inputs=inputs,
59
- outputs=output,
60
- title="ML Model Predictor",
61
- examples=examples,
62
- flagging_options=["Working", "Not Working"],
63
- description="Enter the 8 feature values to get a prediction."
64
- )
65
 
66
  # Launch Gradio UI
67
  iface.launch(share=True, debug=True)
 
1
  import numpy as np
2
+ import pickle
3
  import gradio as gr
4
 
5
+ # Load the saved pickle model
6
+ with open('model8346.pkl', 'rb') as f:
7
+ model = pickle.load(f)
8
 
9
  # Define action mapping
10
  action_map = {
 
19
  # Function to process inputs and get a prediction
20
  def action(e1, e2, e3, e4, e5, e6, e7, e8):
21
  # Duplicate each value 3 times to create a 24-length input
22
+ input_data_reshaped = np.array(input_data).reshape(1, -1)
23
+ predicted_label = loaded_model.predict(input_data_reshaped)[0]
24
+ return action_map.get(predicted_class[0], "Unknown action")
25
+
26
+ # Define Gradio UI with improved styling
27
+ with gr.Blocks(theme=gr.themes.Soft()) as iface:
28
+ gr.Markdown("""
29
+ # πŸ€– ML Model Predictor
30
+ ### Enter the 8 feature values below to get a prediction
31
+ """)
32
 
33
+ with gr.Row():
34
+ inputs = [gr.Number(label=f"Feature {i+1}", interactive=True) for i in range(8)]
35
 
36
+ output = gr.Textbox(label="Prediction", interactive=False)
 
37
 
38
+ submit_btn = gr.Button("πŸ” Predict")
39
+ submit_btn.click(action, inputs=inputs, outputs=output)
40
+
41
+ gr.Examples(
42
+ examples=[
43
+ [-2.00e-05, 1.00e-05, 2.20e-04, 1.80e-04, -1.50e-04, -5.00e-05, 1.00e-05, 0],
44
+ [1.60e-04, -1.00e-04, -2.40e-04, 2.00e-04, 1.00e-04, -9.00e-05, -5.00e-05, -5.00e-05],
45
+ [-1.00e-05, 1.00e-05, 1.00e-05, 0, -2.00e-05, 0, -3.00e-05, -3.00e-05],
46
+ ],
47
+ inputs=inputs,
48
+ label="Try with Example Inputs"
49
+ )
50
+
51
+ gr.Markdown("""
52
+ ### πŸ” How it Works:
53
+ - Enter values for the 8 features.
54
+ - Click the **Predict** button.
55
+ - The model will analyze the input and classify the hand motion.
56
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  # Launch Gradio UI
59
  iface.launch(share=True, debug=True)