Spaces:
Running
on
Zero
Running
on
Zero
update model inference
Browse files- cosyvoice/cli/cosyvoice.py +4 -4
- cosyvoice/cli/model.py +64 -32
- cosyvoice/flow/length_regulator.py +1 -1
- cosyvoice/llm/llm.py +2 -2
- webui.py +14 -25
cosyvoice/cli/cosyvoice.py
CHANGED
@@ -46,9 +46,9 @@ class CosyVoice:
|
|
46 |
return spks
|
47 |
|
48 |
def inference_sft(self, tts_text, spk_id, stream=False):
|
49 |
-
start_time = time.time()
|
50 |
for i in self.frontend.text_normalize(tts_text, split=True):
|
51 |
model_input = self.frontend.frontend_sft(i, spk_id)
|
|
|
52 |
for model_output in self.model.inference(**model_input, stream=stream):
|
53 |
speech_len = model_output['tts_speech'].shape[1] / 22050
|
54 |
logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
|
@@ -56,10 +56,10 @@ class CosyVoice:
|
|
56 |
start_time = time.time()
|
57 |
|
58 |
def inference_zero_shot(self, tts_text, prompt_text, prompt_speech_16k, stream=False):
|
59 |
-
start_time = time.time()
|
60 |
prompt_text = self.frontend.text_normalize(prompt_text, split=False)
|
61 |
for i in self.frontend.text_normalize(tts_text, split=True):
|
62 |
model_input = self.frontend.frontend_zero_shot(i, prompt_text, prompt_speech_16k)
|
|
|
63 |
for model_output in self.model.inference(**model_input, stream=stream):
|
64 |
speech_len = model_output['tts_speech'].shape[1] / 22050
|
65 |
logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
|
@@ -69,9 +69,9 @@ class CosyVoice:
|
|
69 |
def inference_cross_lingual(self, tts_text, prompt_speech_16k, stream=False):
|
70 |
if self.frontend.instruct is True:
|
71 |
raise ValueError('{} do not support cross_lingual inference'.format(self.model_dir))
|
72 |
-
start_time = time.time()
|
73 |
for i in self.frontend.text_normalize(tts_text, split=True):
|
74 |
model_input = self.frontend.frontend_cross_lingual(i, prompt_speech_16k)
|
|
|
75 |
for model_output in self.model.inference(**model_input, stream=stream):
|
76 |
speech_len = model_output['tts_speech'].shape[1] / 22050
|
77 |
logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
|
@@ -81,10 +81,10 @@ class CosyVoice:
|
|
81 |
def inference_instruct(self, tts_text, spk_id, instruct_text, stream=False):
|
82 |
if self.frontend.instruct is False:
|
83 |
raise ValueError('{} do not support instruct inference'.format(self.model_dir))
|
84 |
-
start_time = time.time()
|
85 |
instruct_text = self.frontend.text_normalize(instruct_text, split=False)
|
86 |
for i in self.frontend.text_normalize(tts_text, split=True):
|
87 |
model_input = self.frontend.frontend_instruct(i, spk_id, instruct_text)
|
|
|
88 |
for model_output in self.model.inference(**model_input, stream=stream):
|
89 |
speech_len = model_output['tts_speech'].shape[1] / 22050
|
90 |
logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
|
|
|
46 |
return spks
|
47 |
|
48 |
def inference_sft(self, tts_text, spk_id, stream=False):
|
|
|
49 |
for i in self.frontend.text_normalize(tts_text, split=True):
|
50 |
model_input = self.frontend.frontend_sft(i, spk_id)
|
51 |
+
start_time = time.time()
|
52 |
for model_output in self.model.inference(**model_input, stream=stream):
|
53 |
speech_len = model_output['tts_speech'].shape[1] / 22050
|
54 |
logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
|
|
|
56 |
start_time = time.time()
|
57 |
|
58 |
def inference_zero_shot(self, tts_text, prompt_text, prompt_speech_16k, stream=False):
|
|
|
59 |
prompt_text = self.frontend.text_normalize(prompt_text, split=False)
|
60 |
for i in self.frontend.text_normalize(tts_text, split=True):
|
61 |
model_input = self.frontend.frontend_zero_shot(i, prompt_text, prompt_speech_16k)
|
62 |
+
start_time = time.time()
|
63 |
for model_output in self.model.inference(**model_input, stream=stream):
|
64 |
speech_len = model_output['tts_speech'].shape[1] / 22050
|
65 |
logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
|
|
|
69 |
def inference_cross_lingual(self, tts_text, prompt_speech_16k, stream=False):
|
70 |
if self.frontend.instruct is True:
|
71 |
raise ValueError('{} do not support cross_lingual inference'.format(self.model_dir))
|
|
|
72 |
for i in self.frontend.text_normalize(tts_text, split=True):
|
73 |
model_input = self.frontend.frontend_cross_lingual(i, prompt_speech_16k)
|
74 |
+
start_time = time.time()
|
75 |
for model_output in self.model.inference(**model_input, stream=stream):
|
76 |
speech_len = model_output['tts_speech'].shape[1] / 22050
|
77 |
logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
|
|
|
81 |
def inference_instruct(self, tts_text, spk_id, instruct_text, stream=False):
|
82 |
if self.frontend.instruct is False:
|
83 |
raise ValueError('{} do not support instruct inference'.format(self.model_dir))
|
|
|
84 |
instruct_text = self.frontend.text_normalize(instruct_text, split=False)
|
85 |
for i in self.frontend.text_normalize(tts_text, split=True):
|
86 |
model_input = self.frontend.frontend_instruct(i, spk_id, instruct_text)
|
87 |
+
start_time = time.time()
|
88 |
for model_output in self.model.inference(**model_input, stream=stream):
|
89 |
speech_len = model_output['tts_speech'].shape[1] / 22050
|
90 |
logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
|
cosyvoice/cli/model.py
CHANGED
@@ -13,6 +13,9 @@
|
|
13 |
# limitations under the License.
|
14 |
import torch
|
15 |
import numpy as np
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
class CosyVoiceModel:
|
@@ -25,10 +28,13 @@ class CosyVoiceModel:
|
|
25 |
self.llm = llm
|
26 |
self.flow = flow
|
27 |
self.hift = hift
|
28 |
-
self.stream_win_len = 60
|
29 |
-
self.stream_hop_len = 50
|
30 |
-
self.overlap = 4395 # 10 token equals 4395 sample point
|
31 |
self.window = np.hamming(2 * self.overlap)
|
|
|
|
|
|
|
32 |
|
33 |
def load(self, llm_model, flow_model, hift_model):
|
34 |
self.llm.load_state_dict(torch.load(llm_model, map_location=self.device))
|
@@ -38,13 +44,8 @@ class CosyVoiceModel:
|
|
38 |
self.hift.load_state_dict(torch.load(hift_model, map_location=self.device))
|
39 |
self.hift.to(self.device).eval()
|
40 |
|
41 |
-
def
|
42 |
-
|
43 |
-
llm_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32), llm_prompt_speech_token_len=torch.zeros(1, dtype=torch.int32),
|
44 |
-
flow_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32), flow_prompt_speech_token_len=torch.zeros(1, dtype=torch.int32),
|
45 |
-
prompt_speech_feat=torch.zeros(1, 0, 80), prompt_speech_feat_len=torch.zeros(1, dtype=torch.int32), stream=False):
|
46 |
-
if stream is True:
|
47 |
-
tts_speech_token, cache_speech = [], None
|
48 |
for i in self.llm.inference(text=text.to(self.device),
|
49 |
text_len=text_len.to(self.device),
|
50 |
prompt_text=prompt_text.to(self.device),
|
@@ -56,10 +57,56 @@ class CosyVoiceModel:
|
|
56 |
sampling=25,
|
57 |
max_token_text_ratio=30,
|
58 |
min_token_text_ratio=3,
|
59 |
-
stream=
|
60 |
-
tts_speech_token.append(i)
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
this_tts_mel = self.flow.inference(token=this_tts_speech_token,
|
64 |
token_len=torch.tensor([this_tts_speech_token.size(1)], dtype=torch.int32).to(self.device),
|
65 |
prompt_token=flow_prompt_speech_token.to(self.device),
|
@@ -68,29 +115,14 @@ class CosyVoiceModel:
|
|
68 |
prompt_feat_len=prompt_speech_feat_len.to(self.device),
|
69 |
embedding=flow_embedding.to(self.device))
|
70 |
this_tts_speech = self.hift.inference(mel=this_tts_mel).cpu()
|
71 |
-
# fade in/out if necessary
|
72 |
-
if cache_speech is not None:
|
73 |
-
this_tts_speech[:, :self.overlap] = this_tts_speech[:, :self.overlap] * self.window[:self.overlap] + cache_speech * self.window[-self.overlap:]
|
74 |
-
yield {'tts_speech': this_tts_speech[:, :-self.overlap]}
|
75 |
-
cache_speech = this_tts_speech[:, -self.overlap:]
|
76 |
-
tts_speech_token = tts_speech_token[-(self.stream_win_len - self.stream_hop_len):]
|
77 |
-
# deal with remain tokens
|
78 |
-
if cache_speech is None or len(tts_speech_token) > self.stream_win_len - self.stream_hop_len:
|
79 |
-
this_tts_speech_token = torch.concat(tts_speech_token, dim=1)
|
80 |
-
this_tts_mel = self.flow.inference(token=this_tts_speech_token,
|
81 |
-
token_len=torch.tensor([this_tts_speech_token.size(1)], dtype=torch.int32).to(self.device),
|
82 |
-
prompt_token=flow_prompt_speech_token.to(self.device),
|
83 |
-
prompt_token_len=flow_prompt_speech_token_len.to(self.device),
|
84 |
-
prompt_feat=prompt_speech_feat.to(self.device),
|
85 |
-
prompt_feat_len=prompt_speech_feat_len.to(self.device),
|
86 |
-
embedding=flow_embedding.to(self.device))
|
87 |
-
this_tts_speech = self.hift.inference(mel=this_tts_mel).cpu()
|
88 |
if cache_speech is not None:
|
89 |
this_tts_speech[:, :self.overlap] = this_tts_speech[:, :self.overlap] * self.window[:self.overlap] + cache_speech * self.window[-self.overlap:]
|
90 |
yield {'tts_speech': this_tts_speech}
|
91 |
else:
|
92 |
-
assert len(tts_speech_token) == self.stream_win_len - self.stream_hop_len, 'tts_speech_token not equal to {}'.format(self.stream_win_len - self.stream_hop_len)
|
93 |
yield {'tts_speech': cache_speech}
|
|
|
|
|
94 |
else:
|
95 |
tts_speech_token = []
|
96 |
for i in self.llm.inference(text=text.to(self.device),
|
|
|
13 |
# limitations under the License.
|
14 |
import torch
|
15 |
import numpy as np
|
16 |
+
import threading
|
17 |
+
import time
|
18 |
+
from contextlib import nullcontext
|
19 |
|
20 |
|
21 |
class CosyVoiceModel:
|
|
|
28 |
self.llm = llm
|
29 |
self.flow = flow
|
30 |
self.hift = hift
|
31 |
+
self.stream_win_len = 60 * 4
|
32 |
+
self.stream_hop_len = 50 * 4
|
33 |
+
self.overlap = 4395 * 4 # 10 token equals 4395 sample point
|
34 |
self.window = np.hamming(2 * self.overlap)
|
35 |
+
self.llm_context = torch.cuda.stream(torch.cuda.Stream(self.device)) if torch.cuda.is_available() else nullcontext()
|
36 |
+
self.flow_hift_context = torch.cuda.stream(torch.cuda.Stream(self.device)) if torch.cuda.is_available() else nullcontext()
|
37 |
+
self.lock = threading.Lock()
|
38 |
|
39 |
def load(self, llm_model, flow_model, hift_model):
|
40 |
self.llm.load_state_dict(torch.load(llm_model, map_location=self.device))
|
|
|
44 |
self.hift.load_state_dict(torch.load(hift_model, map_location=self.device))
|
45 |
self.hift.to(self.device).eval()
|
46 |
|
47 |
+
def llm_job(self, text, text_len, prompt_text, prompt_text_len, llm_prompt_speech_token, llm_prompt_speech_token_len, llm_embedding):
|
48 |
+
with self.llm_context:
|
|
|
|
|
|
|
|
|
|
|
49 |
for i in self.llm.inference(text=text.to(self.device),
|
50 |
text_len=text_len.to(self.device),
|
51 |
prompt_text=prompt_text.to(self.device),
|
|
|
57 |
sampling=25,
|
58 |
max_token_text_ratio=30,
|
59 |
min_token_text_ratio=3,
|
60 |
+
stream=True):
|
61 |
+
self.tts_speech_token.append(i)
|
62 |
+
self.llm_end = True
|
63 |
+
|
64 |
+
def token2wav(self, token, prompt_token, prompt_token_len, prompt_feat, prompt_feat_len, embedding):
|
65 |
+
with self.flow_hift_context:
|
66 |
+
tts_mel = self.flow.inference(token=token.to(self.device),
|
67 |
+
token_len=torch.tensor([token.size(1)], dtype=torch.int32).to(self.device),
|
68 |
+
prompt_token=prompt_token.to(self.device),
|
69 |
+
prompt_token_len=prompt_token_len.to(self.device),
|
70 |
+
prompt_feat=prompt_feat.to(self.device),
|
71 |
+
prompt_feat_len=prompt_feat_len.to(self.device),
|
72 |
+
embedding=embedding.to(self.device))
|
73 |
+
tts_speech = self.hift.inference(mel=tts_mel).cpu()
|
74 |
+
return tts_speech
|
75 |
+
|
76 |
+
def inference(self, text, text_len, flow_embedding, llm_embedding=torch.zeros(0, 192),
|
77 |
+
prompt_text=torch.zeros(1, 0, dtype=torch.int32), prompt_text_len=torch.zeros(1, dtype=torch.int32),
|
78 |
+
llm_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32), llm_prompt_speech_token_len=torch.zeros(1, dtype=torch.int32),
|
79 |
+
flow_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32), flow_prompt_speech_token_len=torch.zeros(1, dtype=torch.int32),
|
80 |
+
prompt_speech_feat=torch.zeros(1, 0, 80), prompt_speech_feat_len=torch.zeros(1, dtype=torch.int32), stream=False):
|
81 |
+
if stream is True:
|
82 |
+
self.tts_speech_token, self.llm_end, cache_speech = [], False, None
|
83 |
+
p = threading.Thread(target=self.llm_job, args=(text.to(self.device), text_len.to(self.device), prompt_text.to(self.device), prompt_text_len.to(self.device),
|
84 |
+
llm_prompt_speech_token.to(self.device), llm_prompt_speech_token_len.to(self.device), llm_embedding.to(self.device)))
|
85 |
+
p.start()
|
86 |
+
while True:
|
87 |
+
time.sleep(0.1)
|
88 |
+
if len(self.tts_speech_token) >= self.stream_win_len:
|
89 |
+
this_tts_speech_token = torch.concat(self.tts_speech_token[:self.stream_win_len], dim=1)
|
90 |
+
with self.flow_hift_context:
|
91 |
+
this_tts_speech = self.token2wav(token=this_tts_speech_token,
|
92 |
+
prompt_token=flow_prompt_speech_token.to(self.device),
|
93 |
+
prompt_token_len=flow_prompt_speech_token_len.to(self.device),
|
94 |
+
prompt_feat=prompt_speech_feat.to(self.device),
|
95 |
+
prompt_feat_len=prompt_speech_feat_len.to(self.device),
|
96 |
+
embedding=flow_embedding.to(self.device))
|
97 |
+
# fade in/out if necessary
|
98 |
+
if cache_speech is not None:
|
99 |
+
this_tts_speech[:, :self.overlap] = this_tts_speech[:, :self.overlap] * self.window[:self.overlap] + cache_speech * self.window[-self.overlap:]
|
100 |
+
yield {'tts_speech': this_tts_speech[:, :-self.overlap]}
|
101 |
+
cache_speech = this_tts_speech[:, -self.overlap:]
|
102 |
+
with self.lock:
|
103 |
+
self.tts_speech_token = self.tts_speech_token[self.stream_hop_len:]
|
104 |
+
if self.llm_end is True:
|
105 |
+
break
|
106 |
+
# deal with remain tokens
|
107 |
+
if cache_speech is None or len(self.tts_speech_token) > self.stream_win_len - self.stream_hop_len:
|
108 |
+
this_tts_speech_token = torch.concat(self.tts_speech_token, dim=1)
|
109 |
+
with self.flow_hift_context:
|
110 |
this_tts_mel = self.flow.inference(token=this_tts_speech_token,
|
111 |
token_len=torch.tensor([this_tts_speech_token.size(1)], dtype=torch.int32).to(self.device),
|
112 |
prompt_token=flow_prompt_speech_token.to(self.device),
|
|
|
115 |
prompt_feat_len=prompt_speech_feat_len.to(self.device),
|
116 |
embedding=flow_embedding.to(self.device))
|
117 |
this_tts_speech = self.hift.inference(mel=this_tts_mel).cpu()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
if cache_speech is not None:
|
119 |
this_tts_speech[:, :self.overlap] = this_tts_speech[:, :self.overlap] * self.window[:self.overlap] + cache_speech * self.window[-self.overlap:]
|
120 |
yield {'tts_speech': this_tts_speech}
|
121 |
else:
|
122 |
+
assert len(self.tts_speech_token) == self.stream_win_len - self.stream_hop_len, 'tts_speech_token not equal to {}'.format(self.stream_win_len - self.stream_hop_len)
|
123 |
yield {'tts_speech': cache_speech}
|
124 |
+
p.join()
|
125 |
+
torch.cuda.synchronize()
|
126 |
else:
|
127 |
tts_speech_token = []
|
128 |
for i in self.llm.inference(text=text.to(self.device),
|
cosyvoice/flow/length_regulator.py
CHANGED
@@ -43,7 +43,7 @@ class InterpolateRegulator(nn.Module):
|
|
43 |
def forward(self, x, ylens=None):
|
44 |
# x in (B, T, D)
|
45 |
mask = (~make_pad_mask(ylens)).to(x).unsqueeze(-1)
|
46 |
-
x = F.interpolate(x.transpose(1, 2).contiguous(), size=ylens.max(), mode='
|
47 |
out = self.model(x).transpose(1, 2).contiguous()
|
48 |
olens = ylens
|
49 |
return out * mask, olens
|
|
|
43 |
def forward(self, x, ylens=None):
|
44 |
# x in (B, T, D)
|
45 |
mask = (~make_pad_mask(ylens)).to(x).unsqueeze(-1)
|
46 |
+
x = F.interpolate(x.transpose(1, 2).contiguous(), size=ylens.max(), mode='linear')
|
47 |
out = self.model(x).transpose(1, 2).contiguous()
|
48 |
olens = ylens
|
49 |
return out * mask, olens
|
cosyvoice/llm/llm.py
CHANGED
@@ -174,7 +174,7 @@ class TransformerLM(torch.nn.Module):
|
|
174 |
embedding = self.spk_embed_affine_layer(embedding)
|
175 |
embedding = embedding.unsqueeze(dim=1)
|
176 |
else:
|
177 |
-
embedding = torch.zeros(1, 0, self.llm_input_size).to(device)
|
178 |
|
179 |
# 3. concat llm_input
|
180 |
sos_eos_emb = self.llm_embedding.weight[self.sos_eos].reshape(1, 1, -1)
|
@@ -182,7 +182,7 @@ class TransformerLM(torch.nn.Module):
|
|
182 |
if prompt_speech_token_len != 0:
|
183 |
prompt_speech_token_emb = self.speech_embedding(prompt_speech_token)
|
184 |
else:
|
185 |
-
prompt_speech_token_emb = torch.zeros(1, 0, self.llm_input_size).to(device)
|
186 |
lm_input = torch.concat([sos_eos_emb, embedding, text, task_id_emb, prompt_speech_token_emb], dim=1)
|
187 |
|
188 |
# 4. cal min/max_length
|
|
|
174 |
embedding = self.spk_embed_affine_layer(embedding)
|
175 |
embedding = embedding.unsqueeze(dim=1)
|
176 |
else:
|
177 |
+
embedding = torch.zeros(1, 0, self.llm_input_size, dtype=text.dtype).to(device)
|
178 |
|
179 |
# 3. concat llm_input
|
180 |
sos_eos_emb = self.llm_embedding.weight[self.sos_eos].reshape(1, 1, -1)
|
|
|
182 |
if prompt_speech_token_len != 0:
|
183 |
prompt_speech_token_emb = self.speech_embedding(prompt_speech_token)
|
184 |
else:
|
185 |
+
prompt_speech_token_emb = torch.zeros(1, 0, self.llm_input_size, dtype=text.dtype).to(device)
|
186 |
lm_input = torch.concat([sos_eos_emb, embedding, text, task_id_emb, prompt_speech_token_emb], dim=1)
|
187 |
|
188 |
# 4. cal min/max_length
|
webui.py
CHANGED
@@ -24,14 +24,8 @@ import torchaudio
|
|
24 |
import random
|
25 |
import librosa
|
26 |
|
27 |
-
import logging
|
28 |
-
logging.getLogger('matplotlib').setLevel(logging.WARNING)
|
29 |
-
|
30 |
from cosyvoice.cli.cosyvoice import CosyVoice
|
31 |
-
from cosyvoice.utils.file_utils import load_wav, speed_change
|
32 |
-
|
33 |
-
logging.basicConfig(level=logging.DEBUG,
|
34 |
-
format='%(asctime)s %(levelname)s %(message)s')
|
35 |
|
36 |
def generate_seed():
|
37 |
seed = random.randint(1, 100000000)
|
@@ -63,10 +57,11 @@ instruct_dict = {'预训练音色': '1. 选择预训练音色\n2. 点击生成
|
|
63 |
'3s极速复刻': '1. 选择prompt音频文件,或录入prompt音频,注意不超过30s,若同时提供,优先选择prompt音频文件\n2. 输入prompt文本\n3. 点击生成音频按钮',
|
64 |
'跨语种复刻': '1. 选择prompt音频文件,或录入prompt音频,注意不超过30s,若同时提供,优先选择prompt音频文件\n2. 点击生成音频按钮',
|
65 |
'自然语言控制': '1. 选择预训练音色\n2. 输入instruct文本\n3. 点击生成音频按钮'}
|
|
|
66 |
def change_instruction(mode_checkbox_group):
|
67 |
return instruct_dict[mode_checkbox_group]
|
68 |
|
69 |
-
def generate_audio(tts_text, mode_checkbox_group, sft_dropdown, prompt_text, prompt_wav_upload, prompt_wav_record, instruct_text, seed, speed_factor):
|
70 |
if prompt_wav_upload is not None:
|
71 |
prompt_wav = prompt_wav_upload
|
72 |
elif prompt_wav_record is not None:
|
@@ -117,32 +112,25 @@ def generate_audio(tts_text, mode_checkbox_group, sft_dropdown, prompt_text, pro
|
|
117 |
if mode_checkbox_group == '预训练音色':
|
118 |
logging.info('get sft inference request')
|
119 |
set_all_random_seed(seed)
|
120 |
-
|
|
|
121 |
elif mode_checkbox_group == '3s极速复刻':
|
122 |
logging.info('get zero_shot inference request')
|
123 |
prompt_speech_16k = postprocess(load_wav(prompt_wav, prompt_sr))
|
124 |
set_all_random_seed(seed)
|
125 |
-
|
|
|
126 |
elif mode_checkbox_group == '跨语种复刻':
|
127 |
logging.info('get cross_lingual inference request')
|
128 |
prompt_speech_16k = postprocess(load_wav(prompt_wav, prompt_sr))
|
129 |
set_all_random_seed(seed)
|
130 |
-
|
|
|
131 |
else:
|
132 |
logging.info('get instruct inference request')
|
133 |
set_all_random_seed(seed)
|
134 |
-
|
135 |
-
|
136 |
-
if speed_factor != 1.0:
|
137 |
-
try:
|
138 |
-
audio_data, sample_rate = speed_change(output["tts_speech"], target_sr, str(speed_factor))
|
139 |
-
audio_data = audio_data.numpy().flatten()
|
140 |
-
except Exception as e:
|
141 |
-
print(f"Failed to change speed of audio: \n{e}")
|
142 |
-
else:
|
143 |
-
audio_data = output['tts_speech'].numpy().flatten()
|
144 |
-
|
145 |
-
return (target_sr, audio_data)
|
146 |
|
147 |
def main():
|
148 |
with gr.Blocks() as demo:
|
@@ -155,6 +143,7 @@ def main():
|
|
155 |
mode_checkbox_group = gr.Radio(choices=inference_mode_list, label='选择推理模式', value=inference_mode_list[0])
|
156 |
instruction_text = gr.Text(label="操作步骤", value=instruct_dict[inference_mode_list[0]], scale=0.5)
|
157 |
sft_dropdown = gr.Dropdown(choices=sft_spk, label='选择预训练音色', value=sft_spk[0], scale=0.25)
|
|
|
158 |
with gr.Column(scale=0.25):
|
159 |
seed_button = gr.Button(value="\U0001F3B2")
|
160 |
seed = gr.Number(value=0, label="随机推理种子")
|
@@ -167,11 +156,11 @@ def main():
|
|
167 |
|
168 |
generate_button = gr.Button("生成音频")
|
169 |
|
170 |
-
audio_output = gr.Audio(label="合成音频")
|
171 |
|
172 |
seed_button.click(generate_seed, inputs=[], outputs=seed)
|
173 |
generate_button.click(generate_audio,
|
174 |
-
inputs=[tts_text, mode_checkbox_group, sft_dropdown, prompt_text, prompt_wav_upload, prompt_wav_record, instruct_text, seed, speed_factor],
|
175 |
outputs=[audio_output])
|
176 |
mode_checkbox_group.change(fn=change_instruction, inputs=[mode_checkbox_group], outputs=[instruction_text])
|
177 |
demo.queue(max_size=4, default_concurrency_limit=2)
|
|
|
24 |
import random
|
25 |
import librosa
|
26 |
|
|
|
|
|
|
|
27 |
from cosyvoice.cli.cosyvoice import CosyVoice
|
28 |
+
from cosyvoice.utils.file_utils import load_wav, speed_change, logging
|
|
|
|
|
|
|
29 |
|
30 |
def generate_seed():
|
31 |
seed = random.randint(1, 100000000)
|
|
|
57 |
'3s极速复刻': '1. 选择prompt音频文件,或录入prompt音频,注意不超过30s,若同时提供,优先选择prompt音频文件\n2. 输入prompt文本\n3. 点击生成音频按钮',
|
58 |
'跨语种复刻': '1. 选择prompt音频文件,或录入prompt音频,注意不超过30s,若同时提供,优先选择prompt音频文件\n2. 点击生成音频按钮',
|
59 |
'自然语言控制': '1. 选择预训练音色\n2. 输入instruct文本\n3. 点击生成音频按钮'}
|
60 |
+
stream_mode_list = [('否', False), ('是', True)]
|
61 |
def change_instruction(mode_checkbox_group):
|
62 |
return instruct_dict[mode_checkbox_group]
|
63 |
|
64 |
+
def generate_audio(tts_text, mode_checkbox_group, sft_dropdown, prompt_text, prompt_wav_upload, prompt_wav_record, instruct_text, seed, stream, speed_factor):
|
65 |
if prompt_wav_upload is not None:
|
66 |
prompt_wav = prompt_wav_upload
|
67 |
elif prompt_wav_record is not None:
|
|
|
112 |
if mode_checkbox_group == '预训练音色':
|
113 |
logging.info('get sft inference request')
|
114 |
set_all_random_seed(seed)
|
115 |
+
for i in cosyvoice.inference_sft(tts_text, sft_dropdown, stream=stream):
|
116 |
+
yield (target_sr, i['tts_speech'].numpy().flatten())
|
117 |
elif mode_checkbox_group == '3s极速复刻':
|
118 |
logging.info('get zero_shot inference request')
|
119 |
prompt_speech_16k = postprocess(load_wav(prompt_wav, prompt_sr))
|
120 |
set_all_random_seed(seed)
|
121 |
+
for i in cosyvoice.inference_zero_shot(tts_text, prompt_text, prompt_speech_16k, stream=stream):
|
122 |
+
yield (target_sr, i['tts_speech'].numpy().flatten())
|
123 |
elif mode_checkbox_group == '跨语种复刻':
|
124 |
logging.info('get cross_lingual inference request')
|
125 |
prompt_speech_16k = postprocess(load_wav(prompt_wav, prompt_sr))
|
126 |
set_all_random_seed(seed)
|
127 |
+
for i in cosyvoice.inference_cross_lingual(tts_text, prompt_speech_16k, stream=stream):
|
128 |
+
yield (target_sr, i['tts_speech'].numpy().flatten())
|
129 |
else:
|
130 |
logging.info('get instruct inference request')
|
131 |
set_all_random_seed(seed)
|
132 |
+
for i in cosyvoice.inference_instruct(tts_text, sft_dropdown, instruct_text, stream=stream):
|
133 |
+
yield (target_sr, i['tts_speech'].numpy().flatten())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
def main():
|
136 |
with gr.Blocks() as demo:
|
|
|
143 |
mode_checkbox_group = gr.Radio(choices=inference_mode_list, label='选择推理模式', value=inference_mode_list[0])
|
144 |
instruction_text = gr.Text(label="操作步骤", value=instruct_dict[inference_mode_list[0]], scale=0.5)
|
145 |
sft_dropdown = gr.Dropdown(choices=sft_spk, label='选择预训练音色', value=sft_spk[0], scale=0.25)
|
146 |
+
stream = gr.Radio(choices=stream_mode_list, label='是否流式推理', value=stream_mode_list[0][1])
|
147 |
with gr.Column(scale=0.25):
|
148 |
seed_button = gr.Button(value="\U0001F3B2")
|
149 |
seed = gr.Number(value=0, label="随机推理种子")
|
|
|
156 |
|
157 |
generate_button = gr.Button("生成音频")
|
158 |
|
159 |
+
audio_output = gr.Audio(label="合成音频", autoplay=True, streaming=True)
|
160 |
|
161 |
seed_button.click(generate_seed, inputs=[], outputs=seed)
|
162 |
generate_button.click(generate_audio,
|
163 |
+
inputs=[tts_text, mode_checkbox_group, sft_dropdown, prompt_text, prompt_wav_upload, prompt_wav_record, instruct_text, seed, stream, speed_factor],
|
164 |
outputs=[audio_output])
|
165 |
mode_checkbox_group.change(fn=change_instruction, inputs=[mode_checkbox_group], outputs=[instruction_text])
|
166 |
demo.queue(max_size=4, default_concurrency_limit=2)
|