shiko217 commited on
Commit
fd369f2
·
unverified ·
1 Parent(s): 1f62e32
Files changed (1) hide show
  1. app.py +49 -15
app.py CHANGED
@@ -122,18 +122,53 @@ def pad_or_truncate(spectrogram, max_frames):
122
  return np.pad(spectrogram, pad_width=((0, 0), (0, pad_width)), mode='constant')
123
  return spectrogram
124
 
125
- # Load the model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  model_error = ""
127
- try:
128
- model = tf.keras.models.load_model(
129
- 'best_v2f_generalist.keras',
130
- custom_objects={'SpecAugmentLayer': SpecAugmentLayer},
131
- safe_mode=False
132
- )
133
- except Exception as e:
134
- print("Warning: Could not load model. Ensure the path is correct.", e)
135
- model_error = str(e)
136
- model = None
137
 
138
  preprocessor = MachineListenerPreprocessor()
139
 
@@ -152,9 +187,8 @@ def predict(audio_filepath):
152
 
153
  # Add batch dimensions
154
  spec_2d_batch = np.expand_dims(spec_2d, axis=0)
155
- # Note: If your model expects a specific shape, e.g., (batch, channels, height, width), adjust dimensions below.
156
- spec_2d_batch = np.expand_dims(spec_2d_batch, axis=-1)
157
- stat_1d_batch = np.expand_dims(stat_1d, axis=0)
158
 
159
  # Predict
160
  predictions = model.predict([spec_2d_batch, stat_1d_batch])
@@ -179,4 +213,4 @@ iface = gr.Interface(
179
  )
180
 
181
  if __name__ == "__main__":
182
- iface.launch()
 
122
  return np.pad(spectrogram, pad_width=((0, 0), (0, pad_width)), mode='constant')
123
  return spectrogram
124
 
125
+ def load_model():
126
+ """
127
+ Attempt to load the Keras model, trying multiple strategies to handle
128
+ Lambda layer deserialization safely across different TF/Keras versions.
129
+ """
130
+ model_path = 'best_v2f_generalist.keras'
131
+ custom_objects = {'SpecAugmentLayer': SpecAugmentLayer}
132
+
133
+ # Strategy 1: enable_unsafe_deserialization() — works on Keras 3.x / TF 2.13+
134
+ try:
135
+ tf.keras.config.enable_unsafe_deserialization()
136
+ loaded = tf.keras.models.load_model(model_path, custom_objects=custom_objects)
137
+ print("Model loaded via enable_unsafe_deserialization().")
138
+ return loaded, ""
139
+ except AttributeError:
140
+ pass # tf.keras.config.enable_unsafe_deserialization not available in this version
141
+ except Exception as e:
142
+ return None, str(e)
143
+
144
+ # Strategy 2: safe_mode=False — works on some TF 2.12 / Keras 2.x builds
145
+ try:
146
+ loaded = tf.keras.models.load_model(
147
+ model_path,
148
+ custom_objects=custom_objects,
149
+ safe_mode=False
150
+ )
151
+ print("Model loaded via safe_mode=False.")
152
+ return loaded, ""
153
+ except TypeError:
154
+ pass # safe_mode kwarg not supported in this version
155
+ except Exception as e:
156
+ return None, str(e)
157
+
158
+ # Strategy 3: bare load — last resort, may raise on Lambda layers
159
+ try:
160
+ loaded = tf.keras.models.load_model(model_path, custom_objects=custom_objects)
161
+ print("Model loaded via bare load_model().")
162
+ return loaded, ""
163
+ except Exception as e:
164
+ return None, str(e)
165
+
166
+
167
+ # Load the model at startup
168
  model_error = ""
169
+ model, model_error = load_model()
170
+ if model is None:
171
+ print(f"Warning: Could not load model. Error: {model_error}")
 
 
 
 
 
 
 
172
 
173
  preprocessor = MachineListenerPreprocessor()
174
 
 
187
 
188
  # Add batch dimensions
189
  spec_2d_batch = np.expand_dims(spec_2d, axis=0)
190
+ spec_2d_batch = np.expand_dims(spec_2d_batch, axis=-1) # (batch, n_mels, time, 1)
191
+ stat_1d_batch = np.expand_dims(stat_1d, axis=0) # (batch, n_features)
 
192
 
193
  # Predict
194
  predictions = model.predict([spec_2d_batch, stat_1d_batch])
 
213
  )
214
 
215
  if __name__ == "__main__":
216
+ iface.launch()