ManasKumar28 commited on
Commit
d643891
·
verified ·
1 Parent(s): ed66fb1

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +11 -27
app.py CHANGED
@@ -1,10 +1,10 @@
1
- """
2
  DCASE 2024 Task 4 baseline (CRNN + BEATs) demo for Hugging Face Spaces (ZeroGPU compatible).
3
 
4
  Files expected in the Space root:
5
  - last.ckpt (DCASE 2024 baseline checkpoint)
6
  - BEATs_iter3_plus_AS2M.pt (optional here: if absent, the app tries to download it)
7
- """
8
  import os
9
  import sys
10
  import math
@@ -83,7 +83,7 @@ print("SEDTask4.__init__ signature:", inspect.signature(SEDTask4.__init__))
83
  # 3. Helpers
84
  # --------------------------------------------------------------------------- #
85
  def call_with_supported_kwargs(fn, **kwargs):
86
- """Call fn with only the kwargs it accepts; required-but-unknown params get None."""
87
  sig = inspect.signature(fn)
88
  accepts_var_kw = any(p.kind == p.VAR_KEYWORD for p in sig.parameters.values())
89
  out = {}
@@ -100,20 +100,17 @@ def call_with_supported_kwargs(fn, **kwargs):
100
  out.setdefault(k, v)
101
  return fn(**out)
102
 
103
-
104
  def torch_load(path, **kw):
105
  try:
106
  return torch.load(path, map_location="cpu", weights_only=True, **kw)
107
  except Exception:
108
  return torch.load(path, map_location="cpu", weights_only=False, **kw)
109
 
110
-
111
  def file_ok(path, min_mb):
112
  return os.path.isfile(path) and os.path.getsize(path) > min_mb * 1024 * 1024
113
 
114
-
115
  def ensure_beats_checkpoint():
116
- """BEATs_iter3_plus_AS2M.pt (self-supervised, NOT the AudioSet-finetuned one)."""
117
  if file_ok(BEATS_PATH, 50):
118
  return BEATS_PATH
119
 
@@ -150,9 +147,8 @@ def ensure_beats_checkpoint():
150
  "(github.com/microsoft/unilm/tree/master/beats) and upload it to the root of this Space."
151
  )
152
 
153
-
154
  def find_config(hp):
155
- """The recipe's full config dict has keys like 'data', 'feats', 'net'."""
156
  if isinstance(hp, dict):
157
  if isinstance(hp.get("hparams"), dict) and "net" in hp["hparams"]:
158
  return hp["hparams"]
@@ -160,7 +156,6 @@ def find_config(hp):
160
  return hp
161
  return None
162
 
163
-
164
  def load_config(ckpt):
165
  cfg = find_config(ckpt.get("hyper_parameters"))
166
  if cfg is not None:
@@ -184,7 +179,6 @@ def load_config(ckpt):
184
  raise RuntimeError("Could not find the recipe config (data/feats/net) in the checkpoint or hparams.yaml")
185
  return cfg
186
 
187
-
188
  # --------------------------------------------------------------------------- #
189
  # 4. Build the model on CPU
190
  # --------------------------------------------------------------------------- #
@@ -237,17 +231,15 @@ try:
237
  print("Model loaded successfully!")
238
  except Exception:
239
  LOAD_ERROR = traceback.format_exc()
240
- print("Model loading error:
241
- ", LOAD_ERROR)
242
  finally:
243
  os.chdir(SPACE_ROOT)
244
 
245
-
246
  # --------------------------------------------------------------------------- #
247
  # 5. Inference
248
  # --------------------------------------------------------------------------- #
249
  def compute_embeddings(wav):
250
- """Frame-level BEATs embeddings for a (1, samples) tensor."""
251
  helper = getattr(model, "get_embeddings", None)
252
  if callable(helper):
253
  try:
@@ -258,9 +250,8 @@ def compute_embeddings(wav):
258
  out = pm.extract_features(wav, padding_mask=torch.zeros_like(wav).bool())
259
  return out[0] if isinstance(out, (tuple, list)) else out
260
 
261
-
262
  def forward_strong(wav):
263
- """Returns frame probabilities as a numpy array (frames, classes)."""
264
  net = model.sed_student
265
  feats = model.mel_spec(wav)
266
  emb = compute_embeddings(wav)
@@ -283,21 +274,17 @@ def forward_strong(wav):
283
  p = p.T # -> (frames, classes)
284
  return p
285
 
286
-
287
  def runs(mask):
288
- """Start/end frame indices (end exclusive) of contiguous True runs."""
289
  d = np.diff(np.concatenate([[0], mask.astype(np.int8), [0]]))
290
  return list(zip(np.where(d == 1)[0], np.where(d == -1)[0]))
291
 
292
-
293
  @gpu
294
  def process_audio(audio_file, threshold):
295
  if audio_file is None:
296
  return "Please upload an audio file."
297
  if model is None:
298
- return "Model failed to load at startup:
299
-
300
- " + (LOAD_ERROR or "unknown error")
301
 
302
  try:
303
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -330,14 +317,11 @@ def process_audio(audio_file, threshold):
330
  return "\n".join(f"[{s:6.2f}s - {e:6.2f}s] {n} (max prob {p:.2f})" for s, e, n, p in events)
331
 
332
  except Exception:
333
- return "Error during processing:
334
-
335
- " + traceback.format_exc()
336
  finally:
337
  if model is not None:
338
  model.to("cpu")
339
 
340
-
341
  demo = gr.Interface(
342
  fn=process_audio,
343
  inputs=[
 
1
+
2
  DCASE 2024 Task 4 baseline (CRNN + BEATs) demo for Hugging Face Spaces (ZeroGPU compatible).
3
 
4
  Files expected in the Space root:
5
  - last.ckpt (DCASE 2024 baseline checkpoint)
6
  - BEATs_iter3_plus_AS2M.pt (optional here: if absent, the app tries to download it)
7
+ \"\"\"
8
  import os
9
  import sys
10
  import math
 
83
  # 3. Helpers
84
  # --------------------------------------------------------------------------- #
85
  def call_with_supported_kwargs(fn, **kwargs):
86
+ \"\"\"Call fn with only the kwargs it accepts; required-but-unknown params get None.\"\"\"
87
  sig = inspect.signature(fn)
88
  accepts_var_kw = any(p.kind == p.VAR_KEYWORD for p in sig.parameters.values())
89
  out = {}
 
100
  out.setdefault(k, v)
101
  return fn(**out)
102
 
 
103
  def torch_load(path, **kw):
104
  try:
105
  return torch.load(path, map_location="cpu", weights_only=True, **kw)
106
  except Exception:
107
  return torch.load(path, map_location="cpu", weights_only=False, **kw)
108
 
 
109
  def file_ok(path, min_mb):
110
  return os.path.isfile(path) and os.path.getsize(path) > min_mb * 1024 * 1024
111
 
 
112
  def ensure_beats_checkpoint():
113
+ \"\"\"BEATs_iter3_plus_AS2M.pt (self-supervised, NOT the AudioSet-finetuned one).\"\"\"
114
  if file_ok(BEATS_PATH, 50):
115
  return BEATS_PATH
116
 
 
147
  "(github.com/microsoft/unilm/tree/master/beats) and upload it to the root of this Space."
148
  )
149
 
 
150
  def find_config(hp):
151
+ \"\"\"The recipe's full config dict has keys like 'data', 'feats', 'net'.\"\"\"
152
  if isinstance(hp, dict):
153
  if isinstance(hp.get("hparams"), dict) and "net" in hp["hparams"]:
154
  return hp["hparams"]
 
156
  return hp
157
  return None
158
 
 
159
  def load_config(ckpt):
160
  cfg = find_config(ckpt.get("hyper_parameters"))
161
  if cfg is not None:
 
179
  raise RuntimeError("Could not find the recipe config (data/feats/net) in the checkpoint or hparams.yaml")
180
  return cfg
181
 
 
182
  # --------------------------------------------------------------------------- #
183
  # 4. Build the model on CPU
184
  # --------------------------------------------------------------------------- #
 
231
  print("Model loaded successfully!")
232
  except Exception:
233
  LOAD_ERROR = traceback.format_exc()
234
+ print("Model loading error:\n", LOAD_ERROR)
 
235
  finally:
236
  os.chdir(SPACE_ROOT)
237
 
 
238
  # --------------------------------------------------------------------------- #
239
  # 5. Inference
240
  # --------------------------------------------------------------------------- #
241
  def compute_embeddings(wav):
242
+ \"\"\"Frame-level BEATs embeddings for a (1, samples) tensor.\"\"\"
243
  helper = getattr(model, "get_embeddings", None)
244
  if callable(helper):
245
  try:
 
250
  out = pm.extract_features(wav, padding_mask=torch.zeros_like(wav).bool())
251
  return out[0] if isinstance(out, (tuple, list)) else out
252
 
 
253
  def forward_strong(wav):
254
+ \"\"\"Returns frame probabilities as a numpy array (frames, classes).\"\"\"
255
  net = model.sed_student
256
  feats = model.mel_spec(wav)
257
  emb = compute_embeddings(wav)
 
274
  p = p.T # -> (frames, classes)
275
  return p
276
 
 
277
  def runs(mask):
278
+ \"\"\"Start/end frame indices (end exclusive) of contiguous True runs.\"\"\"
279
  d = np.diff(np.concatenate([[0], mask.astype(np.int8), [0]]))
280
  return list(zip(np.where(d == 1)[0], np.where(d == -1)[0]))
281
 
 
282
  @gpu
283
  def process_audio(audio_file, threshold):
284
  if audio_file is None:
285
  return "Please upload an audio file."
286
  if model is None:
287
+ return "Model failed to load at startup:\n\n" + (LOAD_ERROR or "unknown error")
 
 
288
 
289
  try:
290
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
317
  return "\n".join(f"[{s:6.2f}s - {e:6.2f}s] {n} (max prob {p:.2f})" for s, e, n, p in events)
318
 
319
  except Exception:
320
+ return "Error during processing:\n\n" + traceback.format_exc()
 
 
321
  finally:
322
  if model is not None:
323
  model.to("cpu")
324
 
 
325
  demo = gr.Interface(
326
  fn=process_audio,
327
  inputs=[