JacobLinCool commited on
Commit
7ff1228
·
verified ·
1 Parent(s): b137cbd

fix build: torch 2.8.0, python 3.12.12, module-level load

Browse files
Files changed (3) hide show
  1. README.md +1 -0
  2. app.py +29 -21
  3. requirements.txt +1 -9
README.md CHANGED
@@ -5,6 +5,7 @@ colorFrom: green
5
  colorTo: indigo
6
  sdk: gradio
7
  app_file: app.py
 
8
  pinned: false
9
  short_description: "Taiwan-Mandarin ASR: Traditional + zh-en code-switch"
10
  models:
 
5
  colorTo: indigo
6
  sdk: gradio
7
  app_file: app.py
8
+ python_version: "3.12.12"
9
  pinned: false
10
  short_description: "Taiwan-Mandarin ASR: Traditional + zh-en code-switch"
11
  models:
app.py CHANGED
@@ -1,18 +1,18 @@
1
  """TEA-ASR demo (Hugging Face Spaces, ZeroGPU). Taiwan-Mandarin ASR: Traditional script + Taiwanese lexicon +
2
  Mandarin-English code-switch, adapted from Qwen3-ASR. No runtime OpenCC (the Traditional decode is baked into the
3
- model's own tokenizer). `import spaces` MUST come before torch for ZeroGPU."""
 
 
 
 
4
  import os
5
  import spaces
 
6
  import gradio as gr
 
 
7
 
8
- MODELS = {
9
- "TEA-ASR-1.7B (best)": "JacobLinCool/TEA-ASR-1.7B",
10
- "TEA-ASR-0.6B (fast)": "JacobLinCool/TEA-ASR-0.6B",
11
- }
12
- LANGS = ["auto", "Chinese", "English"]
13
- _cache = {}
14
-
15
- # private model repos -> authenticate from the HF_TOKEN Space secret (also lets transformers find it)
16
  if os.environ.get("HF_TOKEN"):
17
  try:
18
  from huggingface_hub import login
@@ -20,26 +20,34 @@ if os.environ.get("HF_TOKEN"):
20
  except Exception as e:
21
  print("HF login failed:", e)
22
 
 
 
 
 
 
23
 
24
- def _load(repo):
25
- import torch
 
26
  from qwen_asr import Qwen3ASRModel
27
- return Qwen3ASRModel.from_pretrained(repo, dtype=torch.bfloat16, device_map="cuda:0")
 
 
 
 
28
 
29
 
30
  @spaces.GPU(duration=120)
31
  def transcribe(audio_path, model_choice, language, context):
 
 
 
32
  if not audio_path:
33
  return "請提供音訊 / Please provide audio."
34
- import librosa, numpy as np
35
- repo = MODELS[model_choice]
36
- if repo not in _cache:
37
- _cache[repo] = _load(repo)
38
- model = _cache[repo]
39
  wav, _ = librosa.load(audio_path, sr=16000, mono=True)
40
  lang = None if language == "auto" else language
41
- out = model.transcribe(audio=[(np.asarray(wav, dtype="float32"), 16000)],
42
- context=(context or ""), language=lang)[0]
43
  return out.text
44
 
45
 
@@ -53,14 +61,14 @@ DESC = (
53
  "**TEA-ASR (Taiwan Everyday Audio)** — Traditional-script + Taiwanese-lexicon ASR with robust "
54
  "Mandarin–English code-switch, adapted from Qwen3-ASR with a tokenizer-first procedure. "
55
  "Output is Traditional Chinese (軟體/網路/影片…) with **no runtime OpenCC**. "
56
- "Pick a model, optionally set the language hint to *Chinese* (best for Taiwan speech), and transcribe."
57
  )
58
 
59
  demo = gr.Interface(
60
  fn=transcribe,
61
  inputs=[
62
  gr.Audio(type="filepath", label="Audio (upload or record)"),
63
- gr.Dropdown(list(MODELS), value="TEA-ASR-1.7B (best)", label="Model"),
64
  gr.Dropdown(LANGS, value="Chinese", label="Language hint"),
65
  gr.Textbox(label="Context / hotwords (optional)", placeholder="例如:台積電 員工 名稱…"),
66
  ],
 
1
  """TEA-ASR demo (Hugging Face Spaces, ZeroGPU). Taiwan-Mandarin ASR: Traditional script + Taiwanese lexicon +
2
  Mandarin-English code-switch, adapted from Qwen3-ASR. No runtime OpenCC (the Traditional decode is baked into the
3
+ model's own tokenizer).
4
+
5
+ ZeroGPU rules followed: `import spaces` before torch; models placed on cuda at MODULE level (CUDA-emulated at
6
+ startup); GPU-dependent fn decorated with @spaces.GPU. torch pinned to a ZeroGPU-supported version (see
7
+ requirements.txt) and Python pinned to 3.12 (see README)."""
8
  import os
9
  import spaces
10
+ import torch
11
  import gradio as gr
12
+ import numpy as np
13
+ import librosa
14
 
15
+ # private model repos -> need an HF_TOKEN secret (read) in the Space settings
 
 
 
 
 
 
 
16
  if os.environ.get("HF_TOKEN"):
17
  try:
18
  from huggingface_hub import login
 
20
  except Exception as e:
21
  print("HF login failed:", e)
22
 
23
+ REPOS = {
24
+ "TEA-ASR-1.7B (best)": "JacobLinCool/TEA-ASR-1.7B",
25
+ "TEA-ASR-0.6B (fast)": "JacobLinCool/TEA-ASR-0.6B",
26
+ }
27
+ LANGS = ["auto", "Chinese", "English"]
28
 
29
+ # load both models on cuda at module level (recommended ZeroGPU pattern)
30
+ MODELS, LOAD_ERR = {}, None
31
+ try:
32
  from qwen_asr import Qwen3ASRModel
33
+ for _name, _repo in REPOS.items():
34
+ MODELS[_name] = Qwen3ASRModel.from_pretrained(_repo, dtype=torch.bfloat16, device_map="cuda:0")
35
+ except Exception as e:
36
+ LOAD_ERR = repr(e)
37
+ print("model load failed:", LOAD_ERR)
38
 
39
 
40
  @spaces.GPU(duration=120)
41
  def transcribe(audio_path, model_choice, language, context):
42
+ if LOAD_ERR:
43
+ return ("Model load failed. If the models are private, add an HF_TOKEN secret (read) in this Space's "
44
+ f"Settings.\n\n{LOAD_ERR}")
45
  if not audio_path:
46
  return "請提供音訊 / Please provide audio."
 
 
 
 
 
47
  wav, _ = librosa.load(audio_path, sr=16000, mono=True)
48
  lang = None if language == "auto" else language
49
+ out = MODELS[model_choice].transcribe(
50
+ audio=[(np.asarray(wav, dtype="float32"), 16000)], context=(context or ""), language=lang)[0]
51
  return out.text
52
 
53
 
 
61
  "**TEA-ASR (Taiwan Everyday Audio)** — Traditional-script + Taiwanese-lexicon ASR with robust "
62
  "Mandarin–English code-switch, adapted from Qwen3-ASR with a tokenizer-first procedure. "
63
  "Output is Traditional Chinese (軟體/網路/影片…) with **no runtime OpenCC**. "
64
+ "Set the language hint to *Chinese* for Taiwan speech (best results)."
65
  )
66
 
67
  demo = gr.Interface(
68
  fn=transcribe,
69
  inputs=[
70
  gr.Audio(type="filepath", label="Audio (upload or record)"),
71
+ gr.Dropdown(list(REPOS), value="TEA-ASR-1.7B (best)", label="Model"),
72
  gr.Dropdown(LANGS, value="Chinese", label="Language hint"),
73
  gr.Textbox(label="Context / hotwords (optional)", placeholder="例如:台積電 員工 名稱…"),
74
  ],
requirements.txt CHANGED
@@ -1,10 +1,2 @@
1
- spaces
2
- torch==2.4.0
3
- transformers==4.57.6
4
- tokenizers==0.22.2
5
  qwen-asr==0.0.6
6
- qwen-omni-utils==0.0.9
7
- accelerate
8
- soundfile
9
- librosa
10
- numpy
 
1
+ torch==2.8.0
 
 
 
2
  qwen-asr==0.0.6