AIdsadong commited on
Commit
67e7ff5
·
verified ·
1 Parent(s): 1baa65c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -1,40 +1,40 @@
1
  import gradio as gr
2
  import torch
3
- from modeling_voxcpm import VoxCPM
4
- from feature_extraction_voxcpm import VoxCPMFeatureExtractor
5
  import scipy.io.wavfile
6
  import os
7
 
8
  # 检查是否有可用的 GPU,否则使用 CPU
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
- # 加载模型和处理器
12
- # 使用 cache_dir 参数将模型下载到持久化存储中
13
- cache_dir = "./models"
14
- if not os.path.exists(cache_dir):
15
- os.makedirs(cache_dir)
16
-
17
- model = VoxCPM.from_pretrained("openbmb/VoxCPM-0.5B", cache_dir=cache_dir).to(device)
18
- feature_extractor = VoxCPMFeatureExtractor.from_pretrained("openbmb/VoxCPM-0.5B", cache_dir=cache_dir)
19
 
20
  def generate_speech(text):
21
  """
22
  使用 VoxCPM 模型生成语音的函数。
23
  """
24
  if not text or text.strip() == "":
25
- # 返回空的音频和一条提示信息
26
- return None, "请输入有效文本"
27
 
28
- # 使用模型生成语音波形
29
- wav = model.generate(
30
- text=text,
31
- prompt_wav_path=None,
32
- prompt_text=None,
33
- cfg_value=2.0
34
- )
 
 
 
35
 
36
- # 获取采样率
37
- sampling_rate = feature_extractor.sampling_rate
38
 
39
  # 将生成的波形保存为临时的 .wav 文件
40
  output_filename = "output.wav"
@@ -48,7 +48,7 @@ iface = gr.Interface(
48
  inputs=gr.Textbox(lines=5, label="输入文本", placeholder="在这里输入你想要转换为语音的中文或英文文本..."),
49
  outputs=gr.Audio(label="生成的语音"),
50
  title="🎙️ VoxCPM-0.5B 文本到语音转换",
51
- description="这是一个使用 openbmb/VoxCPM-0.5B 模型进行文本到语音合成的演示。输入一些文本,然后点击 'Submit' 来生成语音。",
52
  examples=[
53
  ["VoxCPM 是一个创新的端到端 TTS 模型,旨在生成高度富有表现力的语音。"],
54
  ["今天天气真不错,我们一起去散步吧!"],
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import AutoProcessor, AutoModel
 
4
  import scipy.io.wavfile
5
  import os
6
 
7
  # 检查是否有可用的 GPU,否则使用 CPU
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
 
10
+ # 模型 ID
11
+ model_id = "openbmb/VoxCPM-0.5B"
12
+
13
+ # 使用 trust_remote_code=True 加载模型和处理器
14
+ # 这会自动处理背后所需的代码,无需我们手动添加 .py 文件
15
+ model = AutoModel.from_pretrained(model_id, trust_remote_code=True).to(device)
16
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
 
17
 
18
  def generate_speech(text):
19
  """
20
  使用 VoxCPM 模型生成语音的函数。
21
  """
22
  if not text or text.strip() == "":
23
+ return None
 
24
 
25
+ # 使用处理器准备输入
26
+ inputs = processor(text=text, return_tensors="pt").to(device)
27
+
28
+ # 生成语音波形
29
+ # 注意: .generate() 可能会返回一个包含波形的元组
30
+ output = model.generate(**inputs, cfg_value=2.0)
31
+
32
+ # 提取波形数据,它通常是输出的第一个元素
33
+ wav = output[0] if isinstance(output, tuple) else output
34
+ wav = wav.cpu().numpy()
35
 
36
+ # 从处理器获取采样率
37
+ sampling_rate = processor.sampling_rate
38
 
39
  # 将生成的波形保存为临时的 .wav 文件
40
  output_filename = "output.wav"
 
48
  inputs=gr.Textbox(lines=5, label="输入文本", placeholder="在这里输入你想要转换为语音的中文或英文文本..."),
49
  outputs=gr.Audio(label="生成的语音"),
50
  title="🎙️ VoxCPM-0.5B 文本到语音转换",
51
+ description="这是一个使用 openbmb/VoxCPM-0.5B 模型进行文本到语音合成的演示。",
52
  examples=[
53
  ["VoxCPM 是一个创新的端到端 TTS 模型,旨在生成高度富有表现力的语音。"],
54
  ["今天天气真不错,我们一起去散步吧!"],