File size: 973 Bytes
ada3adc e9a9152 62ebe36 7024d57 62ebe36 7024d57 0b87faa f2a6be8 e9a9152 f2a6be8 d0d8f5e f2a6be8 e9a9152 83affda 2e8d421 f2a6be8 433df58 0b87faa 60880d4 f2a6be8 83affda 1c7f961 60880d4 d30f66c 5dfa8d8 79c8e9e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import gradio as gr
import os
import nltk
from GPT_SoVITS.test import synthesize # 假設這是您的語音合成函數
import asyncio
# 確保下載所需的資源
nltk.download('averaged_perceptron_tagger')
# 定義異步接口函數
async def text_to_speech(target_text):
# 呼叫語音合成函數,假設返回的是文件路徑
output_wav_path = await synthesize(target_text)
# 返回生成的音頻文件
if output_wav_path and os.path.exists(output_wav_path):
return output_wav_path # 返回音頻文件的路徑
else:
return "合成失敗!"
# 創建 Gradio 接口,設置為 API 端點
iface = gr.Interface(
fn=text_to_speech, # 設定處理文本並生成語音的函數
inputs=gr.Textbox(label="輸入文本"), # 用戶輸入文本
outputs=gr.Audio(type="filepath", label="生成的語音"), # 音頻輸出
live=True # 實時生成語音
)
# 啟動 Gradio 接口並啟用 API
iface.launch()
|