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

169 lines
4.8 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"
]
},
{
"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"
]
},
{
"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",
"请尽量使用日常词汇,尽量优先使用短语动词或者习惯用语。\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": "4a92f81f-fe8d-4cc8-a770-13ea1e12259f",
"metadata": {},
"outputs": [],
"source": [
"# Create audios for each versions (three was set as default previously.)\n",
"\n",
"voice_performer = \"alloy\"\n",
"# alloy, echo, fable, onyx, nova, and shimmer, the last two of which are femail voices.\n",
"\n",
"for i in range(number_of_choices):\n",
" speech_file_path = f'{datetime.now().strftime(\"%Y%m%d_%H%M%S\")}_speech.mp3'\n",
" \n",
" rspd_audio = client.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=voice_performer,\n",
" input=rspd_translation.choices[i].message.content\n",
" )\n",
" \n",
" rspd_audio.stream_to_file(speech_file_path)\n",
" \n",
" IPython.display.Audio(speech_file_path)"
]
},
{
"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": "4696c0dc-8983-475b-b376-ec0f059cffa8",
"metadata": {},
"outputs": [],
"source": [
"# Create the audio of your version\n",
"speech_file_path = f'{datetime.now().strftime(\"%Y%m%d_%H%M%S\")}_speech.mp3'\n",
"\n",
"voice_performer = \"alloy\"\n",
"# alloy, echo, fable, onyx, nova, and shimmer, the last two of which are femail voices.\n",
"\n",
"rspd_audio = client.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=voice_performer,\n",
" input=your_version\n",
")\n",
"\n",
"rspd_audio.stream_to_file(speech_file_path)\n",
"\n",
"IPython.display.Audio(speech_file_path)"
]
}
],
"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
}