Text-to-speech conversion turns textual content into spoken audio. It enables voice-based interaction and can read translated text aloud for users. section 7.0 section 7.4.3
A practical speech-generation workflow connects several stages:
- Record audio from a microphone and save it as a WAV file. section 7.4.3
- Transcribe the recording into text. section 7.4.3
- Translate or otherwise transform the text using a prompt that specifies the desired language or style. section 7.4.3
- Generate speech from the resulting text and play the audio file. section 7.4.3
A spoken translator
A Python translator can convert recorded English into French text and spoken French. The same input can be transformed into polished English in the style of Shakespeare, showing that the requested output style affects both the generated text and audio. section 7.4.4
Listing 7.3 Translating spoken input into a different language
import argparse #1
import openai
import playsound
import requests
import scipy.io.wavfile
import sounddevice
import time
client = openai.OpenAI()
def record(output_path): #2
""" Record audio and store in .wav file.
Args:
output_path: store audio recording there.
"""
sample_rate = 44100
nr_frames = 5 * sample_rate
recording = sounddevice.rec(
nr_frames, samplerate=sample_rate, channels=1)
sounddevice.wait()
scipy.io.wavfile.write(output_path, sample_rate, recording)
def transcribe(audio_path): #3
""" Transcribe audio file to text.
Args:
audio_path: path to audio file.
Returns:
transcribed text.
"""
with open(audio_path, 'rb') as audio_file:
transcription = client.audio.transcriptions.create(
file=audio_file, model='whisper-1')
return transcription.text
#4
def create_prompt(to_translate, to_language):
""" Generate prompt to translate text to target language.
Args:
to_translate: translate this text.
to_language: translate text to this language.
Returns:
Translated text.
"""
parts = []
parts += [f'Translate this text to {to_language}:']
parts += [to_translate]
parts += ['Translated text:']
return '\n'.join(parts)
def call_llm(prompt): #5
""" Query large language model and return answer.
Args:
prompt: input prompt for language model.
Returns:
Answer by language model.
"""
for nr_retries in range(1, 4):
try:
response = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role':'user', 'content':prompt}
]
)
return response.choices[0].message.content
except:
time.sleep(nr_retries * 2)
raise Exception('Cannot query OpenAI model!')
def generate_speech(speech_text): #6
""" Generates speech for given text.
Args:
speech_text: generate speech for this text.
Returns:
query result.
"""
response = client.audio.speech.create(
model='tts-1', voice='alloy',
input=speech_text)
return response.content
if __name__ == '__main__': #7
parser = argparse.ArgumentParser()
parser.add_argument('tolanguage', type=str, help='Target language')
args = parser.parse_args()
while True: #8
user_input = input('Press enter to record (type quit to quit).')
if user_input == 'quit':
break
audio_path = 'to_translate.wav' #9
record(audio_path)
to_translate = transcribe(audio_path)
print(f'Original text: {to_translate}')
#10
prompt = create_prompt(to_translate, args.tolanguage)
translated = call_llm(prompt)
print(f'Translated text: {translated}')
speech = generate_speech(translated) #11
with open('translation.mp3', 'wb') as file:
file.write(speech)
playsound.playsound('translation.mp3') #12
The complete listing shows how recording, transcription, prompting, speech generation, HTTP requests, and audio playback fit together. section 7.4.3



