Step by step guide to creating a Text-To-Voice-To-Text email assistant
By: Anirudh Lohia
Tags: Speech Recognition, Text To Speech, Python, Gmail Api, Google Cloud Platform

The following steps of subsequently enabling APIs, creating service keys and credentials are unfortunately quite elaborate but follow on

2b. Create a Service Account Key

Create Service Account Key — Input

On the completion of this step, a key file with a .json extension is created. Download this service account key. This key is provided as required for authentication by your program

2c. Create OAuth2.0 Credential

This step is to get consent for your application to be able to access GMAIL. We will apply for ‘compose’ and ‘send’ email to be able to duly run our prototype

After this step, the credentials page will look as following i.e. with service account key and OAuth client id.

Final Credentials Page

Download the relevant credentials on to your computer in the folder where you will be storing the python scripts (anywhere is fine as long as you provide the correct path to the application)

2d. Activating text-to-speech, speech-to-text, and Gmail APIs

Finally, we activate the text-to-speech API, speech-to-text API, and the Gmail API. On the dashboard, you will see the ‘Enable APIs and Services’ Option.

Enable APIS on the dashboard

The easiest way to enable from the plethora of APIs & services is by searching (common, it’s Google what else would you do there). See the example below for searching text-to-speech and speech-to-text API.

Searching for APIS to enable

Once you have found the API, simply click on Enable (sample for Gmail below)

Enable Gmail API

Finally, we are ready to look into the program itself.

Step3: Running the Application

Now comes the easier part. Easy as in most of the code samples are available on the GCP tutorial site with an option to copy and lots of explanations too. The flip side is a lot of these links have many deep links that provides the user with all the API options. This results that the new user tends to get lost.

Here I describe to you the relevant portion to get the prototype up and running. After that, it is much easier to configure as per your specific requirements based on the documentation on the GCP site. I have provided the links of the GCP tutorials for each of the specific portion

The steps to run the prototype would be

Download the code from github into a specific folder

Copy the credentials (both service account key as well as OAuth key) into the same folder as the code

In Main.py replace the path of GOOGLE_APPLICATION_CREDENTIALS to be exported

#Replace this with the correct file path of the service account key

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = \

"/Users/name/Documents/GitHub/Python-Exercises/googleSpeechToText/GMAIL_gTtS_gStT_ServiceAccountKey.json"

In RaSmails.py replace the name of the OAuth2.0 file

#Replace this with correct file path of credentials .json

flow = InstalledAppFlow.from_client_secrets_file(

'oath2_credentials_mailreceiver349.json', SCOPES)

In terminal window activate the virtual environment (#conda activate — name p36)

Run the code as #python Main.py

A brief overview of the flow of code is:

Get Gmail service instance > Get snippets of latest 5 emails > Convert the latest snippet to audio > Play the audio file (latest email) > Record reply email > Convert the audio to text > Send out the reply email

The following links describe in detail the APIs being used

I hope you enjoyed this prototype. Happy to answer any questions. Let me know what you think