Spaces:
Running
Running
Fix from_hparams for speechbrain 1.0.3
Browse files- models/embedder.py +19 -7
models/embedder.py
CHANGED
|
@@ -39,37 +39,49 @@ class EcapaTDNNEmbedder:
|
|
| 39 |
return
|
| 40 |
|
| 41 |
try:
|
|
|
|
| 42 |
import speechbrain.utils.fetching as _fetching
|
| 43 |
-
import shutil as _shutil
|
| 44 |
-
from pathlib import Path as _Path
|
| 45 |
|
| 46 |
def _patched_link(src, dst, local_strategy):
|
|
|
|
| 47 |
dst = _Path(dst)
|
| 48 |
src = _Path(src)
|
| 49 |
dst.parent.mkdir(parents=True, exist_ok=True)
|
| 50 |
if dst.exists() or dst.is_symlink():
|
| 51 |
dst.unlink()
|
| 52 |
-
|
| 53 |
|
| 54 |
_fetching.link_with_strategy = _patched_link
|
| 55 |
|
| 56 |
from speechbrain.inference.classifiers import EncoderClassifier
|
|
|
|
| 57 |
logger.info(f"Loading ECAPA-TDNN from {self.MODEL_SOURCE}...")
|
| 58 |
|
| 59 |
-
savedir =
|
| 60 |
-
import os
|
| 61 |
os.makedirs(savedir, exist_ok=True)
|
| 62 |
|
| 63 |
self._model = EncoderClassifier.from_hparams(
|
| 64 |
source=self.MODEL_SOURCE,
|
| 65 |
savedir=savedir,
|
| 66 |
run_opts={"device": self.device},
|
|
|
|
| 67 |
)
|
| 68 |
self._model.eval()
|
| 69 |
logger.success("ECAPA-TDNN model loaded successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
except ImportError:
|
| 71 |
-
raise ImportError("SpeechBrain not installed.
|
| 72 |
-
|
| 73 |
def preprocess_audio(
|
| 74 |
self, audio: Union[np.ndarray, torch.Tensor], sample_rate: int
|
| 75 |
) -> torch.Tensor:
|
|
|
|
| 39 |
return
|
| 40 |
|
| 41 |
try:
|
| 42 |
+
import shutil
|
| 43 |
import speechbrain.utils.fetching as _fetching
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def _patched_link(src, dst, local_strategy):
|
| 46 |
+
from pathlib import Path as _Path
|
| 47 |
dst = _Path(dst)
|
| 48 |
src = _Path(src)
|
| 49 |
dst.parent.mkdir(parents=True, exist_ok=True)
|
| 50 |
if dst.exists() or dst.is_symlink():
|
| 51 |
dst.unlink()
|
| 52 |
+
shutil.copy2(str(src), str(dst))
|
| 53 |
|
| 54 |
_fetching.link_with_strategy = _patched_link
|
| 55 |
|
| 56 |
from speechbrain.inference.classifiers import EncoderClassifier
|
| 57 |
+
|
| 58 |
logger.info(f"Loading ECAPA-TDNN from {self.MODEL_SOURCE}...")
|
| 59 |
|
| 60 |
+
savedir = "/tmp/model_cache/ecapa_tdnn"
|
|
|
|
| 61 |
os.makedirs(savedir, exist_ok=True)
|
| 62 |
|
| 63 |
self._model = EncoderClassifier.from_hparams(
|
| 64 |
source=self.MODEL_SOURCE,
|
| 65 |
savedir=savedir,
|
| 66 |
run_opts={"device": self.device},
|
| 67 |
+
huggingface_cache_dir="/tmp/hf_cache",
|
| 68 |
)
|
| 69 |
self._model.eval()
|
| 70 |
logger.success("ECAPA-TDNN model loaded successfully.")
|
| 71 |
+
except TypeError as e:
|
| 72 |
+
# huggingface_cache_dir not supported in this version, try without
|
| 73 |
+
from speechbrain.inference.classifiers import EncoderClassifier
|
| 74 |
+
savedir = "/tmp/model_cache/ecapa_tdnn"
|
| 75 |
+
self._model = EncoderClassifier.from_hparams(
|
| 76 |
+
source=self.MODEL_SOURCE,
|
| 77 |
+
savedir=savedir,
|
| 78 |
+
run_opts={"device": self.device},
|
| 79 |
+
)
|
| 80 |
+
self._model.eval()
|
| 81 |
+
logger.success("ECAPA-TDNN model loaded (fallback).")
|
| 82 |
except ImportError:
|
| 83 |
+
raise ImportError("SpeechBrain not installed.")
|
| 84 |
+
|
| 85 |
def preprocess_audio(
|
| 86 |
self, audio: Union[np.ndarray, torch.Tensor], sample_rate: int
|
| 87 |
) -> torch.Tensor:
|