File size: 9,063 Bytes
2ef4b5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aae23b3
2ef4b5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import random

import gradio as gr
import numpy as np
import torch
import torchaudio
from huggingface_hub import snapshot_download

from play_voice_inference.utils.voice_tokenizer import VoiceBpeTokenizer
from play_voice_inference.models.play_voice import LanguageIdentifiers, SpeakerAttributes, SpeechAttributes, load_play_voice
from play_voice_inference.utils.play_voice_sampler import PlayVoiceSampler
from play_voice_inference.utils.pv_diff_sampler import PlayVoiceDiffusionDecoderSampler

torch.set_grad_enabled(False)
device = torch.device('cuda')

HF_TOKEN = os.environ['HF_TOKEN']
print("Loading models...")

tokenizer = VoiceBpeTokenizer()

MODEL_DIR = snapshot_download('PlayHT/play-voice-v0-multi', token=HF_TOKEN)

PV_AR_PT = MODEL_DIR + '/pv-v1-ar.pth'
play_voice = load_play_voice(PV_AR_PT, device)
sampler = PlayVoiceSampler(play_voice).to(device)

NUM_DIFFUSION_STEPS: int = 150
DIFFUSION_PT = MODEL_DIR + '/pv-v1-diff-xf.pth'
DIFFUSION_VOCODER_PT = MODEL_DIR + '/pv-v1-diff-bigvgan.pt'
vocoder = PlayVoiceDiffusionDecoderSampler.from_path(
    DIFFUSION_PT,
    DIFFUSION_VOCODER_PT,
    steps=NUM_DIFFUSION_STEPS,
    silent=True,
    use_fp16=True,
    device=device
)

print("Preparing voices...")
VOICES_DIR = snapshot_download('PlayHT/play-voice-voices', repo_type='dataset', token=HF_TOKEN)

def load_audio(path: str, sr=24000):
    audio, orig_sr = torchaudio.load(path)
    if orig_sr != sr:
        audio = torchaudio.transforms.Resample(orig_sr, sr)(audio)
    return audio

def make_pcm(audio: torch.Tensor):
    # Must convert to 16-bit PCM for gradio
    # remove batch dim if any
    # if len(audio.shape) > 2:
    #     audio = audio[0]
    # audio = audio.transpose(0, 1) # gradio expects [samples, channels] and throws very unhelpful errors if it's wrong
    gen_np = audio.squeeze().cpu().numpy()
    i = np.iinfo("int16")
    abs_max = 2 ** (i.bits - 1)
    offset = i.min + abs_max
    gen_np = (gen_np * abs_max + offset).clip(i.min, i.max).astype("int16")
    return gen_np

initial_voices = []
for item in os.listdir(VOICES_DIR):
    if item.endswith(".wav"):
        name = os.path.splitext(item)[0]
        initial_voices.append({"name": name, "audio": load_audio(os.path.join(VOICES_DIR, item))})
initial_voices.sort(key=lambda x: x["name"])
print(f"Found {len(initial_voices)} initial voices")

def get_voice_labels(voices: list[dict]):
    labels = []
    for voice in voices:
        labels.append(voice["name"])
    return labels


with gr.Blocks(analytics_enabled=False, title="Play Voice", mode="tts") as iface:
    local_voices = gr.State(initial_voices)

    def get_selected_voice_by_label(voices, label: str):
        labels = get_voice_labels(voices)
        for i, voice_label in enumerate(labels):
            if voice_label == label:
                return voices[i]
        raise Exception("Voice not found: " + label)

    def make_voice_dropdown(voices):
        choices = get_voice_labels(voices)
        return gr.Dropdown(
            choices=choices,
            value=choices[-1] if len(choices) > 0 else None,
            label="Voice",
        )

    def make_enum_dropdown(enum, label, default=None, allow_none=False):
        choices = [e.name for e in enum]
        if allow_none:
            choices.append("none")
        return gr.Dropdown(
            choices=choices,
            value=default,
            label=label,
        )

    def get_enum_value(enum, value):
        if value == "none":
            return None
        return enum[value]

    gr.Markdown("# Play Voice (pretrained)\n")

    with gr.Tab("TTS"):
        speak_text = gr.Textbox(lines=2, placeholder="What would you like to say?", label="Text")
        speak_voice = make_voice_dropdown(initial_voices)

        with gr.Accordion("Settings", open=False):
            speaker_attributes = make_enum_dropdown(
                SpeakerAttributes, "Speaker Attributes", "full_sentence", allow_none=True
            )
            speech_attributes = make_enum_dropdown(SpeechAttributes, "Speech Attributes", "none", allow_none=True)
            language = make_enum_dropdown(LanguageIdentifiers, "Language", "none", allow_none=True)

            temperature = gr.Slider(minimum=0, maximum=2.0, value=0.3, label="Temperature")
            repetition_penalty = gr.Slider(minimum=1.0, maximum=10.0, value=1.8, label="Repetition Penalty")
            filter_thresh = gr.Slider(minimum=0.1, maximum=1.0, value=0.75, label="Top-p Threshold")

            voice_guidance = gr.Slider(minimum=0.0, maximum=6.0, value=0.4, label="Voice Guidance")
            style_guidance = gr.Slider(minimum=0.0, maximum=6.0, value=0.1, label="Style Guidance")
            text_guidance = gr.Slider(minimum=0.0, maximum=6.0, value=0.6, label="Text Guidance")

        speak_submit = gr.Button("Speak!")
        speak_result = gr.Audio(label="Result", interactive=False)
        ref_voice = gr.Audio(label="Reference Voice", interactive=False)

    @torch.no_grad()
    def handle_speak(
        text,
        voices,
        voice_name,
        voice_guidance,
        speaker_attributes,
        speech_attributes,
        language,
        temperature,
        repetition_penalty,
        top_p,
        style_guidance,
        text_guidance,
    ):
        if text.strip() == "":
            text = "I am PlayVoice, the voice of the future. Feed me your words and I will speak them, hahahaha!"
        voice = get_selected_voice_by_label(voices, voice_name)
        seed = random.randint(0, 2**32 - 1)

        print(f"Voice: {voice['name']} Text: {text}")

        voice_emb = sampler.get_voice_embedding(voice["audio"])

        text_tokens = []
        text_tokens.append(torch.tensor(tokenizer.encode(text), dtype=torch.int, device=device))
        text_tokens = torch.nn.utils.rnn.pad_sequence(text_tokens, batch_first=True, padding_value=0)

        torch.manual_seed(seed)
        sample_result = sampler.sample_batched(
            text_tokens=text_tokens,
            text_guidance=text_guidance,
            voice_emb=voice_emb,
            voice_guidance=voice_guidance,
            speaker_attributes=get_enum_value(SpeakerAttributes, speaker_attributes),
            speech_attributes=get_enum_value(SpeechAttributes, speech_attributes),
            language_identifier=get_enum_value(LanguageIdentifiers, language),
            style_guidance=float(style_guidance),
            temperature=float(temperature),
            repetition_penalty=float(repetition_penalty),
            top_p=float(top_p),
        )

        latents = sample_result["latents"]

        audio = vocoder.sample(text_tokens, latents, ref_wav=voice["audio"])
        audio = make_pcm(audio)

        return {
            speak_result: (vocoder.OUTPUT_FREQUENCY, audio),
            ref_voice: (22050, make_pcm(voice["audio"])),
        }

    speak_submit.click(
        handle_speak,
        inputs=[
            speak_text,
            local_voices,
            speak_voice,
            voice_guidance,
            speaker_attributes,
            speech_attributes,
            language,
            temperature,
            repetition_penalty,
            filter_thresh,
            style_guidance,
            text_guidance,
        ],
        outputs=[
            speak_result,
            ref_voice,
        ],
    )

    with gr.Tab("Clone Voice"):
        new_voice_name = gr.Textbox(value="cloned-voice", label="Voice Name")
        new_voice_audio = gr.Audio(label="Voice Audio (20s min, ideally 30s, anything longer will be truncated)",
            sources=["upload", "microphone"],
        )
        new_voice_submit = gr.Button("Create!")
        new_voice_result = gr.Label("")

    def on_new_voice_submit(voices, name, raw_audio):
        assert raw_audio is not None, "Must provide audio"

        sr = raw_audio[0]
        torch_audio = torch.from_numpy(raw_audio[1]).float() / 32768.0

        if torch_audio.ndim == 1:
            torch_audio = torch_audio.unsqueeze(0)
        else:
            torch_audio = torch_audio.transpose(0, 1).mean(dim=0, keepdim=True)

        if sr != 24000:
            if sr < 16000:
                raise Exception(
                    "Garbage in, garbage out. Please provide audio with a sample rate of at least 16kHz, ideally 24kHz."
                )
            torch_audio = torchaudio.transforms.Resample(sr, 24000)(torch_audio)

        # trim to 30s
        if torch_audio.shape[1] > 24000 * 30:
            torch_audio = torch_audio[:, : 24000 * 30]

        # add to local voices
        voices.append({"name": name, "audio": torch_audio})

        return {
            speak_voice: make_voice_dropdown(voices),
            new_voice_result: f"Created voice {name}",
        }

    new_voice_submit.click(
        on_new_voice_submit,
        inputs = [
            local_voices,
            new_voice_name,
            new_voice_audio
        ],
        outputs=[
            speak_voice,
            new_voice_result
        ]
    )


iface.launch(show_error=True, share=False)