🦜 Make It Speak


Install gTTS

gTTS is a text-to-speech tool. We use it to turn our text into an audio file, spoken by a computer.

First, install the package inside the environment.

!pip install gtts

Then, import the library in Python.

Import, too, the library to play or display audio.

from gtts import gTTS
from IPython.display import Audio, display

Generate audio

Generate, save, and display the audio file you generated from the result.

tts = gTTS(generated_text, lang='en')
tts.save("gossip.wav")
Audio("gossip.wav")

Congratulations! You gave a voice to your gossip machine ✨


Extra: Continuous Inference + TTS

To run this continuously, you can use a "while loop": a type of command that loops over the text and audio generator until you stop the cell.

while True:
  output = model.generate(
      input_ids,
      attention_mask=attention_mask,
      max_length=25,
      do_sample=True,
      top_p=0.95,
      temperature=0.8,
      num_return_sequences=1,
      pad_token_id=tokenizer.eos_token_id
  )
  generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
  print(generated_text)
  tts = gTTS(generated_text, lang='en')
  tts.save("gossip.wav")
  display(Audio("gossip.wav"))

🍬 Bravo!