Files
everyone-can-use-english/new-edition-drafts/jupyter-notebooks/ja-daily-speech-practice.ipynb
2024-01-23 08:44:16 +08:00

162 lines
5.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "f330d08f-fa3c-4cf4-bf53-136be20393e9",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Install or update openai modules \n",
"%pip install openai\n",
"# %pip install --upgrade openai\n",
"%pip install edge-tts"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71ad2e48-3b33-42d3-8e4c-9f89c61110de",
"metadata": {},
"outputs": [],
"source": [
"# Load Modules\n",
"from openai import OpenAI\n",
"import IPython\n",
"from datetime import datetime\n",
"import asyncio\n",
"import edge_tts"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a492b90-fa90-4d8b-934a-05d0cc2ff2c1",
"metadata": {},
"outputs": [],
"source": [
"client = OpenAI(\n",
" # replace openai api key below (which is invalid) with your own\n",
" api_key=\"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\" \n",
")\n",
"\n",
"role_definition = \"\"\"\n",
"你是我的日语教练。\n",
"请将我的话改写成日文。\n",
"不需要逐字翻译。\n",
"请分析清楚我的内容,而后用英文重新逻辑清晰地组织它。\n",
"请使用地道的日语,东京腔调。\n",
"yb xm使用敬语。\n",
"每个句子最长不应该超过 20 个字。\n",
"\"\"\"\n",
"\n",
"user_prompt = \"\"\"\n",
"人们对高管、首席执行官或庞大业务部门的领导者有不一样的憧憬。\n",
"他们认为,在那个级别的每个人都有足够的经验和智慧,至少看起来知道自己在做什么。\n",
"他们假定那里有深思熟虑、战略和长远思考,以及握手言和的合理交易。\n",
"但有些时候,它是高中;甚至有些时候,它是幼儿园。\n",
"\"\"\"\n",
"\n",
"# how many versions needed.\n",
"number_of_choices = 3\n",
"\n",
"# your openai subscription might not support gpt-4...\n",
"# gpt-3.5 is ok too.\n",
"rspd_translation = client.chat.completions.create(\n",
" model=\"gpt-4\",\n",
" messages=[\n",
" {\n",
" \"role\": \"system\", \n",
" \"content\": role_definition\n",
" },\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": user_prompt\n",
" }\n",
" ],\n",
" n = number_of_choices \n",
")\n",
"\n",
"for rspd in rspd_translation.choices:\n",
" print(f\"{rspd.index+1}.\\n{rspd.message.content}\\n\\n\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d28f1714-bdbe-4f29-b52c-56a07af6d60e",
"metadata": {},
"outputs": [],
"source": [
"# Or else, you could rewrite your own version for open ai tts\n",
"your_version = \"\"\"\n",
"\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "927a94e4-40ec-47d9-8e39-c69e5c5205d9",
"metadata": {},
"outputs": [],
"source": [
"TEXT = \"\"\"\n",
"人々は、高位の経営者、CEO、大きなビジネス部門のリーダーに対して夢想を抱くものです。\n",
"そのレベルの人々が、少なくとも行っていることを理解しているかのように見える程度の経験と知恵を持っていると思われます。\n",
"深い思考、戦略、長期的な思考、そして適切な取引のための握手があると想定されます。\n",
"しかし、時にはそれは高校レベルのもので、ある時は幼稚園レベルのものさえあります。\n",
"\"\"\"\n",
"# VOICE = \"ja-JP-KeitaNeural\" # male\n",
"VOICE = \"ja-JP-NanamiNeural\" # female\n",
"\n",
"OUTPUT_FILE_NAME = f\"{VOICE}_{TEXT[:15]}.mp3\"\n",
"\n",
"OUTPUT_FILE = f\"{OUTPUT_FILE_NAME}.mp3\"\n",
"WEBVTT_FILE = f\"{OUTPUT_FILE_NAME}.vtt\"\n",
"\n",
"communicate = edge_tts.Communicate(TEXT, VOICE)\n",
"await communicate.save(OUTPUT_FILE)\n",
"\n",
"submaker = edge_tts.SubMaker()\n",
"with open(OUTPUT_FILE, \"wb\") as file:\n",
" async for chunk in communicate.stream():\n",
" if chunk[\"type\"] == \"audio\":\n",
" file.write(chunk[\"data\"])\n",
" elif chunk[\"type\"] == \"WordBoundary\":\n",
" submaker.create_sub((chunk[\"offset\"], chunk[\"duration\"]), chunk[\"text\"])\n",
"\n",
"with open(WEBVTT_FILE, \"w\", encoding=\"utf-8\") as file:\n",
" file.write(submaker.generate_subs())\n",
"\n",
"print(f\"Audio files created: {OUTPUT_FILE}\")\n",
"\n",
"IPython.display.Audio(OUTPUT_FILE)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}