new-edition-drafts/jupyter-notebooks/daily-speech-practice.ipynb
# Install or update openai modules
%pip install openai
# %pip install --upgrade openai
# Load Modules
from openai import OpenAI
import IPython
from datetime import datetime
client = OpenAI(
# replace openai api key below (which is invalid) with your own
api_key="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"
)
role_definition = """
你是我的英语教练。
请将我的话改写成英文。
不需要逐字翻译。
请分析清楚我的内容,而后用英文重新逻辑清晰地组织它。
请使用地道的美式英语,纽约腔调。
请尽量使用日常词汇,尽量优先使用短语动词或者习惯用语。
每个句子最长不应该超过 20 个单词。
"""
user_prompt = """
人们对高管、首席执行官或庞大业务部门的领导者有不一样的憧憬。
他们认为,在那个级别的每个人都有足够的经验和智慧,至少看起来知道自己在做什么。
他们假定那里有深思熟虑、战略和长远思考,以及握手言和的合理交易。
但有些时候,它是高中;甚至有些时候,它是幼儿园。
"""
# how many versions needed.
number_of_choices = 3
# your openai subscription might not support gpt-4...
# gpt-3.5 is ok too.
rspd_translation = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "system",
"content": role_definition
},
{
"role": "user",
"content": user_prompt
}
],
n = number_of_choices
)
for rspd in rspd_translation.choices:
print(f"{rspd.index+1}.\n{rspd.message.content}\n\n")
# Create audios for each versions (three was set as default previously.)
voice_performer = "alloy"
# alloy, echo, fable, onyx, nova, and shimmer, the last two of which are femail voices.
for i in range(number_of_choices):
speech_file_path = f'{datetime.now().strftime("%Y%m%d_%H%M%S")}_speech.mp3'
rspd_audio = client.audio.speech.create(
model="tts-1",
voice=voice_performer,
input=rspd_translation.choices[i].message.content
)
rspd_audio.stream_to_file(speech_file_path)
IPython.display.Audio(speech_file_path)
# Or else, you could rewrite your own version for open ai tts
your_version = """
"""
# Create the audio of your version
speech_file_path = f'{datetime.now().strftime("%Y%m%d_%H%M%S")}_speech.mp3'
voice_performer = "alloy"
# alloy, echo, fable, onyx, nova, and shimmer, the last two of which are femail voices.
rspd_audio = client.audio.speech.create(
model="tts-1",
voice=voice_performer,
input=your_version
)
rspd_audio.stream_to_file(speech_file_path)
IPython.display.Audio(speech_file_path)