Spaces:
Runtime error
Runtime error
Commit
·
d9a62b3
1
Parent(s):
990159e
added text chunking for text over 4,000 chars
Browse files- app.ipynb +125 -99
- app.py +110 -13
- packages.txt +1 -0
- requirements.txt +3 -2
app.ipynb
CHANGED
|
@@ -15,16 +15,7 @@
|
|
| 15 |
"execution_count": null,
|
| 16 |
"id": "667802a7-0f36-4136-a381-e66210b20462",
|
| 17 |
"metadata": {},
|
| 18 |
-
"outputs": [
|
| 19 |
-
{
|
| 20 |
-
"name": "stdout",
|
| 21 |
-
"output_type": "stream",
|
| 22 |
-
"text": [
|
| 23 |
-
"OPENAI_API_KEY var not found. Trying import tts_openai_secrets\n",
|
| 24 |
-
"import tts_openai_secrets succeeded\n"
|
| 25 |
-
]
|
| 26 |
-
}
|
| 27 |
-
],
|
| 28 |
"source": [
|
| 29 |
"#| export\n",
|
| 30 |
"#tts_openai_secrets.py content:\n",
|
|
@@ -74,7 +65,9 @@
|
|
| 74 |
"source": [
|
| 75 |
"#| export\n",
|
| 76 |
"import gradio as gr\n",
|
| 77 |
-
"import openai"
|
|
|
|
|
|
|
| 78 |
]
|
| 79 |
},
|
| 80 |
{
|
|
@@ -82,15 +75,7 @@
|
|
| 82 |
"execution_count": null,
|
| 83 |
"id": "0ffd33b4-cb9b-4c01-bff6-4c3102854ab6",
|
| 84 |
"metadata": {},
|
| 85 |
-
"outputs": [
|
| 86 |
-
{
|
| 87 |
-
"name": "stdout",
|
| 88 |
-
"output_type": "stream",
|
| 89 |
-
"text": [
|
| 90 |
-
"successfully got tts model list: ['tts-1-hd', 'tts-1-hd-1106', 'canary-tts', 'tts-1', 'tts-1-1106']\n"
|
| 91 |
-
]
|
| 92 |
-
}
|
| 93 |
-
],
|
| 94 |
"source": [
|
| 95 |
"#| export\n",
|
| 96 |
"try:\n",
|
|
@@ -111,6 +96,85 @@
|
|
| 111 |
"tts_voices = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']"
|
| 112 |
]
|
| 113 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
{
|
| 115 |
"cell_type": "code",
|
| 116 |
"execution_count": null,
|
|
@@ -119,16 +183,46 @@
|
|
| 119 |
"outputs": [],
|
| 120 |
"source": [
|
| 121 |
"#| export\n",
|
| 122 |
-
"def create_speech(input_text, model='tts-1', voice='alloy'):\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
" client = openai.OpenAI()\n",
|
| 124 |
-
"
|
| 125 |
-
"
|
| 126 |
-
"
|
| 127 |
-
"
|
| 128 |
-
"
|
| 129 |
-
" )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
" client.close()\n",
|
| 131 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
]
|
| 133 |
},
|
| 134 |
{
|
|
@@ -186,37 +280,7 @@
|
|
| 186 |
"execution_count": null,
|
| 187 |
"id": "4b534fe7-4337-423e-846a-1bdb7cccc4ea",
|
| 188 |
"metadata": {},
|
| 189 |
-
"outputs": [
|
| 190 |
-
{
|
| 191 |
-
"name": "stdout",
|
| 192 |
-
"output_type": "stream",
|
| 193 |
-
"text": [
|
| 194 |
-
"Running on local URL: http://0.0.0.0:7860\n",
|
| 195 |
-
"\n",
|
| 196 |
-
"To create a public link, set `share=True` in `launch()`.\n"
|
| 197 |
-
]
|
| 198 |
-
},
|
| 199 |
-
{
|
| 200 |
-
"data": {
|
| 201 |
-
"text/html": [
|
| 202 |
-
"<div><iframe src=\"http://localhost:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 203 |
-
],
|
| 204 |
-
"text/plain": [
|
| 205 |
-
"<IPython.core.display.HTML object>"
|
| 206 |
-
]
|
| 207 |
-
},
|
| 208 |
-
"metadata": {},
|
| 209 |
-
"output_type": "display_data"
|
| 210 |
-
},
|
| 211 |
-
{
|
| 212 |
-
"data": {
|
| 213 |
-
"text/plain": []
|
| 214 |
-
},
|
| 215 |
-
"execution_count": null,
|
| 216 |
-
"metadata": {},
|
| 217 |
-
"output_type": "execute_result"
|
| 218 |
-
}
|
| 219 |
-
],
|
| 220 |
"source": [
|
| 221 |
"#| hide\n",
|
| 222 |
"#Notebook launch\n",
|
|
@@ -228,37 +292,7 @@
|
|
| 228 |
"execution_count": null,
|
| 229 |
"id": "cb886d45",
|
| 230 |
"metadata": {},
|
| 231 |
-
"outputs": [
|
| 232 |
-
{
|
| 233 |
-
"name": "stdout",
|
| 234 |
-
"output_type": "stream",
|
| 235 |
-
"text": [
|
| 236 |
-
"Running on local URL: http://0.0.0.0:7861\n",
|
| 237 |
-
"\n",
|
| 238 |
-
"To create a public link, set `share=True` in `launch()`.\n"
|
| 239 |
-
]
|
| 240 |
-
},
|
| 241 |
-
{
|
| 242 |
-
"data": {
|
| 243 |
-
"text/html": [
|
| 244 |
-
"<div><iframe src=\"http://localhost:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 245 |
-
],
|
| 246 |
-
"text/plain": [
|
| 247 |
-
"<IPython.core.display.HTML object>"
|
| 248 |
-
]
|
| 249 |
-
},
|
| 250 |
-
"metadata": {},
|
| 251 |
-
"output_type": "display_data"
|
| 252 |
-
},
|
| 253 |
-
{
|
| 254 |
-
"data": {
|
| 255 |
-
"text/plain": []
|
| 256 |
-
},
|
| 257 |
-
"execution_count": null,
|
| 258 |
-
"metadata": {},
|
| 259 |
-
"output_type": "execute_result"
|
| 260 |
-
}
|
| 261 |
-
],
|
| 262 |
"source": [
|
| 263 |
"#| export\n",
|
| 264 |
"#.py launch\n",
|
|
@@ -271,15 +305,7 @@
|
|
| 271 |
"execution_count": null,
|
| 272 |
"id": "28e8d888-e790-46fa-bbac-4511b9ab796c",
|
| 273 |
"metadata": {},
|
| 274 |
-
"outputs": [
|
| 275 |
-
{
|
| 276 |
-
"name": "stdout",
|
| 277 |
-
"output_type": "stream",
|
| 278 |
-
"text": [
|
| 279 |
-
"Closing server running on port: 7861\n"
|
| 280 |
-
]
|
| 281 |
-
}
|
| 282 |
-
],
|
| 283 |
"source": [
|
| 284 |
"#| hide\n",
|
| 285 |
"app.close()"
|
|
|
|
| 15 |
"execution_count": null,
|
| 16 |
"id": "667802a7-0f36-4136-a381-e66210b20462",
|
| 17 |
"metadata": {},
|
| 18 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"source": [
|
| 20 |
"#| export\n",
|
| 21 |
"#tts_openai_secrets.py content:\n",
|
|
|
|
| 65 |
"source": [
|
| 66 |
"#| export\n",
|
| 67 |
"import gradio as gr\n",
|
| 68 |
+
"import openai\n",
|
| 69 |
+
"from pydub import AudioSegment\n",
|
| 70 |
+
"import io"
|
| 71 |
]
|
| 72 |
},
|
| 73 |
{
|
|
|
|
| 75 |
"execution_count": null,
|
| 76 |
"id": "0ffd33b4-cb9b-4c01-bff6-4c3102854ab6",
|
| 77 |
"metadata": {},
|
| 78 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
"source": [
|
| 80 |
"#| export\n",
|
| 81 |
"try:\n",
|
|
|
|
| 96 |
"tts_voices = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']"
|
| 97 |
]
|
| 98 |
},
|
| 99 |
+
{
|
| 100 |
+
"cell_type": "code",
|
| 101 |
+
"execution_count": null,
|
| 102 |
+
"id": "24674094-4d47-4e48-b591-55faabcff8df",
|
| 103 |
+
"metadata": {},
|
| 104 |
+
"outputs": [],
|
| 105 |
+
"source": [
|
| 106 |
+
"#| export\n",
|
| 107 |
+
"def split_text(input_text, max_length=4000, lookback=1000):\n",
|
| 108 |
+
" # If the text is shorter than the max_length, return it as is\n",
|
| 109 |
+
" if len(input_text) <= max_length:\n",
|
| 110 |
+
" return [input_text]\n",
|
| 111 |
+
"\n",
|
| 112 |
+
" chunks = []\n",
|
| 113 |
+
" while input_text:\n",
|
| 114 |
+
" # Check if the remaining text is shorter than the max_length\n",
|
| 115 |
+
" if len(input_text) <= max_length:\n",
|
| 116 |
+
" chunks.append(input_text)\n",
|
| 117 |
+
" break\n",
|
| 118 |
+
"\n",
|
| 119 |
+
" # Define the split point, initially set to max_length\n",
|
| 120 |
+
" split_point = max_length\n",
|
| 121 |
+
"\n",
|
| 122 |
+
" # Look for a newline in the last 'lookback' characters\n",
|
| 123 |
+
" newline_index = input_text.rfind('\\n', max_length-lookback, max_length)\n",
|
| 124 |
+
" if newline_index != -1:\n",
|
| 125 |
+
" split_point = newline_index + 1 # Include the newline in the current chunk\n",
|
| 126 |
+
"\n",
|
| 127 |
+
" # If no newline, look for a period followed by space\n",
|
| 128 |
+
" elif '. ' in input_text[max_length-lookback:max_length]:\n",
|
| 129 |
+
" # Find the last '. ' in the lookback range\n",
|
| 130 |
+
" period_index = input_text.rfind('. ', max_length-lookback, max_length)\n",
|
| 131 |
+
" split_point = period_index + 2 # Split after the space\n",
|
| 132 |
+
"\n",
|
| 133 |
+
" # Split the text and update the input_text\n",
|
| 134 |
+
" chunks.append(input_text[:split_point])\n",
|
| 135 |
+
" input_text = input_text[split_point:]\n",
|
| 136 |
+
"\n",
|
| 137 |
+
" return chunks"
|
| 138 |
+
]
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"cell_type": "code",
|
| 142 |
+
"execution_count": null,
|
| 143 |
+
"id": "e6224ae5-3792-42b2-8392-3abd42998a50",
|
| 144 |
+
"metadata": {},
|
| 145 |
+
"outputs": [],
|
| 146 |
+
"source": [
|
| 147 |
+
"#| export\n",
|
| 148 |
+
"def concatenate_mp3(mp3_files):\n",
|
| 149 |
+
" if len(mp3_files) == 1:\n",
|
| 150 |
+
" return mp3_files[0]\n",
|
| 151 |
+
" else:\n",
|
| 152 |
+
" # Initialize an empty AudioSegment object for concatenation\n",
|
| 153 |
+
" combined = AudioSegment.empty()\n",
|
| 154 |
+
" \n",
|
| 155 |
+
" # Write out audio file responses as individual files for debugging\n",
|
| 156 |
+
" # for idx, mp3_data in enumerate(mp3_files):\n",
|
| 157 |
+
" # with open(f'./{idx}.mp3', 'wb') as f:\n",
|
| 158 |
+
" # f.write(mp3_data)\n",
|
| 159 |
+
"\n",
|
| 160 |
+
" # Loop through the list of mp3 binary data\n",
|
| 161 |
+
" for mp3_data in mp3_files:\n",
|
| 162 |
+
" # Convert binary data to an audio segment\n",
|
| 163 |
+
" audio_segment = AudioSegment.from_file(io.BytesIO(mp3_data), format=\"mp3\")\n",
|
| 164 |
+
" # Concatenate this segment to the combined segment\n",
|
| 165 |
+
" combined += audio_segment\n",
|
| 166 |
+
"\n",
|
| 167 |
+
" # Export the combined segment to a new mp3 file\n",
|
| 168 |
+
" # Use a BytesIO object to handle this in memory\n",
|
| 169 |
+
" combined_mp3 = io.BytesIO()\n",
|
| 170 |
+
" combined.export(combined_mp3, format=\"mp3\")\n",
|
| 171 |
+
"\n",
|
| 172 |
+
" # Seek to the start so it's ready for reading\n",
|
| 173 |
+
" combined_mp3.seek(0)\n",
|
| 174 |
+
"\n",
|
| 175 |
+
" return combined_mp3.getvalue()"
|
| 176 |
+
]
|
| 177 |
+
},
|
| 178 |
{
|
| 179 |
"cell_type": "code",
|
| 180 |
"execution_count": null,
|
|
|
|
| 183 |
"outputs": [],
|
| 184 |
"source": [
|
| 185 |
"#| export\n",
|
| 186 |
+
"def create_speech(input_text, model='tts-1', voice='alloy', progress=gr.Progress()):\n",
|
| 187 |
+
" # Split the input text into chunks\n",
|
| 188 |
+
" chunks = split_text(input_text)\n",
|
| 189 |
+
"\n",
|
| 190 |
+
" # Initialize the progress bar\n",
|
| 191 |
+
" progress(0, desc=\"Starting TTS processing...\")\n",
|
| 192 |
+
"\n",
|
| 193 |
+
" # Initialize a list to hold the audio data of each chunk\n",
|
| 194 |
+
" audio_data = []\n",
|
| 195 |
+
"\n",
|
| 196 |
+
" # Create a client instance for OpenAI\n",
|
| 197 |
" client = openai.OpenAI()\n",
|
| 198 |
+
"\n",
|
| 199 |
+
" # Calculate the progress increment for each chunk\n",
|
| 200 |
+
" progress_increment = 1.0 / len(chunks)\n",
|
| 201 |
+
"\n",
|
| 202 |
+
" # Process each chunk\n",
|
| 203 |
+
" for i, chunk in enumerate(chunks):\n",
|
| 204 |
+
" response = client.audio.speech.create(\n",
|
| 205 |
+
" model=model,\n",
|
| 206 |
+
" voice=voice,\n",
|
| 207 |
+
" input=chunk,\n",
|
| 208 |
+
" speed=1.0\n",
|
| 209 |
+
" )\n",
|
| 210 |
+
" # Append the audio content of the response to the list\n",
|
| 211 |
+
" audio_data.append(response.content)\n",
|
| 212 |
+
"\n",
|
| 213 |
+
" # Update the progress bar\n",
|
| 214 |
+
" progress((i + 1) * progress_increment, desc=f\"Processing chunk {i + 1} of {len(chunks)}\")\n",
|
| 215 |
+
"\n",
|
| 216 |
+
" # Close the client connection\n",
|
| 217 |
" client.close()\n",
|
| 218 |
+
"\n",
|
| 219 |
+
" # Concatenate the audio data from all chunks\n",
|
| 220 |
+
" combined_audio = concatenate_mp3(audio_data)\n",
|
| 221 |
+
"\n",
|
| 222 |
+
" # Final update to the progress bar\n",
|
| 223 |
+
" progress(1, desc=\"Processing completed\")\n",
|
| 224 |
+
"\n",
|
| 225 |
+
" return combined_audio\n"
|
| 226 |
]
|
| 227 |
},
|
| 228 |
{
|
|
|
|
| 280 |
"execution_count": null,
|
| 281 |
"id": "4b534fe7-4337-423e-846a-1bdb7cccc4ea",
|
| 282 |
"metadata": {},
|
| 283 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
"source": [
|
| 285 |
"#| hide\n",
|
| 286 |
"#Notebook launch\n",
|
|
|
|
| 292 |
"execution_count": null,
|
| 293 |
"id": "cb886d45",
|
| 294 |
"metadata": {},
|
| 295 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
"source": [
|
| 297 |
"#| export\n",
|
| 298 |
"#.py launch\n",
|
|
|
|
| 305 |
"execution_count": null,
|
| 306 |
"id": "28e8d888-e790-46fa-bbac-4511b9ab796c",
|
| 307 |
"metadata": {},
|
| 308 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
"source": [
|
| 310 |
"#| hide\n",
|
| 311 |
"app.close()"
|
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
|
| 2 |
|
| 3 |
# %% auto 0
|
| 4 |
-
__all__ = ['secret_import_failed', 'tts_voices', 'launch_kwargs', '
|
|
|
|
| 5 |
|
| 6 |
# %% app.ipynb 1
|
| 7 |
#tts_openai_secrets.py content:
|
|
@@ -30,6 +31,8 @@ if secret_import_failed == True:
|
|
| 30 |
# %% app.ipynb 3
|
| 31 |
import gradio as gr
|
| 32 |
import openai
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# %% app.ipynb 4
|
| 35 |
try:
|
|
@@ -42,22 +45,116 @@ except:
|
|
| 42 |
tts_voices = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']
|
| 43 |
|
| 44 |
# %% app.ipynb 6
|
| 45 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
client = openai.OpenAI()
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
client.close()
|
| 54 |
-
return response.content
|
| 55 |
|
| 56 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def get_input_text_len(input_text):
|
| 58 |
return len(input_text)
|
| 59 |
|
| 60 |
-
# %% app.ipynb
|
| 61 |
with gr.Blocks(title='OpenAI TTS', head='OpenAI TTS') as app:
|
| 62 |
gr.Markdown("# OpenAI TTS")
|
| 63 |
gr.Markdown("Start typing below and then click **Go** to create the speech from your text. The current limit is 4,000 characters.")
|
|
@@ -75,11 +172,11 @@ with gr.Blocks(title='OpenAI TTS', head='OpenAI TTS') as app:
|
|
| 75 |
clear_btn.click(fn=lambda: '', outputs=input_text)
|
| 76 |
|
| 77 |
|
| 78 |
-
# %% app.ipynb
|
| 79 |
launch_kwargs = {'auth':('username',GRADIO_PASSWORD),
|
| 80 |
'auth_message':'Please log in to Mat\'s TTS App with username: username and password.'}
|
| 81 |
|
| 82 |
-
# %% app.ipynb
|
| 83 |
#.py launch
|
| 84 |
if __name__ == "__main__":
|
| 85 |
app.launch(**launch_kwargs)
|
|
|
|
| 1 |
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
|
| 2 |
|
| 3 |
# %% auto 0
|
| 4 |
+
__all__ = ['secret_import_failed', 'tts_voices', 'launch_kwargs', 'split_text', 'concatenate_mp3', 'create_speech',
|
| 5 |
+
'get_input_text_len']
|
| 6 |
|
| 7 |
# %% app.ipynb 1
|
| 8 |
#tts_openai_secrets.py content:
|
|
|
|
| 31 |
# %% app.ipynb 3
|
| 32 |
import gradio as gr
|
| 33 |
import openai
|
| 34 |
+
from pydub import AudioSegment
|
| 35 |
+
import io
|
| 36 |
|
| 37 |
# %% app.ipynb 4
|
| 38 |
try:
|
|
|
|
| 45 |
tts_voices = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']
|
| 46 |
|
| 47 |
# %% app.ipynb 6
|
| 48 |
+
def split_text(input_text, max_length=4000, lookback=1000):
|
| 49 |
+
# If the text is shorter than the max_length, return it as is
|
| 50 |
+
if len(input_text) <= max_length:
|
| 51 |
+
return [input_text]
|
| 52 |
+
|
| 53 |
+
chunks = []
|
| 54 |
+
while input_text:
|
| 55 |
+
# Check if the remaining text is shorter than the max_length
|
| 56 |
+
if len(input_text) <= max_length:
|
| 57 |
+
chunks.append(input_text)
|
| 58 |
+
break
|
| 59 |
+
|
| 60 |
+
# Define the split point, initially set to max_length
|
| 61 |
+
split_point = max_length
|
| 62 |
+
|
| 63 |
+
# Look for a newline in the last 'lookback' characters
|
| 64 |
+
newline_index = input_text.rfind('\n', max_length-lookback, max_length)
|
| 65 |
+
if newline_index != -1:
|
| 66 |
+
split_point = newline_index + 1 # Include the newline in the current chunk
|
| 67 |
+
|
| 68 |
+
# If no newline, look for a period followed by space
|
| 69 |
+
elif '. ' in input_text[max_length-lookback:max_length]:
|
| 70 |
+
# Find the last '. ' in the lookback range
|
| 71 |
+
period_index = input_text.rfind('. ', max_length-lookback, max_length)
|
| 72 |
+
split_point = period_index + 2 # Split after the space
|
| 73 |
+
|
| 74 |
+
# Split the text and update the input_text
|
| 75 |
+
chunks.append(input_text[:split_point])
|
| 76 |
+
input_text = input_text[split_point:]
|
| 77 |
+
|
| 78 |
+
return chunks
|
| 79 |
+
|
| 80 |
+
# %% app.ipynb 7
|
| 81 |
+
def concatenate_mp3(mp3_files):
|
| 82 |
+
if len(mp3_files) == 1:
|
| 83 |
+
return mp3_files[0]
|
| 84 |
+
else:
|
| 85 |
+
# Initialize an empty AudioSegment object for concatenation
|
| 86 |
+
combined = AudioSegment.empty()
|
| 87 |
+
|
| 88 |
+
# Write out audio file responses as individual files for debugging
|
| 89 |
+
# for idx, mp3_data in enumerate(mp3_files):
|
| 90 |
+
# with open(f'./{idx}.mp3', 'wb') as f:
|
| 91 |
+
# f.write(mp3_data)
|
| 92 |
+
|
| 93 |
+
# Loop through the list of mp3 binary data
|
| 94 |
+
for mp3_data in mp3_files:
|
| 95 |
+
# Convert binary data to an audio segment
|
| 96 |
+
audio_segment = AudioSegment.from_file(io.BytesIO(mp3_data), format="mp3")
|
| 97 |
+
# Concatenate this segment to the combined segment
|
| 98 |
+
combined += audio_segment
|
| 99 |
+
|
| 100 |
+
# Export the combined segment to a new mp3 file
|
| 101 |
+
# Use a BytesIO object to handle this in memory
|
| 102 |
+
combined_mp3 = io.BytesIO()
|
| 103 |
+
combined.export(combined_mp3, format="mp3")
|
| 104 |
+
|
| 105 |
+
# Seek to the start so it's ready for reading
|
| 106 |
+
combined_mp3.seek(0)
|
| 107 |
+
|
| 108 |
+
return combined_mp3.getvalue()
|
| 109 |
+
|
| 110 |
+
# %% app.ipynb 8
|
| 111 |
+
def create_speech(input_text, model='tts-1', voice='alloy', progress=gr.Progress()):
|
| 112 |
+
# Split the input text into chunks
|
| 113 |
+
chunks = split_text(input_text)
|
| 114 |
+
|
| 115 |
+
# Initialize the progress bar
|
| 116 |
+
progress(0, desc="Starting TTS processing...")
|
| 117 |
+
|
| 118 |
+
# Initialize a list to hold the audio data of each chunk
|
| 119 |
+
audio_data = []
|
| 120 |
+
|
| 121 |
+
# Create a client instance for OpenAI
|
| 122 |
client = openai.OpenAI()
|
| 123 |
+
|
| 124 |
+
# Calculate the progress increment for each chunk
|
| 125 |
+
progress_increment = 1.0 / len(chunks)
|
| 126 |
+
|
| 127 |
+
# Process each chunk
|
| 128 |
+
for i, chunk in enumerate(chunks):
|
| 129 |
+
response = client.audio.speech.create(
|
| 130 |
+
model=model,
|
| 131 |
+
voice=voice,
|
| 132 |
+
input=chunk,
|
| 133 |
+
speed=1.0
|
| 134 |
+
)
|
| 135 |
+
# Append the audio content of the response to the list
|
| 136 |
+
audio_data.append(response.content)
|
| 137 |
+
|
| 138 |
+
# Update the progress bar
|
| 139 |
+
progress((i + 1) * progress_increment, desc=f"Processing chunk {i + 1} of {len(chunks)}")
|
| 140 |
+
|
| 141 |
+
# Close the client connection
|
| 142 |
client.close()
|
|
|
|
| 143 |
|
| 144 |
+
# Concatenate the audio data from all chunks
|
| 145 |
+
combined_audio = concatenate_mp3(audio_data)
|
| 146 |
+
|
| 147 |
+
# Final update to the progress bar
|
| 148 |
+
progress(1, desc="Processing completed")
|
| 149 |
+
|
| 150 |
+
return combined_audio
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
# %% app.ipynb 9
|
| 154 |
def get_input_text_len(input_text):
|
| 155 |
return len(input_text)
|
| 156 |
|
| 157 |
+
# %% app.ipynb 10
|
| 158 |
with gr.Blocks(title='OpenAI TTS', head='OpenAI TTS') as app:
|
| 159 |
gr.Markdown("# OpenAI TTS")
|
| 160 |
gr.Markdown("Start typing below and then click **Go** to create the speech from your text. The current limit is 4,000 characters.")
|
|
|
|
| 172 |
clear_btn.click(fn=lambda: '', outputs=input_text)
|
| 173 |
|
| 174 |
|
| 175 |
+
# %% app.ipynb 11
|
| 176 |
launch_kwargs = {'auth':('username',GRADIO_PASSWORD),
|
| 177 |
'auth_message':'Please log in to Mat\'s TTS App with username: username and password.'}
|
| 178 |
|
| 179 |
+
# %% app.ipynb 13
|
| 180 |
#.py launch
|
| 181 |
if __name__ == "__main__":
|
| 182 |
app.launch(**launch_kwargs)
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
-
openai==1.
|
| 2 |
-
gradio==4.
|
|
|
|
|
|
| 1 |
+
openai==1.10.0
|
| 2 |
+
gradio==4.16.0
|
| 3 |
+
pydub==0.25.1
|