# -*- Mode: Python -*-
# 01_tts_course_description.py
# Author : Marcel Turcotte
# Created On : Tue Feb 13 16:29:41 2024
# Last Modified By: Marcel Turcotte
# Last Modified On: Tue Feb 13 17:22:44 2024
from openai import OpenAI
# Initialize OpenAI client
client = OpenAI()
# Creating an audio text ouput
def create_audio(prompt, speech_file_path):
with client.audio.speech.with_streaming_response.create(
model="tts-1-hd",
voice="nova",
input=prompt,
) as response:
response.stream_to_file(speech_file_path)
# Translating to French
def translate_to_french(input_text):
output_text_fr = ""
try:
# Create translation response
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": "You will be provided with a text in English."
},
{
"role": "system",
"content": "Your task is to translate it into Canadian French."
},
{
"role": "system",
"content": "Note that the course code CSI4106 translates to CSI4506 in French."
},
{
"role": "user",
"content": input_text
}
],
temperature=0.2,
max_tokens=4095,
)
output_text_fr = response.choices[0].message.content
except Exception as e:
print(f"Error: {e}")
return output_text_fr
# Define file path and input text
speech_file_path_fr = "01_tts_course_description-fr-nova.mp3"
speech_file_path_en = "01_tts_course_description-en-nova.mp3"
input_text_en = (
'Welcome to CSI4106, "introduction to artificial intelligence"! '
"In this course, you will learn about the roots and scope of Artificial Intelligence. "
"Knowledge and knowledge representation. Search, informed search, adversarial search. "
"Deduction and reasoning. Uncertainty in Artificial Intelligence. "
"Introduction to Natural Language Processing. Elements of planning. Basics of Machine Learning."
)
input_text_fr = translate_to_french(input_text_en)
create_audio(input_text_fr, speech_file_path_fr)
create_audio(input_text_en, speech_file_path_en)