Spaces:
Running
Running
fix local model path err
Browse files- app/asr_worker.py +15 -29
app/asr_worker.py
CHANGED
@@ -1,21 +1,13 @@
|
|
1 |
import numpy as np
|
2 |
import sherpa_onnx
|
3 |
-
from pathlib import Path
|
4 |
import scipy.signal
|
5 |
from opencc import OpenCC
|
6 |
from huggingface_hub import hf_hub_download
|
7 |
|
8 |
converter = OpenCC('s2t')
|
9 |
|
10 |
-
|
11 |
-
def resample_audio(audio, orig_sr, target_sr):
|
12 |
-
return scipy.signal.resample_poly(audio, target_sr, orig_sr)
|
13 |
-
|
14 |
-
# HuggingFace repository ID and local model directory
|
15 |
REPO_ID = "pfluo/k2fsa-zipformer-chinese-english-mixed"
|
16 |
-
MODEL_DIR = Path("models") / REPO_ID.replace("/", "_")
|
17 |
-
|
18 |
-
# Map logical names to paths within the repo
|
19 |
FILES = {
|
20 |
"tokens": "data/lang_char_bpe/tokens.txt",
|
21 |
"encoder": "exp/encoder-epoch-99-avg-1.int8.onnx",
|
@@ -23,31 +15,25 @@ FILES = {
|
|
23 |
"joiner": "exp/joiner-epoch-99-avg-1.int8.onnx",
|
24 |
}
|
25 |
|
26 |
-
# Download
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
local_path.parent.mkdir(parents=True, exist_ok=True)
|
34 |
-
hf_hub_download(
|
35 |
-
repo_id=repo_id,
|
36 |
-
filename=subpath,
|
37 |
-
local_dir=str(model_dir),
|
38 |
-
local_dir_use_symlinks=False,
|
39 |
-
)
|
40 |
|
41 |
-
#
|
42 |
-
|
|
|
43 |
|
44 |
# Build the online recognizer with int8 weights
|
45 |
def create_recognizer():
|
46 |
return sherpa_onnx.OnlineRecognizer.from_transducer(
|
47 |
-
tokens=
|
48 |
-
encoder=
|
49 |
-
decoder=
|
50 |
-
joiner=
|
51 |
provider="cpu",
|
52 |
num_threads=1,
|
53 |
sample_rate=16000,
|
|
|
1 |
import numpy as np
|
2 |
import sherpa_onnx
|
|
|
3 |
import scipy.signal
|
4 |
from opencc import OpenCC
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
|
7 |
converter = OpenCC('s2t')
|
8 |
|
9 |
+
# ASR model repository and file paths
|
|
|
|
|
|
|
|
|
10 |
REPO_ID = "pfluo/k2fsa-zipformer-chinese-english-mixed"
|
|
|
|
|
|
|
11 |
FILES = {
|
12 |
"tokens": "data/lang_char_bpe/tokens.txt",
|
13 |
"encoder": "exp/encoder-epoch-99-avg-1.int8.onnx",
|
|
|
15 |
"joiner": "exp/joiner-epoch-99-avg-1.int8.onnx",
|
16 |
}
|
17 |
|
18 |
+
# Download and cache each file via HuggingFace Hub
|
19 |
+
LOCAL_PATHS = {}
|
20 |
+
for key, path in FILES.items():
|
21 |
+
LOCAL_PATHS[key] = hf_hub_download(
|
22 |
+
repo_id=REPO_ID,
|
23 |
+
filename=path,
|
24 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Audio resampling utility
|
27 |
+
def resample_audio(audio: np.ndarray, orig_sr: int, target_sr: int) -> np.ndarray:
|
28 |
+
return scipy.signal.resample_poly(audio, target_sr, orig_sr)
|
29 |
|
30 |
# Build the online recognizer with int8 weights
|
31 |
def create_recognizer():
|
32 |
return sherpa_onnx.OnlineRecognizer.from_transducer(
|
33 |
+
tokens=LOCAL_PATHS['tokens'],
|
34 |
+
encoder=LOCAL_PATHS['encoder'],
|
35 |
+
decoder=LOCAL_PATHS['decoder'],
|
36 |
+
joiner=LOCAL_PATHS['joiner'],
|
37 |
provider="cpu",
|
38 |
num_threads=1,
|
39 |
sample_rate=16000,
|