Utilising Google Colab to Perform Text-to-Speech Conversion

Colab provides an interactive platform to execute your Python Scripts and AI Algorithms, but what if you want a speech output instead of Console Text?

Dr. Joyjit Chatterjee
2 min readNov 7, 2020
Image by OpenClipart-Vectors from Pixabay (Free to Use)

Google Colab has indeed become quite popular, and why not, you get access to free GPUs, can directly read/write files from your Google Drive, and the best, perform all tasks in a simple and easy to use interface. It is always nice to get some outputs out there in the console after say, you’ve trained your Machine Learning Models, and are inferring the predicted output on some new samples.

Pythonically, you would simply output the results:

#Suppose x contains predicted result from an ML model i.e. “Dog”print(“This image is predicted to be a”,x)

This would give us a very basic output

This image is predicted to be a dog

But what if you want a nice voice to speak out your model’s output? Just like one would have in a production environment/deployed products? Wouldn’t it be lovely to have Google Colab’s Kernel speak out the prediction for you?

This is really simple, just use Google’s Text-to-Speech library, and you’re good to go! The basic idea is to initially import Google’s Text to Speech module, and you could then use the Interactive IPython module to get the Audio method. Finally, the gTTS method converts your provided string into an audio file, which you can save. And then, just play the audio file and you’re good to go! Note that autoplay needs to be set to True for the sound to automatically play, otherwise, you would need to click the play icon which would appear in your Colab notebook to hear the audio.

Here is the code for achieving text-to-speech conversion in Colab, and I base this on my existing Stackoverflow answer https://stackoverflow.com/questions/57563060/how-to-do-text-to-speech-conversion-in-google-colab. You can find the Colab notebook here

from gtts import gTTS #Import Google Text to Speech
from IPython.display import Audio #Import Audio method from IPython's Display Class
tts = gTTS('hello joyjit') #Provide the string to convert to speech
tts.save('1.wav') #save the string converted to speech as a .wav file
sound_file = '1.wav'
Audio(sound_file, autoplay=True)

#Autoplay = True will play the sound automatically
#If you would not like to play the sound automatically, simply pass Autoplay = False.

You can find the Colab notebook on my Github here https://github.com/joyjitchatterjee/Text2Speech-Python/blob/main/Colab_TTS_Example.ipynb. This was a rather simple (and very basic) post, but I hope it helps you try something fun (and interesting)- achieve text to speech conversion in Colab! Thank you for reading this post.

If you would like, you can connect with me on LinkedIn http://linkedin.com/in/joyjitchatterjee/

--

--

Dr. Joyjit Chatterjee
Dr. Joyjit Chatterjee

Written by Dr. Joyjit Chatterjee

Data Science & AI Innovation | Forbes Under 30 Europe | Green Talents Awardee (German Govt.) | PhD in Machine Learning (Hull, UK) | All views my own!