Luigi commited on
Commit
b26d94c
·
1 Parent(s): ab9b679

fix local model path err

Browse files
Files changed (1) hide show
  1. 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 model files from HuggingFace Hub if missing
27
- def download_model(repo_id: str, model_dir: Path):
28
- for subpath in FILES.values():
29
- local_path = model_dir / subpath
30
- if not local_path.exists():
31
- print(f"Downloading {subpath} from {repo_id}...")
32
- # Ensure parent directories exist
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
- # Initialize and download model on import
42
- download_model(REPO_ID, MODEL_DIR)
 
43
 
44
  # Build the online recognizer with int8 weights
45
  def create_recognizer():
46
  return sherpa_onnx.OnlineRecognizer.from_transducer(
47
- tokens=str(MODEL_DIR / FILES['tokens']),
48
- encoder=str(MODEL_DIR / FILES['encoder']),
49
- decoder=str(MODEL_DIR / FILES['decoder']),
50
- joiner=str(MODEL_DIR / FILES['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,