File size: 13,471 Bytes
fa9714e
 
 
 
 
 
 
 
 
 
 
 
 
 
ca21c99
 
 
 
 
fa9714e
 
 
 
 
 
 
 
 
 
 
 
 
ca21c99
fa9714e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca21c99
fa9714e
 
 
 
 
ca21c99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa9714e
 
 
 
ca21c99
 
 
 
 
 
 
 
 
fa9714e
 
ca21c99
 
 
 
 
 
 
 
 
fa9714e
 
ca21c99
 
 
 
 
 
 
 
 
fa9714e
 
ca21c99
 
 
 
 
 
 
 
 
fa9714e
ca21c99
 
 
 
 
 
 
 
 
fa9714e
ca21c99
 
 
 
 
 
 
 
 
fa9714e
 
ca21c99
 
 
 
 
 
 
 
 
fa9714e
ca21c99
 
 
 
 
 
 
 
 
fa9714e
ca21c99
 
 
 
 
 
 
 
 
fa9714e
ca21c99
fa9714e
 
 
 
 
ca21c99
 
fa9714e
 
 
 
ca21c99
fa9714e
 
 
 
 
 
 
 
 
ca21c99
fa9714e
 
 
 
 
 
 
 
 
 
 
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
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("""
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 that you have internet access for these images, or replace with local image files)
st.image("https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Robotic_arm_by_Robust_automation_in_Australia.jpg/640px-Robotic_arm_by_Robust_automation_in_Australia.jpg", caption="Robotic Arm", use_column_width=True)
st.image("https://upload.wikimedia.org/wikipedia/commons/9/95/Robotic_arm.jpg", caption="Advanced Robotics", use_column_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:", ("CSV Upload", "Manual Input"))

# Define the list of features expected by the model.
# Update these names to match your dataset after cleaning.
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"
]

# Option 1: CSV Upload
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"The following required feature(s) are missing from the file: {', '.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}")
            
# Option 2: Manual Input
else:
    st.header("Manual Input")
    st.markdown("Enter the sensor values for prediction:")
    
    # Radio button for input type selection inside manual input
    input_type = st.radio("Select input mode:", ("Custom Input", "API Default Values"))
    
    # Define default API values (adjust these as needed)
    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,
    }
    
    input_data = {}
    
    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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="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_type=="API Default Values" else 0.0, 
                                                    format="%.5f")
    
    # When the "Predict" button is pressed, perform prediction
    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 to visualize the prediction.
        # The axis range [0, 100] is an example; adjust based on your data.
        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)
        
        # If the model supports feature importances, display them.
        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.")