dw2026 commited on
Commit
d127435
·
verified ·
1 Parent(s): 2912540

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +4 -11
  2. app.py +34 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,12 +1,5 @@
1
- ---
2
- title: Cv2026
3
- emoji: 👀
4
- colorFrom: green
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 5.32.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
+ # CosyVoice2 中文语音合成 Demo
 
 
 
 
 
 
 
 
 
2
 
3
+ 这是使用 Hugging Face 上的 FunAudioLLM/CosyVoice2-0.5B 模型构建的语音合成演示空间。你可以输入任意中文文本,在线生成高质量语音。
4
+
5
+ 模型地址:https://huggingface.co/FunAudioLLM/CosyVoice2-0.5B
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoProcessor, AutoModelForSpeechT5
3
+ import torch
4
+ import scipy.io.wavfile
5
+ import tempfile
6
+
7
+ # 加载模型和处理器
8
+ processor = AutoProcessor.from_pretrained("FunAudioLLM/CosyVoice2-0.5B")
9
+ model = AutoModelForSpeechT5.from_pretrained("FunAudioLLM/CosyVoice2-0.5B")
10
+
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ model = model.to(device)
13
+
14
+ # 合成语音的函数
15
+ def tts_fn(text):
16
+ inputs = processor(text, return_tensors="pt").to(device)
17
+ with torch.no_grad():
18
+ speech = model.generate(**inputs)
19
+ speech = speech.cpu().numpy()
20
+
21
+ # 保存为临时 wav 文件
22
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
23
+ scipy.io.wavfile.write(f.name, rate=16000, data=speech)
24
+ return f.name
25
+
26
+ # Gradio 界面
27
+ demo = gr.Interface(
28
+ fn=tts_fn,
29
+ inputs=gr.Textbox(label="请输入中文文本", placeholder="例如:你好,这是 CosyVoice2 的语音演示"),
30
+ outputs=gr.Audio(label="合成语音"),
31
+ title="CosyVoice2 中文语音合成演示"
32
+ )
33
+
34
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ scipy
4
+ gradio