shiko217 commited on
Commit
2e9e5c9
·
unverified ·
1 Parent(s): 870d43b
README.md CHANGED
@@ -7,7 +7,6 @@ sdk: gradio
7
  sdk_version: 6.13.0
8
  app_file: app.py
9
  pinned: false
10
- python_version: "3.11"
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
7
  sdk_version: 6.13.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
app.py CHANGED
@@ -1,23 +1,21 @@
1
  import os
2
-
3
- os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
4
- os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
5
-
6
  import numpy as np
7
  import librosa
8
- import scipy.signal
9
- from scipy.stats import kurtosis
10
  import gradio as gr
11
- import tensorflow as tf
12
- import zipfile
13
- import tempfile
14
  import warnings
15
 
16
- warnings.filterwarnings('ignore', category=UserWarning)
17
 
18
- MAX_TIME_FRAMES = 313
19
- N_MELS = 128
20
- NUM_CLASSES = 6
 
 
 
 
 
21
 
22
  LABEL_MAP_INVERSE = {
23
  0: 'Machine 1_Normal', 1: 'Machine 1_Abnormal',
@@ -27,205 +25,103 @@ LABEL_MAP_INVERSE = {
27
 
28
 
29
  # ==========================================
30
- # PREPROCESSOR
31
  # ==========================================
32
- class MachineListenerPreprocessor:
33
- def __init__(self, target_sr=16000, n_fft=2048, hop_length=512, n_mels=N_MELS, n_mfcc=20):
34
- self.sr = target_sr
35
- self.n_fft = n_fft
36
- self.hop_length = hop_length
37
- self.n_mels = n_mels
38
- self.n_mfcc = n_mfcc
39
-
40
- def _apply_highpass_filter(self, y, cutoff=60.0):
41
- nyquist = 0.5 * self.sr
42
- normal_cutoff = cutoff / nyquist
43
- if normal_cutoff >= 1.0:
44
- return y
45
- b, a = scipy.signal.butter(4, normal_cutoff, btype='high', analog=False)
46
- return scipy.signal.filtfilt(b, a, y)
47
-
48
- def _truncate_silence(self, y, top_db=25):
49
- y_trimmed, _ = librosa.effects.trim(y, top_db=top_db, frame_length=self.n_fft, hop_length=self.hop_length)
50
- return y_trimmed
51
-
52
- def _mean_variance_normalize(self, y):
53
- return (y - np.mean(y)) / (np.std(y) + 1e-8)
54
-
55
- def process_audio(self, file_path):
56
- y, _ = librosa.load(file_path, sr=self.sr)
57
- y = self._apply_highpass_filter(y)
58
- y = self._truncate_silence(y, top_db=25)
59
-
60
- if len(y) == 0:
61
- raise ValueError(f"Silence only: {file_path}")
62
-
63
- y = self._mean_variance_normalize(y)
64
-
65
- mel_spec = librosa.feature.melspectrogram(
66
- y=y, sr=self.sr, n_fft=self.n_fft, hop_length=self.hop_length, n_mels=self.n_mels
67
- )
68
- log_mel_spec = librosa.power_to_db(mel_spec, ref=np.max)
69
- return log_mel_spec
70
-
71
-
72
- def pad_or_truncate(spectrogram, max_frames):
73
- if spectrogram.shape[1] > max_frames:
74
- return spectrogram[:, :max_frames]
75
- elif spectrogram.shape[1] < max_frames:
76
- pad_width = max_frames - spectrogram.shape[1]
77
- return np.pad(spectrogram, pad_width=((0, 0), (0, pad_width)), mode='constant')
78
- return spectrogram
79
 
 
 
 
 
80
 
81
- # ==========================================
82
- # CUSTOM LAYERS (replace Lambda layers from training)
83
- # ==========================================
84
- class NormLayer(tf.keras.layers.Layer):
85
- """Replaces Lambda(lambda t: t / 80.0) used for spectrogram normalization."""
86
- def call(self, x):
87
- return x / 80.0
88
-
89
-
90
- class FreqReduceLayer(tf.keras.layers.Layer):
91
- """Replaces Lambda(lambda t: tf.reduce_mean(t, axis=1)) used to collapse frequency."""
92
- def call(self, x):
93
- return tf.reduce_mean(x, axis=1)
94
-
95
-
96
- class SpecAugmentLayer(tf.keras.layers.Layer):
97
- """Training-only augmentation. Passes through during inference."""
98
- def __init__(self, freq_mask_param=15, time_mask_param=30, **kwargs):
99
- super().__init__(**kwargs)
100
- self.freq_mask_param = freq_mask_param
101
- self.time_mask_param = time_mask_param
102
-
103
- def call(self, inputs, training=None):
104
- if not training:
105
- return inputs
106
- freq_max = tf.shape(inputs)[1]
107
- time_max = tf.shape(inputs)[2]
108
- f = tf.random.uniform([], minval=0, maxval=self.freq_mask_param, dtype=tf.int32)
109
- f0 = tf.random.uniform([], minval=0, maxval=freq_max - f, dtype=tf.int32)
110
- freq_indices = tf.range(freq_max)
111
- freq_mask = tf.logical_or(freq_indices < f0, freq_indices >= f0 + f)
112
- freq_mask = tf.cast(freq_mask, inputs.dtype)
113
- freq_mask = tf.reshape(freq_mask, [1, -1, 1, 1])
114
- inputs = inputs * freq_mask
115
- t = tf.random.uniform([], minval=0, maxval=self.time_mask_param, dtype=tf.int32)
116
- t0 = tf.random.uniform([], minval=0, maxval=time_max - t, dtype=tf.int32)
117
- time_indices = tf.range(time_max)
118
- time_mask = tf.logical_or(time_indices < t0, time_indices >= t0 + t)
119
- time_mask = tf.cast(time_mask, inputs.dtype)
120
- time_mask = tf.reshape(time_mask, [1, 1, -1, 1])
121
- inputs = inputs * time_mask
122
- return inputs
123
-
124
- def get_config(self):
125
- config = super().get_config()
126
- config.update({
127
- "freq_mask_param": self.freq_mask_param,
128
- "time_mask_param": self.time_mask_param,
129
- })
130
- return config
131
 
 
 
 
 
 
 
132
 
133
- # ==========================================
134
- # MODEL ARCHITECTURE (exact replica of V2-F)
135
- # ==========================================
136
- def se_block(x, filters, ratio=8):
137
- se = tf.keras.layers.GlobalAveragePooling2D()(x)
138
- se = tf.keras.layers.Dense(filters // ratio, activation='relu')(se)
139
- se = tf.keras.layers.Dense(filters, activation='sigmoid')(se)
140
- se = tf.keras.layers.Reshape([1, 1, filters])(se)
141
- return x * se
142
-
143
-
144
- def build_v2f():
145
- inp = tf.keras.Input(shape=(N_MELS, MAX_TIME_FRAMES, 1))
146
-
147
- x = SpecAugmentLayer(freq_mask_param=15, time_mask_param=30)(inp)
148
- x = NormLayer()(x)
149
-
150
- # Block 1
151
- x = tf.keras.layers.Conv2D(32, (3, 3), padding='same',
152
- kernel_regularizer=tf.keras.regularizers.l2(1e-4))(x)
153
- x = tf.keras.layers.BatchNormalization()(x)
154
- x = tf.keras.layers.Activation('relu')(x)
155
- x = se_block(x, 32)
156
- x = tf.keras.layers.MaxPooling2D(pool_size=(2, 1))(x)
157
- x = tf.keras.layers.Dropout(0.2)(x)
158
-
159
- # Block 2
160
- x = tf.keras.layers.Conv2D(64, (3, 3), padding='same',
161
- kernel_regularizer=tf.keras.regularizers.l2(1e-4))(x)
162
- x = tf.keras.layers.BatchNormalization()(x)
163
- x = tf.keras.layers.Activation('relu')(x)
164
- x = se_block(x, 64)
165
- x = tf.keras.layers.MaxPooling2D(pool_size=(2, 1))(x)
166
- x = tf.keras.layers.Dropout(0.2)(x)
167
-
168
- # Block 3
169
- x = tf.keras.layers.Conv2D(128, (3, 3), padding='same',
170
- kernel_regularizer=tf.keras.regularizers.l2(1e-4))(x)
171
- x = tf.keras.layers.BatchNormalization()(x)
172
- x = tf.keras.layers.Activation('relu')(x)
173
- x = se_block(x, 128)
174
- x = tf.keras.layers.MaxPooling2D(pool_size=(2, 1))(x)
175
- x = tf.keras.layers.Dropout(0.2)(x)
176
-
177
- # Bridge: reduce channels and collapse frequency
178
- x = tf.keras.layers.Conv2D(64, (1, 1), padding='same', activation='relu')(x)
179
- x = FreqReduceLayer()(x)
180
-
181
- # BiLSTM
182
- x = tf.keras.layers.Bidirectional(
183
- tf.keras.layers.LSTM(64, return_sequences=True)
184
- )(x)
185
- x = tf.keras.layers.Dropout(0.3)(x)
186
-
187
- # MultiHead Attention
188
- x = tf.keras.layers.MaxPooling1D(pool_size=4)(x)
189
- x = tf.keras.layers.MultiHeadAttention(num_heads=4, key_dim=32)(x, x)
190
- x = tf.keras.layers.GlobalAveragePooling1D()(x)
191
- x = tf.keras.layers.Dropout(0.4)(x)
192
-
193
- out = tf.keras.layers.Dense(NUM_CLASSES, activation='softmax')(x)
194
- return tf.keras.Model(inputs=inp, outputs=out)
195
 
196
 
197
  # ==========================================
198
- # LOAD MODEL (rebuild + weights only)
199
  # ==========================================
200
- def load_model():
201
- """
202
- Rebuild the V2-F architecture in code, then load ONLY the weights
203
- from the .keras file. This completely bypasses Lambda deserialization
204
- and Python bytecode compatibility issues.
205
- """
206
- keras_path = 'best_v2f_generalist.keras'
207
- try:
208
- model = build_v2f()
209
- # .keras file is a zip; extract the weights h5 and load
210
- with tempfile.TemporaryDirectory() as tmpdir:
211
- with zipfile.ZipFile(keras_path, 'r') as z:
212
- z.extract('model.weights.h5', tmpdir)
213
- model.load_weights(os.path.join(tmpdir, 'model.weights.h5'))
214
- print("Model rebuilt and weights loaded successfully.")
215
- return model, ""
216
- except Exception as e:
217
- return None, str(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
 
220
  # ==========================================
221
- # STARTUP
222
  # ==========================================
223
- model, model_error = load_model()
224
- if model is None:
 
 
 
 
 
225
  print(f"Warning: Could not load model. Error: {model_error}")
226
 
227
- preprocessor = MachineListenerPreprocessor()
228
-
229
 
230
  # ==========================================
231
  # PREDICTION
@@ -237,19 +133,27 @@ def predict(audio_filepath):
237
  return "Please upload an audio file."
238
 
239
  try:
240
- log_mel_spec = preprocessor.process_audio(audio_filepath)
241
- spec = pad_or_truncate(log_mel_spec, MAX_TIME_FRAMES)
242
-
243
- # Shape: (1, 128, 313, 1) — single channel spectrogram
244
- spec_batch = spec[np.newaxis, ..., np.newaxis].astype(np.float32)
245
-
246
- predictions = model(spec_batch, training=False).numpy()
247
-
248
- predicted_class_idx = np.argmax(predictions, axis=-1)[0]
249
- predicted_label = LABEL_MAP_INVERSE.get(predicted_class_idx, "Unknown")
250
- confidence = float(np.max(predictions))
251
-
252
- return f"Prediction: {predicted_label} (Confidence: {confidence:.2f})"
 
 
 
 
 
 
 
 
253
 
254
  except Exception as e:
255
  return f"Error processing file: {str(e)}"
 
1
  import os
 
 
 
 
2
  import numpy as np
3
  import librosa
4
+ import joblib
 
5
  import gradio as gr
6
+ from scipy import stats as scipy_stats
 
 
7
  import warnings
8
 
9
+ warnings.filterwarnings('ignore')
10
 
11
+ # ==========================================
12
+ # CONFIGURATION (must match training notebook)
13
+ # ==========================================
14
+ TARGET_SR = 48000
15
+ TARGET_DURATION = 10.0
16
+ TARGET_LENGTH = int(TARGET_SR * TARGET_DURATION)
17
+ TRIM_TOP_DB = 20
18
+ N_MFCC = 20
19
 
20
  LABEL_MAP_INVERSE = {
21
  0: 'Machine 1_Normal', 1: 'Machine 1_Abnormal',
 
25
 
26
 
27
  # ==========================================
28
+ # PREPROCESSING (from notebook 02)
29
  # ==========================================
30
+ def preprocess_audio(file_path):
31
+ y, sr = librosa.load(file_path, sr=TARGET_SR)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ # Trim silence
34
+ y_trimmed, _ = librosa.effects.trim(y, top_db=TRIM_TOP_DB)
35
+ if len(y_trimmed) == 0:
36
+ y_trimmed = y
37
 
38
+ # DC offset removal
39
+ y_centered = y_trimmed - np.mean(y_trimmed)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
+ # Z-score normalization
42
+ std = np.std(y_centered)
43
+ if std > 1e-8:
44
+ y_norm = y_centered / std
45
+ else:
46
+ y_norm = y_centered
47
 
48
+ # Fixed length
49
+ if len(y_norm) < TARGET_LENGTH:
50
+ y_norm = np.pad(y_norm, (0, TARGET_LENGTH - len(y_norm)), mode='constant')
51
+ else:
52
+ y_norm = y_norm[:TARGET_LENGTH]
53
+
54
+ return y_norm
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
 
57
  # ==========================================
58
+ # FEATURE EXTRACTION (from notebook 04)
59
  # ==========================================
60
+ def compute_stats(feature_array, prefix):
61
+ result = {}
62
+ if feature_array.ndim == 1:
63
+ feature_array = feature_array.reshape(1, -1)
64
+
65
+ for i in range(feature_array.shape[0]):
66
+ row = feature_array[i]
67
+ suffix = f"_{i}" if feature_array.shape[0] > 1 else ""
68
+ result[f"{prefix}{suffix}_mean"] = np.mean(row)
69
+ result[f"{prefix}{suffix}_std"] = np.std(row)
70
+ result[f"{prefix}{suffix}_min"] = np.min(row)
71
+ result[f"{prefix}{suffix}_max"] = np.max(row)
72
+ return result
73
+
74
+
75
+ def extract_ml_features(y, sr=TARGET_SR, n_mfcc=N_MFCC):
76
+ features = {}
77
+
78
+ # 1. MFCCs (20 × 4 = 80)
79
+ mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc)
80
+ features.update(compute_stats(mfccs, "mfcc"))
81
+
82
+ # 2. Spectral Centroid (4)
83
+ features.update(compute_stats(librosa.feature.spectral_centroid(y=y, sr=sr), "spectral_centroid"))
84
+
85
+ # 3. Spectral Flatness (4)
86
+ features.update(compute_stats(librosa.feature.spectral_flatness(y=y), "spectral_flatness"))
87
+
88
+ # 4. Zero-Crossing Rate (4)
89
+ features.update(compute_stats(librosa.feature.zero_crossing_rate(y), "zcr"))
90
+
91
+ # 5. RMS Energy (4)
92
+ features.update(compute_stats(librosa.feature.rms(y=y), "rms"))
93
+
94
+ # 6. Spectral Rolloff at 85% (4)
95
+ features.update(compute_stats(librosa.feature.spectral_rolloff(y=y, sr=sr, roll_percent=0.85), "spectral_rolloff"))
96
+
97
+ # 7. Spectral Bandwidth (4)
98
+ features.update(compute_stats(librosa.feature.spectral_bandwidth(y=y, sr=sr), "spectral_bandwidth"))
99
+
100
+ # 8. Spectral Contrast (7 bands × 4 = 28)
101
+ features.update(compute_stats(librosa.feature.spectral_contrast(y=y, sr=sr, n_bands=6), "spectral_contrast"))
102
+
103
+ # 9. Chroma STFT (12 × 4 = 48)
104
+ features.update(compute_stats(librosa.feature.chroma_stft(y=y, sr=sr), "chroma"))
105
+
106
+ # 10. Spectral Kurtosis (1)
107
+ S = np.abs(librosa.stft(y))
108
+ features["spectral_kurtosis"] = float(scipy_stats.kurtosis(np.mean(S, axis=1)))
109
+
110
+ return features
111
 
112
 
113
  # ==========================================
114
+ # LOAD MODEL
115
  # ==========================================
116
+ model = None
117
+ model_error = ""
118
+ try:
119
+ model = joblib.load("ml_XGBoost_v1_immed.joblib")
120
+ print("XGBoost model loaded successfully.")
121
+ except Exception as e:
122
+ model_error = str(e)
123
  print(f"Warning: Could not load model. Error: {model_error}")
124
 
 
 
125
 
126
  # ==========================================
127
  # PREDICTION
 
133
  return "Please upload an audio file."
134
 
135
  try:
136
+ # 1. Preprocess
137
+ y = preprocess_audio(audio_filepath)
138
+
139
+ # 2. Extract features
140
+ features = extract_ml_features(y)
141
+
142
+ # 3. Convert to array in the same column order the model expects
143
+ feature_names = sorted(features.keys())
144
+ X = np.array([[features[name] for name in feature_names]])
145
+
146
+ # 4. Predict
147
+ predicted_class = model.predict(X)[0]
148
+ predicted_label = LABEL_MAP_INVERSE.get(int(predicted_class), "Unknown")
149
+
150
+ # Get probabilities if available
151
+ if hasattr(model, 'predict_proba'):
152
+ proba = model.predict_proba(X)[0]
153
+ confidence = float(np.max(proba))
154
+ return f"Prediction: {predicted_label} (Confidence: {confidence:.2f})"
155
+ else:
156
+ return f"Prediction: {predicted_label}"
157
 
158
  except Exception as e:
159
  return f"Error processing file: {str(e)}"
best_v2f_generalist.keras → ml_XGBoost_v1_immed.joblib RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:b6b374074a6eaa9c1588263282b63536f09ed8b2cc75439cdbca0492949ecb9e
3
- size 3069230
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2e80bff346c7895461984c34edc41667489208e66fd5de49881bb136a096d664
3
+ size 3011600
requirements.txt CHANGED
@@ -1,5 +1,7 @@
1
- tensorflow-cpu==2.16.*
2
  gradio
3
  librosa
4
- numpy<2
5
  scipy
 
 
 
 
 
1
  gradio
2
  librosa
3
+ numpy
4
  scipy
5
+ scikit-learn
6
+ xgboost
7
+ joblib