gz412 commited on
Commit
4144748
·
1 Parent(s): c54dd77

update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -53
app.py CHANGED
@@ -18,12 +18,12 @@ converter = opencc.OpenCC('s2t.json')
18
 
19
  # 加载模型
20
  cosyvoice_base = CosyVoice2(
21
- 'ASLP-lab/Cosyvoice2-Yue-ZoengJyutGaai',
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, uploaded_audio, preset_audio_choice):
49
- # 选择模型
50
  if model_choice == "CosyVoice2-张悦楷粤语评书":
51
  model = cosyvoice_zjg
52
- default_prompt = "asset/sg_017_090.wav"
 
 
 
53
  elif model_choice == "CosyVoice2-base":
54
  model = cosyvoice_base
55
- default_prompt = None
 
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
- # ---- Gradio 界面 ----
88
- with gr.Blocks() as demo:
89
- gr.Markdown("## TTS Demo")
90
-
91
- model_choice = gr.Dropdown(["CosyVoice2-base", "CosyVoice2-张悦楷粤语评书"], label="选择模型")
92
- text_input = gr.Textbox(lines=2, label="输入文本")
93
- uploaded_audio = gr.Audio(source="upload", type="filepath", label="上传参考音频(仅 CosyVoice2-base 必需)")
94
-
95
- preset_audio_choice = gr.Radio(list(preset_audio_options.keys()), label="选择预设音频(可试听)", value=None)
96
- preview_audio = gr.Audio(type="filepath", label="试听预设音频")
97
-
98
- # 预览按钮
99
- preset_audio_choice.change(fn=preview_preset_audio, inputs=preset_audio_choice, outputs=preview_audio)
100
-
101
- # TTS 生成按钮
102
- generate_btn = gr.Button("生成 TTS")
103
- output_audio = gr.Audio(type="numpy", label="生成的语音")
104
- status = gr.Textbox(label="状态信息")
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()