Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files14 unmodified lines1516171819202122232425262728293031322 unmodified lines3536373839402 unmodified lines434445464748495051134 unmodified lines18618718818919019119219314 unmodified linesos.environ.setdefault("HF_XET_HIGH_PERFORMANCE", "1")DATASETS_SERVER = "https://datasets-server.huggingface.co"MODEL_ID = "microsoft/wavlm-base-plus-sv"TARGET_SR = 16000_feature_extractor = None_model = None_embed_lock = threading.Lock() # WavLM inference is not thread-safedef _load_model():global _feature_extractor, _modelwith _embed_lock:if _model is None:_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(MODEL_ID)_model = WavLMForXVector.from_pretrained(MODEL_ID)2 unmodified linesdef _embed(audio_array: np.ndarray, sr: int, max_sec: int) -> np.ndarray:fe, mdl = _load_model()waveform = torch.tensor(audio_array, dtype=torch.float32)if waveform.ndim == 2:2 unmodified lineswaveform = torchaudio.functional.resample(waveform, sr, TARGET_SR)waveform = waveform[: max_sec * TARGET_SR]inputs = fe(waveform.numpy(), sampling_rate=TARGET_SR, return_tensors="pt")with _embed_lock:with torch.no_grad():out = mdl(**inputs)return out.embeddings.squeeze().numpy()134 unmodified lineserrors: list[str] = []done = 0# Process all repos in parallel (downloads are I/O bound)with concurrent.futures.ThreadPoolExecutor(max_workers=8) as ex:future_to_repo = {ex.submit(_process_repo, repo, int(samples_per_book), int(audio_sec), token): repofor repo in repos14 unmodified lines1516171819202122232425262728293031323334353637382 unmodified lines414243444546472 unmodified lines5051525354555657134 unmodified lines19219319419519619719819914 unmodified linesos.environ.setdefault("HF_XET_HIGH_PERFORMANCE", "1")# 1 PyTorch thread per worker — lets N_CPUS threads run inference in parallel# instead of one wide inference that blocks everyone else.torch.set_num_threads(1)N_CPUS = os.cpu_count() or 2DATASETS_SERVER = "https://datasets-server.huggingface.co"MODEL_ID = "microsoft/wavlm-base-plus-sv"TARGET_SR = 16000_feature_extractor = None_model = None_init_lock = threading.Lock() # only for one-time model initdef _load_model():global _feature_extractor, _modelwith _init_lock:if _model is None:_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(MODEL_ID)_model = WavLMForXVector.from_pretrained(MODEL_ID)2 unmodified linesdef _embed(audio_array: np.ndarray, sr: int, max_sec: int) -> np.ndarray:# Thread-safe: eval() + no_grad() — weights are read-only, GIL released in C++ opsfe, mdl = _load_model()waveform = torch.tensor(audio_array, dtype=torch.float32)if waveform.ndim == 2:2 unmodified lineswaveform = torchaudio.functional.resample(waveform, sr, TARGET_SR)waveform = waveform[: max_sec * TARGET_SR]inputs = fe(waveform.numpy(), sampling_rate=TARGET_SR, return_tensors="pt")with torch.no_grad():out = mdl(**inputs)return out.embeddings.squeeze().numpy()134 unmodified lineserrors: list[str] = []done = 0# Process repos in parallel — capped at N_CPUS since embed is the bottleneckwith concurrent.futures.ThreadPoolExecutor(max_workers=N_CPUS) as ex:future_to_repo = {ex.submit(_process_repo, repo, int(samples_per_book), int(audio_sec), token): repofor repo in repos
app.py+13-7