Sompote commited on
Commit
5db4d23
Β·
verified Β·
1 Parent(s): ce5ae7f

Upload 5 files

Browse files
Files changed (5) hide show
  1. Data_syw.xlsx +0 -0
  2. README.md +32 -6
  3. app.py +227 -0
  4. friction_model.pt +3 -0
  5. requirements.txt +9 -0
Data_syw.xlsx ADDED
Binary file (37.7 kB). View file
 
README.md CHANGED
@@ -1,12 +1,38 @@
1
  ---
2
- title: Friction Angle Solid Waste
3
- emoji: πŸš€
4
- colorFrom: pink
5
- colorTo: blue
6
  sdk: streamlit
7
- sdk_version: 1.42.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Fricitonangle prediction of solid waste
3
+ emoji: πŸš—
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: streamlit
7
+ sdk_version: "1.29.0"
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ # Friction Angle Predictor
13
+
14
+ This Streamlit app predicts the friction angle of waste materials based on their composition and characteristics. The app uses a deep learning model trained on waste composition data to make predictions and provides SHAP value explanations for model interpretability.
15
+
16
+ ## Features
17
+
18
+ - Interactive input for waste composition parameters
19
+ - Real-time prediction of friction angle
20
+ - SHAP waterfall plot for model interpretation
21
+ - User-friendly interface
22
+
23
+ ## Usage
24
+
25
+ 1. Enter the waste composition values in the input fields
26
+ 2. Click "Predict Friction Angle" to get the prediction
27
+ 3. View the results and SHAP waterfall plot for explanation
28
+
29
+ ## Model
30
+
31
+ The model is a neural network trained on waste composition data. It uses the following features:
32
+ - Waste composition percentages
33
+ - Physical properties
34
+ - Material characteristics
35
+
36
+ ## Requirements
37
+
38
+ See `requirements.txt` for all dependencies.
app.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ # Disable OpenMP
3
+ os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
4
+ os.environ['OMP_NUM_THREADS'] = '1'
5
+ os.environ['OPENBLAS_NUM_THREADS'] = '1'
6
+ os.environ['MKL_NUM_THREADS'] = '1'
7
+ os.environ['VECLIB_MAXIMUM_THREADS'] = '1'
8
+ os.environ['NUMEXPR_NUM_THREADS'] = '1'
9
+
10
+ import streamlit as st
11
+ import torch
12
+ import numpy as np
13
+ import pandas as pd
14
+ import matplotlib.pyplot as plt
15
+ import shap
16
+ from sklearn.preprocessing import MinMaxScaler
17
+ import plotly.graph_objects as go
18
+ import io
19
+ from matplotlib.figure import Figure
20
+
21
+ # Set page config
22
+ st.set_page_config(
23
+ page_title="Friction Angle Predictor",
24
+ page_icon="πŸ”„",
25
+ layout="wide"
26
+ )
27
+
28
+ # Custom CSS to improve the app's appearance
29
+ st.markdown("""
30
+ <style>
31
+ .stApp {
32
+ max-width: 1200px;
33
+ margin: 0 auto;
34
+ }
35
+ .main {
36
+ padding: 2rem;
37
+ }
38
+ .stButton>button {
39
+ width: 100%;
40
+ }
41
+ </style>
42
+ """, unsafe_allow_html=True)
43
+
44
+ # Load the trained model and recreate the architecture
45
+ class Net(torch.nn.Module):
46
+ def __init__(self, input_size):
47
+ super(Net, self).__init__()
48
+ self.fc1 = torch.nn.Linear(input_size, 64)
49
+ self.fc2 = torch.nn.Linear(64, 1000)
50
+ self.fc3 = torch.nn.Linear(1000, 200)
51
+ self.fc4 = torch.nn.Linear(200, 8)
52
+ self.fc5 = torch.nn.Linear(8, 1)
53
+ self.dropout = torch.nn.Dropout(0.2)
54
+
55
+ # Initialize weights
56
+ self.apply(self._init_weights)
57
+
58
+ def _init_weights(self, module):
59
+ if isinstance(module, torch.nn.Linear):
60
+ torch.nn.init.xavier_uniform_(module.weight)
61
+ if module.bias is not None:
62
+ module.bias.data.zero_()
63
+
64
+ def forward(self, x):
65
+ x = torch.nn.functional.relu(self.fc1(x))
66
+ x = self.dropout(x)
67
+ x = torch.nn.functional.relu(self.fc2(x))
68
+ x = self.dropout(x)
69
+ x = torch.nn.functional.relu(self.fc3(x))
70
+ x = self.dropout(x)
71
+ x = torch.nn.functional.relu(self.fc4(x))
72
+ x = self.dropout(x)
73
+ x = self.fc5(x)
74
+ return x
75
+
76
+ @st.cache_resource
77
+ def load_model_and_data():
78
+ # Set device and random seeds
79
+ np.random.seed(32)
80
+ torch.manual_seed(42)
81
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
82
+
83
+ # Load data
84
+ data = pd.read_excel("Data_syw.xlsx")
85
+ X = data.iloc[:, list(range(1, 17)) + list(range(21, 23))]
86
+ y = data.iloc[:, 28].values
87
+
88
+ # Calculate correlation and select features
89
+ correlation_with_target = abs(X.corrwith(pd.Series(y)))
90
+ selected_features = correlation_with_target[correlation_with_target > 0.1].index
91
+ X = X[selected_features]
92
+
93
+ # Initialize and fit scalers
94
+ scaler_X = MinMaxScaler()
95
+ scaler_y = MinMaxScaler()
96
+ scaler_X.fit(X)
97
+ scaler_y.fit(y.reshape(-1, 1))
98
+
99
+ # Load model
100
+ model = Net(input_size=len(selected_features)).to(device)
101
+ model.load_state_dict(torch.load('friction_model.pt'))
102
+ model.eval()
103
+
104
+ return model, X.columns, scaler_X, scaler_y, device, X
105
+
106
+ def predict_friction(input_values, model, scaler_X, scaler_y, device):
107
+ # Scale input values
108
+ input_scaled = scaler_X.transform(input_values)
109
+ input_tensor = torch.FloatTensor(input_scaled).to(device)
110
+
111
+ # Make prediction
112
+ with torch.no_grad():
113
+ prediction_scaled = model(input_tensor)
114
+ prediction = scaler_y.inverse_transform(prediction_scaled.cpu().numpy().reshape(-1, 1))
115
+
116
+ return prediction[0][0]
117
+
118
+ def calculate_shap_values(input_values, model, X, scaler_X, scaler_y, device):
119
+ def model_predict(X):
120
+ X_scaled = scaler_X.transform(X)
121
+ X_tensor = torch.FloatTensor(X_scaled).to(device)
122
+ with torch.no_grad():
123
+ scaled_pred = model(X_tensor).cpu().numpy()
124
+ return scaler_y.inverse_transform(scaled_pred.reshape(-1, 1)).flatten()
125
+
126
+ try:
127
+ # Use a smaller background dataset and fewer samples for stability
128
+ background = shap.kmeans(X.values, k=5) # Reduced from 10 to 5
129
+ explainer = shap.KernelExplainer(model_predict, background)
130
+ shap_values = explainer.shap_values(input_values.values, nsamples=100) # Added nsamples parameter
131
+
132
+ if isinstance(shap_values, list):
133
+ shap_values = np.array(shap_values[0])
134
+
135
+ return shap_values[0], explainer.expected_value
136
+ except Exception as e:
137
+ st.error(f"Error calculating SHAP values: {str(e)}")
138
+ # Return dummy values in case of error
139
+ return np.zeros(len(input_values.columns)), 0.0
140
+
141
+ def create_waterfall_plot(shap_values, feature_names, base_value, input_data):
142
+ # Create SHAP explanation object
143
+ explanation = shap.Explanation(
144
+ values=shap_values,
145
+ base_values=base_value,
146
+ data=input_data,
147
+ feature_names=list(feature_names)
148
+ )
149
+
150
+ # Create figure
151
+ fig = plt.figure(figsize=(12, 8))
152
+ shap.plots.waterfall(explanation, show=False)
153
+ plt.title('Local SHAP Value Contributions')
154
+ plt.tight_layout()
155
+
156
+ # Save plot to a buffer
157
+ buf = io.BytesIO()
158
+ plt.savefig(buf, format='png', bbox_inches='tight', dpi=300)
159
+ plt.close(fig)
160
+ buf.seek(0)
161
+ return buf
162
+
163
+ def main():
164
+ st.title("πŸ”„ Friction Angle Predictor")
165
+ st.write("This app predicts the friction angle based on waste composition and characteristics.")
166
+
167
+ try:
168
+ # Load model and data
169
+ model, feature_names, scaler_X, scaler_y, device, X = load_model_and_data()
170
+
171
+ # Create two columns for input
172
+ col1, col2 = st.columns(2)
173
+
174
+ # Dictionary to store input values
175
+ input_values = {}
176
+
177
+ # Create input fields for each feature
178
+ for i, feature in enumerate(feature_names):
179
+ with col1 if i < len(feature_names)//2 else col2:
180
+ min_val = float(X[feature].min())
181
+ max_val = float(X[feature].max())
182
+ mean_val = float(X[feature].mean())
183
+
184
+ input_values[feature] = st.number_input(
185
+ f"{feature}",
186
+ min_value=min_val,
187
+ max_value=max_val,
188
+ value=mean_val,
189
+ help=f"Range: {min_val:.2f} to {max_val:.2f}"
190
+ )
191
+
192
+ # Create DataFrame from input values
193
+ input_df = pd.DataFrame([input_values])
194
+
195
+ if st.button("Predict Friction Angle"):
196
+ with st.spinner("Calculating prediction and SHAP values..."):
197
+ # Make prediction
198
+ prediction = predict_friction(input_df, model, scaler_X, scaler_y, device)
199
+
200
+ # Calculate SHAP values
201
+ shap_values, base_value = calculate_shap_values(input_df, model, X, scaler_X, scaler_y, device)
202
+
203
+ # Display results
204
+ st.header("Results")
205
+ col1, col2 = st.columns(2)
206
+
207
+ with col1:
208
+ st.metric("Predicted Friction Angle", f"{prediction:.2f}Β°")
209
+ with col2:
210
+ st.metric("Base Value", f"{base_value:.2f}Β°")
211
+
212
+ # Create and display waterfall plot
213
+ st.header("SHAP Waterfall Plot")
214
+ waterfall_plot = create_waterfall_plot(
215
+ shap_values=shap_values,
216
+ feature_names=feature_names,
217
+ base_value=base_value,
218
+ input_data=input_df.values[0]
219
+ )
220
+ st.image(waterfall_plot)
221
+
222
+ except Exception as e:
223
+ st.error(f"An error occurred: {str(e)}")
224
+ st.info("Please try refreshing the page. If the error persists, contact support.")
225
+
226
+ if __name__ == "__main__":
227
+ main()
friction_model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2b003757ab36efc16f7160b0a3e8aa495fe722030ac8088da0804b3edec34f30
3
+ size 1075034
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ torch
3
+ numpy
4
+ pandas
5
+ matplotlib
6
+ shap
7
+ scikit-learn
8
+ plotly
9
+ openpyxl