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as CosyVoiceTTS_base
|
| 6 |
+
from tts_model.sft_model.cosyvoice import CosyVoice as CosyVoiceTTS_sft
|
| 7 |
+
from uuid import uuid1
|
| 8 |
+
import uuid
|
| 9 |
+
from tts_model.speaker_minus.cosyvoice import CosyVoice as CosyVoiceTTS_speakerminus
|
| 10 |
+
# from tts_model.model_cosy2_instruct import CosyVoiceTTS as CosyVoiceTTS_cosy2
|
| 11 |
+
from pydub import AudioSegment
|
| 12 |
+
import tempfile
|
| 13 |
+
import soundfile as sf
|
| 14 |
+
import subprocess
|
| 15 |
+
import numpy as np
|
| 16 |
+
import random
|
| 17 |
+
import numpy
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
from pydub import AudioSegment
|
| 21 |
+
# AudioSegment.converter = "/mnt/by079416/fengping/ffmpeg-7.0.2-amd64-static/ffmpeg"
|
| 22 |
+
# AudioSegment.ffprobe = "/mnt/by079416/fengping/ffmpeg-7.0.2-amd64-static/ffprobe"
|
| 23 |
+
|
| 24 |
+
ffmpeg_path = os.path.expanduser("/mnt/by079416/fengping/ffmpeg-7.0.2-amd64-static/ffmpeg/")
|
| 25 |
+
os.environ["PATH"] += os.pathsep + ffmpeg_path
|
| 26 |
+
|
| 27 |
+
sys.path.append('third_party/Matcha-TTS')
|
| 28 |
+
os.system('export PYTHONPATH=third_party/Matcha-TTS')
|
| 29 |
+
|
| 30 |
+
tts_base = CosyVoiceTTS_base(model_dir="./pretrained_models/CosyVoice-300M/")
|
| 31 |
+
tts_speakerminus = CosyVoiceTTS_speakerminus(model_dir="./pretrained_models/CosyVoice-300M-speakerminus/")
|
| 32 |
+
# tts_cosy2_instruct = CosyVoiceTTS_cosy2(model_path="./pretrained_models/CosyVoice-300M-Instruct_cosy2/")
|
| 33 |
+
tts_sft = CosyVoiceTTS_base(model_dir="./pretrained_models/CosyVoice-300M-SFT/")
|
| 34 |
+
|
| 35 |
+
text_prompt = {
|
| 36 |
+
"翟佳宁": "这个节目就是把四个男嘉宾,四个女嘉宾放一个大别墅里让他们朝夕相处一整个月,月末选择心动的彼此。",
|
| 37 |
+
"范志毅": "没这个能力知道吗,我已经说了,你像这样的比赛本身就没有打好基础。",
|
| 38 |
+
"呼兰": "发完之后那个工作人员说,老师,呼兰老师你还要再加个标签儿,我说加什么标签儿,他说你就加一个呼兰太好笑了。",
|
| 39 |
+
"江梓浩": "就是很多我们这帮演员一整年也就上这么一个脱口秀类型的节目。",
|
| 40 |
+
"李雪琴": "我就劝他,我说你呀,你没事儿也放松放松,你那身体都亮红灯儿了你还不注意。",
|
| 41 |
+
"刘旸": "比如这三年我在街上开车,会在开车的时候进行一些哲思,我有天开车的时候路过一个地方。",
|
| 42 |
+
"唐香玉": "大家好我叫唐香玉, 我年前把我的工作辞了,成了一个全职脱口秀演员。",
|
| 43 |
+
"小鹿": "然后我就老家的亲戚太多了,我也记不清谁该叫谁,所以我妈带着我和我。",
|
| 44 |
+
"于祥宇": "我大学专业学的是哲学,然后节目组就说那这期主题你可以刚好聊一下哲学专业毕业之后的就业方向。",
|
| 45 |
+
"赵晓卉": "终于没有人问我为什么不辞职了,结果谈到现在,谈恋爱第一天人家问我,能打个电话吗?我说你有啥事儿。",
|
| 46 |
+
"徐志胜": "最舒服的一个方式,这个舞台也不一定就是说是来第一年就好嘛,只要你坚持,肯定会有发光发热的那天嘛。"
|
| 47 |
+
}
|
| 48 |
+
audio_prompt = {
|
| 49 |
+
"翟佳宁": "zhaijianing",
|
| 50 |
+
"范志毅": "fanzhiyi",
|
| 51 |
+
"呼兰": "hulan",
|
| 52 |
+
"江梓浩": "jiangzhihao",
|
| 53 |
+
"李雪琴": "lixueqin",
|
| 54 |
+
"刘旸": "liuchang",
|
| 55 |
+
"唐香玉": "tangxiangyu",
|
| 56 |
+
"小鹿": "xiaolu",
|
| 57 |
+
"于祥宇": "yuxiangyu",
|
| 58 |
+
"赵晓卉": "zhaoxiaohui",
|
| 59 |
+
"徐志胜": "xuzhisheng"
|
| 60 |
+
}
|
| 61 |
+
audio_prompt_path = "/mnt/by079416/fengping/CosyVoice2/talk_show_prompt/"
|
| 62 |
+
|
| 63 |
+
def load_audio_and_convert_to_16bit(file_path, target_sample_rate=16000):
|
| 64 |
+
audio = AudioSegment.from_file(file_path)
|
| 65 |
+
|
| 66 |
+
if audio.channels > 1:
|
| 67 |
+
audio = audio.set_channels(1)
|
| 68 |
+
|
| 69 |
+
if audio.frame_rate != target_sample_rate:
|
| 70 |
+
audio = audio.set_frame_rate(target_sample_rate)
|
| 71 |
+
|
| 72 |
+
audio_data = np.array(audio.get_array_of_samples(), dtype=np.float32)
|
| 73 |
+
|
| 74 |
+
audio_data = audio_data / np.max(np.abs(audio_data))
|
| 75 |
+
|
| 76 |
+
audio_data = (audio_data * 32767).astype(np.int16)
|
| 77 |
+
|
| 78 |
+
return torch.tensor(audio_data), target_sample_rate
|
| 79 |
+
|
| 80 |
+
def convert_audio_with_sox(input_file, output_file, target_sample_rate=16000):
|
| 81 |
+
try:
|
| 82 |
+
# command = [
|
| 83 |
+
# './ffmpeg-7.0.2-amd64-static/ffmpeg', input_file,
|
| 84 |
+
# '-r', str(target_sample_rate),
|
| 85 |
+
# '-b', '16',
|
| 86 |
+
# '-c', '1',
|
| 87 |
+
# output_file
|
| 88 |
+
# ]
|
| 89 |
+
command = [
|
| 90 |
+
'./ffmpeg-7.0.2-amd64-static/ffmpeg',
|
| 91 |
+
'-i', input_file, # 必须显式指定 -i 标记输入文件
|
| 92 |
+
'-ar', str(target_sample_rate), # 设置音频采样率
|
| 93 |
+
'-ac', '1', # 设置单通道 (mono)
|
| 94 |
+
'-b:a', '16k', # 设置音频比特率为 16kbps
|
| 95 |
+
'-f', 'wav', # 强制输出格式为 WAV
|
| 96 |
+
output_file
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
subprocess.run(command, check=True)
|
| 100 |
+
print(f"Audio converted successfully: {output_file}")
|
| 101 |
+
except subprocess.CalledProcessError as e:
|
| 102 |
+
print(f"Error during conversion: {e}")
|
| 103 |
+
|
| 104 |
+
os.makedirs("./tmp", exist_ok=True)
|
| 105 |
+
|
| 106 |
+
def generate_speech_sft(tts_text, speaker):
|
| 107 |
+
# if not ref_audio and not ref_text:
|
| 108 |
+
# ref_text = text_prompt.get(speaker, "")
|
| 109 |
+
# ref_audio = os.path.join(audio_prompt_path, f"{audio_prompt.get(speaker)}.wav")
|
| 110 |
+
# else:
|
| 111 |
+
# random_int = random.randint(0, 90)
|
| 112 |
+
# soxsed_ref_audio = "/tmp/{random_int}_ref.wav"
|
| 113 |
+
# convert_audio_with_sox(ref_audio, soxsed_ref_audio)
|
| 114 |
+
# ref_audio = load_wav(ref_audio, 16000)
|
| 115 |
+
# # ref_audio, target_sample_rate = load_audio_and_convert_to_16bit(ref_audio)
|
| 116 |
+
|
| 117 |
+
sample_rate, full_audio = tts_sft.inference_sft(
|
| 118 |
+
tts_text,
|
| 119 |
+
spk_id = speaker
|
| 120 |
+
# instruct = instruct
|
| 121 |
+
# prompt_text = ref_text,
|
| 122 |
+
# prompt_speech_16k = ref_audio,
|
| 123 |
+
# speed=speed,
|
| 124 |
+
# speaker=speaker,
|
| 125 |
+
# emotion=emotion,
|
| 126 |
+
|
| 127 |
+
)
|
| 128 |
+
full_audio = full_audio.astype(np.float32)
|
| 129 |
+
if full_audio.max() > 1.0 or full_audio.min() < -1.0:
|
| 130 |
+
full_audio /= 32768.0 # int16 → [-1,1]
|
| 131 |
+
|
| 132 |
+
print("dtype:", full_audio.dtype,
|
| 133 |
+
"shape:", full_audio.shape,
|
| 134 |
+
"max:", full_audio.max(), "min:", full_audio.min())
|
| 135 |
+
|
| 136 |
+
out_path = os.path.join("./tmp", f"{uuid.uuid4().hex}.wav")
|
| 137 |
+
|
| 138 |
+
audio_segment = AudioSegment(
|
| 139 |
+
full_audio.tobytes(),
|
| 140 |
+
frame_rate=sample_rate,
|
| 141 |
+
sample_width=full_audio.dtype.itemsize,
|
| 142 |
+
channels=1
|
| 143 |
+
)
|
| 144 |
+
audio_segment.export(out_path, format="wav")
|
| 145 |
+
|
| 146 |
+
print(">>> audio path:", os.path.abspath(out_path))
|
| 147 |
+
# return out_path
|
| 148 |
+
return (sample_rate, full_audio)
|
| 149 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
|
| 150 |
+
# output_audio_path = temp_audio_file.name
|
| 151 |
+
# audio_segment = AudioSegment(
|
| 152 |
+
# full_audio.tobytes(),
|
| 153 |
+
# frame_rate=sample_rate,
|
| 154 |
+
# sample_width=full_audio.dtype.itemsize,
|
| 155 |
+
# channels=1
|
| 156 |
+
# )
|
| 157 |
+
# audio_segment.export(output_audio_path, format="wav")
|
| 158 |
+
# print(f"Audio saved to {output_audio_path}")
|
| 159 |
+
|
| 160 |
+
# return output_audio_path
|
| 161 |
+
|
| 162 |
+
# def generate_speech_sft(tts_text, speaker):
|
| 163 |
+
# sr = 22050 # 采样率
|
| 164 |
+
# t = np.linspace(0, 1, sr, dtype=np.float32)
|
| 165 |
+
# audio_np = np.sin(2 * np.pi * 440 * t) # 1 秒 440 Hz 正弦波
|
| 166 |
+
# return (sr, audio_np)
|
| 167 |
+
|
| 168 |
+
def generate_speech_base(tts_text, speed, speaker, ref_audio, ref_text):
|
| 169 |
+
# import pdb;pdb.set_trace()
|
| 170 |
+
if not ref_audio and not ref_text:
|
| 171 |
+
ref_text = text_prompt.get(speaker, "")
|
| 172 |
+
ref_audio = os.path.join(audio_prompt_path, f"{audio_prompt.get(speaker)}.wav")
|
| 173 |
+
ref_audio = load_wav(ref_audio, 16000)
|
| 174 |
+
else:
|
| 175 |
+
random_int = random.randint(0, 90000)
|
| 176 |
+
soxsed_ref_audio = f"/tmp/{random_int}_ref.wav"
|
| 177 |
+
convert_audio_with_sox(ref_audio, soxsed_ref_audio)
|
| 178 |
+
ref_audio = load_wav(soxsed_ref_audio, 16000)
|
| 179 |
+
# ref_audio, target_sample_rate = load_audio_and_convert_to_16bit(ref_audio)
|
| 180 |
+
sample_rate, full_audio = tts_base.inference_zero_shot(
|
| 181 |
+
tts_text,
|
| 182 |
+
prompt_text = ref_text,
|
| 183 |
+
prompt_speech_16k = ref_audio,
|
| 184 |
+
speed=speed,
|
| 185 |
+
# speaker=speaker,
|
| 186 |
+
# emotion=emotion,
|
| 187 |
+
|
| 188 |
+
)
|
| 189 |
+
print("sample_rate:", sample_rate, "full_audio:", full_audio.min(), full_audio.max())
|
| 190 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
|
| 191 |
+
output_audio_path = temp_audio_file.name
|
| 192 |
+
audio_segment = AudioSegment(
|
| 193 |
+
full_audio.tobytes(),
|
| 194 |
+
frame_rate=sample_rate,
|
| 195 |
+
sample_width=full_audio.dtype.itemsize,
|
| 196 |
+
channels=1
|
| 197 |
+
)
|
| 198 |
+
audio_segment.export(output_audio_path, format="wav")
|
| 199 |
+
print(f"Audio saved to {output_audio_path}")
|
| 200 |
+
|
| 201 |
+
return output_audio_path
|
| 202 |
+
|
| 203 |
+
def generate_speech_speakerminus(tts_text, speed, speaker, key, ref_audio, ref_text):
|
| 204 |
+
# import pdb;pdb.set_trace()
|
| 205 |
+
if not ref_audio and not ref_text:
|
| 206 |
+
ref_text = text_prompt.get(speaker, "")
|
| 207 |
+
ref_audio = os.path.join(audio_prompt_path, f"{audio_prompt.get(speaker)}.wav")
|
| 208 |
+
else:
|
| 209 |
+
random_int = random.randint(0, 90)
|
| 210 |
+
soxsed_ref_audio = f"/tmp/{random_int}_ref.wav"
|
| 211 |
+
convert_audio_with_sox(ref_audio, soxsed_ref_audio)
|
| 212 |
+
# print("output_file:", output_file)
|
| 213 |
+
# ref_audio, target_sample_rate = load_audio_and_convert_to_16bit(ref_audio)
|
| 214 |
+
ref_audio = load_wav(ref_audio, 16000)
|
| 215 |
+
# if key == "Surprise":
|
| 216 |
+
# emotion_info = torch.load("/mnt/by079416/surprise.pt")
|
| 217 |
+
# if key == "Sad":
|
| 218 |
+
# emotion_info = torch.load("/mnt/by079416/sad.pt")
|
| 219 |
+
# if key == "Angry":
|
| 220 |
+
# emotion_info = torch.load("/mnt/by079416/angry.pt")
|
| 221 |
+
# if key == "Happy":
|
| 222 |
+
# emotion_info = torch.load("/mnt/by079416/happy.pt")
|
| 223 |
+
|
| 224 |
+
emotion_info = torch.load("/mnt/by079416/fengping/CosyVoice2/embedding_info.pt")["0002"][key]
|
| 225 |
+
sample_rate, full_audio = tts_speakerminus.inference_zero_shot(
|
| 226 |
+
tts_text,
|
| 227 |
+
prompt_text = ref_text,
|
| 228 |
+
# speaker=speaker,
|
| 229 |
+
prompt_speech_16k = ref_audio,
|
| 230 |
+
key = key,
|
| 231 |
+
emotion_speakerminus=emotion_info,
|
| 232 |
+
# ref_audio = ref_audio,
|
| 233 |
+
speed=speed
|
| 234 |
+
|
| 235 |
+
)
|
| 236 |
+
print("sample_rate:", sample_rate, "full_audio:", full_audio.min(), full_audio.max())
|
| 237 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
|
| 238 |
+
output_audio_path = temp_audio_file.name
|
| 239 |
+
audio_segment = AudioSegment(
|
| 240 |
+
full_audio.tobytes(),
|
| 241 |
+
frame_rate=sample_rate,
|
| 242 |
+
sample_width=full_audio.dtype.itemsize,
|
| 243 |
+
channels=1
|
| 244 |
+
)
|
| 245 |
+
audio_segment.export(output_audio_path, format="wav")
|
| 246 |
+
print(f"Audio saved to {output_audio_path}")
|
| 247 |
+
return output_audio_path
|
| 248 |
+
|
| 249 |
+
names = [
|
| 250 |
+
"于祥宇",
|
| 251 |
+
"刘旸",
|
| 252 |
+
"呼兰",
|
| 253 |
+
"唐香玉",
|
| 254 |
+
"小鹿",
|
| 255 |
+
"李雪琴",
|
| 256 |
+
"江梓浩",
|
| 257 |
+
"翟佳宁",
|
| 258 |
+
"范志毅",
|
| 259 |
+
"赵晓卉",
|
| 260 |
+
"徐志胜"
|
| 261 |
+
]
|
| 262 |
+
|
| 263 |
+
custom_css = """
|
| 264 |
+
:root {
|
| 265 |
+
--primary-color: #6a11cb;
|
| 266 |
+
--secondary-color: #2575fc;
|
| 267 |
+
--accent-color: #ff6b6b;
|
| 268 |
+
--light-bg: #f8f9fa;
|
| 269 |
+
--dark-bg: #212529;
|
| 270 |
+
--card-bg: #ffffff;
|
| 271 |
+
--text-color: #343a40;
|
| 272 |
+
--border-radius: 12px;
|
| 273 |
+
--box-shadow: 0 6px 16px rgba(0,0,0,0.1);
|
| 274 |
+
--transition: all 0.3s ease;
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
body {
|
| 278 |
+
background: linear-gradient(135deg, var(--light-bg) 0%, #e9ecef 100%);
|
| 279 |
+
min-height: 100vh;
|
| 280 |
+
font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
|
| 281 |
+
color: var(--text-color);
|
| 282 |
+
line-height: 1.6;
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
.gradio-container {
|
| 286 |
+
max-width: 1200px !important;
|
| 287 |
+
margin: 2rem auto !important;
|
| 288 |
+
padding: 0 1rem;
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
.header {
|
| 292 |
+
padding: 0 !important;
|
| 293 |
+
border-radius: 10px; /* 整个 Header 的圆角 */
|
| 294 |
+
overflow: hidden; /* 隐藏超出圆角部分的内容,非常重要! */
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
/* 2. 行容器:使用 Flex 布局,让左右两部分等高 */
|
| 298 |
+
#header-row {
|
| 299 |
+
display: flex;
|
| 300 |
+
align-items: stretch; /* 让子项高度自动拉伸以填满容器 */
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
/* 3. Logo 容器:移除 Gradio 默认的所有样式,让它变成一个纯净的盒子 */
|
| 304 |
+
#logo-container {
|
| 305 |
+
padding: 0 !important;
|
| 306 |
+
border: none !important;
|
| 307 |
+
background: none !important;
|
| 308 |
+
box-shadow: none !important;
|
| 309 |
+
min-width: 150px; /* 给 Logo 区域一个固定的最小宽度 */
|
| 310 |
+
flex-shrink: 0; /* 防止在空间不足时被压缩 */
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
/* 4. Logo 图片本身:让图片填满它的容器,就像背景图一样 */
|
| 314 |
+
#logo-container img {
|
| 315 |
+
width: 100%;
|
| 316 |
+
height: 100%;
|
| 317 |
+
object-fit: cover; /* 裁剪并填充,保持宽高比,确保填满容器 */
|
| 318 |
+
display: block; /* 移除图片底部的微小空隙 */
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
/* 5. 标题区域:设置背景、内边距和文本对齐 */
|
| 322 |
+
#title-area {
|
| 323 |
+
background: linear-gradient(to right, #5e57c2, #42a5f5); /* 右侧的渐变背景 */
|
| 324 |
+
padding: 20px 25px; /* 给标题文本留出足够的空间 */
|
| 325 |
+
display: flex;
|
| 326 |
+
flex-direction: column;
|
| 327 |
+
justify-content: center; /* 垂直居中标题 */
|
| 328 |
+
flex-grow: 1; /* 占据所有剩余空间 */
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
/* 6. 标题文本样式 */
|
| 332 |
+
#header-title h1 {
|
| 333 |
+
color: white;
|
| 334 |
+
font-size: 28px;
|
| 335 |
+
margin: 0;
|
| 336 |
+
font-weight: 600;
|
| 337 |
+
text-shadow: 1px 1px 2px rgba(0,0,0,0.2);
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
.tabs {
|
| 341 |
+
background: transparent !important;
|
| 342 |
+
border: none !important;
|
| 343 |
+
box-shadow: none !important;
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
.tab-nav {
|
| 347 |
+
background: var(--card-bg) !important;
|
| 348 |
+
border-radius: var(--border-radius) !important;
|
| 349 |
+
padding: 0.5rem !important;
|
| 350 |
+
margin-bottom: 1.5rem !important;
|
| 351 |
+
box-shadow: var(--box-shadow) !important;
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
.tab-button {
|
| 355 |
+
padding: 1rem 1.5rem !important;
|
| 356 |
+
border-radius: 8px !important;
|
| 357 |
+
font-weight: 600 !important;
|
| 358 |
+
transition: var(--transition) !important;
|
| 359 |
+
border: none !important;
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
.tab-button.selected {
|
| 363 |
+
background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%) !important;
|
| 364 |
+
color: white !important;
|
| 365 |
+
box-shadow: 0 4px 12px rgba(106, 17, 203, 0.3) !important;
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
.tab-content {
|
| 369 |
+
background: var(--card-bg) !important;
|
| 370 |
+
border-radius: var(--border-radius) !important;
|
| 371 |
+
padding: 2rem !important;
|
| 372 |
+
box-shadow: var(--box-shadow) !important;
|
| 373 |
+
margin-bottom: 2rem;
|
| 374 |
+
border: none !important;
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
.input-section {
|
| 378 |
+
background: #f9fafb;
|
| 379 |
+
padding: 1.5rem;
|
| 380 |
+
border-radius: var(--border-radius);
|
| 381 |
+
margin-bottom: 1.5rem;
|
| 382 |
+
border: 1px solid #e9ecef;
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
.output-section {
|
| 386 |
+
background: #edf2f7;
|
| 387 |
+
padding: 1.5rem;
|
| 388 |
+
border-radius: var(--border-radius);
|
| 389 |
+
border: 1px solid #e9ecef;
|
| 390 |
+
display: flex;
|
| 391 |
+
flex-direction: column;
|
| 392 |
+
height: 100%;
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
.control-group {
|
| 396 |
+
margin-bottom: 1.2rem;
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
.control-group label {
|
| 400 |
+
display: block;
|
| 401 |
+
margin-bottom: 0.5rem;
|
| 402 |
+
font-weight: 600;
|
| 403 |
+
color: #495057;
|
| 404 |
+
font-size: 0.95rem;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
input[type="text"], textarea {
|
| 408 |
+
border-radius: 8px !important;
|
| 409 |
+
padding: 0.8rem 1rem !important;
|
| 410 |
+
border: 1px solid #ced4da !important;
|
| 411 |
+
transition: var(--transition) !important;
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
input[type="text"]:focus, textarea:focus {
|
| 415 |
+
border-color: var(--primary-color) !important;
|
| 416 |
+
box-shadow: 0 0 0 3px rgba(106, 17, 203, 0.1) !important;
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
.slider {
|
| 420 |
+
margin-top: 0.5rem !important;
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
.btn-generate {
|
| 424 |
+
background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%) !important;
|
| 425 |
+
color: white !important;
|
| 426 |
+
font-weight: 600 !important;
|
| 427 |
+
padding: 1rem 1.8rem !important;
|
| 428 |
+
border-radius: 8px !important;
|
| 429 |
+
border: none !important;
|
| 430 |
+
transition: var(--transition) !important;
|
| 431 |
+
font-size: 1rem !important;
|
| 432 |
+
margin-top: auto;
|
| 433 |
+
width: 100%;
|
| 434 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1) !important;
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
.btn-generate:hover {
|
| 438 |
+
transform: translateY(-3px);
|
| 439 |
+
box-shadow: 0 6px 12px rgba(106, 17, 203, 0.25) !important;
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
.example-text {
|
| 443 |
+
background: #e9ecef;
|
| 444 |
+
padding: 0.8rem;
|
| 445 |
+
border-radius: 8px;
|
| 446 |
+
font-style: italic;
|
| 447 |
+
margin-top: 0.5rem;
|
| 448 |
+
font-size: 0.9rem;
|
| 449 |
+
color: #495057;
|
| 450 |
+
}
|
| 451 |
+
|
| 452 |
+
.audio-player {
|
| 453 |
+
width: 100%;
|
| 454 |
+
margin-top: 1rem;
|
| 455 |
+
border-radius: 8px;
|
| 456 |
+
overflow: hidden;
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
.model-info {
|
| 460 |
+
background: #e6f7ff;
|
| 461 |
+
padding: 1rem;
|
| 462 |
+
border-radius: 8px;
|
| 463 |
+
margin-top: 1.5rem;
|
| 464 |
+
border-left: 4px solid #1890ff;
|
| 465 |
+
font-size: 0.9rem;
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
.info-icon {
|
| 469 |
+
color: #1890ff;
|
| 470 |
+
margin-right: 8px;
|
| 471 |
+
font-weight: bold;
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
.footer {
|
| 475 |
+
text-align: center;
|
| 476 |
+
color: #6c757d;
|
| 477 |
+
font-size: 0.9rem;
|
| 478 |
+
padding: 1.5rem 0;
|
| 479 |
+
border-top: 1px solid #e9ecef;
|
| 480 |
+
margin-top: 2rem;
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
.accordion {
|
| 484 |
+
background: #f8f9fa !important;
|
| 485 |
+
border-radius: 8px !important;
|
| 486 |
+
padding: 0.8rem !important;
|
| 487 |
+
margin-top: 1rem;
|
| 488 |
+
border: 1px solid #e9ecef !important;
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
.accordion-title {
|
| 492 |
+
font-weight: 600 !important;
|
| 493 |
+
color: var(--primary-color) !important;
|
| 494 |
+
}
|
| 495 |
+
|
| 496 |
+
.audio-upload {
|
| 497 |
+
border: 2px dashed #ced4da !important;
|
| 498 |
+
border-radius: 8px !important;
|
| 499 |
+
padding: 1.5rem !important;
|
| 500 |
+
background: #f8f9fa !important;
|
| 501 |
+
transition: var(--transition) !important;
|
| 502 |
+
}
|
| 503 |
+
|
| 504 |
+
.audio-upload:hover {
|
| 505 |
+
border-color: var(--primary-color) !important;
|
| 506 |
+
background: #f1f3f5 !important;
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
.audio-upload-label {
|
| 510 |
+
font-weight: 500 !important;
|
| 511 |
+
color: #495057 !important;
|
| 512 |
+
margin-bottom: 0.5rem !important;
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
.radio-group {
|
| 516 |
+
display: flex;
|
| 517 |
+
flex-wrap: wrap;
|
| 518 |
+
gap: 0.8rem;
|
| 519 |
+
margin-top: 0.5rem;
|
| 520 |
+
}
|
| 521 |
+
|
| 522 |
+
.radio-item {
|
| 523 |
+
flex: 1;
|
| 524 |
+
min-width: 100px;
|
| 525 |
+
text-align: center;
|
| 526 |
+
padding: 0.8rem;
|
| 527 |
+
border: 1px solid #ced4da;
|
| 528 |
+
border-radius: 8px;
|
| 529 |
+
cursor: pointer;
|
| 530 |
+
transition: var(--transition);
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
.radio-item.selected {
|
| 534 |
+
border-color: var(--primary-color);
|
| 535 |
+
background: rgba(106, 17, 203, 0.05);
|
| 536 |
+
color: var(--primary-color);
|
| 537 |
+
font-weight: 500;
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
.radio-item:hover {
|
| 541 |
+
border-color: var(--primary-color);
|
| 542 |
+
}
|
| 543 |
+
|
| 544 |
+
@media (max-width: 768px) {
|
| 545 |
+
.header h1 {
|
| 546 |
+
font-size: 2.2rem;
|
| 547 |
+
}
|
| 548 |
+
|
| 549 |
+
.header p {
|
| 550 |
+
font-size: 1rem;
|
| 551 |
+
}
|
| 552 |
+
|
| 553 |
+
.gradio-container {
|
| 554 |
+
padding: 0 0.5rem;
|
| 555 |
+
}
|
| 556 |
+
|
| 557 |
+
.tab-button {
|
| 558 |
+
padding: 0.8rem 1rem !important;
|
| 559 |
+
font-size: 0.9rem !important;
|
| 560 |
+
}
|
| 561 |
+
}
|
| 562 |
+
"""
|
| 563 |
+
|
| 564 |
+
# 创建界面
|
| 565 |
+
logo_path = "/mnt/by079416/fengping/CosyVoice2/logo2.png"
|
| 566 |
+
|
| 567 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 568 |
+
with gr.Column(elem_classes="header"):
|
| 569 |
+
# 行容器,用于左右布局
|
| 570 |
+
with gr.Row(elem_id="header-row", variant="compact"):
|
| 571 |
+
# 左侧:Logo
|
| 572 |
+
gr.Image(value=logo_path,
|
| 573 |
+
elem_id="logo-container",
|
| 574 |
+
show_label=False,
|
| 575 |
+
show_download_button=False,
|
| 576 |
+
show_share_button=False) # 隐藏分享按钮
|
| 577 |
+
|
| 578 |
+
# 右侧:标题区域
|
| 579 |
+
with gr.Column(elem_id="title-area"):
|
| 580 |
+
gr.Markdown("# 🎤 Marco-Voice 语音合成系统", elem_id="header-title")
|
| 581 |
+
|
| 582 |
+
# gr.Markdown("")
|
| 583 |
+
|
| 584 |
+
# 标签页
|
| 585 |
+
with gr.Tabs(elem_classes="tabs") as tabs:
|
| 586 |
+
# Tab 1: 音色克隆
|
| 587 |
+
with gr.TabItem("🎭 音色克隆", id=0):
|
| 588 |
+
with gr.Row():
|
| 589 |
+
with gr.Column(scale=2, elem_classes="input-section"):
|
| 590 |
+
gr.Markdown("### 输入设置")
|
| 591 |
+
tts_text_v1 = gr.Textbox(
|
| 592 |
+
lines=3,
|
| 593 |
+
placeholder="请输入要合成的文本内容...",
|
| 594 |
+
label="合成文本",
|
| 595 |
+
value="大家好,欢迎使用Marco Voice语音合成系统,这是一个强大的语音生成工具。"
|
| 596 |
+
)
|
| 597 |
+
|
| 598 |
+
with gr.Row():
|
| 599 |
+
with gr.Column():
|
| 600 |
+
speed_v1 = gr.Slider(
|
| 601 |
+
minimum=0.5,
|
| 602 |
+
maximum=2.0,
|
| 603 |
+
value=1.0,
|
| 604 |
+
step=0.1,
|
| 605 |
+
label="语速控制",
|
| 606 |
+
interactive=True
|
| 607 |
+
)
|
| 608 |
+
with gr.Column():
|
| 609 |
+
speaker_v1 = gr.Dropdown(
|
| 610 |
+
choices=names,
|
| 611 |
+
value="徐志胜",
|
| 612 |
+
label="预设音色",
|
| 613 |
+
info="选择脱口秀演员音色"
|
| 614 |
+
)
|
| 615 |
+
# [tts_text_v1, speed_v1, speaker_v1, emotion, ref_audio_v1, ref_text_v1]
|
| 616 |
+
with gr.Accordion("高级设置", open=False, elem_classes="accordion"):
|
| 617 |
+
gr.Markdown("上传3-10秒清晰人声作为参考音频")
|
| 618 |
+
with gr.Row():
|
| 619 |
+
ref_audio_v1 = gr.Audio(
|
| 620 |
+
type="filepath",
|
| 621 |
+
label="上传参考音频",
|
| 622 |
+
elem_classes="audio-upload"
|
| 623 |
+
)
|
| 624 |
+
ref_text_v1 = gr.Textbox(
|
| 625 |
+
lines=2,
|
| 626 |
+
placeholder="参考音频对应的文本...",
|
| 627 |
+
label="参考文本"
|
| 628 |
+
)
|
| 629 |
+
|
| 630 |
+
gr.Markdown("""
|
| 631 |
+
<div class="model-info">
|
| 632 |
+
<p><span class="info-icon">ℹ️</span> <strong>模型说明:</strong> 此模型使用零样本音色克隆技术,只需3-10秒参考音频即可模仿目标音色。</p>
|
| 633 |
+
</div>
|
| 634 |
+
""")
|
| 635 |
+
|
| 636 |
+
with gr.Column(scale=1, elem_classes="output-section"):
|
| 637 |
+
gr.Markdown("### 输出结果")
|
| 638 |
+
tts_base_output = gr.Audio(
|
| 639 |
+
type="filepath",
|
| 640 |
+
label="生成语音",
|
| 641 |
+
elem_id="tts_output_audio",
|
| 642 |
+
interactive=False
|
| 643 |
+
)
|
| 644 |
+
tts_base_button = gr.Button(
|
| 645 |
+
"🚀 生成语音",
|
| 646 |
+
variant="primary",
|
| 647 |
+
elem_classes="btn-generate"
|
| 648 |
+
)
|
| 649 |
+
gr.Examples(
|
| 650 |
+
examples=[
|
| 651 |
+
["大家好,欢迎使用Marco-Voice语音合成系统,这是一个强大的语音生成工具。", "徐志胜"],
|
| 652 |
+
["科技改变生活,创新引领未来。人工智能正在深刻改变我们的世界。", "李雪琴"],
|
| 653 |
+
["在这个充满机遇的时代,我们要勇于探索,敢于创新,不断突破自我。", "范志毅"]
|
| 654 |
+
],
|
| 655 |
+
inputs=[tts_text_v1, speaker_v1],
|
| 656 |
+
label="示例文本"
|
| 657 |
+
)
|
| 658 |
+
|
| 659 |
+
# Tab 2: 多语种合成
|
| 660 |
+
with gr.TabItem("🌍 多语种合成", id=1):
|
| 661 |
+
with gr.Row():
|
| 662 |
+
with gr.Column(scale=2, elem_classes="input-section"):
|
| 663 |
+
gr.Markdown("### 输入设置")
|
| 664 |
+
tts_text_sft = gr.Textbox(
|
| 665 |
+
lines=3,
|
| 666 |
+
placeholder="请输入要合成的文本内容...",
|
| 667 |
+
label="合成文本",
|
| 668 |
+
value="Hello, welcome to Marco-Voice text-to-speech system. This is a powerful multilingual TTS tool."
|
| 669 |
+
)
|
| 670 |
+
|
| 671 |
+
speaker_sft = gr.Dropdown(
|
| 672 |
+
choices=["中文男", "中文女", "英文男", "英文女", "韩语女", "日语男"],
|
| 673 |
+
value="英文男",
|
| 674 |
+
label="说话人",
|
| 675 |
+
info="选择语言和性别"
|
| 676 |
+
)
|
| 677 |
+
|
| 678 |
+
gr.Markdown("""
|
| 679 |
+
<div class="model-info">
|
| 680 |
+
<p><span class="info-icon">ℹ️</span> <strong>模型说明:</strong> 此模型支持多个语种,无需参考音频即可生成自然语音。</p>
|
| 681 |
+
<p><span class="info-icon">💡</span> <strong>使用技巧:</strong> 输入文本语言应与选择的说话人语言一致以获得最佳效果。</p>
|
| 682 |
+
</div>
|
| 683 |
+
""")
|
| 684 |
+
|
| 685 |
+
with gr.Column(scale=1, elem_classes="output-section"):
|
| 686 |
+
gr.Markdown("### 输出结果")
|
| 687 |
+
tts_sft_output = gr.Audio(
|
| 688 |
+
type="numpy",
|
| 689 |
+
label="生成语音",
|
| 690 |
+
interactive=False
|
| 691 |
+
)
|
| 692 |
+
tts_sft_button = gr.Button(
|
| 693 |
+
"🚀 生成语音",
|
| 694 |
+
variant="primary",
|
| 695 |
+
elem_classes="btn-generate"
|
| 696 |
+
)
|
| 697 |
+
gr.Examples(
|
| 698 |
+
examples=[
|
| 699 |
+
["Hello, welcome to Marco-Voice text-to-speech system.", "英文男"],
|
| 700 |
+
["こんにちは、Marco-Voiceテキスト読み上げシステムへようこそ。", "日语男"],
|
| 701 |
+
["안녕하세요, Marco-Voice 텍스트 음성 변환 시스템에 오신 것을 환영합니다.", "韩语女"]
|
| 702 |
+
],
|
| 703 |
+
inputs=[tts_text_sft, speaker_sft],
|
| 704 |
+
label="多语种示例"
|
| 705 |
+
)
|
| 706 |
+
|
| 707 |
+
# Tab 3: 情感控制
|
| 708 |
+
with gr.TabItem("😄 情感控制", id=2):
|
| 709 |
+
with gr.Row():
|
| 710 |
+
with gr.Column(scale=2, elem_classes="input-section"):
|
| 711 |
+
gr.Markdown("### 输入设置")
|
| 712 |
+
tts_text_v3 = gr.Textbox(
|
| 713 |
+
lines=3,
|
| 714 |
+
placeholder="请输入要合成的文本内容...",
|
| 715 |
+
label="合成文本",
|
| 716 |
+
value="这真是太令人兴奋了!我们刚刚完成了一个重大突破!"
|
| 717 |
+
)
|
| 718 |
+
|
| 719 |
+
with gr.Row():
|
| 720 |
+
with gr.Column():
|
| 721 |
+
speed_v3 = gr.Slider(
|
| 722 |
+
minimum=0.5,
|
| 723 |
+
maximum=2.0,
|
| 724 |
+
value=1.0,
|
| 725 |
+
step=0.1,
|
| 726 |
+
label="语速控制"
|
| 727 |
+
)
|
| 728 |
+
with gr.Column():
|
| 729 |
+
emotion_v3 = gr.Radio(
|
| 730 |
+
choices=["Angry", "Happy", "Surprise", "Sad"],
|
| 731 |
+
value="Happy",
|
| 732 |
+
label="情感选择"
|
| 733 |
+
)
|
| 734 |
+
|
| 735 |
+
with gr.Row():
|
| 736 |
+
with gr.Column():
|
| 737 |
+
speaker_v3 = gr.Dropdown(
|
| 738 |
+
choices=names,
|
| 739 |
+
value="徐志胜",
|
| 740 |
+
label="预设音色"
|
| 741 |
+
)
|
| 742 |
+
with gr.Column():
|
| 743 |
+
gr.Markdown("### 或使用自定义音色")
|
| 744 |
+
with gr.Accordion("上传参考音频", open=False, elem_classes="accordion"):
|
| 745 |
+
gr.Markdown("上传3-10秒清晰人声作为参考音频")
|
| 746 |
+
ref_audio_v3 = gr.Audio(
|
| 747 |
+
type="filepath",
|
| 748 |
+
label="上传参考音频",
|
| 749 |
+
elem_classes="audio-upload"
|
| 750 |
+
)
|
| 751 |
+
ref_text_v3 = gr.Textbox(
|
| 752 |
+
lines=2,
|
| 753 |
+
placeholder="参考音频对应的文本...",
|
| 754 |
+
label="参考文本"
|
| 755 |
+
)
|
| 756 |
+
|
| 757 |
+
gr.Markdown("""
|
| 758 |
+
<div class="model-info">
|
| 759 |
+
<p><span class="info-icon">ℹ️</span> <strong>模型说明:</strong> 此模型在音色克隆基础上增加了情感控制能力,可生成带有特定情感的语音。</p>
|
| 760 |
+
<p><span class="info-icon">💡</span> <strong>使用技巧:</strong> 情感表达效果与文本内容相关,请确保文本与所选情感匹配。</p>
|
| 761 |
+
</div>
|
| 762 |
+
""")
|
| 763 |
+
|
| 764 |
+
with gr.Column(scale=1, elem_classes="output-section"):
|
| 765 |
+
gr.Markdown("### 输出结果")
|
| 766 |
+
tts_v3_output = gr.Audio(
|
| 767 |
+
type="filepath",
|
| 768 |
+
label="生成语音",
|
| 769 |
+
interactive=False
|
| 770 |
+
)
|
| 771 |
+
tts_v3_button = gr.Button(
|
| 772 |
+
"🚀 生成语音",
|
| 773 |
+
variant="primary",
|
| 774 |
+
elem_classes="btn-generate"
|
| 775 |
+
)
|
| 776 |
+
gr.Examples(
|
| 777 |
+
examples=[
|
| 778 |
+
["这真是太令人兴奋了!我们刚刚完成了一个重大突破!", "Happy", "徐志胜"],
|
| 779 |
+
["我简直不敢相信!这怎么可能发生?", "Surprise", "李雪琴"],
|
| 780 |
+
["这太让人失望了,我们所有的努力都白费了。", "Sad", "范志毅"]
|
| 781 |
+
],
|
| 782 |
+
inputs=[tts_text_v3, emotion_v3, speaker_v3],
|
| 783 |
+
label="情感示例"
|
| 784 |
+
)
|
| 785 |
+
|
| 786 |
+
# 页脚
|
| 787 |
+
gr.Markdown("""
|
| 788 |
+
<div class="footer">
|
| 789 |
+
<p>Marco-Voice 语音合成系统 v1.0 | 基于优秀的tts 模型 | 技术支持: tech@marco-voice.com</p>
|
| 790 |
+
<p>注意: 生成内容仅用于技术演示,请勿用于非法用途</p>
|
| 791 |
+
</div>
|
| 792 |
+
""")
|
| 793 |
+
|
| 794 |
+
# 绑定事件 # tts_text, speed, speaker, emotion, ref_audio, ref_text
|
| 795 |
+
tts_base_button.click(
|
| 796 |
+
fn=generate_speech_base,
|
| 797 |
+
inputs=[tts_text_v1, speed_v1, speaker_v1, ref_audio_v1, ref_text_v1],
|
| 798 |
+
outputs=tts_base_output
|
| 799 |
+
)
|
| 800 |
+
|
| 801 |
+
tts_sft_button.click(
|
| 802 |
+
fn=generate_speech_sft,
|
| 803 |
+
inputs=[tts_text_sft, speaker_sft],
|
| 804 |
+
outputs=tts_sft_output
|
| 805 |
+
)
|
| 806 |
+
# tts_text, speed, speaker, key, ref_audio, ref_text
|
| 807 |
+
tts_v3_button.click(
|
| 808 |
+
fn=generate_speech_speakerminus,
|
| 809 |
+
inputs=[tts_text_v3, speed_v3, speaker_v3, emotion_v3, ref_audio_v3, ref_text_v3],
|
| 810 |
+
outputs=tts_v3_output
|
| 811 |
+
)
|
| 812 |
+
|
| 813 |
+
if __name__ == "__main__":
|
| 814 |
+
demo.launch(
|
| 815 |
+
server_name="0.0.0.0",
|
| 816 |
+
server_port=10163,
|
| 817 |
+
share=True,
|
| 818 |
+
favicon_path="/mnt/by079416/fengping/CosyVoice2/logo.png"
|
| 819 |
+
)
|