notebooks updated.

This commit is contained in:
xiaolai
2024-01-23 08:44:16 +08:00
parent 76ffdaa902
commit cc1f5f4960
4 changed files with 335 additions and 107 deletions

View File

@@ -59,6 +59,8 @@
"# 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",

View File

@@ -0,0 +1,162 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "b94f103d-ac43-4d13-83cb-eb5090220881",
"metadata": {},
"source": [
"# EdgeTTS\n",
"\n",
"https://github.com/rany2/edge-tts\n",
"\n",
"edge-tts is a Python module that allows you to use Microsoft Edge's online text-to-speech service from within your Python code or using the provided edge-tts or edge-playback command."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77deb08f-fec3-4327-b2f9-1c893aacaddc",
"metadata": {},
"outputs": [],
"source": [
"!pip install edge-tts\n",
"!pip install pygame"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9aad6634-e833-4ec4-b285-84e051329712",
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"import edge_tts\n",
"import pygame\n",
"\n",
"TEXT = \"\"\"\n",
"Once upon a time, the King's youngest son became filled with the desire to go abroad, and see the world.\n",
"\"\"\"\n",
"\n",
"VOICE = \"en-US-GuyNeural\" # Male\n",
"# VOICE = \"en-US-AnaNeural\" # Female\n",
"# VOICE = \"en-US-AndrewNeural\" # Male\n",
"# VOICE = \"en-US-AriaNeural\" # Female\n",
"# VOICE = \"en-US-AvaNeural\" # Female\n",
"# VOICE = \"en-US-BrianNeural\" # Male\n",
"# VOICE = \"en-US-ChristopherNeural\" # Male\n",
"# VOICE = \"en-US-EmmaNeural\" # Female\n",
"# VOICE = \"en-US-EricNeural\" # Male\n",
"# VOICE = \"en-US-GuyNeural\" # Male\n",
"# VOICE = \"en-US-JennyNeural\" # Female\n",
"# VOICE = \"en-US-MichelleNeural\" # Female\n",
"# VOICE = \"en-US-RogerNeural\" # Male\n",
"# VOICE = \"en-US-SteffanNeural\" # Male\n",
"# VOICE = \"en-GB-LibbyNeural\" # Female\n",
"# VOICE = \"en-GB-MaisieNeural\" # Female\n",
"# VOICE = \"en-GB-RyanNeural\" # Male\n",
"# VOICE = \"en-GB-SoniaNeural\" # Female\n",
"# VOICE = \"en-GB-ThomasNeural\" # Male\n",
"# VOICE = \"en-AU-NatashaNeural\" # Female\n",
"# VOICE = \"en-AU-WilliamNeural\" # Male\n",
"# VOICE = \"en-CA-ClaraNeural\" # Female\n",
"# VOICE = \"en-CA-LiamNeural\" # Male\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(\"Files created!\") "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "862f0948",
"metadata": {},
"outputs": [],
"source": [
"pygame.mixer.init()\n",
"pygame.mixer.music.load(OUTPUT_FILE)\n",
"pygame.mixer.music.play()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a73c84c-e221-4f2e-982a-0312929ffba4",
"metadata": {},
"outputs": [],
"source": [
"!pip install amfm_decompy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc271b37-fdf4-4888-bf46-31086b2299f3",
"metadata": {},
"outputs": [],
"source": [
"import amfm_decompy.basic_tools as basic\n",
"import amfm_decompy.pYAAPT as pYAAPT\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"#load audio 注意必须是wav\n",
"signal = basic.SignalObj(\"/Users/joker/Desktop/Speaking Training/The Case Against Education/Clips/Chapter 01 (Selection) 01.wav\")\n",
"\n",
"#pYAAPT pitches 生成pitches文件\n",
"pitch = pYAAPT.yaapt(signal, frame_length = 40, tda_frame_length=40,f0_min=75, f0_max=600)\n",
"\n",
"#plot \n",
"plt.plot(pitch.samp_values)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6a4cae2-78c1-4211-ac7b-e4339c95d945",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}

View File

@@ -0,0 +1,161 @@
{
"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
}

File diff suppressed because one or more lines are too long