Update README.md
Browse files
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("
|
| 40 |
-
model = SeamlessM4TModel.from_pretrained("
|
| 41 |
```
|
| 42 |
|
| 43 |
You can seamlessly use this model on text or on audio, to generated either translated text or translated audio.
|
| 44 |
|
| 45 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
```
|
| 55 |
|
| 56 |
-
You can also translate directly from a speech waveform. Here is an example from Arabic to English:
|
| 57 |
|
| 58 |
-
|
| 59 |
-
from datasets import load_dataset
|
| 60 |
|
| 61 |
-
|
| 62 |
-
audio_sample = dataset["audio"][0]["array"]
|
| 63 |
-
inputs = processor(audios = audio_sample, return_tensors="pt")
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
```
|
| 68 |
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
from IPython.display import Audio
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
```
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 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 |
-
|
| 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 |
-
|
| 93 |
-
from transformers import SeamlessM4TForSpeechToSpeech
|
| 94 |
-
model = SeamlessM4TForSpeechToSpeech.from_pretrained("ylacombe/hf-seamless-m4t-large")
|
| 95 |
-
```
|
| 96 |
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
|
| 102 |
```python
|
| 103 |
-
from transformers import
|
| 104 |
-
model =
|
| 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 |
-
|
| 113 |
|
| 114 |
-
|
| 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 |
-
|
| 120 |
-
translated_text = processor.decode(output_tokens.tolist()[0], skip_special_tokens=True)
|
| 121 |
-
```
|
| 122 |
|
| 123 |
-
####
|
| 124 |
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 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 !
|