Ryanus commited on
Commit
8ee181e
·
verified ·
1 Parent(s): d4b54ef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from parler_tts import ParlerTTSForConditionalGeneration
3
+ from transformers import AutoTokenizer
4
+ import soundfile as sf
5
+ import gradio as gr
6
+ import numpy as np
7
+ import tempfile
8
+
9
+ # 載入模型與 tokenizer(建議使用 mini-expresso 版本,兼容性較好)
10
+ MODEL_ID = "parler-tts/parler-tts-mini-expresso"
11
+ device = "cpu"
12
+
13
+ model = ParlerTTSForConditionalGeneration.from_pretrained(MODEL_ID).to(device)
14
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
15
+
16
+ def tts(text, description):
17
+ # 將描述和文本進行編碼
18
+ input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
19
+ prompt_input_ids = tokenizer(text, return_tensors="pt").input_ids.to(device)
20
+ # 生成語音
21
+ generation = model.generate(
22
+ input_ids=input_ids,
23
+ prompt_input_ids=prompt_input_ids
24
+ )
25
+ audio_arr = generation.cpu().numpy().squeeze()
26
+ # 輸出為 wav 格式
27
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
28
+ sf.write(fp.name, audio_arr, model.config.sampling_rate)
29
+ return fp.name
30
+
31
+ # Gradio 介面
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown("## Parler-TTS Mini (Huggingface Space CPU)")
34
+ with gr.Row():
35
+ text = gr.Textbox(label="輸入文字", value="Hello, this is Parler-TTS mini running on CPU.")
36
+ with gr.Row():
37
+ description = gr.Textbox(label="語音描述", value="A young female speaker, neutral tone, high quality audio.")
38
+ with gr.Row():
39
+ out_audio = gr.Audio(label="生成語音", type="filepath")
40
+ btn = gr.Button("生成語音")
41
+ btn.click(fn=tts, inputs=[text, description], outputs=out_audio)
42
+
43
+ if __name__ == "__main__":
44
+ demo.launch()
45
+