Spaces:
Running
on
Zero
Running
on
Zero
update app.py
Browse files
app.py
CHANGED
|
@@ -18,12 +18,12 @@ converter = opencc.OpenCC('s2t.json')
|
|
| 18 |
|
| 19 |
# 加载模型
|
| 20 |
cosyvoice_base = CosyVoice2(
|
| 21 |
-
'ASLP-lab/Cosyvoice2-Yue
|
| 22 |
load_jit=False, load_trt=False, load_vllm=False, fp16=False
|
| 23 |
)
|
| 24 |
print('load model 1')
|
| 25 |
cosyvoice_zjg = CosyVoice2(
|
| 26 |
-
'ASLP-lab/Cosyvoice2-Yue',
|
| 27 |
load_jit=False, load_trt=False, load_vllm=False, fp16=False
|
| 28 |
)
|
| 29 |
print('load model 2')
|
|
@@ -31,42 +31,26 @@ print('load model 2')
|
|
| 31 |
# 'pretrained_models/CosyVoice2-yue-biaobei',
|
| 32 |
# load_jit=False, load_trt=False, load_vllm=False, fp16=False
|
| 33 |
# )
|
| 34 |
-
import gradio as gr
|
| 35 |
-
|
| 36 |
-
# 预设音频字典
|
| 37 |
-
preset_audio_options = {
|
| 38 |
-
"张悦楷示例": "asset/sg_017_090.wav",
|
| 39 |
-
"精品女音示例": "asset/F01_中立_20054.wav",
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
def preview_preset_audio(choice):
|
| 43 |
-
if choice is None:
|
| 44 |
-
return None
|
| 45 |
-
return choice # 返回文件路径即可被 gr.Audio 播放
|
| 46 |
|
| 47 |
@spaces.GPU
|
| 48 |
-
def tts_inference(model_choice, text,
|
| 49 |
-
#
|
| 50 |
if model_choice == "CosyVoice2-张悦楷粤语评书":
|
| 51 |
model = cosyvoice_zjg
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
elif model_choice == "CosyVoice2-base":
|
| 54 |
model = cosyvoice_base
|
| 55 |
-
|
|
|
|
| 56 |
else:
|
| 57 |
return None, "未知模型"
|
| 58 |
-
|
| 59 |
-
# 决定使用哪条音频
|
| 60 |
-
if preset_audio_choice is not None:
|
| 61 |
-
prompt_audio = preset_audio_options[preset_audio_choice]
|
| 62 |
-
elif uploaded_audio is not None:
|
| 63 |
-
prompt_audio = uploaded_audio
|
| 64 |
-
elif default_prompt is not None:
|
| 65 |
-
prompt_audio = default_prompt
|
| 66 |
-
else:
|
| 67 |
-
return None, "请上传参考音频或选择预设音频"
|
| 68 |
-
|
| 69 |
model.model.cuda()
|
|
|
|
|
|
|
| 70 |
text = converter.convert(text)
|
| 71 |
prompt_speech_16k = load_wav(prompt_audio, 16000)
|
| 72 |
|
|
@@ -84,29 +68,24 @@ def tts_inference(model_choice, text, uploaded_audio, preset_audio_choice):
|
|
| 84 |
|
| 85 |
return (sample_rate, audio_numpy), f"生成成功:{text}"
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
generate_btn.click(
|
| 107 |
-
fn=tts_inference,
|
| 108 |
-
inputs=[model_choice, text_input, uploaded_audio, preset_audio_choice],
|
| 109 |
-
outputs=[output_audio, status]
|
| 110 |
-
)
|
| 111 |
|
| 112 |
demo.launch()
|
|
|
|
| 18 |
|
| 19 |
# 加载模型
|
| 20 |
cosyvoice_base = CosyVoice2(
|
| 21 |
+
'ASLP-lab/Cosyvoice2-Yue',
|
| 22 |
load_jit=False, load_trt=False, load_vllm=False, fp16=False
|
| 23 |
)
|
| 24 |
print('load model 1')
|
| 25 |
cosyvoice_zjg = CosyVoice2(
|
| 26 |
+
'ASLP-lab/Cosyvoice2-Yue-ZoengJyutGaai',
|
| 27 |
load_jit=False, load_trt=False, load_vllm=False, fp16=False
|
| 28 |
)
|
| 29 |
print('load model 2')
|
|
|
|
| 31 |
# 'pretrained_models/CosyVoice2-yue-biaobei',
|
| 32 |
# load_jit=False, load_trt=False, load_vllm=False, fp16=False
|
| 33 |
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
@spaces.GPU
|
| 36 |
+
def tts_inference(model_choice, text, prompt_audio):
|
| 37 |
+
# 选择模型和默认音频
|
| 38 |
if model_choice == "CosyVoice2-张悦楷粤语评书":
|
| 39 |
model = cosyvoice_zjg
|
| 40 |
+
prompt_audio = "asset/sg_017_090.wav"
|
| 41 |
+
elif model_choice == "CosyVoice2-精品女音":
|
| 42 |
+
model = cosyvoice_base
|
| 43 |
+
prompt_audio = "asset/F01_中立_20054.wav"
|
| 44 |
elif model_choice == "CosyVoice2-base":
|
| 45 |
model = cosyvoice_base
|
| 46 |
+
if prompt_audio is None:
|
| 47 |
+
return None, "请上传参考音频"
|
| 48 |
else:
|
| 49 |
return None, "未知模型"
|
| 50 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
model.model.cuda()
|
| 52 |
+
|
| 53 |
+
# 繁简转换
|
| 54 |
text = converter.convert(text)
|
| 55 |
prompt_speech_16k = load_wav(prompt_audio, 16000)
|
| 56 |
|
|
|
|
| 68 |
|
| 69 |
return (sample_rate, audio_numpy), f"生成成功:{text}"
|
| 70 |
|
| 71 |
+
|
| 72 |
+
# ---- Gradio Interface ----
|
| 73 |
+
demo = gr.Interface(
|
| 74 |
+
fn=tts_inference,
|
| 75 |
+
inputs=[
|
| 76 |
+
gr.Dropdown(
|
| 77 |
+
["CosyVoice2-base", "CosyVoice2-张悦楷粤语评书"],
|
| 78 |
+
# ["CosyVoice2-base", "CosyVoice2-张悦楷粤语评书", "CosyVoice2-精品女音"],
|
| 79 |
+
label="选择模型", value="CosyVoice2-base"
|
| 80 |
+
),
|
| 81 |
+
gr.Textbox(lines=2, label="输入文本"),
|
| 82 |
+
# gr.Audio(source="upload", type="filepath", label="上传参考音频(仅 CosyVoice2-base 必需)")
|
| 83 |
+
gr.Audio(sources=["upload"], type="filepath", label="上传参考音频(仅 CosyVoice2-base 必需)")
|
| 84 |
+
],
|
| 85 |
+
outputs=[
|
| 86 |
+
gr.Audio(type="numpy", label="生成的语音"),
|
| 87 |
+
gr.Textbox(label="状态信息")
|
| 88 |
+
]
|
| 89 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
demo.launch()
|