Sompote commited on
Commit
9929c10
·
verified ·
1 Parent(s): ab8f969

Upload 6 files

Browse files
Files changed (6) hide show
  1. Data_syw.xlsx +0 -0
  2. README.md +39 -14
  3. app.py +347 -0
  4. cohesion_model.pt +3 -0
  5. friction_model.pt +3 -0
  6. requirements.txt +10 -0
Data_syw.xlsx ADDED
Binary file (37.7 kB). View file
 
README.md CHANGED
@@ -1,14 +1,39 @@
1
- ---
2
- title: MSW Shear
3
- emoji: 🏃
4
- colorFrom: indigo
5
- colorTo: purple
6
- sdk: streamlit
7
- sdk_version: 1.42.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- short_description: shear strength of solid waste
12
- ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Waste Properties Predictor
2
+
3
+ This Streamlit app predicts both friction angle and cohesion based on waste composition and characteristics using deep learning models.
4
+
5
+ ## Features
6
+
7
+ - Predicts both friction angle and cohesion simultaneously
8
+ - Supports Excel file input for batch predictions
9
+ - Provides SHAP value explanations for predictions
10
+ - Interactive input interface with value range validation
11
+ - Supports custom data upload
12
+
13
+ ## Files Description
14
+
15
+ - `app.py`: Main application file
16
+ - `requirements.txt`: Required Python packages
17
+ - `friction_model.pt`: Pre-trained model for friction angle prediction
18
+ - `cohesion_model.pt`: Pre-trained model for cohesion prediction
19
+ - `Data_syw.xlsx`: Default data file with example values
20
+
21
+ ## Usage
22
+
23
+ 1. The app loads with default values from the first row of `Data_syw.xlsx`
24
+ 2. You can either:
25
+ - Use the default values
26
+ - Upload your own Excel file with waste composition data
27
+ - Manually adjust individual values using the input fields
28
+ 3. Click "Predict Properties" to get predictions and SHAP explanations
29
+
30
+ ## Input Parameters
31
+
32
+ The app accepts various waste composition and characteristic parameters. All inputs are validated against the training data ranges to ensure reliable predictions.
33
+
34
+ ## Output
35
+
36
+ For each prediction, the app provides:
37
+ - Predicted friction angle (degrees)
38
+ - Predicted cohesion (kPa)
39
+ - SHAP waterfall plots explaining the contribution of each feature to the predictions
app.py ADDED
@@ -0,0 +1,347 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="Waste Properties 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 for both friction and cohesion
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
+
87
+ # Friction data
88
+ y_friction = data.iloc[:, 28].values
89
+ correlation_with_friction = abs(X.corrwith(pd.Series(y_friction)))
90
+ selected_features_friction = correlation_with_friction[correlation_with_friction > 0.1].index
91
+ X_friction = X[selected_features_friction]
92
+
93
+ # Cohesion data
94
+ y_cohesion = data.iloc[:, 25].values
95
+ correlation_with_cohesion = abs(X.corrwith(pd.Series(y_cohesion)))
96
+ selected_features_cohesion = correlation_with_cohesion[correlation_with_cohesion > 0.1].index
97
+ X_cohesion = X[selected_features_cohesion]
98
+
99
+ # Initialize and fit scalers for friction
100
+ scaler_X_friction = MinMaxScaler()
101
+ scaler_y_friction = MinMaxScaler()
102
+ scaler_X_friction.fit(X_friction)
103
+ scaler_y_friction.fit(y_friction.reshape(-1, 1))
104
+
105
+ # Initialize and fit scalers for cohesion
106
+ scaler_X_cohesion = MinMaxScaler()
107
+ scaler_y_cohesion = MinMaxScaler()
108
+ scaler_X_cohesion.fit(X_cohesion)
109
+ scaler_y_cohesion.fit(y_cohesion.reshape(-1, 1))
110
+
111
+ # Load models
112
+ friction_model = Net(input_size=len(selected_features_friction)).to(device)
113
+ friction_model.load_state_dict(torch.load('friction_model.pt'))
114
+ friction_model.eval()
115
+
116
+ cohesion_model = Net(input_size=len(selected_features_cohesion)).to(device)
117
+ cohesion_model.load_state_dict(torch.load('cohesion_model.pt'))
118
+ cohesion_model.eval()
119
+
120
+ return (friction_model, X_friction.columns, scaler_X_friction, scaler_y_friction,
121
+ cohesion_model, X_cohesion.columns, scaler_X_cohesion, scaler_y_cohesion,
122
+ device, X_friction, X_cohesion)
123
+
124
+ def predict_friction(input_values, model, scaler_X, scaler_y, device):
125
+ # Scale input values
126
+ input_scaled = scaler_X.transform(input_values)
127
+ input_tensor = torch.FloatTensor(input_scaled).to(device)
128
+
129
+ # Make prediction
130
+ with torch.no_grad():
131
+ prediction_scaled = model(input_tensor)
132
+ prediction = scaler_y.inverse_transform(prediction_scaled.cpu().numpy().reshape(-1, 1))
133
+
134
+ return prediction[0][0]
135
+
136
+ def predict_cohesion(input_values, model, scaler_X, scaler_y, device):
137
+ # Scale input values
138
+ input_scaled = scaler_X.transform(input_values)
139
+ input_tensor = torch.FloatTensor(input_scaled).to(device)
140
+
141
+ # Make prediction
142
+ with torch.no_grad():
143
+ prediction_scaled = model(input_tensor)
144
+ prediction = scaler_y.inverse_transform(prediction_scaled.cpu().numpy().reshape(-1, 1))
145
+
146
+ return prediction[0][0]
147
+
148
+ def calculate_shap_values(input_values, model, X, scaler_X, scaler_y, device):
149
+ def model_predict(X):
150
+ X_scaled = scaler_X.transform(X)
151
+ X_tensor = torch.FloatTensor(X_scaled).to(device)
152
+ with torch.no_grad():
153
+ scaled_pred = model(X_tensor).cpu().numpy()
154
+ return scaler_y.inverse_transform(scaled_pred.reshape(-1, 1)).flatten()
155
+
156
+ try:
157
+ # Set random seed for reproducibility
158
+ np.random.seed(42)
159
+
160
+ # Use a fixed background dataset
161
+ # Take a sample size that's at most the size of the dataset
162
+ n_samples = min(50, len(X))
163
+ background_indices = np.random.choice(len(X), size=n_samples, replace=False)
164
+ background = X.iloc[background_indices].values
165
+
166
+ # Create explainer with more samples for stability
167
+ explainer = shap.KernelExplainer(model_predict, background)
168
+ shap_values = explainer.shap_values(input_values.values, nsamples=200) # Reduced from 500 to 200
169
+
170
+ if isinstance(shap_values, list):
171
+ shap_values = np.array(shap_values[0])
172
+
173
+ return shap_values[0], explainer.expected_value
174
+ except Exception as e:
175
+ st.error(f"Error calculating SHAP values: {str(e)}")
176
+ return np.zeros(len(input_values.columns)), 0.0
177
+
178
+ @st.cache_resource
179
+ def create_background_data(X, n_samples=50): # Changed from 100 to 50
180
+ """Create and cache background data for SHAP calculations"""
181
+ np.random.seed(42)
182
+ # Ensure n_samples is not larger than dataset
183
+ n_samples = min(n_samples, len(X))
184
+ background_indices = np.random.choice(len(X), size=n_samples, replace=False)
185
+ return X.iloc[background_indices].values
186
+
187
+ def create_waterfall_plot(shap_values, feature_names, base_value, input_data, title):
188
+ # Create SHAP explanation object
189
+ explanation = shap.Explanation(
190
+ values=shap_values,
191
+ base_values=base_value,
192
+ data=input_data,
193
+ feature_names=list(feature_names)
194
+ )
195
+
196
+ # Create figure
197
+ fig = plt.figure(figsize=(12, 8))
198
+ shap.plots.waterfall(explanation, show=False)
199
+ plt.title(f'{title} - Local SHAP Value Contributions')
200
+ plt.tight_layout()
201
+
202
+ # Save plot to a buffer
203
+ buf = io.BytesIO()
204
+ plt.savefig(buf, format='png', bbox_inches='tight', dpi=300)
205
+ plt.close(fig)
206
+ buf.seek(0)
207
+ return buf
208
+
209
+ def main():
210
+ st.title("🔄 Waste Properties Predictor")
211
+ st.write("This app predicts both friction angle and cohesion based on waste composition and characteristics.")
212
+
213
+ try:
214
+ # Load models and data
215
+ (friction_model, friction_features, scaler_X_friction, scaler_y_friction,
216
+ cohesion_model, cohesion_features, scaler_X_cohesion, scaler_y_cohesion,
217
+ device, X_friction, X_cohesion) = load_model_and_data()
218
+
219
+ # Create and cache background data for SHAP calculations
220
+ # No need to store these since they're not used
221
+ # friction_background = create_background_data(X_friction)
222
+ # cohesion_background = create_background_data(X_cohesion)
223
+
224
+ # Combine all unique features
225
+ all_features = sorted(list(set(friction_features) | set(cohesion_features)))
226
+
227
+ st.header("Input Parameters")
228
+
229
+ # Add file upload option
230
+ uploaded_file = st.file_uploader("Upload Excel file with input values", type=['xlsx', 'xls'])
231
+
232
+ # Initialize input values from the data file
233
+ input_values = {}
234
+
235
+ # Load default values from Data_syw.xlsx
236
+ default_data = pd.read_excel("Data_syw.xlsx")
237
+ if len(default_data) > 0:
238
+ for feature in all_features:
239
+ if feature in default_data.columns:
240
+ input_values[feature] = float(default_data[feature].iloc[0])
241
+
242
+ # Override with uploaded file if provided
243
+ if uploaded_file is not None:
244
+ try:
245
+ # Read the uploaded file
246
+ df = pd.read_excel(uploaded_file)
247
+ if len(df) > 0:
248
+ # Use the first row of the uploaded file
249
+ for feature in all_features:
250
+ if feature in df.columns:
251
+ input_values[feature] = float(df[feature].iloc[0])
252
+ except Exception as e:
253
+ st.error(f"Error reading file: {str(e)}")
254
+
255
+ st.write("Enter the waste composition and characteristics below to predict both friction angle and cohesion.")
256
+
257
+ # Create two columns for input
258
+ col1, col2 = st.columns(2)
259
+
260
+ # Create input fields for each feature
261
+ for i, feature in enumerate(all_features):
262
+ with col1 if i < len(all_features)//2 else col2:
263
+ # Get min and max values considering both friction and cohesion datasets
264
+ if feature in X_friction.columns and feature in X_cohesion.columns:
265
+ min_val = min(float(X_friction[feature].min()), float(X_cohesion[feature].min()))
266
+ max_val = max(float(X_friction[feature].max()), float(X_cohesion[feature].max()))
267
+ elif feature in X_friction.columns:
268
+ min_val = float(X_friction[feature].min())
269
+ max_val = float(X_friction[feature].max())
270
+ else:
271
+ min_val = float(X_cohesion[feature].min())
272
+ max_val = float(X_cohesion[feature].max())
273
+
274
+ # Use the value from input_values if available, otherwise use 0
275
+ default_value = input_values.get(feature, 0.0)
276
+
277
+ input_values[feature] = st.number_input(
278
+ f"{feature}",
279
+ min_value=min_val,
280
+ max_value=max_val,
281
+ value=default_value,
282
+ help=f"Range: {min_val:.2f} to {max_val:.2f}"
283
+ )
284
+
285
+ # Create DataFrames for both predictions
286
+ friction_input_df = pd.DataFrame([[input_values.get(feature, 0) for feature in friction_features]],
287
+ columns=friction_features)
288
+ cohesion_input_df = pd.DataFrame([[input_values.get(feature, 0) for feature in cohesion_features]],
289
+ columns=cohesion_features)
290
+
291
+ if st.button("Predict Properties"):
292
+ with st.spinner("Calculating predictions and SHAP values..."):
293
+ # Make predictions
294
+ friction_prediction = predict_friction(friction_input_df, friction_model, scaler_X_friction, scaler_y_friction, device)
295
+ cohesion_prediction = predict_cohesion(cohesion_input_df, cohesion_model, scaler_X_cohesion, scaler_y_cohesion, device)
296
+
297
+ # Set random seed before SHAP calculations
298
+ np.random.seed(42)
299
+ torch.manual_seed(42)
300
+ if torch.cuda.is_available():
301
+ torch.cuda.manual_seed(42)
302
+
303
+ # Calculate SHAP values using cached background data
304
+ friction_shap_values, friction_base_value = calculate_shap_values(friction_input_df, friction_model, X_friction, scaler_X_friction, scaler_y_friction, device)
305
+ cohesion_shap_values, cohesion_base_value = calculate_shap_values(cohesion_input_df, cohesion_model, X_cohesion, scaler_X_cohesion, scaler_y_cohesion, device)
306
+
307
+ # Display results
308
+ st.header("Prediction Results")
309
+ col1, col2 = st.columns(2)
310
+
311
+ with col1:
312
+ st.metric("Friction Angle", f"{friction_prediction:.2f}°")
313
+
314
+ with col2:
315
+ st.metric("Cohesion", f"{cohesion_prediction:.2f} kPa")
316
+
317
+ # Create and display waterfall plots
318
+ col1, col2 = st.columns(2)
319
+
320
+ with col1:
321
+ st.subheader("Friction Angle SHAP Analysis")
322
+ friction_waterfall_plot = create_waterfall_plot(
323
+ shap_values=friction_shap_values,
324
+ feature_names=friction_features,
325
+ base_value=friction_base_value,
326
+ input_data=friction_input_df.values[0],
327
+ title="Friction Angle"
328
+ )
329
+ st.image(friction_waterfall_plot)
330
+
331
+ with col2:
332
+ st.subheader("Cohesion SHAP Analysis")
333
+ cohesion_waterfall_plot = create_waterfall_plot(
334
+ shap_values=cohesion_shap_values,
335
+ feature_names=cohesion_features,
336
+ base_value=cohesion_base_value,
337
+ input_data=cohesion_input_df.values[0],
338
+ title="Cohesion"
339
+ )
340
+ st.image(cohesion_waterfall_plot)
341
+
342
+ except Exception as e:
343
+ st.error(f"An error occurred: {str(e)}")
344
+ st.info("Please try refreshing the page. If the error persists, contact support.")
345
+
346
+ if __name__ == "__main__":
347
+ main()
cohesion_model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cb057d6ede51c755acc5c8bd66708fd304e57788f528088e5ae39b90920f9222
3
+ size 1073754
friction_model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c26fcb099fc3b77691b2a64e1f69a72843f101dbce382cd2be40a3516899e36c
3
+ size 1075034
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ torch
3
+ numpy
4
+ pandas
5
+ matplotlib
6
+ shap
7
+ scikit-learn
8
+ plotly
9
+ openpyxl
10
+ xlrd