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()