File size: 1,474 Bytes
9223239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import requests
import tempfile

import dashscope
import gradio as gr

# 从环境变量读取你的 API Key,或者直接替换成你的 key 字符串
API_KEY = os.environ['API_KEY']

def tts_gradio(text: str, voice: str) -> str:
    """
    调用 Qwen-TTS 接口合成语音,并将返回的 wav 保存到临时文件,
    返回文件路径给 Gradio 播放。
    """
    # 调用合成
    response = dashscope.audio.qwen_tts.SpeechSynthesizer.call(
        model="qwen-tts-latest",
        api_key=API_KEY,
        text=text,
        voice=voice,
    )
    audio_url = response.output.audio["url"]

    # 下载音频
    resp = requests.get(audio_url)
    resp.raise_for_status()

    # 写入临时文件
    tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
    tmp.write(resp.content)
    tmp.flush()
    tmp.close()

    # 返回文件路径,Gradio 会自动播放
    return tmp.name

# 定义 Gradio 界面
demo = gr.Interface(
    fn=tts_gradio,
    inputs=[
        gr.Textbox(lines=4, label="input"),
        gr.Dropdown(choices=["Dylan", "Sunny", "Jada","Cherry","Ethan",'Serena','Chelsie'], value="Dylan", label="speaker"),
    ],
    outputs=gr.Audio(label="output"),
    title="Qwen-TTS Gradio demo",
    description="input text,choose speaker,click “submit”",
    allow_flagging="never",
)

if __name__ == "__main__":
    # 本地调试用:localhost:7860
    demo.launch(server_name="0.0.0.0", server_port=7860)