1000-hours/public/jupyter-notebooks/ja-daily-speech-practice.ipynb
# Install or update openai modules
%pip install openai
# %pip install --upgrade openai
%pip install edge-tts
# Load Modules
from openai import OpenAI
import IPython
from datetime import datetime
import asyncio
import edge_tts
client = OpenAI(
# replace openai api key below (which is invalid) with your own
api_key="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"
)
role_definition = """
你是我的日语教练。
请将我的话改写成日文。
不需要逐字翻译。
请分析清楚我的内容,而后用英文重新逻辑清晰地组织它。
请使用地道的日语,东京腔调。
yb xm使用敬语。
每个句子最长不应该超过 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")
# Or else, you could rewrite your own version for open ai tts
your_version = """
"""
TEXT = """
人々は、高位の経営者、CEO、大きなビジネス部門のリーダーに対して夢想を抱くものです。
そのレベルの人々が、少なくとも行っていることを理解しているかのように見える程度の経験と知恵を持っていると思われます。
深い思考、戦略、長期的な思考、そして適切な取引のための握手があると想定されます。
しかし、時にはそれは高校レベルのもので、ある時は幼稚園レベルのものさえあります。
"""
# VOICE = "ja-JP-KeitaNeural" # male
VOICE = "ja-JP-NanamiNeural" # female
OUTPUT_FILE_NAME = f"{VOICE}_{TEXT[:15]}.mp3"
OUTPUT_FILE = f"{OUTPUT_FILE_NAME}.mp3"
WEBVTT_FILE = f"{OUTPUT_FILE_NAME}.vtt"
communicate = edge_tts.Communicate(TEXT, VOICE)
await communicate.save(OUTPUT_FILE)
submaker = edge_tts.SubMaker()
with open(OUTPUT_FILE, "wb") as file:
async for chunk in communicate.stream():
if chunk["type"] == "audio":
file.write(chunk["data"])
elif chunk["type"] == "WordBoundary":
submaker.create_sub((chunk["offset"], chunk["duration"]), chunk["text"])
with open(WEBVTT_FILE, "w", encoding="utf-8") as file:
file.write(submaker.generate_subs())
print(f"Audio files created: {OUTPUT_FILE}")
IPython.display.Audio(OUTPUT_FILE)