File size: 8,330 Bytes
3e423f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "_pIZ3ZXNp7cf"
   },
   "source": [
    "Welcome to Tortoise! 🐢🐢🐢🐢\n",
    "\n",
    "Before you begin, I **strongly** recommend you turn on a GPU runtime.\n",
    "\n",
    "There's a reason this is called \"Tortoise\" - this model takes up to a minute to perform inference for a single sentence on a GPU. Expect waits on the order of hours on a CPU."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "id": "JrK20I32grP6",
    "outputId": "9711e23e-3bfc-4cb0-c030-25a1cf460972"
   },
   "outputs": [],
   "source": [
    "!git clone https://github.com/DjKesu/tortoise-tts-fast-cloning.git\n",
    "%cd tortoise-tts-fast-cloning\n",
    "!pip3 install -r requirements.txt --no-deps\n",
    "!pip3 install -e .\n",
    "!pip3 install git+https://github.com/152334H/BigVGAN.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip uninstall transformers\n",
    "!pip install transformers==4.29.2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "zRW4p3ftjZ3Y"
   },
   "source": [
    "## **Restart the runtime!**\n",
    "## Ctrl+M for Colab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "Gen09NM4hONQ"
   },
   "outputs": [],
   "source": [
    "#@title # Setup\n",
    "# Imports used through the rest of the notebook.\n",
    "import torch\n",
    "import torchaudio\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "\n",
    "import IPython\n",
    "\n",
    "from tortoise.api import TextToSpeech\n",
    "from tortoise.utils.audio import load_audio, load_voice, load_voices\n",
    "\n",
    "# This will download all the models used by Tortoise from the HuggingFace hub.\n",
    "tts = TextToSpeech()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bt_aoxONjfL2"
   },
   "outputs": [],
   "source": [
    "# This is the text that will be spoken.\n",
    "text = \"Joining two modalities results in a surprising increase in generalization! What would happen if we combined them all?\" #@param {type:\"string\"}\n",
    "#@markdown Show code for multiline text input\n",
    "# Here's something for the poetically inclined.. (set text=)\n",
    "\"\"\"\n",
    "Then took the other, as just as fair,\n",
    "And having perhaps the better claim,\n",
    "Because it was grassy and wanted wear;\n",
    "Though as for that the passing there\n",
    "Had worn them really about the same,\"\"\"\n",
    "\n",
    "# Pick a \"preset mode\" to determine quality. Options: {\"ultra_fast\", \"fast\" (default), \"standard\", \"high_quality\"}. See docs in api.py\n",
    "# added very_fast preset param option, since it involves resulution with dpm++2m, expected to give best,fastest results\n",
    "preset = \"ultra_fast\" #@param [\"ultra_fast\", \"fast\", \"standard\", \"high_quality\", \"very_fast\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 211
    },
    "id": "SSleVnRAiEE2",
    "outputId": "45b950c7-5c39-4075-bb34-0a76bf19e1bc"
   },
   "outputs": [],
   "source": [
    "#@markdown Tortoise will attempt to mimic voices you provide. It comes pre-packaged\n",
    "#@markdown with some voices you might recognize.\n",
    "\n",
    "#@markdown Let's list all the voices available. These are just some random clips I've gathered\n",
    "#@markdown from the internet as well as a few voices from the training dataset.\n",
    "#@markdown Feel free to add your own clips to the voices/ folder.\n",
    "#@markdown Currently stored my voice clips under voices/krish/ and displaying the random rumblings of my voice.\n",
    "#@markdown each cell is the samples used, skip unless you wanna listen to them\n",
    "%cd tortoise-tts-fast-cloning\n",
    "%ls tortoise/voices/krish\n",
    "import IPython\n",
    "IPython.display.Audio('tortoise/voices/krish/1.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%cd tortoise-tts-fast-cloning\n",
    "%ls tortoise/voices/krish\n",
    "import IPython\n",
    "IPython.display.Audio('tortoise/voices/krish/2.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%cd tortoise-tts-fast-cloning\n",
    "%ls tortoise/voices/krish\n",
    "import IPython\n",
    "IPython.display.Audio('tortoise/voices/krish/3.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%cd tortoise-tts-fast-cloning\n",
    "%ls tortoise/voices/krish\n",
    "import IPython\n",
    "IPython.display.Audio('tortoise/voices/krish/4.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "form",
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 192
    },
    "id": "KEXOKjIvn6NW",
    "outputId": "90c803f3-0b9b-4f24-ccbc-d3f3dcbde48c"
   },
   "outputs": [],
   "source": [
    "#@markdown Pick one of the voices from the output above\n",
    "voice = 'krish' #@param {type:\"string\"}\n",
    "\n",
    "#@markdown Load it and send it through Tortoise.\n",
    "voice_samples, conditioning_latents = load_voice(voice)\n",
    "print(voice_samples)\n",
    "# conditioning_latents = tts.get_conditioning_latents(\n",
    "#     voice_samples,\n",
    "#     return_mels=False,  # Set to True if you want mel spectrograms to be returned\n",
    "#     latent_averaging_mode=1,  # Choose the mode (0, 1, or 2) as needed\n",
    "#     original_tortoise=False,  # Set to True or False as needed\n",
    "# )\n",
    "gen = tts.tts_with_preset(text, voice_samples=voice_samples, conditioning_latents=conditioning_latents, \n",
    "                          preset=preset)\n",
    "torchaudio.save('generated.wav', gen.squeeze(0).cpu(), 24000)\n",
    "IPython.display.Audio('generated.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 41
    },
    "id": "VQgw3KeV8Yqb",
    "outputId": "13db770e-3fcc-4b27-ab78-07a603a299d9"
   },
   "outputs": [],
   "source": [
    "#@markdown Optionally, upload use your own voice by running the next two cells. Change the name of the voice to a voice you want before running\n",
    "#@markdown you upload at least 2 audio clips. They must be a WAV file, 6-10 seconds long.\n",
    "CUSTOM_VOICE_NAME = \"custom\"\n",
    "\n",
    "import os\n",
    "from google.colab import files\n",
    "\n",
    "custom_voice_folder = f\"tortoise/voices/{CUSTOM_VOICE_NAME}\"\n",
    "os.makedirs(custom_voice_folder)\n",
    "for i, file_data in enumerate(files.upload().values()):\n",
    "  with open(os.path.join(custom_voice_folder, f'{i}.wav'), 'wb') as f:\n",
    "    f.write(file_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "jJnJwv3R9uWT"
   },
   "outputs": [],
   "source": [
    "# Generate speech with the custotm voice.\n",
    "voice_samples, conditioning_latents = load_voices(CUSTOM_VOICE_NAME)\n",
    "gen = tts.tts_with_preset(text, voice_samples=voice_samples, conditioning_latents=conditioning_latents, \n",
    "                          preset=preset)\n",
    "torchaudio.save(f'generated-{CUSTOM_VOICE_NAME}.wav', gen.squeeze(0).cpu(), 24000)\n",
    "IPython.display.Audio(f'generated-{CUSTOM_VOICE_NAME}.wav')"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "provenance": []
  },
  "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}