eustlb HF Staff commited on
Commit
65267af
·
verified ·
1 Parent(s): 6f4cda2

Add 🤗 Transformers usage to model card

Browse files
Files changed (1) hide show
  1. README.md +122 -0
README.md CHANGED
@@ -47,6 +47,7 @@ datasets:
47
  - voxpopuli
48
  - europarl
49
  tags:
 
50
  - speech-recognition
51
  - cache-aware ASR
52
  - automatic-speech-recognition
@@ -366,6 +367,8 @@ pip install git+https://github.com/NVIDIA/NeMo.git@main#egg=nemo_toolkit[asr]
366
 
367
  The model is available for use in the NeMo Framework, and can be used as a pre-trained checkpoint for inference or for fine-tuning on another dataset.
368
 
 
 
369
  ### Loading the Model
370
 
371
  ```python
@@ -407,6 +410,125 @@ Latency is defined by the `att_context_size` param, where att_context_size = `{n
407
 
408
  Here, chunk size = current frame + right context; each chunk is processed in non-overlapping fashion.
409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410
  ### Input(s): <br>
411
 
412
  **Input Type(s):** Audio, Lang ID <br>
 
47
  - voxpopuli
48
  - europarl
49
  tags:
50
+ - transformers
51
  - speech-recognition
52
  - cache-aware ASR
53
  - automatic-speech-recognition
 
367
 
368
  The model is available for use in the NeMo Framework, and can be used as a pre-trained checkpoint for inference or for fine-tuning on another dataset.
369
 
370
+ You can also run it with [🤗 Transformers](https://github.com/huggingface/transformers) (more below).
371
+
372
  ### Loading the Model
373
 
374
  ```python
 
410
 
411
  Here, chunk size = current frame + right context; each chunk is processed in non-overlapping fashion.
412
 
413
+ ### 🤗 Transformers usage
414
+
415
+ This checkpoint also runs with [🤗 Transformers](https://github.com/huggingface/transformers). The target language is passed through the processor's `language` argument: a locale such as `en-US`/`de-DE`, a bare code such as `de`, or `auto` for automatic language detection. In `auto` mode the model appends an `<xx-XX>` language tag after the transcript's terminal punctuation; it is a special token, so decoding with `skip_special_tokens=True` strips it (clean transcript) and `skip_special_tokens=False` keeps it for language labeling.
416
+
417
+ Until Nemotron3_5Asr is part of an official Transformers release, install Transformers from source:
418
+
419
+ ```bash
420
+ pip install git+https://github.com/huggingface/transformers
421
+ ```
422
+
423
+ <details>
424
+ <summary>➡️ Offline transcription</summary>
425
+
426
+ ```python
427
+ from transformers import AutoModelForRNNT, AutoProcessor
428
+ from transformers.audio_utils import load_audio
429
+
430
+ model_id = "nvidia/nemotron-3.5-asr-streaming-0.6b"
431
+ revision = "refs/pr/20"
432
+ processor = AutoProcessor.from_pretrained(model_id, revision=revision)
433
+ model = AutoModelForRNNT.from_pretrained(model_id, revision=revision, device_map="auto")
434
+
435
+ audio = load_audio(
436
+ "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/bcn_weather.mp3",
437
+ sampling_rate=processor.feature_extractor.sampling_rate,
438
+ )
439
+
440
+ # Condition on a known language ...
441
+ inputs = processor(audio, sampling_rate=processor.feature_extractor.sampling_rate, language="en-US")
442
+ inputs.to(model.device, dtype=model.dtype)
443
+ output = model.generate(**inputs, return_dict_in_generate=True)
444
+ print(processor.decode(output.sequences, skip_special_tokens=True))
445
+
446
+ # ... or let the model detect it and keep the emitted <xx-XX> language tag.
447
+ inputs = processor(audio, sampling_rate=processor.feature_extractor.sampling_rate, language="auto")
448
+ inputs.to(model.device, dtype=model.dtype)
449
+ output = model.generate(**inputs, return_dict_in_generate=True)
450
+ print(processor.decode(output.sequences, skip_special_tokens=False))
451
+ ```
452
+ </details>
453
+
454
+ <details>
455
+ <summary>➡️ Streaming transcription</summary>
456
+
457
+ ```python
458
+ from threading import Thread
459
+ from transformers import AutoModelForRNNT, AutoProcessor, TextIteratorStreamer
460
+ from transformers.audio_utils import load_audio
461
+
462
+ model_id = "nvidia/nemotron-3.5-asr-streaming-0.6b"
463
+ revision = "refs/pr/20"
464
+ processor = AutoProcessor.from_pretrained(model_id, revision=revision)
465
+ model = AutoModelForRNNT.from_pretrained(model_id, revision=revision, device_map="auto")
466
+
467
+ processor.set_num_lookahead_tokens(6)
468
+ print(f"Streaming latency: {processor.streaming_latency_ms} ms")
469
+
470
+ # The language prompt rides along on every chunk; use a locale (e.g. "de-DE") or "auto".
471
+ language = "en-US"
472
+
473
+ sampling_rate = processor.feature_extractor.sampling_rate
474
+ audio = load_audio(
475
+ "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/obama.mp3",
476
+ sampling_rate=sampling_rate,
477
+ )
478
+
479
+ first_chunk_inputs = processor(
480
+ audio[: processor.num_samples_first_audio_chunk],
481
+ sampling_rate=sampling_rate,
482
+ is_streaming=True,
483
+ is_first_audio_chunk=True,
484
+ language=language,
485
+ return_tensors="pt",
486
+ )
487
+ first_chunk_inputs = first_chunk_inputs.to(model.device, dtype=model.dtype)
488
+
489
+
490
+ def input_features_generator():
491
+ yield first_chunk_inputs.input_features[:, : processor.num_mel_frames_first_audio_chunk, :]
492
+
493
+ mel_frame_idx = processor.num_mel_frames_first_audio_chunk
494
+ hop_length = processor.feature_extractor.hop_length
495
+ n_fft = processor.feature_extractor.n_fft
496
+
497
+ start_idx = mel_frame_idx * hop_length - n_fft // 2
498
+ while (end_idx := start_idx + processor.num_samples_per_audio_chunk) < audio.shape[0]:
499
+ inputs = processor(
500
+ audio[start_idx:end_idx],
501
+ sampling_rate=sampling_rate,
502
+ is_streaming=True,
503
+ is_first_audio_chunk=False,
504
+ language=language,
505
+ return_tensors="pt",
506
+ )
507
+ inputs = inputs.to(model.device, dtype=model.dtype)
508
+ yield inputs.input_features
509
+
510
+ mel_frame_idx += processor.num_mel_frames_per_audio_chunk
511
+ start_idx = mel_frame_idx * hop_length - n_fft // 2
512
+
513
+
514
+ streamer = TextIteratorStreamer(processor.tokenizer, skip_special_tokens=True)
515
+ generate_kwargs = {
516
+ **first_chunk_inputs,
517
+ "input_features": input_features_generator(),
518
+ "streamer": streamer,
519
+ }
520
+ thread = Thread(target=model.generate, kwargs=generate_kwargs)
521
+ thread.start()
522
+
523
+ print("Model output (streaming):", end=" ", flush=True)
524
+ for text_chunk in streamer:
525
+ print(text_chunk, end="", flush=True)
526
+ thread.join()
527
+ ```
528
+ </details>
529
+
530
+ For more details about usage, please refer to the [Transformers documentation](https://huggingface.co/docs/transformers/en/model_doc/nemotron3_5_asr).
531
+
532
  ### Input(s): <br>
533
 
534
  **Input Type(s):** Audio, Lang ID <br>