Views
No views yet
SWRA (SWARA) is a Speech to Text Transformer (S2T) model trained by @binarybardakshat for automatic speech recognition (ASR).generate method to generate the transcripts by passing the speech features to the model.Speech2TextProcessor object uses torchaudio to extract the filter bank features. Make sure to install the torchaudio package before running this example.pip install transformers"[speech, sentencepiece]" or install the packages separately with pip install torchaudio sentencepiece.1import torch
2from transformers import Speech2TextProcessor, Speech2TextForConditionalGeneration
3from datasets import load_dataset
4
5model = Speech2TextForConditionalGeneration.from_pretrained("binarybardakshat/swra-swara")
6processor = Speech2TextProcessor.from_pretrained("binarybardakshat/swra-swara")
7
8ds = load_dataset(
9 "patrickvonplaten/librispeech_asr_dummy",
10 "clean",
11 split="validation"
12)
13
14input_features = processor(
15 ds[0]["audio"]["array"],
16 sampling_rate=16_000,
17 return_tensors="pt"
18).input_features # Batch size 1
19generated_ids = model.generate(input_features=input_features)
20
21transcription = processor.batch_decode(generated_ids)
22
23#### Evaluation on LibriSpeech Test
24
25The following script shows how to evaluate this model on the [LibriSpeech](https://huggingface.co/datasets/librispeech_asr)
26*"clean"* and *"other"* test dataset.
27
28```python
29from datasets import load_dataset
30from evaluate import load
31from transformers import Speech2TextForConditionalGeneration, Speech2TextProcessor
32
33librispeech_eval = load_dataset("librispeech_asr", "clean", split="test") # change to "other" for other test dataset
34wer = load("wer")
35
36model = Speech2TextForConditionalGeneration.from_pretrained("facebook/s2t-small-librispeech-asr").to("cuda")
37processor = Speech2TextProcessor.from_pretrained("facebook/s2t-small-librispeech-asr", do_upper_case=True)
38
39def map_to_pred(batch):
40 features = processor(batch["audio"]["array"], sampling_rate=16000, padding=True, return_tensors="pt")
41 input_features = features.input_features.to("cuda")
42 attention_mask = features.attention_mask.to("cuda")
43
44 gen_tokens = model.generate(input_features=input_features, attention_mask=attention_mask)
45 batch["transcription"] = processor.batch_decode(gen_tokens, skip_special_tokens=True)[0]
46 return batch
47
48result = librispeech_eval.map(map_to_pred, remove_columns=["audio"])
49
50print("WER:", wer.compute(predictions=result["transcription"], references=result["text"]))| "clean" | "other" |
|---|---|
| 4.3 | 9.0 |