Spaces:
Runtime error
Runtime error
init
Browse files- app.py +44 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gtts import gTTS
|
3 |
+
|
4 |
+
# 1. 將對話轉換為語音的函數
|
5 |
+
def dialogue_to_audio(dialogue):
|
6 |
+
text = " ".join([item["text"] for item in dialogue])
|
7 |
+
tts = gTTS(text=text, lang='en') # 如果你的對話是中文,改成 lang='zh'
|
8 |
+
file_path = "temp_audio.mp3"
|
9 |
+
tts.save(file_path)
|
10 |
+
return file_path
|
11 |
+
|
12 |
+
# 2. 生成對話的函數
|
13 |
+
def generate_dialogue(rounds: int, method: str) -> list:
|
14 |
+
dialogue = []
|
15 |
+
for i in range(rounds):
|
16 |
+
if method == "auto":
|
17 |
+
dialogue.append({"role": "A" if i % 2 == 0 else "B", "text": f"自動文本 {i + 1}"})
|
18 |
+
else:
|
19 |
+
dialogue.append({"role": "A" if i % 2 == 0 else "B", "text": ""})
|
20 |
+
return dialogue
|
21 |
+
|
22 |
+
# 3. 修改 Gradio 函數來返回音頻
|
23 |
+
def generate_dialogue_with_audio(rounds: int, method: str):
|
24 |
+
dialogue = generate_dialogue(rounds, method)
|
25 |
+
audio_path = dialogue_to_audio(dialogue)
|
26 |
+
return audio_path, dialogue
|
27 |
+
|
28 |
+
# 4. 創建 Gradio 界面並啟動
|
29 |
+
def main_interface():
|
30 |
+
interface = gr.Interface(
|
31 |
+
fn=generate_dialogue_with_audio,
|
32 |
+
inputs=[
|
33 |
+
gr.inputs.Slider(minimum=2, maximum=6, step=2, default=2, label="對話輪數"),
|
34 |
+
gr.inputs.Dropdown(choices=["auto", "manual"], label="生成方式"),
|
35 |
+
],
|
36 |
+
outputs=[
|
37 |
+
gr.outputs.Audio(type="file", label="對話朗讀"),
|
38 |
+
gr.outputs.JSON(label="生成的對話")
|
39 |
+
]
|
40 |
+
)
|
41 |
+
interface.launch()
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
main_interface()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
gtts
|