Create decoder.py
Browse files- orpheus-tts/decoder.py +141 -0
orpheus-tts/decoder.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from snac import SNAC
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
import asyncio
|
5 |
+
import threading
|
6 |
+
import queue
|
7 |
+
import os
|
8 |
+
|
9 |
+
|
10 |
+
model = SNAC.from_pretrained("hubertsiuzdak/snac_24khz").eval()
|
11 |
+
|
12 |
+
snac_device = os.environ.get("SNAC_DEVICE", "cuda" if torch.cuda.is_available() else "cpu")
|
13 |
+
model = model.to(snac_device)
|
14 |
+
|
15 |
+
|
16 |
+
def convert_to_audio(multiframe, count):
|
17 |
+
frames = []
|
18 |
+
if len(multiframe) < 7:
|
19 |
+
return
|
20 |
+
|
21 |
+
codes_0 = torch.tensor([], device=snac_device, dtype=torch.int32)
|
22 |
+
codes_1 = torch.tensor([], device=snac_device, dtype=torch.int32)
|
23 |
+
codes_2 = torch.tensor([], device=snac_device, dtype=torch.int32)
|
24 |
+
|
25 |
+
num_frames = len(multiframe) // 7
|
26 |
+
frame = multiframe[:num_frames*7]
|
27 |
+
|
28 |
+
for j in range(num_frames):
|
29 |
+
i = 7*j
|
30 |
+
if codes_0.shape[0] == 0:
|
31 |
+
codes_0 = torch.tensor([frame[i]], device=snac_device, dtype=torch.int32)
|
32 |
+
else:
|
33 |
+
codes_0 = torch.cat([codes_0, torch.tensor([frame[i]], device=snac_device, dtype=torch.int32)])
|
34 |
+
|
35 |
+
if codes_1.shape[0] == 0:
|
36 |
+
|
37 |
+
codes_1 = torch.tensor([frame[i+1]], device=snac_device, dtype=torch.int32)
|
38 |
+
codes_1 = torch.cat([codes_1, torch.tensor([frame[i+4]], device=snac_device, dtype=torch.int32)])
|
39 |
+
else:
|
40 |
+
codes_1 = torch.cat([codes_1, torch.tensor([frame[i+1]], device=snac_device, dtype=torch.int32)])
|
41 |
+
codes_1 = torch.cat([codes_1, torch.tensor([frame[i+4]], device=snac_device, dtype=torch.int32)])
|
42 |
+
|
43 |
+
if codes_2.shape[0] == 0:
|
44 |
+
codes_2 = torch.tensor([frame[i+2]], device=snac_device, dtype=torch.int32)
|
45 |
+
codes_2 = torch.cat([codes_2, torch.tensor([frame[i+3]], device=snac_device, dtype=torch.int32)])
|
46 |
+
codes_2 = torch.cat([codes_2, torch.tensor([frame[i+5]], device=snac_device, dtype=torch.int32)])
|
47 |
+
codes_2 = torch.cat([codes_2, torch.tensor([frame[i+6]], device=snac_device, dtype=torch.int32)])
|
48 |
+
else:
|
49 |
+
codes_2 = torch.cat([codes_2, torch.tensor([frame[i+2]], device=snac_device, dtype=torch.int32)])
|
50 |
+
codes_2 = torch.cat([codes_2, torch.tensor([frame[i+3]], device=snac_device, dtype=torch.int32)])
|
51 |
+
codes_2 = torch.cat([codes_2, torch.tensor([frame[i+5]], device=snac_device, dtype=torch.int32)])
|
52 |
+
codes_2 = torch.cat([codes_2, torch.tensor([frame[i+6]], device=snac_device, dtype=torch.int32)])
|
53 |
+
|
54 |
+
codes = [codes_0.unsqueeze(0), codes_1.unsqueeze(0), codes_2.unsqueeze(0)]
|
55 |
+
# check that all tokens are between 0 and 4096 otherwise return *
|
56 |
+
if torch.any(codes[0] < 0) or torch.any(codes[0] > 4096) or torch.any(codes[1] < 0) or torch.any(codes[1] > 4096) or torch.any(codes[2] < 0) or torch.any(codes[2] > 4096):
|
57 |
+
return
|
58 |
+
|
59 |
+
with torch.inference_mode():
|
60 |
+
audio_hat = model.decode(codes)
|
61 |
+
|
62 |
+
audio_slice = audio_hat[:, :, 2048:4096]
|
63 |
+
detached_audio = audio_slice.detach().cpu()
|
64 |
+
audio_np = detached_audio.numpy()
|
65 |
+
audio_int16 = (audio_np * 32767).astype(np.int16)
|
66 |
+
audio_bytes = audio_int16.tobytes()
|
67 |
+
return audio_bytes
|
68 |
+
|
69 |
+
def turn_token_into_id(token_string, index):
|
70 |
+
# Strip whitespace
|
71 |
+
token_string = token_string.strip()
|
72 |
+
|
73 |
+
# Find the last token in the string
|
74 |
+
last_token_start = token_string.rfind("<custom_token_")
|
75 |
+
|
76 |
+
if last_token_start == -1:
|
77 |
+
print("No token found in the string")
|
78 |
+
return None
|
79 |
+
|
80 |
+
# Extract the last token
|
81 |
+
last_token = token_string[last_token_start:]
|
82 |
+
|
83 |
+
# Process the last token
|
84 |
+
if last_token.startswith("<custom_token_") and last_token.endswith(">"):
|
85 |
+
try:
|
86 |
+
number_str = last_token[14:-1]
|
87 |
+
return int(number_str) - 10 - ((index % 7) * 4096)
|
88 |
+
except ValueError:
|
89 |
+
return None
|
90 |
+
else:
|
91 |
+
return None
|
92 |
+
|
93 |
+
|
94 |
+
async def tokens_decoder(token_gen):
|
95 |
+
buffer = []
|
96 |
+
count = 0
|
97 |
+
async for token_sim in token_gen:
|
98 |
+
token = turn_token_into_id(token_sim, count)
|
99 |
+
if token is None:
|
100 |
+
pass
|
101 |
+
else:
|
102 |
+
if token > 0:
|
103 |
+
buffer.append(token)
|
104 |
+
count += 1
|
105 |
+
|
106 |
+
if count % 7 == 0 and count > 27:
|
107 |
+
buffer_to_proc = buffer[-28:]
|
108 |
+
audio_samples = convert_to_audio(buffer_to_proc, count)
|
109 |
+
if audio_samples is not None:
|
110 |
+
yield audio_samples
|
111 |
+
|
112 |
+
|
113 |
+
# ------------------ Synchronous Tokens Decoder Wrapper ------------------ #
|
114 |
+
def tokens_decoder_sync(syn_token_gen):
|
115 |
+
|
116 |
+
audio_queue = queue.Queue()
|
117 |
+
|
118 |
+
# Convert the synchronous token generator into an async generator.
|
119 |
+
async def async_token_gen():
|
120 |
+
for token in syn_token_gen:
|
121 |
+
yield token
|
122 |
+
|
123 |
+
async def async_producer():
|
124 |
+
# tokens_decoder.tokens_decoder is assumed to be an async generator that processes tokens.
|
125 |
+
async for audio_chunk in tokens_decoder(async_token_gen()):
|
126 |
+
audio_queue.put(audio_chunk)
|
127 |
+
audio_queue.put(None) # Sentinel
|
128 |
+
|
129 |
+
def run_async():
|
130 |
+
asyncio.run(async_producer())
|
131 |
+
|
132 |
+
thread = threading.Thread(target=run_async)
|
133 |
+
thread.start()
|
134 |
+
|
135 |
+
while True:
|
136 |
+
audio = audio_queue.get()
|
137 |
+
if audio is None:
|
138 |
+
break
|
139 |
+
yield audio
|
140 |
+
|
141 |
+
thread.join()
|