Spaces:
Running
Running
Commit
·
eaf7016
1
Parent(s):
f5a656a
Works
Browse filesSigned-off-by: Piotr Żelasko <[email protected]>
- app.py +81 -213
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -12,240 +12,108 @@ from nemo.collections.speechlm2 import SALM
|
|
| 12 |
from nemo.collections.asr.parts.utils.streaming_utils import FrameBatchMultiTaskAED
|
| 13 |
from nemo.collections.asr.parts.utils.transcribe_utils import get_buffered_pred_feat_multitaskAED
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
SAMPLE_RATE = 16000 # Hz
|
| 18 |
MAX_AUDIO_MINUTES = 10 # wont try to transcribe if longer than this
|
|
|
|
|
|
|
| 19 |
|
| 20 |
with device:
|
| 21 |
torch.set_default_dtype(torch.bfloat16) # speed up start-up time
|
| 22 |
-
model = SALM.from_pretrained("nvidia/canary-qwen-2.5b").bfloat16().eval().
|
| 23 |
torch.set_default_dtype(torch.float32)
|
| 24 |
|
| 25 |
-
feature_stride = model.cfg.preprocessor['window_stride']
|
| 26 |
-
model_stride_in_secs = feature_stride * 8 # 8 = model stride, which is 8 for FastConformer
|
| 27 |
-
|
| 28 |
-
#frame_asr = FrameBatchMultiTaskAED(
|
| 29 |
-
# asr_model=model,
|
| 30 |
-
# frame_len=40.0,
|
| 31 |
-
# total_buffer=40.0,
|
| 32 |
-
# batch_size=16,
|
| 33 |
-
#)
|
| 34 |
|
| 35 |
def as_batches(audio_filepath, utt_id):
|
| 36 |
rec = Recording.from_file(audio_filepath, recording_id=utt_id)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
|
| 48 |
|
| 49 |
-
def transcribe(audio_filepath
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
pred_text = []
|
| 54 |
for batch in as_batches(audio_filepath, str(utt_id)):
|
| 55 |
audio, audio_lens = batch.load_audio(collate=True)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# add logic to make sure dropdown menus only suggest valid combos
|
| 67 |
-
def on_src_or_tgt_lang_change(src_lang_value, tgt_lang_value, pnc_value):
|
| 68 |
-
"""Callback function for when src_lang or tgt_lang dropdown menus are changed.
|
| 69 |
-
|
| 70 |
-
Args:
|
| 71 |
-
src_lang_value(string), tgt_lang_value (string), pnc_value(bool) - the current
|
| 72 |
-
chosen "values" of each Gradio component
|
| 73 |
-
Returns:
|
| 74 |
-
src_lang, tgt_lang, pnc - these are the new Gradio components that will be displayed
|
| 75 |
-
|
| 76 |
-
Note: I found the required logic is easier to understand if you think about the possible src & tgt langs as
|
| 77 |
-
a matrix, e.g. with English, Spanish, French, German as the langs, and only transcription in the same language,
|
| 78 |
-
and X -> English and English -> X translation being allowed, the matrix looks like the diagram below ("Y" means it is
|
| 79 |
-
allowed to go into that state).
|
| 80 |
-
It is easier to understand the code if you think about which state you are in, given the current src_lang_value and
|
| 81 |
-
tgt_lang_value, and then which states you can go to from there.
|
| 82 |
-
|
| 83 |
-
tgt lang
|
| 84 |
-
- |EN |ES |FR |DE
|
| 85 |
-
------------------
|
| 86 |
-
EN| Y | Y | Y | Y
|
| 87 |
-
------------------
|
| 88 |
-
src ES| Y | Y | |
|
| 89 |
-
lang ------------------
|
| 90 |
-
FR| Y | | Y |
|
| 91 |
-
------------------
|
| 92 |
-
DE| Y | | | Y
|
| 93 |
-
"""
|
| 94 |
-
|
| 95 |
-
if src_lang_value == "English" and tgt_lang_value == "English":
|
| 96 |
-
# src_lang and tgt_lang can go anywhere
|
| 97 |
-
src_lang = gr.Dropdown(
|
| 98 |
-
choices=["English", "Spanish", "French", "German"],
|
| 99 |
-
value=src_lang_value,
|
| 100 |
-
label="Input audio is spoken in:"
|
| 101 |
-
)
|
| 102 |
-
tgt_lang = gr.Dropdown(
|
| 103 |
-
choices=["English", "Spanish", "French", "German"],
|
| 104 |
-
value=tgt_lang_value,
|
| 105 |
-
label="Transcribe in language:"
|
| 106 |
-
)
|
| 107 |
-
elif src_lang_value == "English":
|
| 108 |
-
# src is English & tgt is non-English
|
| 109 |
-
# => src can only be English or current tgt_lang_values
|
| 110 |
-
# & tgt can be anything
|
| 111 |
-
src_lang = gr.Dropdown(
|
| 112 |
-
choices=["English", tgt_lang_value],
|
| 113 |
-
value=src_lang_value,
|
| 114 |
-
label="Input audio is spoken in:"
|
| 115 |
-
)
|
| 116 |
-
tgt_lang = gr.Dropdown(
|
| 117 |
-
choices=["English", "Spanish", "French", "German"],
|
| 118 |
-
value=tgt_lang_value,
|
| 119 |
-
label="Transcribe in language:"
|
| 120 |
-
)
|
| 121 |
-
elif tgt_lang_value == "English":
|
| 122 |
-
# src is non-English & tgt is English
|
| 123 |
-
# => src can be anything
|
| 124 |
-
# & tgt can only be English or current src_lang_value
|
| 125 |
-
src_lang = gr.Dropdown(
|
| 126 |
-
choices=["English", "Spanish", "French", "German"],
|
| 127 |
-
value=src_lang_value,
|
| 128 |
-
label="Input audio is spoken in:"
|
| 129 |
-
)
|
| 130 |
-
tgt_lang = gr.Dropdown(
|
| 131 |
-
choices=["English", src_lang_value],
|
| 132 |
-
value=tgt_lang_value,
|
| 133 |
-
label="Transcribe in language:"
|
| 134 |
-
)
|
| 135 |
-
else:
|
| 136 |
-
# both src and tgt are non-English
|
| 137 |
-
# => both src and tgt can only be switch to English or themselves
|
| 138 |
-
src_lang = gr.Dropdown(
|
| 139 |
-
choices=["English", src_lang_value],
|
| 140 |
-
value=src_lang_value,
|
| 141 |
-
label="Input audio is spoken in:"
|
| 142 |
-
)
|
| 143 |
-
tgt_lang = gr.Dropdown(
|
| 144 |
-
choices=["English", tgt_lang_value],
|
| 145 |
-
value=tgt_lang_value,
|
| 146 |
-
label="Transcribe in language:"
|
| 147 |
-
)
|
| 148 |
-
# let pnc be anything if src_lang_value == tgt_lang_value, else fix to True
|
| 149 |
-
if src_lang_value == tgt_lang_value:
|
| 150 |
-
pnc = gr.Checkbox(
|
| 151 |
-
value=pnc_value,
|
| 152 |
-
label="Punctuation & Capitalization in transcript?",
|
| 153 |
-
interactive=True
|
| 154 |
-
)
|
| 155 |
-
else:
|
| 156 |
-
pnc = gr.Checkbox(
|
| 157 |
-
value=True,
|
| 158 |
-
label="Punctuation & Capitalization in transcript?",
|
| 159 |
-
interactive=False
|
| 160 |
-
)
|
| 161 |
-
return src_lang, tgt_lang, pnc
|
| 162 |
|
| 163 |
|
| 164 |
with gr.Blocks(
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
) as demo:
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
model_output_text_box = gr.Textbox(
|
| 219 |
-
label="Model Output",
|
| 220 |
-
elem_id="model_output_text_box",
|
| 221 |
-
)
|
| 222 |
-
|
| 223 |
-
with gr.Row():
|
| 224 |
-
|
| 225 |
-
gr.HTML(
|
| 226 |
-
"<p style='text-align: center'>"
|
| 227 |
-
"🐤 <a href='https://huggingface.co/nvidia/canary-qwen-2.5b' target='_blank'>Canary model</a> | "
|
| 228 |
-
"🧑💻 <a href='https://github.com/NVIDIA/NeMo' target='_blank'>NeMo Repository</a>"
|
| 229 |
-
"</p>"
|
| 230 |
-
)
|
| 231 |
-
|
| 232 |
-
go_button.click(
|
| 233 |
-
fn=transcribe,
|
| 234 |
-
inputs = [audio_file, src_lang, tgt_lang, pnc],
|
| 235 |
-
outputs = [model_output_text_box]
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
# call on_src_or_tgt_lang_change whenever src_lang or tgt_lang dropdown menus are changed
|
| 239 |
-
src_lang.change(
|
| 240 |
-
fn=on_src_or_tgt_lang_change,
|
| 241 |
-
inputs=[src_lang, tgt_lang, pnc],
|
| 242 |
-
outputs=[src_lang, tgt_lang, pnc],
|
| 243 |
-
)
|
| 244 |
-
tgt_lang.change(
|
| 245 |
-
fn=on_src_or_tgt_lang_change,
|
| 246 |
-
inputs=[src_lang, tgt_lang, pnc],
|
| 247 |
-
outputs=[src_lang, tgt_lang, pnc],
|
| 248 |
-
)
|
| 249 |
|
| 250 |
|
| 251 |
demo.queue()
|
|
|
|
| 12 |
from nemo.collections.asr.parts.utils.streaming_utils import FrameBatchMultiTaskAED
|
| 13 |
from nemo.collections.asr.parts.utils.transcribe_utils import get_buffered_pred_feat_multitaskAED
|
| 14 |
|
| 15 |
+
if torch.cuda.is_available():
|
| 16 |
+
device = torch.device("cuda")
|
| 17 |
+
else:
|
| 18 |
+
device = torch.device("cpu")
|
| 19 |
|
| 20 |
SAMPLE_RATE = 16000 # Hz
|
| 21 |
MAX_AUDIO_MINUTES = 10 # wont try to transcribe if longer than this
|
| 22 |
+
CHUNK_SECONDS = 40.0 # max audio length seen by the model
|
| 23 |
+
BATCH_SIZE = 4 # for parallel transcription of audio longer than CHUNK_SECONDS
|
| 24 |
|
| 25 |
with device:
|
| 26 |
torch.set_default_dtype(torch.bfloat16) # speed up start-up time
|
| 27 |
+
model = SALM.from_pretrained("nvidia/canary-qwen-2.5b").bfloat16().eval().to(device)
|
| 28 |
torch.set_default_dtype(torch.float32)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def as_batches(audio_filepath, utt_id):
|
| 32 |
rec = Recording.from_file(audio_filepath, recording_id=utt_id)
|
| 33 |
+
if rec.duration / 60.0 > MAX_AUDIO_MINUTES:
|
| 34 |
+
raise gr.Error(
|
| 35 |
+
f"This demo can transcribe up to {MAX_AUDIO_MINUTES} minutes of audio. "
|
| 36 |
+
"If you wish, you may trim the audio using the Audio viewer in Step 1 "
|
| 37 |
+
"(click on the scissors icon to start trimming audio)."
|
| 38 |
+
)
|
| 39 |
+
cut = rec.resample(SAMPLE_RATE).to_cut()
|
| 40 |
+
if cut.num_channels > 1:
|
| 41 |
+
cut = cut.to_mono(mono_downmix=True)
|
| 42 |
+
return DynamicCutSampler(cut.cut_into_windows(CHUNK_SECONDS), max_cuts=BATCH_SIZE)
|
| 43 |
|
| 44 |
|
| 45 |
+
def transcribe(audio_filepath):
|
| 46 |
+
if audio_filepath is None:
|
| 47 |
+
raise gr.Error("Please provide some input audio: either upload an audio file or use the microphone")
|
| 48 |
+
utt_id = uuid.uuid4()
|
| 49 |
pred_text = []
|
| 50 |
for batch in as_batches(audio_filepath, str(utt_id)):
|
| 51 |
audio, audio_lens = batch.load_audio(collate=True)
|
| 52 |
+
with torch.inference_mode():
|
| 53 |
+
output_ids = model.generate(
|
| 54 |
+
prompts=[[{"role": "user", "content": f"Transcribe the following: {model.audio_locator_tag}"}]] * len(batch),
|
| 55 |
+
audios=torch.as_tensor(audio).to(device, non_blocking=True),
|
| 56 |
+
audio_lens=torch.as_tensor(audio_lens).to(device, non_blocking=True),
|
| 57 |
+
max_new_tokens=256,
|
| 58 |
+
)
|
| 59 |
+
pred_text.extend(model.tokenizer.ids_to_text(oids) for oids in output_ids.cpu())
|
| 60 |
+
return ' '.join(pred_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
with gr.Blocks(
|
| 64 |
+
title="NeMo Canary-Qwen-2.5B Model",
|
| 65 |
+
css="""
|
| 66 |
+
textarea { font-size: 18px;}
|
| 67 |
+
#model_output_text_box span {
|
| 68 |
+
font-size: 18px;
|
| 69 |
+
font-weight: bold;
|
| 70 |
+
}
|
| 71 |
+
""",
|
| 72 |
+
theme=gr.themes.Default(text_size=gr.themes.sizes.text_lg) # make text slightly bigger (default is text_md )
|
| 73 |
) as demo:
|
| 74 |
|
| 75 |
+
gr.HTML("<h1 style='text-align: center'>NeMo Canary-Qwen-2.5B model: Transcribe audio</h1>")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column():
|
| 79 |
+
gr.HTML(
|
| 80 |
+
"<p><b>Step 1:</b> Upload an audio file or record with your microphone.</p>"
|
| 81 |
+
|
| 82 |
+
"<p style='color: #A0A0A0;'>This demo supports audio files up to 10 mins long. "
|
| 83 |
+
"You can transcribe longer files locally with NeMo. "
|
| 84 |
+
#"<a href='https://github.com/NVIDIA/NeMo/blob/main/examples/asr/asr_chunked_inference/aed/speech_to_text_aed_chunked_infer.py'>script</a>.</p>"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
audio_file = gr.Audio(sources=["microphone", "upload"], type="filepath")
|
| 88 |
+
|
| 89 |
+
with gr.Column():
|
| 90 |
+
|
| 91 |
+
gr.HTML("<p><b>Step 2:</b> Run the model.</p>")
|
| 92 |
+
|
| 93 |
+
go_button = gr.Button(
|
| 94 |
+
value="Run model",
|
| 95 |
+
variant="primary", # make "primary" so it stands out (default is "secondary")
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
model_output_text_box = gr.Textbox(
|
| 99 |
+
label="Model Output",
|
| 100 |
+
elem_id="model_output_text_box",
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
|
| 105 |
+
gr.HTML(
|
| 106 |
+
"<p style='text-align: center'>"
|
| 107 |
+
"🐤 <a href='https://huggingface.co/nvidia/canary-qwen-2.5b' target='_blank'>Canary model</a> | "
|
| 108 |
+
"🧑💻 <a href='https://github.com/NVIDIA/NeMo' target='_blank'>NeMo Repository</a>"
|
| 109 |
+
"</p>"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
go_button.click(
|
| 113 |
+
fn=transcribe,
|
| 114 |
+
inputs=[audio_file],
|
| 115 |
+
outputs=[model_output_text_box]
|
| 116 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
|
| 119 |
demo.queue()
|
requirements.txt
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
nemo_toolkit[asr] @ git+https://github.com/NVIDIA/NeMo.git
|
|
|
|
|
|
|
|
|
| 1 |
nemo_toolkit[asr] @ git+https://github.com/NVIDIA/NeMo.git
|
| 2 |
+
sacrebleu
|
| 3 |
+
seaborn
|