PRIYANSHUDHAKED commited on
Commit
e7f86fc
·
verified ·
1 Parent(s): fd3bc53

Delete src/miocodec/util.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/miocodec/util.py +0 -347
src/miocodec/util.py DELETED
@@ -1,347 +0,0 @@
1
- import logging
2
- import os
3
- from pathlib import Path
4
- from contextlib import contextmanager
5
-
6
- import torch
7
- import torch.nn as nn
8
-
9
- # Configure logger
10
- logger = logging.getLogger("miocodec")
11
- logger.setLevel(logging.INFO)
12
- handler = logging.StreamHandler()
13
- handler.setLevel(logging.INFO)
14
- handler.setFormatter(logging.Formatter("[%(asctime)s] %(levelname)s %(name)s: %(message)s"))
15
- logger.addHandler(handler)
16
-
17
-
18
- def get_logger() -> logging.Logger:
19
- return logger
20
-
21
-
22
- def freeze_modules(modules: list[nn.Module] | None):
23
- for module in modules:
24
- if module is not None:
25
- for param in module.parameters():
26
- param.requires_grad = False
27
-
28
-
29
- def _env_truthy(name: str) -> bool:
30
- value = os.environ.get(name, "").strip().lower()
31
- return value in {"1", "true", "yes", "on"}
32
-
33
-
34
- @contextmanager
35
- def _suppress_stderr(enabled: bool):
36
- if not enabled:
37
- yield
38
- return
39
- devnull = os.open(os.devnull, os.O_WRONLY)
40
- old_stderr = os.dup(2)
41
- try:
42
- os.dup2(devnull, 2)
43
- yield
44
- finally:
45
- os.dup2(old_stderr, 2)
46
- os.close(old_stderr)
47
- os.close(devnull)
48
-
49
-
50
- def _load_audio_internal(
51
- path: str, frame_offset: int | None = None, num_frames: int | None = None
52
- ) -> tuple[torch.Tensor, int]:
53
- # TorchAudio >= 2.9.0 removed decoding and encoding capabilities to TorchCodec.
54
- # See: https://github.com/pytorch/audio/issues/3902
55
- # waveform, sample_rate = torchaudio.load(path, frame_offset=frame_offset or 0, num_frames=num_frames or -1)
56
-
57
- import soundfile as sf
58
-
59
- suppress_warnings = _env_truthy("MIOCODEC_SUPPRESS_AUDIO_WARNINGS") or _env_truthy(
60
- "KANADE_TOKENIZER_SUPPRESS_AUDIO_WARNINGS"
61
- )
62
- with _suppress_stderr(suppress_warnings):
63
- with sf.SoundFile(path) as f:
64
- if frame_offset is not None:
65
- f.seek(frame_offset)
66
- frames = f.read(frames=num_frames or -1, dtype="float32", always_2d=True)
67
- waveform = torch.from_numpy(frames.T)
68
- sample_rate = f.samplerate
69
- return waveform, sample_rate
70
-
71
-
72
- def load_audio(audio_path: str, sample_rate: int = 24000) -> torch.Tensor:
73
- import torchaudio
74
-
75
- """Load and preprocess audio file."""
76
- waveform, sr = _load_audio_internal(audio_path)
77
-
78
- # Convert to mono if stereo
79
- if waveform.shape[0] > 1:
80
- waveform = torch.mean(waveform, dim=0, keepdim=True)
81
-
82
- # Resample if necessary
83
- if sr != sample_rate:
84
- resampler = torchaudio.transforms.Resample(sr, sample_rate)
85
- waveform = resampler(waveform)
86
-
87
- # Normalize waveform
88
- max_val = torch.max(torch.abs(waveform)) + 1e-8
89
- waveform = waveform / max_val # Normalize to [-1, 1]
90
-
91
- return waveform.squeeze(0) # Remove channel dimension
92
-
93
-
94
- class _PupuVocoderWrapper(nn.Module):
95
- def __init__(self, model: nn.Module):
96
- super().__init__()
97
- self.model = model
98
-
99
- def decode(self, mel: torch.Tensor) -> torch.Tensor:
100
- # PupuVocoder returns (B, 1, T). Match Vocos-style output (B, T).
101
- audio = self.model(mel)
102
- if audio.dim() == 3:
103
- audio = audio.squeeze(1)
104
- return audio
105
-
106
-
107
- def _iter_unique_paths(paths: list[Path]) -> list[Path]:
108
- seen: set[Path] = set()
109
- unique: list[Path] = []
110
- for path in paths:
111
- if path not in seen:
112
- seen.add(path)
113
- unique.append(path)
114
- return unique
115
-
116
-
117
- def _resolve_pupu_roots(afgen_root: str | None) -> list[Path]:
118
- roots: list[Path] = []
119
- if afgen_root is not None:
120
- root = Path(afgen_root)
121
- if not root.exists():
122
- raise FileNotFoundError(f"afgen_root not found: {afgen_root}")
123
- roots.append(root)
124
-
125
- internal_root = Path(__file__).resolve().parent / "pupu" / "assets"
126
- if internal_root.exists():
127
- roots.append(internal_root)
128
-
129
- try:
130
- file_path = Path(__file__).resolve()
131
- for parent in file_path.parents:
132
- candidate = parent / "AliasingFreeNeuralAudioSynthesis"
133
- if candidate.exists():
134
- roots.append(candidate)
135
- break
136
- except IndexError:
137
- pass
138
- cwd_candidate = Path.cwd() / "AliasingFreeNeuralAudioSynthesis"
139
- if cwd_candidate.exists():
140
- roots.append(cwd_candidate)
141
-
142
- return _iter_unique_paths(roots)
143
-
144
-
145
- def _select_pupu_checkpoint(checkpoint_path: str | None) -> Path | None:
146
- if checkpoint_path is None:
147
- return None
148
-
149
- path = Path(checkpoint_path)
150
- if path.is_file():
151
- return path
152
-
153
- if path.is_dir():
154
- model_path = path / "model.safetensors"
155
- if model_path.exists():
156
- return model_path
157
-
158
- subdirs = [p for p in path.iterdir() if p.is_dir()]
159
- if subdirs:
160
- # Names are zero-padded; lexicographic sort works for epoch/step.
161
- subdirs.sort(key=lambda p: p.name, reverse=True)
162
- candidate = subdirs[0] / "model.safetensors"
163
- if candidate.exists():
164
- return candidate
165
-
166
- raise FileNotFoundError(f"Unsupported checkpoint path: {checkpoint_path}")
167
-
168
-
169
- def _load_pupu_state_dict(checkpoint_path: Path) -> dict[str, torch.Tensor]:
170
- if checkpoint_path.suffix == ".safetensors":
171
- from safetensors.torch import load_file
172
-
173
- state_dict = load_file(str(checkpoint_path))
174
- else:
175
- pkg = torch.load(checkpoint_path, map_location="cpu")
176
- state_dict = pkg["generator"] if isinstance(pkg, dict) and "generator" in pkg else pkg
177
-
178
- if any(key.startswith("module.") for key in state_dict.keys()):
179
- state_dict = {key.replace("module.", "", 1): value for key, value in state_dict.items()}
180
- return state_dict
181
-
182
-
183
- def _strip_state_dict_prefix(state_dict: dict[str, torch.Tensor], prefix: str) -> dict[str, torch.Tensor]:
184
- if not prefix:
185
- return state_dict
186
- if not any(key.startswith(prefix) for key in state_dict):
187
- return state_dict
188
- return {key[len(prefix) :] if key.startswith(prefix) else key: value for key, value in state_dict.items()}
189
-
190
-
191
- def _hf_download_first(repo_id: str, candidates: list[str], revision: str | None) -> Path:
192
- from huggingface_hub import hf_hub_download
193
-
194
- last_error: Exception | None = None
195
- for filename in candidates:
196
- try:
197
- return Path(hf_hub_download(repo_id=repo_id, filename=filename, revision=revision))
198
- except Exception as exc: # pragma: no cover - hub errors are opaque
199
- last_error = exc
200
- raise FileNotFoundError(
201
- f"Could not find any of {candidates} in HuggingFace repo {repo_id}."
202
- ) from last_error
203
-
204
-
205
- def _hf_select_pupu_checkpoint(
206
- repo_id: str, checkpoint_path: str, revision: str | None
207
- ) -> Path:
208
- path = checkpoint_path.rstrip("/")
209
- if path.endswith((".safetensors", ".ckpt", ".pt", ".pth", ".bin")):
210
- return _hf_download_first(repo_id, [path], revision)
211
-
212
- candidates = [
213
- f"{path}/model.safetensors",
214
- f"{path}/model.pt",
215
- f"{path}/model.pth",
216
- f"{path}/model.bin",
217
- ]
218
- return _hf_download_first(repo_id, candidates, revision)
219
-
220
-
221
- def load_pupu_vocoder(
222
- config_path: str | None = None,
223
- checkpoint_path: str | None = None,
224
- hf_repo: str | None = None,
225
- hf_revision: str | None = None,
226
- hf_config_path: str | None = None,
227
- hf_checkpoint_path: str | None = None,
228
- afgen_root: str | None = None,
229
- device: str | torch.device | None = None,
230
- ) -> nn.Module:
231
- from miocodec.pupu.config import DEFAULT_PUPU_CONFIG, JsonHParams, load_config
232
- from miocodec.pupu.models.vocoders.gan.generator.pupuvocoder import PupuVocoder
233
-
234
- search_roots = _resolve_pupu_roots(afgen_root)
235
- default_root = search_roots[0] if search_roots else None
236
-
237
- if hf_repo is not None:
238
- if config_path is None:
239
- if hf_config_path is not None:
240
- config_path = str(_hf_download_first(hf_repo, [hf_config_path], hf_revision))
241
- else:
242
- config_candidates = [
243
- "egs/pupuvocoder/exp_config_pupuvocoder.json",
244
- "egs/pupuvocoder/exp_config_pupuvocoder_large.json",
245
- "egs/exp_config_pupuvocoder.json",
246
- "egs/exp_config_pupuvocoder_large.json",
247
- "exp_config_pupuvocoder.json",
248
- "exp_config_pupuvocoder_large.json",
249
- "config.json",
250
- ]
251
- config_path = str(_hf_download_first(hf_repo, config_candidates, hf_revision))
252
- if hf_checkpoint_path is not None:
253
- # Explicit HF checkpoint takes precedence over any local checkpoint_path.
254
- checkpoint_path = str(
255
- _hf_select_pupu_checkpoint(hf_repo, hf_checkpoint_path, hf_revision)
256
- )
257
- elif checkpoint_path is None:
258
- raise ValueError(
259
- "hf_repo is set but no checkpoint provided. "
260
- "Pass hf_checkpoint_path (e.g., 'pupuvocoder/checkpoint/epoch-XXXX...')."
261
- )
262
- if config_path is None:
263
- if default_root is not None:
264
- config_path = str(default_root / "egs/pupuvocoder/exp_config_pupuvocoder.json")
265
- else:
266
- cfg = JsonHParams(**DEFAULT_PUPU_CONFIG)
267
- model = PupuVocoder(cfg).eval()
268
- selected_ckpt = _select_pupu_checkpoint(checkpoint_path)
269
- if selected_ckpt is not None:
270
- state_dict = _load_pupu_state_dict(selected_ckpt)
271
- model.load_state_dict(state_dict, strict=False)
272
- else:
273
- logger.warning("Pupu-Vocoder loaded without checkpoint; outputs will be random.")
274
- if device is not None:
275
- model = model.to(device)
276
- return _PupuVocoderWrapper(model)
277
-
278
- cfg = load_config(config_path, search_roots=search_roots)
279
- model = PupuVocoder(cfg).eval()
280
-
281
- selected_ckpt = _select_pupu_checkpoint(checkpoint_path)
282
- if selected_ckpt is not None:
283
- state_dict = _load_pupu_state_dict(selected_ckpt)
284
- model.load_state_dict(state_dict, strict=False)
285
- else:
286
- logger.warning("Pupu-Vocoder loaded without checkpoint; outputs will be random.")
287
-
288
- if device is not None:
289
- model = model.to(device)
290
-
291
- return _PupuVocoderWrapper(model)
292
-
293
-
294
- def load_pupu_vocoder_state_dict(
295
- state_dict: dict[str, torch.Tensor],
296
- config_path: str | None = None,
297
- afgen_root: str | None = None,
298
- device: str | torch.device | None = None,
299
- ) -> nn.Module:
300
- """Instantiate a Pupu-Vocoder and load weights from an in-memory state dict."""
301
- if not state_dict:
302
- raise ValueError("Empty state_dict passed to load_pupu_vocoder_state_dict.")
303
-
304
- from miocodec.pupu.config import DEFAULT_PUPU_CONFIG, JsonHParams, load_config
305
- from miocodec.pupu.models.vocoders.gan.generator.pupuvocoder import PupuVocoder
306
-
307
- search_roots = _resolve_pupu_roots(afgen_root)
308
- default_root = search_roots[0] if search_roots else None
309
-
310
- if config_path is None and default_root is not None:
311
- config_path = str(default_root / "egs/pupuvocoder/exp_config_pupuvocoder.json")
312
-
313
- if config_path is None:
314
- cfg = JsonHParams(**DEFAULT_PUPU_CONFIG)
315
- else:
316
- cfg = load_config(config_path, search_roots=search_roots)
317
-
318
- model = PupuVocoder(cfg).eval()
319
- normalized_state = _strip_state_dict_prefix(state_dict, "model.")
320
- model.load_state_dict(normalized_state, strict=False)
321
-
322
- wrapper = _PupuVocoderWrapper(model)
323
- if device is not None:
324
- wrapper = wrapper.to(device)
325
- return wrapper
326
-
327
-
328
- def load_vocoder(backend: str = "pupu", **kwargs):
329
- if backend != "pupu":
330
- raise ValueError(
331
- "Only the 'pupu' vocoder backend is supported in this inference-only build."
332
- )
333
- return load_pupu_vocoder(**kwargs)
334
-
335
-
336
- def vocode(vocoder, mel_spectrogram: torch.Tensor) -> torch.Tensor:
337
- """Convert mel spectrogram to waveform using the selected vocoder.
338
- Args:
339
- vocoder (nn.Module): Vocoder with a decode() method.
340
- mel_spectrogram (torch.Tensor): Input mel spectrogram tensor (..., n_mels, frame).
341
- Returns:
342
- torch.Tensor: Generated audio waveform tensor (..., samples).
343
- """
344
- mel_spectrogram = mel_spectrogram.to(torch.float32) # Ensure mel spectrogram is in float32
345
- with torch.inference_mode():
346
- generated_waveform = vocoder.decode(mel_spectrogram)
347
- return generated_waveform