| | | |

Text to speech automation via python

I created a python script to monitor an email address for inbound email and change the text of the body into speech.

I used ChatGPT for the initial results (“Generate a python script to monitor an email address, and then use openai’s TTS API to convert it to audio and email it back to the sender”, and was 90% done in about 2 minutes.

Over the next couple days, with the assistance of chatgpt, I added logging (“Please add logging to the code”); exception handling, (“This should have exception handling, and better comments”), etc.

The end result also sends the content through the OpenAI Moderation Enpoint to ensure it is nothing ‘unsafe’.

Feel free to test it out as there is a demo running. Note that when using the demo email address it will fail silently (no email reply sent) under the following cases:

  • It will fail if there are more than 2000 characters in the body.
  • It will fail if the subject has “TTS” in lowercase (tts).
  • If the content is deemed ‘unsafe’ (violence, sexual, etc) by the OpenAI Moderation endpoint, it will not respond.

Compose an email to ai_tools@adrianhensler. The email subject line must be the three letters “TTS”, and the body must be under 2000 characters. It is set to process the inbound email once per 15 minutes.

Example email:

When processed, you will get an email back:

And then you have the audio file:

Another feature is the ability to change the voice to any of the ones provided by openai; if you adjust the ‘subject’ line to “TTS: shimmer” for example, you will get a different voice:

The full list of voices (with samples) and more detail about the openai api can be found here: https://platform.openai.com/docs/guides/text-to-speech

The code is on github: https://github.com/adrianhensler/ajh_ai_tools

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *