Views
No views yet
s2t-medium-mustc-multilingual-st is a Speech to Text Transformer (S2T) model trained for end-to-end Multilingual Speech Translation (ST).
The S2T model was proposed in this paper and released in
this repositorygenerate method to generate the
transcripts by passing the speech features to the model.eos_token_id is used as the decoder_start_token_id and
the target language id is forced as the first generated token. To force the target language id as the first
generated token, pass the forced_bos_token_id parameter to the generate() method. The following
example shows how to transate English speech to French and German text using the facebook/s2t-medium-mustc-multilingual-st
checkpoint.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 seperatly
with pip install torchaudio sentencepiece.1import torch
2from transformers import Speech2TextProcessor, Speech2TextForConditionalGeneration
3from datasets import load_dataset
4import soundfile as sf
5
6model = Speech2TextForConditionalGeneration.from_pretrained("facebook/s2t-medium-mustc-multilingual-st")
7processor = Speech2TextProcessor.from_pretrained("facebook/s2t-medium-mustc-multilingual-st")
8
9def map_to_array(batch):
10 speech, _ = sf.read(batch["file"])
11 batch["speech"] = speech
12 return batch
13
14ds = load_dataset("patrickvonplaten/librispeech_asr_dummy", "clean", split="validation")
15ds = ds.map(map_to_array)
16
17inputs = processor(ds["speech"][0], sampling_rate=16_000, return_tensors="pt")
18
19# translate English Speech To French Text
20generated_ids = model.generate(
21 input_ids=inputs["input_features"],
22 attention_mask=inputs["attention_mask"],
23 forced_bos_token_id=processor.tokenizer.lang_code_to_id["fr"]
24)
25translation_fr = processor.batch_decode(generated_ids)
26
27# translate English Speech To German Text
28generated_ids = model.generate(
29 input_ids=inputs["input_features"],
30 attention_mask=inputs["attention_mask"],
31 forced_bos_token_id=processor.tokenizer.lang_code_to_id["de"]
32)
33translation_de = processor.batch_decode(generated_ids, skip_special_tokens=True)| En-De | En-Nl | En-Es | En-Fr | En-It | En-Pt | En-Ro | En-Ru |
|---|---|---|---|---|---|---|---|
| 24.5 | 28.6 | 28.2 | 34.9 | 24.6 | 31.1 | 23.8 | 16.0 |
1@inproceedings{wang2020fairseqs2t,
2 title = {fairseq S2T: Fast Speech-to-Text Modeling with fairseq},
3 author = {Changhan Wang and Yun Tang and Xutai Ma and Anne Wu and Dmytro Okhonko and Juan Pino},
4 booktitle = {Proceedings of the 2020 Conference of the Asian Chapter of the Association for Computational Linguistics (AACL): System Demonstrations},
5 year = {2020},
6}
7