Skip to content
Fondazione ITS INCOM

Examples · Programming

Text to speech in Python

Turn a text into an MP3 with one of the school's six voices, then let the transcription model listen to it and check that it says what you wrote.

Beginner · 20 min

What you need

A key and the openai library, as in Your first API call in Python.

The voices

Six voices, each made for one language. Write the text in the language of its voice: an English voice reads an Italian text with English pronunciation.

Voice Language
paola Italian, female
riccardo Italian, male
amy English, female
ryan English, male
thorsten German, male
siwis French, female

Names are in lower case: Amy is refused, and the error lists the valid names.

The program

Save it as speak.py:

import os

from openai import OpenAI

client = OpenAI(
    base_url="https://api.ai.itsincom.org/v1",
    api_key=os.environ["API_KEY"],
)

speech = client.audio.speech.create(
    model="tts-1",
    voice="amy",
    input="Hello! I am a program written by a student of ITS INCOM, and today I learned to speak.",
    response_format="mp3",
)
speech.write_to_file("hello.mp3")
print("hello.mp3 is ready")

Run it with python3 speak.py and open hello.mp3 with any player. It takes a second or two.

Two things work differently from OpenAI:

Did it say what you wrote?

You cannot listen to a hundred files. Let the transcription model listen for you. Save it as check.py:

import os

from openai import OpenAI

client = OpenAI(
    base_url="https://api.ai.itsincom.org/v1",
    api_key=os.environ["API_KEY"],
)

with open("hello.mp3", "rb") as f:
    heard = client.audio.transcriptions.create(model="whisper-1", file=f, language="en")

print("heard:", heard.text)

Run it with python3 check.py.

What you should see

heard: Hello, I am a program written by a student of its income, and today I learned to speak.

Almost. Names and acronyms are where speech goes wrong: the voice may read letters as a word, and the transcription may write the word it knows best. Tell the transcription which names to expect with prompt, in check.py:

with open("hello.mp3", "rb") as f:
    heard = client.audio.transcriptions.create(model="whisper-1", file=f, language="en", prompt="ITS INCOM")

When we tried it, the school's name came back right.

Good to know

Exercise

All examples