Files
everyone-can-use-english/1000-hours/public/jupyter-notebooks/sentence-openai.ipynb
2024-07-19 09:19:51 +08:00

133 lines
3.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"id": "e6525deb-267c-475f-a73a-7939c5bdca86",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/k_/t1vf2gd95jbc1j3k49xxdfy80000gn/T/ipykernel_4713/2720232930.py:29: DeprecationWarning: Due to a bug, this method doesn't actually stream the response content, `.with_streaming_response.method()` should be used instead\n",
" rspd_audio.stream_to_file(speech_file_path)\n"
]
}
],
"source": [
"from openai import OpenAI\n",
"import IPython\n",
"from datetime import datetime\n",
"import sys\n",
"from dotenv import load_dotenv\n",
"import os\n",
"\n",
"load_dotenv()\n",
"client = OpenAI(\n",
")\n",
"\n",
"\n",
"text = \"\"\"\n",
"I heard him singing.\n",
"\"\"\"\n",
"# Create audios for each versions (three was set as default previously.)\n",
"\n",
"for v in [\"alloy\", \"nova\"]:\n",
" # alloy, echo, fable, onyx, nova, and shimmer, the last two of which are femail voices.\n",
" \n",
" speech_file_path = f'sentence-{text.split(\" \")[1]}-{text.split(\" \")[2]}-{v}.mp3'\n",
" \n",
" rspd_audio = client.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=v,\n",
" input=text\n",
" )\n",
" \n",
" rspd_audio.stream_to_file(speech_file_path)\n",
" \n",
" # IPython.display.Audio(speech_file_path)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "603fb48b-2fd5-482c-9c96-38a65fc43824",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/k_/t1vf2gd95jbc1j3k49xxdfy80000gn/T/ipykernel_5661/2864483681.py:30: DeprecationWarning: Due to a bug, this method doesn't actually stream the response content, `.with_streaming_response.method()` should be used instead\n",
" rspd_audio.stream_to_file(speech_file_path)\n"
]
}
],
"source": [
"from openai import OpenAI\n",
"import IPython\n",
"from datetime import datetime\n",
"import sys\n",
"from dotenv import load_dotenv\n",
"import os\n",
"\n",
"load_dotenv()\n",
"client = OpenAI(\n",
")\n",
"\n",
"\n",
"text = \"\"\"\n",
"lookout,seekout\n",
"\"\"\"\n",
"# Create audios for each versions (three was set as default previously.)\n",
"\n",
"for w in text.split(\",\"):\n",
" for v in [\"alloy\", \"nova\"]:\n",
" # alloy, echo, fable, onyx, nova, and shimmer, the last two of which are femail voices.\n",
" \n",
" speech_file_path = f\"{w.strip()}-us-{\"male\" if v == \"alloy\" else \"female\"}.mp3\"\n",
" \n",
" rspd_audio = client.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=v,\n",
" input=w.strip()\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": "a3c6184a-430f-491e-ad78-2f6e4fd36a54",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}