File size: 13,104 Bytes
304986b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e9c6f6
304986b
 
 
 
 
 
 
 
 
 
 
 
234500f
304986b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import streamlit as st
import pandas as pd
import joblib
import numpy as np
import plotly.graph_objects as go
import plotly.express as px

# Set the title of the app
st.title("πŸ€– Robotic Grasp Robustness Predictor")
st.markdown("""
Welcome! This application predicts the **stability/robustness** of a robotic grasp based on sensor data.  
You can either upload a CSV file with sensor features or input values manually. πŸš€
""")

# Display robotics images at the top (ensure the URLs are accessible)
st.image("industrial_robotic_arm_for_automated.png",
         caption="Robotic Arm 🦾", use_container_width=True)

# Load the saved model (make sure the model file 'model.pkl' is in the same directory)
@st.cache_resource
def load_model():
    model = joblib.load("model.pkl")
    return model

model = load_model()

# Sidebar for input selection
st.sidebar.header("Input Options")
input_method = st.sidebar.radio("Choose input method:", ("Custom / API Input","CSV Upload"))

# Define the list of features expected by the model.
FEATURES = [
    "H1_F1J2_pos", "H1_F1J2_vel", "H1_F1J2_eff",
    "H1_F1J3_pos", "H1_F1J3_vel", "H1_F1J3_eff",
    "H1_F1J1_pos", "H1_F1J1_vel", "H1_F1J1_eff",
    "H1_F3J1_pos", "H1_F3J1_vel", "H1_F3J1_eff",
    "H1_F3J2_pos", "H1_F3J2_vel", "H1_F3J2_eff",
    "H1_F3J3_pos", "H1_F3J3_vel", "H1_F3J3_eff",
    "H1_F2J1_pos", "H1_F2J1_vel", "H1_F2J1_eff",
    "H1_F2J3_pos", "H1_F2J3_vel", "H1_F2J3_eff",
    "H1_F2J2_pos", "H1_F2J2_vel", "H1_F2J2_eff"
]

# CSV Upload Option
if input_method == "CSV Upload":
    st.header("Upload CSV File πŸ“")
    uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
    if uploaded_file is not None:
        try:
            input_df = pd.read_csv(uploaded_file)
            st.success("CSV file successfully loaded! βœ…")
            # Clean column names
            input_df.columns = input_df.columns.str.strip()
            missing_features = [col for col in FEATURES if col not in input_df.columns]
            if missing_features:
                st.error(f"Missing required feature(s): {', '.join(missing_features)} 😟")
            else:
                # Make predictions
                predictions = model.predict(input_df[FEATURES])
                input_df["Predicted Robustness"] = predictions
                
                # Interactive histogram of predictions
                fig_hist = px.histogram(input_df, x="Predicted Robustness", nbins=30, 
                                        title="Distribution of Predicted Robustness")
                st.plotly_chart(fig_hist, use_container_width=True)
                
                st.subheader("Predictions")
                st.dataframe(input_df)
        except Exception as e:
            st.error(f"Error processing file: {e}")

# Manual Input Option
else:
    st.header("Manual Input ✍️")
    st.markdown("Enter the sensor values for prediction below:")

    # Add a radio button for selecting the input mode within manual input
    input_mode = st.radio("Select input mode:", ("Custom Input", "API Default Values"))
    
    # Default API values (you can adjust these as appropriate)
    default_values = {
        "H1_F1J2_pos": 0.5, "H1_F1J2_vel": 0.0, "H1_F1J2_eff": 0.1,
        "H1_F1J3_pos": 0.5, "H1_F1J3_vel": 0.0, "H1_F1J3_eff": 0.1,
        "H1_F1J1_pos": 0.5, "H1_F1J1_vel": 0.0, "H1_F1J1_eff": 0.1,
        "H1_F3J1_pos": 1.0, "H1_F3J1_vel": 0.0, "H1_F3J1_eff": 0.2,
        "H1_F3J2_pos": 1.0, "H1_F3J2_vel": 0.0, "H1_F3J2_eff": 0.2,
        "H1_F3J3_pos": 1.0, "H1_F3J3_vel": 0.0, "H1_F3J3_eff": 0.2,
        "H1_F2J1_pos": 0.7, "H1_F2J1_vel": 0.0, "H1_F2J1_eff": 0.15,
        "H1_F2J3_pos": 0.7, "H1_F2J3_vel": 0.0, "H1_F2J3_eff": 0.15,
        "H1_F2J2_pos": 0.7, "H1_F2J2_vel": 0.0, "H1_F2J2_eff": 0.15,
    }
    
    # Container dictionary for all input values.
    input_data = {}
    
    # Create expandable sections for sensor groups.
    with st.expander("F1 Joint Sensors"):
        st.write("Sensors for Finger 1:")
        input_data["H1_F1J2_pos"] = st.number_input("H1_F1J2_pos", 
                                                    value=default_values["H1_F1J2_pos"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F1J2_vel"] = st.number_input("H1_F1J2_vel", 
                                                    value=default_values["H1_F1J2_vel"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F1J2_eff"] = st.number_input("H1_F1J2_eff", 
                                                    value=default_values["H1_F1J2_eff"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        
        st.write("Sensors for Finger 3:")
        input_data["H1_F1J3_pos"] = st.number_input("H1_F1J3_pos", 
                                                    value=default_values["H1_F1J3_pos"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F1J3_vel"] = st.number_input("H1_F1J3_vel", 
                                                    value=default_values["H1_F1J3_vel"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F1J3_eff"] = st.number_input("H1_F1J3_eff", 
                                                    value=default_values["H1_F1J3_eff"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        
        st.write("Sensors for Finger 1 (alternate):")
        input_data["H1_F1J1_pos"] = st.number_input("H1_F1J1_pos", 
                                                    value=default_values["H1_F1J1_pos"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F1J1_vel"] = st.number_input("H1_F1J1_vel", 
                                                    value=default_values["H1_F1J1_vel"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F1J1_eff"] = st.number_input("H1_F1J1_eff", 
                                                    value=default_values["H1_F1J1_eff"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
    
    with st.expander("F3 Joint Sensors"):
        input_data["H1_F3J1_pos"] = st.number_input("H1_F3J1_pos", 
                                                    value=default_values["H1_F3J1_pos"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F3J1_vel"] = st.number_input("H1_F3J1_vel", 
                                                    value=default_values["H1_F3J1_vel"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F3J1_eff"] = st.number_input("H1_F3J1_eff", 
                                                    value=default_values["H1_F3J1_eff"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        
        input_data["H1_F3J2_pos"] = st.number_input("H1_F3J2_pos", 
                                                    value=default_values["H1_F3J2_pos"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F3J2_vel"] = st.number_input("H1_F3J2_vel", 
                                                    value=default_values["H1_F3J2_vel"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F3J2_eff"] = st.number_input("H1_F3J2_eff", 
                                                    value=default_values["H1_F3J2_eff"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        
        input_data["H1_F3J3_pos"] = st.number_input("H1_F3J3_pos", 
                                                    value=default_values["H1_F3J3_pos"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F3J3_vel"] = st.number_input("H1_F3J3_vel", 
                                                    value=default_values["H1_F3J3_vel"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F3J3_eff"] = st.number_input("H1_F3J3_eff", 
                                                    value=default_values["H1_F3J3_eff"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        
    with st.expander("F2 Joint Sensors"):
        input_data["H1_F2J1_pos"] = st.number_input("H1_F2J1_pos", 
                                                    value=default_values["H1_F2J1_pos"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F2J1_vel"] = st.number_input("H1_F2J1_vel", 
                                                    value=default_values["H1_F2J1_vel"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F2J1_eff"] = st.number_input("H1_F2J1_eff", 
                                                    value=default_values["H1_F2J1_eff"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        
        input_data["H1_F2J3_pos"] = st.number_input("H1_F2J3_pos", 
                                                    value=default_values["H1_F2J3_pos"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F2J3_vel"] = st.number_input("H1_F2J3_vel", 
                                                    value=default_values["H1_F2J3_vel"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F2J3_eff"] = st.number_input("H1_F2J3_eff", 
                                                    value=default_values["H1_F2J3_eff"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        
        input_data["H1_F2J2_pos"] = st.number_input("H1_F2J2_pos", 
                                                    value=default_values["H1_F2J2_pos"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F2J2_vel"] = st.number_input("H1_F2J2_vel", 
                                                    value=default_values["H1_F2J2_vel"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
        input_data["H1_F2J2_eff"] = st.number_input("H1_F2J2_eff", 
                                                    value=default_values["H1_F2J2_eff"] if input_mode=="API Default Values" else 0.0, 
                                                    format="%.5f")
    
    # Predict button for manual input
    if st.button("Predict 🏁"):
        input_df = pd.DataFrame([input_data])
        prediction = model.predict(input_df)
        st.success(f"The predicted grasp robustness is: {prediction[0]:.3f}")
        
        # Create a gauge chart using Plotly for visualization.
        fig_gauge = go.Figure(go.Indicator(
            mode="gauge+number",
            value=prediction[0],
            title={'text': "Grasp Robustness"},
            gauge={
                'axis': {'range': [0, 100]},
                'bar': {'color': "darkblue"},
                'steps': [
                    {'range': [0, 30], 'color': "red"},
                    {'range': [30, 70], 'color': "yellow"},
                    {'range': [70, 100], 'color': "green"}
                ],
            }
        ))
        st.plotly_chart(fig_gauge, use_container_width=True)
        
        # Display feature importances if available
        try:
            feature_importances = model.named_steps['model'].feature_importances_
            imp_df = pd.DataFrame({
                'Feature': FEATURES,
                'Importance': feature_importances
            }).sort_values(by='Importance', ascending=False)
            st.subheader("Feature Importances πŸ“Š")
            fig_imp = px.bar(imp_df, x='Feature', y='Importance', title="Feature Importances")
            st.plotly_chart(fig_imp, use_container_width=True)
        except Exception as ex:
            st.info("Feature importance is not available for this model.")