Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
import dashscope
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# 从环境变量读取你的 API Key,或者直接替换成你的 key 字符串
|
| 9 |
+
API_KEY = os.environ['API_KEY']
|
| 10 |
+
|
| 11 |
+
def tts_gradio(text: str, voice: str) -> str:
|
| 12 |
+
"""
|
| 13 |
+
调用 Qwen-TTS 接口合成语音,并将返回的 wav 保存到临时文件,
|
| 14 |
+
返回文件路径给 Gradio 播放。
|
| 15 |
+
"""
|
| 16 |
+
# 调用合成
|
| 17 |
+
response = dashscope.audio.qwen_tts.SpeechSynthesizer.call(
|
| 18 |
+
model="qwen-tts-latest",
|
| 19 |
+
api_key=API_KEY,
|
| 20 |
+
text=text,
|
| 21 |
+
voice=voice,
|
| 22 |
+
)
|
| 23 |
+
audio_url = response.output.audio["url"]
|
| 24 |
+
|
| 25 |
+
# 下载音频
|
| 26 |
+
resp = requests.get(audio_url)
|
| 27 |
+
resp.raise_for_status()
|
| 28 |
+
|
| 29 |
+
# 写入临时文件
|
| 30 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 31 |
+
tmp.write(resp.content)
|
| 32 |
+
tmp.flush()
|
| 33 |
+
tmp.close()
|
| 34 |
+
|
| 35 |
+
# 返回文件路径,Gradio 会自动播放
|
| 36 |
+
return tmp.name
|
| 37 |
+
|
| 38 |
+
# 定义 Gradio 界面
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
fn=tts_gradio,
|
| 41 |
+
inputs=[
|
| 42 |
+
gr.Textbox(lines=4, label="input"),
|
| 43 |
+
gr.Dropdown(choices=["Dylan", "Sunny", "Jada","Cherry","Ethan",'Serena','Chelsie'], value="Dylan", label="speaker"),
|
| 44 |
+
],
|
| 45 |
+
outputs=gr.Audio(label="output"),
|
| 46 |
+
title="Qwen-TTS Gradio demo",
|
| 47 |
+
description="input text,choose speaker,click “submit”",
|
| 48 |
+
allow_flagging="never",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
# 本地调试用:localhost:7860
|
| 53 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 54 |
+
|