Views
No views yet
SeamlessM4TModel, but each task also has its own dedicated sub-model.1>>> from transformers import AutoProcessor, SeamlessM4TModel
2
3>>> processor = AutoProcessor.from_pretrained("facebook/hf-seamless-m4t-large")
4>>> model = SeamlessM4TModel.from_pretrained("facebook/hf-seamless-m4t-large")1>>> # let's load an audio sample from an Arabic speech corpus
2>>> from datasets import load_dataset
3>>> dataset = load_dataset("arabic_speech_corpus", split="test", streaming=True)
4>>> audio_sample = next(iter(dataset))["audio"]
5
6>>> # now, process it
7>>> audio_inputs = processor(audios=audio_sample["array"], return_tensors="pt")
8
9>>> # now, process some English test as well
10>>> text_inputs = processor(text = "Hello, my dog is cute", src_lang="eng", return_tensors="pt")SeamlessM4TModel can seamlessly generate text or speech with few or no changes. Let's target Russian voice translation:1>>> audio_array_from_text = model.generate(**text_inputs, tgt_lang="rus")[0].cpu().numpy().squeeze()
2>>> audio_array_from_audio = model.generate(**audio_inputs, tgt_lang="rus")[0].cpu().numpy().squeeze()generate_speech=False to SeamlessM4TModel.generate.
This time, let's translate to French.1>>> # from audio
2>>> output_tokens = model.generate(**audio_inputs, tgt_lang="fra", generate_speech=False)
3>>> translated_text_from_audio = processor.decode(output_tokens[0].tolist(), skip_special_tokens=True)
4
5>>> # from text
6>>> output_tokens = model.generate(**text_inputs, tgt_lang="fra", generate_speech=False)
7>>> translated_text_from_text = processor.decode(output_tokens[0].tolist(), skip_special_tokens=True)SeamlessM4TModel is transformers top level model to generate speech and text, but you can also use dedicated models that perform the task without additional components, thus reducing the memory footprint.
For example, you can replace the audio-to-audio generation snippet with the model dedicated to the S2ST task, the rest is exactly the same code:1>>> from transformers import SeamlessM4TForSpeechToSpeech
2>>> model = SeamlessM4TForSpeechToSpeech.from_pretrained("facebook/hf-seamless-m4t-large")generate_speech=False.1>>> from transformers import SeamlessM4TForTextToText
2>>> model = SeamlessM4TForTextToText.from_pretrained("facebook/hf-seamless-m4t-large")SeamlessM4TForSpeechToText and SeamlessM4TForTextToSpeech as well.spkr_id argument. Some spkr_id works better than other for some languages!.generate(input_ids=input_ids, text_num_beams=4, speech_do_sample=True) which will successively perform beam-search decoding on the text model, and multinomial sampling on the speech model.return_intermediate_token_ids=True with SeamlessM4TModel to return both speech and text !