ylacombe commited on
Commit
fd22a1e
1 Parent(s): 0263898

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +47 -58
README.md CHANGED
@@ -34,97 +34,86 @@ You can perform all the above tasks from one single model, [`SeamlessM4TModel`](
34
  First, load the processor and a checkpoint of the model:
35
 
36
  ```python
37
- from transformers import AutoProcessor, SeamlessM4TModel
38
 
39
- processor = AutoProcessor.from_pretrained("ylacombe/hf-seamless-m4t-large")
40
- model = SeamlessM4TModel.from_pretrained("ylacombe/hf-seamless-m4t-large")
41
  ```
42
 
43
  You can seamlessly use this model on text or on audio, to generated either translated text or translated audio.
44
 
45
- ### Speech
46
-
47
- You can easily generate translated speech with [`SeamlessM4TModel.generate`](https://moon-ci-docs.huggingface.co/docs/transformers/pr_25693/en/model_doc/seamless_m4t#transformers.SeamlessM4TModel.generate). Here is an example showing how to generate speech from English to Russian.
48
 
49
  ```python
50
- inputs = processor(text = "Hello, my dog is cute", src_lang="eng", return_tensors="pt")
 
 
 
 
 
 
51
 
52
- audio_array = model.generate(**inputs, tgt_lang="rus")
53
- audio_array = audio_array[0].cpu().numpy().squeeze()
54
  ```
55
 
56
- You can also translate directly from a speech waveform. Here is an example from Arabic to English:
57
 
58
- ```python
59
- from datasets import load_dataset
60
 
61
- dataset = load_dataset("arabic_speech_corpus", split="test[0:1]")
62
- audio_sample = dataset["audio"][0]["array"]
63
- inputs = processor(audios = audio_sample, return_tensors="pt")
64
 
65
- audio_array = model.generate(**inputs, tgt_lang="rus")
66
- audio_array = audio_array[0].cpu().numpy().squeeze()
 
67
  ```
68
 
69
- Listen to the speech samples either in an ipynb notebook:
70
 
71
- ```python
72
- from IPython.display import Audio
73
 
74
- sampling_rate = model.config.sample_rate
75
- Audio(audio_array, rate=sampling_rate)
76
- ```
77
 
78
- Or save them as a `.wav` file using a third-party library, e.g. `scipy`:
 
 
 
79
 
80
- ```python
81
- import scipy
82
-
83
- sampling_rate = model.config.sample_rate
84
- scipy.io.wavfile.write("seamless_m4t_out.wav", rate=sampling_rate, data=audio_array)
85
  ```
86
 
87
- #### Tips
88
 
89
- [`SeamlessM4TModel`](https://moon-ci-docs.huggingface.co/docs/transformers/pr_25693/en/model_doc/seamless_m4t#transformers.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.
90
- For example, you can replace the previous snippet with the model dedicated to the S2ST task:
91
 
92
- ```python
93
- from transformers import SeamlessM4TForSpeechToSpeech
94
- model = SeamlessM4TForSpeechToSpeech.from_pretrained("ylacombe/hf-seamless-m4t-large")
95
- ```
96
 
 
 
97
 
98
- ### Text
 
 
 
99
 
100
- Similarly, you can generate translated text from text or audio files. This time, let's use the dedicated models as example.
101
 
102
  ```python
103
- from transformers import SeamlessM4TForSpeechToText
104
- model = SeamlessM4TForSpeechToText.from_pretrained("ylacombe/hf-seamless-m4t-large")
105
- audio_sample = dataset["audio"][0]["array"]
106
- inputs = processor(audios = audio_sample, return_tensors="pt")
107
-
108
- output_tokens = model.generate(**inputs, tgt_lang="fra")
109
- translated_text = processor.decode(output_tokens.tolist()[0], skip_special_tokens=True)
110
  ```
111
 
112
- And from text:
113
 
114
- ```python
115
- from transformers import SeamlessM4TForTextToText
116
- model = SeamlessM4TForTextToText.from_pretrained("ylacombe/hf-seamless-m4t-large")
117
- inputs = processor(text = "Hello, my dog is cute", src_lang="eng", return_tensors="pt")
118
 
119
- output_tokens = model.generate(**inputs, tgt_lang="fra")
120
- translated_text = processor.decode(output_tokens.tolist()[0], skip_special_tokens=True)
121
- ```
122
 
123
- #### Tips
124
 
125
- Three last tips:
126
 
127
- 1. [`SeamlessM4TModel`](https://moon-ci-docs.huggingface.co/docs/transformers/pr_25693/en/model_doc/seamless_m4t#transformers.SeamlessM4TModel) can generate text and/or speech. Pass `generate_speech=False` to [`SeamlessM4TModel.generate`](https://moon-ci-docs.huggingface.co/docs/transformers/pr_25693/en/model_doc/seamless_m4t#transformers.SeamlessM4TModel.generate) to only generate text. You also have the possibility to pass `return_intermediate_token_ids=True`, to get both text token ids and the generated speech.
128
- 2. You have the possibility to change the speaker used for speech synthesis with the `spkr_id` argument.
129
- 3. You can use different [generation strategies](./generation_strategies) for speech and text generation, e.g `.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.
130
 
 
 
34
  First, load the processor and a checkpoint of the model:
35
 
36
  ```python
37
+ >>> from transformers import AutoProcessor, SeamlessM4TModel
38
 
39
+ >>> processor = AutoProcessor.from_pretrained("facebook/hf-seamless-m4t-large")
40
+ >>> model = SeamlessM4TModel.from_pretrained("facebook/hf-seamless-m4t-large")
41
  ```
42
 
43
  You can seamlessly use this model on text or on audio, to generated either translated text or translated audio.
44
 
45
+ Here is how to use the processor to process text and audio:
 
 
46
 
47
  ```python
48
+ >>> # let's load an audio sample from an Arabic speech corpus
49
+ >>> from datasets import load_dataset
50
+ >>> dataset = load_dataset("arabic_speech_corpus", split="test", streaming=True)
51
+ >>> audio_sample = next(iter(dataset))["audio"]
52
+
53
+ >>> # now, process it
54
+ >>> audio_inputs = processor(audios=audio_sample["array"], return_tensors="pt")
55
 
56
+ >>> # now, process some English test as well
57
+ >>> text_inputs = processor(text = "Hello, my dog is cute", src_lang="eng", return_tensors="pt")
58
  ```
59
 
 
60
 
61
+ ### Speech
 
62
 
63
+ [`SeamlessM4TModel`] can *seamlessly* generate text or speech with few or no changes. Let's target Russian voice translation:
 
 
64
 
65
+ ```python
66
+ >>> audio_array_from_text = model.generate(**text_inputs, tgt_lang="rus")[0].cpu().numpy().squeeze()
67
+ >>> audio_array_from_audio = model.generate(**audio_inputs, tgt_lang="rus")[0].cpu().numpy().squeeze()
68
  ```
69
 
70
+ With basically the same code, I've translated English text and Arabic speech to Russian speech samples.
71
 
72
+ ### Text
 
73
 
74
+ Similarly, you can generate translated text from audio files or from text with the same model. You only have to pass `generate_speech=False` to [`SeamlessM4TModel.generate`].
75
+ This time, let's translate to French.
 
76
 
77
+ ```python
78
+ >>> # from audio
79
+ >>> output_tokens = model.generate(**audio_inputs, tgt_lang="fra", generate_speech=False)
80
+ >>> translated_text_from_audio = processor.decode(output_tokens[0].tolist()[0], skip_special_tokens=True)
81
 
82
+ >>> # from text
83
+ >>> output_tokens = model.generate(**text_inputs, tgt_lang="fra", generate_speech=False)
84
+ >>> translated_text_from_text = processor.decode(output_tokens[0].tolist()[0], skip_special_tokens=True)
 
 
85
  ```
86
 
87
+ ### Tips
88
 
 
 
89
 
90
+ #### 1. Use dedicated models
 
 
 
91
 
92
+ [`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.
93
+ 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:
94
 
95
+ ```python
96
+ >>> from transformers import SeamlessM4TForSpeechToSpeech
97
+ >>> model = SeamlessM4TForSpeechToSpeech.from_pretrained("facebook/hf-seamless-m4t-large")
98
+ ```
99
 
100
+ Or you can replace the text-to-text generation snippet with the model dedicated to the T2TT task, you only have to remove `generate_speech=False`.
101
 
102
  ```python
103
+ >>> from transformers import SeamlessM4TForTextToText
104
+ >>> model = SeamlessM4TForTextToText.from_pretrained("facebook/hf-seamless-m4t-large")
 
 
 
 
 
105
  ```
106
 
107
+ Feel free to try out [`SeamlessM4TForSpeechToText`] and [`SeamlessM4TForTextToSpeech`] as well.
108
 
109
+ #### 2. Change the speaker identity
 
 
 
110
 
111
+ You have the possibility to change the speaker used for speech synthesis with the `spkr_id` argument. Some `spkr_id` works better than other for some languages!
 
 
112
 
113
+ #### 3. Change the speaker identity
114
 
115
+ You can use different [generation strategies](./generation_strategies) for speech and text generation, e.g `.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.
116
 
117
+ #### 4. Generate speech and text at the same time
 
 
118
 
119
+ Use `return_intermediate_token_ids=True` with [`SeamlessM4TModel`] to return both speech and text !