File size: 6,438 Bytes
56b10cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a064290
 
 
 
 
 
 
 
 
 
 
 
 
56b10cc
 
 
 
f96d866
56b10cc
f96d866
56b10cc
 
cfbeb2d
2ca4ea9
 
56b10cc
 
 
 
 
 
 
 
 
 
 
26611b1
 
2ca4ea9
 
56b10cc
a064290
 
56b10cc
 
f96d866
56b10cc
 
f96d866
56b10cc
 
 
 
 
a064290
 
 
 
 
 
 
 
 
 
 
 
56b10cc
 
 
 
 
 
 
 
 
 
2ca4ea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56b10cc
2ca4ea9
 
 
 
26611b1
 
56b10cc
 
f96d866
56b10cc
 
f96d866
56b10cc
 
 
 
2ca4ea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a064290
 
2ca4ea9
56b10cc
2ca4ea9
 
26611b1
 
56b10cc
 
f96d866
56b10cc
 
f96d866
2ca4ea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56b10cc
 
 
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
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "tortoise-tts.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "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."
      ],
      "metadata": {
        "id": "_pIZ3ZXNp7cf"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "JrK20I32grP6"
      },
      "outputs": [],
      "source": [
        "!git clone https://github.com/neonbjb/tortoise-tts.git\n",
        "%cd tortoise-tts\n",
        "!pip3 install -r requirements.txt\n",
        "!python3 setup.py install"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# 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 HF hub.\n",
        "tts = TextToSpeech()"
      ],
      "metadata": {
        "id": "Gen09NM4hONQ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "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?\"\n",
        "\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",
        "preset = \"fast\""
      ],
      "metadata": {
        "id": "bt_aoxONjfL2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Tortoise will attempt to mimic voices you provide. It comes pre-packaged\n",
        "# with some voices you might recognize.\n",
        "\n",
        "# Let's list all the voices available. These are just some random clips I've gathered\n",
        "# from the internet as well as a few voices from the training dataset.\n",
        "# Feel free to add your own clips to the voices/ folder.\n",
        "%ls tortoise/voices\n",
        "\n",
        "IPython.display.Audio('tortoise/voices/tom/1.wav')"
      ],
      "metadata": {
        "id": "SSleVnRAiEE2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Pick one of the voices from the output above\n",
        "voice = 'tom'\n",
        "\n",
        "# Load it and send it through Tortoise.\n",
        "voice_samples, conditioning_latents = load_voice(voice)\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')"
      ],
      "metadata": {
        "id": "KEXOKjIvn6NW"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Tortoise can also generate speech using a random voice. The voice changes each time you execute this!\n",
        "# (Note: random voices can be prone to strange utterances)\n",
        "gen = tts.tts_with_preset(text, voice_samples=None, conditioning_latents=None, preset=preset)\n",
        "torchaudio.save('generated.wav', gen.squeeze(0).cpu(), 24000)\n",
        "IPython.display.Audio('generated.wav')"
      ],
      "metadata": {
        "id": "16Xs2SSC3BXa"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# You can also combine conditioning voices. Combining voices produces a new voice\n",
        "# with traits from all the parents.\n",
        "#\n",
        "# Lets see what it would sound like if Picard and Kirk had a kid with a penchant for philosophy:\n",
        "voice_samples, conditioning_latents = load_voices(['pat', 'william'])\n",
        "\n",
        "gen = tts.tts_with_preset(\"They used to say that if man was meant to fly, he’d have wings. But he did fly. He discovered he had to.\", \n",
        "                          voice_samples=None, conditioning_latents=None, preset=preset)\n",
        "torchaudio.save('captain_kirkard.wav', gen.squeeze(0).cpu(), 24000)\n",
        "IPython.display.Audio('captain_kirkard.wav')"
      ],
      "metadata": {
        "id": "fYTk8KUezUr5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "del tts  # Will break other cells, but necessary to conserve RAM if you want to run this cell.\n",
        "\n",
        "# Tortoise comes with some scripts that does a lot of the lifting for you. For example,\n",
        "# read.py will read a text file for you.\n",
        "!python3 tortoise/read.py --voice=train_atkins --textfile=tortoise/data/riding_hood.txt --preset=ultra_fast --output_path=.\n",
        "\n",
        "IPython.display.Audio('train_atkins/combined.wav')\n",
        "# This will take awhile.."
      ],
      "metadata": {
        "id": "t66yqWgu68KL"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}