csukuangfj commited on
Commit
e62a940
·
1 Parent(s): 1a38224

fix models

Browse files
Files changed (3) hide show
  1. app.py +38 -1
  2. model.py +30 -2
  3. requirements.txt +3 -3
app.py CHANGED
@@ -33,9 +33,11 @@ import torchaudio
33
 
34
  from examples import examples
35
  from model import (
 
36
  decode,
37
  get_pretrained_model,
38
  get_punct_model,
 
39
  language_to_models,
40
  sample_rate,
41
  )
@@ -77,6 +79,7 @@ def build_html_output(s: str, style: str = "result_item_success"):
77
  def process_url(
78
  language: str,
79
  repo_id: str,
 
80
  decoding_method: str,
81
  num_active_paths: int,
82
  add_punct: str,
@@ -91,6 +94,7 @@ def process_url(
91
  in_filename=f.name,
92
  language=language,
93
  repo_id=repo_id,
 
94
  decoding_method=decoding_method,
95
  num_active_paths=num_active_paths,
96
  add_punct=add_punct,
@@ -103,6 +107,7 @@ def process_url(
103
  def process_uploaded_file(
104
  language: str,
105
  repo_id: str,
 
106
  decoding_method: str,
107
  num_active_paths: int,
108
  add_punct: str,
@@ -121,6 +126,7 @@ def process_uploaded_file(
121
  in_filename=in_filename,
122
  language=language,
123
  repo_id=repo_id,
 
124
  decoding_method=decoding_method,
125
  num_active_paths=num_active_paths,
126
  add_punct=add_punct,
@@ -133,6 +139,7 @@ def process_uploaded_file(
133
  def process_microphone(
134
  language: str,
135
  repo_id: str,
 
136
  decoding_method: str,
137
  num_active_paths: int,
138
  add_punct: str,
@@ -152,6 +159,7 @@ def process_microphone(
152
  in_filename=in_filename,
153
  language=language,
154
  repo_id=repo_id,
 
155
  decoding_method=decoding_method,
156
  num_active_paths=num_active_paths,
157
  add_punct=add_punct,
@@ -165,6 +173,7 @@ def process_microphone(
165
  def process(
166
  language: str,
167
  repo_id: str,
 
168
  decoding_method: str,
169
  num_active_paths: int,
170
  add_punct: str,
@@ -172,6 +181,7 @@ def process(
172
  ):
173
  MyPrint(f"language: {language}")
174
  MyPrint(f"repo_id: {repo_id}")
 
175
  MyPrint(f"decoding_method: {decoding_method}")
176
  MyPrint(f"num_active_paths: {num_active_paths}")
177
  MyPrint(f"in_filename: {in_filename}")
@@ -204,7 +214,8 @@ def process(
204
  num_active_paths=num_active_paths,
205
  )
206
 
207
- text = decode(recognizer, filename)
 
208
  if add_punct == "Yes" and language in ("Chinese", "四川话"):
209
  punct = get_punct_model()
210
  text = punct.add_punctuation(text)
@@ -291,6 +302,10 @@ def update_model_dropdown(language: str):
291
  raise ValueError(f"Unsupported language: {language}")
292
 
293
 
 
 
 
 
294
  demo = gr.Blocks(css=css)
295
 
296
 
@@ -308,12 +323,31 @@ with demo:
308
  label="Select a model",
309
  value=language_to_models[language_choices[0]][0],
310
  )
 
 
 
 
 
 
 
311
 
312
  language_radio.change(
313
  update_model_dropdown,
314
  inputs=language_radio,
315
  outputs=model_dropdown,
316
  )
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
  decoding_method_radio = gr.Radio(
319
  label="Decoding method",
@@ -402,6 +436,7 @@ with demo:
402
  inputs=[
403
  language_radio,
404
  model_dropdown,
 
405
  decoding_method_radio,
406
  num_active_paths_slider,
407
  punct_radio,
@@ -415,6 +450,7 @@ with demo:
415
  inputs=[
416
  language_radio,
417
  model_dropdown,
 
418
  decoding_method_radio,
419
  num_active_paths_slider,
420
  punct_radio,
@@ -428,6 +464,7 @@ with demo:
428
  inputs=[
429
  language_radio,
430
  model_dropdown,
 
431
  decoding_method_radio,
432
  num_active_paths_slider,
433
  punct_radio,
 
33
 
34
  from examples import examples
35
  from model import (
36
+ COHERE_TRANSCRIBE_LANGUAGE_CODES,
37
  decode,
38
  get_pretrained_model,
39
  get_punct_model,
40
+ is_cohere_transcribe_model,
41
  language_to_models,
42
  sample_rate,
43
  )
 
79
  def process_url(
80
  language: str,
81
  repo_id: str,
82
+ cohere_language: str,
83
  decoding_method: str,
84
  num_active_paths: int,
85
  add_punct: str,
 
94
  in_filename=f.name,
95
  language=language,
96
  repo_id=repo_id,
97
+ cohere_language=cohere_language,
98
  decoding_method=decoding_method,
99
  num_active_paths=num_active_paths,
100
  add_punct=add_punct,
 
107
  def process_uploaded_file(
108
  language: str,
109
  repo_id: str,
110
+ cohere_language: str,
111
  decoding_method: str,
112
  num_active_paths: int,
113
  add_punct: str,
 
126
  in_filename=in_filename,
127
  language=language,
128
  repo_id=repo_id,
129
+ cohere_language=cohere_language,
130
  decoding_method=decoding_method,
131
  num_active_paths=num_active_paths,
132
  add_punct=add_punct,
 
139
  def process_microphone(
140
  language: str,
141
  repo_id: str,
142
+ cohere_language: str,
143
  decoding_method: str,
144
  num_active_paths: int,
145
  add_punct: str,
 
159
  in_filename=in_filename,
160
  language=language,
161
  repo_id=repo_id,
162
+ cohere_language=cohere_language,
163
  decoding_method=decoding_method,
164
  num_active_paths=num_active_paths,
165
  add_punct=add_punct,
 
173
  def process(
174
  language: str,
175
  repo_id: str,
176
+ cohere_language: str,
177
  decoding_method: str,
178
  num_active_paths: int,
179
  add_punct: str,
 
181
  ):
182
  MyPrint(f"language: {language}")
183
  MyPrint(f"repo_id: {repo_id}")
184
+ MyPrint(f"cohere_language: {cohere_language}")
185
  MyPrint(f"decoding_method: {decoding_method}")
186
  MyPrint(f"num_active_paths: {num_active_paths}")
187
  MyPrint(f"in_filename: {in_filename}")
 
214
  num_active_paths=num_active_paths,
215
  )
216
 
217
+ stream_language = cohere_language if is_cohere_transcribe_model(repo_id) else None
218
+ text = decode(recognizer, filename, language=stream_language)
219
  if add_punct == "Yes" and language in ("Chinese", "四川话"):
220
  punct = get_punct_model()
221
  text = punct.add_punctuation(text)
 
302
  raise ValueError(f"Unsupported language: {language}")
303
 
304
 
305
+ def update_cohere_language_visibility(repo_id: str):
306
+ return gr.update(visible=is_cohere_transcribe_model(repo_id))
307
+
308
+
309
  demo = gr.Blocks(css=css)
310
 
311
 
 
323
  label="Select a model",
324
  value=language_to_models[language_choices[0]][0],
325
  )
326
+ cohere_language_dropdown = gr.Dropdown(
327
+ choices=COHERE_TRANSCRIBE_LANGUAGE_CODES,
328
+ value="en",
329
+ label="Cohere Transcribe language code",
330
+ info="Shown only for Cohere Transcribe. Select the language of the input audio.",
331
+ visible=False,
332
+ )
333
 
334
  language_radio.change(
335
  update_model_dropdown,
336
  inputs=language_radio,
337
  outputs=model_dropdown,
338
  )
339
+ language_radio.change(
340
+ lambda language: gr.update(
341
+ visible=is_cohere_transcribe_model(language_to_models[language][0])
342
+ ),
343
+ inputs=language_radio,
344
+ outputs=cohere_language_dropdown,
345
+ )
346
+ model_dropdown.change(
347
+ update_cohere_language_visibility,
348
+ inputs=model_dropdown,
349
+ outputs=cohere_language_dropdown,
350
+ )
351
 
352
  decoding_method_radio = gr.Radio(
353
  label="Decoding method",
 
436
  inputs=[
437
  language_radio,
438
  model_dropdown,
439
+ cohere_language_dropdown,
440
  decoding_method_radio,
441
  num_active_paths_slider,
442
  punct_radio,
 
450
  inputs=[
451
  language_radio,
452
  model_dropdown,
453
+ cohere_language_dropdown,
454
  decoding_method_radio,
455
  num_active_paths_slider,
456
  punct_radio,
 
464
  inputs=[
465
  language_radio,
466
  model_dropdown,
467
+ cohere_language_dropdown,
468
  decoding_method_radio,
469
  num_active_paths_slider,
470
  punct_radio,
model.py CHANGED
@@ -16,7 +16,7 @@
16
 
17
  import os
18
  from functools import lru_cache
19
- from typing import Union
20
 
21
  from pathlib import Path
22
  import torch
@@ -113,8 +113,11 @@ def decode_online_recognizer(
113
  def decode_offline_recognizer_sherpa_onnx(
114
  recognizer: sherpa_onnx.OfflineRecognizer,
115
  filename: str,
 
116
  ) -> str:
117
  s = recognizer.create_stream()
 
 
118
  samples, sample_rate = read_wave(filename)
119
  s.accept_waveform(sample_rate, samples)
120
  recognizer.decode_stream(s)
@@ -154,19 +157,40 @@ def decode(
154
  sherpa_onnx.OnlineRecognizer,
155
  ],
156
  filename: str,
 
157
  ) -> str:
158
  if isinstance(recognizer, sherpa.OfflineRecognizer):
159
  return decode_offline_recognizer(recognizer, filename)
160
  elif isinstance(recognizer, sherpa.OnlineRecognizer):
161
  return decode_online_recognizer(recognizer, filename)
162
  elif isinstance(recognizer, sherpa_onnx.OfflineRecognizer):
163
- return decode_offline_recognizer_sherpa_onnx(recognizer, filename)
 
 
164
  elif isinstance(recognizer, sherpa_onnx.OnlineRecognizer):
165
  return decode_online_recognizer_sherpa_onnx(recognizer, filename)
166
  else:
167
  raise ValueError(f"Unknown recognizer type {type(recognizer)}")
168
 
169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  @lru_cache(maxsize=30)
171
  def get_pretrained_model(
172
  repo_id: str,
@@ -2562,6 +2586,10 @@ cohere_transcribe_14_languages_models = {
2562
  "csukuangfj2/sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01": _get_cohere_transcribe,
2563
  }
2564
 
 
 
 
 
2565
  qwen3_asr_52_languages_models = {
2566
  "csukuangfj2/sherpa-onnx-qwen3-asr-0.6B-int8-2026-03-25": _get_qwen3_asr,
2567
  }
 
16
 
17
  import os
18
  from functools import lru_cache
19
+ from typing import Optional, Union
20
 
21
  from pathlib import Path
22
  import torch
 
113
  def decode_offline_recognizer_sherpa_onnx(
114
  recognizer: sherpa_onnx.OfflineRecognizer,
115
  filename: str,
116
+ language: Optional[str] = None,
117
  ) -> str:
118
  s = recognizer.create_stream()
119
+ if language:
120
+ s.set_option("language", language)
121
  samples, sample_rate = read_wave(filename)
122
  s.accept_waveform(sample_rate, samples)
123
  recognizer.decode_stream(s)
 
157
  sherpa_onnx.OnlineRecognizer,
158
  ],
159
  filename: str,
160
+ language: Optional[str] = None,
161
  ) -> str:
162
  if isinstance(recognizer, sherpa.OfflineRecognizer):
163
  return decode_offline_recognizer(recognizer, filename)
164
  elif isinstance(recognizer, sherpa.OnlineRecognizer):
165
  return decode_online_recognizer(recognizer, filename)
166
  elif isinstance(recognizer, sherpa_onnx.OfflineRecognizer):
167
+ return decode_offline_recognizer_sherpa_onnx(
168
+ recognizer, filename, language=language
169
+ )
170
  elif isinstance(recognizer, sherpa_onnx.OnlineRecognizer):
171
  return decode_online_recognizer_sherpa_onnx(recognizer, filename)
172
  else:
173
  raise ValueError(f"Unknown recognizer type {type(recognizer)}")
174
 
175
 
176
+ COHERE_TRANSCRIBE_LANGUAGE_CODES = [
177
+ "en",
178
+ "fr",
179
+ "de",
180
+ "it",
181
+ "es",
182
+ "pt",
183
+ "el",
184
+ "nl",
185
+ "pl",
186
+ "zh",
187
+ "ja",
188
+ "ko",
189
+ "vi",
190
+ "ar",
191
+ ]
192
+
193
+
194
  @lru_cache(maxsize=30)
195
  def get_pretrained_model(
196
  repo_id: str,
 
2586
  "csukuangfj2/sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01": _get_cohere_transcribe,
2587
  }
2588
 
2589
+
2590
+ def is_cohere_transcribe_model(repo_id: str) -> bool:
2591
+ return repo_id in cohere_transcribe_14_languages_models
2592
+
2593
  qwen3_asr_52_languages_models = {
2594
  "csukuangfj2/sherpa-onnx-qwen3-asr-0.6B-int8-2026-03-25": _get_qwen3_asr,
2595
  }
requirements.txt CHANGED
@@ -10,7 +10,7 @@ numpy<2
10
 
11
  huggingface_hub
12
 
13
- # https://huggingface.co/csukuangfj2/sherpa-onnx-wheels/resolve/main/cpu/1.12.27/sherpa_onnx_core-1.12.27-py3-none-manylinux2014_x86_64.whl
14
- # https://huggingface.co/csukuangfj2/sherpa-onnx-wheels/resolve/main/cpu/1.12.27/sherpa_onnx-1.12.27-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
15
 
16
- sherpa-onnx>=1.12.35
 
10
 
11
  huggingface_hub
12
 
13
+ https://huggingface.co/csukuangfj2/sherpa-onnx-wheels/resolve/main/cpu/1.12.35/sherpa_onnx_core-1.12.35-py3-none-manylinux2014_x86_64.whl
14
+ https://huggingface.co/csukuangfj2/sherpa-onnx-wheels/resolve/main/cpu/1.12.35/sherpa_onnx-1.12.35-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
15
 
16
+ # sherpa-onnx>=1.12.35