File size: 1,488 Bytes
1646f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from parler_tts import ParlerTTSForConditionalGeneration
from transformers import AutoTokenizer
import soundfile as sf
import pygame
from dora import DoraStatus

model = ParlerTTSForConditionalGeneration.from_pretrained(
    "parler-tts/parler_tts_mini_v0.1"
).to("cuda:0")
tokenizer = AutoTokenizer.from_pretrained("parler-tts/parler_tts_mini_v0.1")

pygame.mixer.init()

input_ids = tokenizer(
    "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast.",
    return_tensors="pt",
).input_ids.to("cuda:0")


class Operator:
    def on_event(
        self,
        dora_event,
        send_output,
    ):
        if dora_event["type"] == "INPUT":
            generation = model.generate(
                max_new_tokens=300,
                input_ids=input_ids,
                prompt_input_ids=tokenizer(
                    dora_event["value"][0].as_py(), return_tensors="pt"
                ).input_ids.to("cuda:0"),
            )
            print(dora_event["value"][0].as_py(), flush=True)
            sf.write(
                f"parler_tts_out.wav",
                generation.cpu().numpy().squeeze(),
                model.config.sampling_rate,
            )

            while pygame.mixer.get_busy():
                pass
            pygame.mixer.music.load(f"parler_tts_out.wav")
            pygame.mixer.music.play()

        return DoraStatus.CONTINUE