Update app.py
Browse files
app.py
CHANGED
|
@@ -14,9 +14,9 @@ def generate_unique_filename(folder, prefix="audio", ext="mp3"):
|
|
| 14 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
| 15 |
return os.path.join(folder, f"{prefix}_{timestamp}.{ext}")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
async def generate_speech(text, voice, rate, pitch):
|
| 19 |
-
output_file = generate_unique_filename(
|
| 20 |
communicate = edge_tts.Communicate(text, voice, rate=rate, pitch=pitch)
|
| 21 |
await communicate.save(output_file)
|
| 22 |
return output_file
|
|
@@ -26,41 +26,38 @@ async def get_voices():
|
|
| 26 |
voices = await edge_tts.list_voices()
|
| 27 |
return [voice["ShortName"] for voice in voices]
|
| 28 |
|
| 29 |
-
# 已儲存語音列表
|
| 30 |
def list_saved_audios():
|
| 31 |
files = sorted(os.listdir(AUDIO_DIR), reverse=True)
|
| 32 |
return [os.path.join(AUDIO_DIR, f) for f in files if f.endswith(".mp3")]
|
| 33 |
|
| 34 |
-
# 已儲存播客列表
|
| 35 |
def list_saved_podcasts():
|
| 36 |
files = sorted(os.listdir(PODCAST_DIR), reverse=True)
|
| 37 |
return [os.path.join(PODCAST_DIR, f) for f in files if f.endswith(".mp3")]
|
| 38 |
|
| 39 |
-
#
|
| 40 |
async def tts_interface(text, voice, rate_percentage, pitch_hz):
|
| 41 |
rate = f"{'+' if rate_percentage >= 0 else ''}{rate_percentage}%"
|
| 42 |
pitch = f"{'+' if pitch_hz >= 0 else ''}{pitch_hz}Hz"
|
| 43 |
audio_path = await generate_speech(text, voice, rate, pitch)
|
| 44 |
return audio_path
|
| 45 |
|
| 46 |
-
# 播放已儲存語音
|
| 47 |
def play_saved_audio(audio_file):
|
| 48 |
return audio_file
|
| 49 |
|
| 50 |
# 播客製作:多段腳本合成並拼接、可插入背景音樂
|
| 51 |
-
async def podcast_produce(
|
| 52 |
rate = f"{'+' if rate_percentage >= 0 else ''}{rate_percentage}%"
|
| 53 |
pitch = f"{'+' if pitch_hz >= 0 else ''}{pitch_hz}Hz"
|
| 54 |
-
# 每段腳本合成語音
|
| 55 |
audio_segments = []
|
| 56 |
-
for idx, text in enumerate(
|
| 57 |
if text.strip():
|
| 58 |
temp_audio = generate_unique_filename(PODCAST_DIR, prefix=f"segment{idx}")
|
| 59 |
communicate = edge_tts.Communicate(text, voice, rate=rate, pitch=pitch)
|
| 60 |
await communicate.save(temp_audio)
|
| 61 |
audio_segments.append(AudioSegment.from_file(temp_audio))
|
| 62 |
os.remove(temp_audio)
|
| 63 |
-
|
|
|
|
| 64 |
podcast_audio = sum(audio_segments)
|
| 65 |
# 插入背景音樂(可選)
|
| 66 |
if bgm_file is not None and os.path.isfile(bgm_file):
|
|
@@ -76,14 +73,31 @@ async def podcast_produce(scripts, voice, rate_percentage, pitch_hz, bgm_file, p
|
|
| 76 |
f.write(f"Title: {podcast_title}\nDescription: {podcast_desc}\n")
|
| 77 |
return podcast_file
|
| 78 |
|
| 79 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
async def main():
|
| 81 |
voices = await get_voices()
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
with gr.Blocks() as demo:
|
| 84 |
-
gr.Markdown("## Edge TTS 語音合成與播客製作")
|
| 85 |
with gr.Tab("語音合成"):
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
voice_input = gr.Dropdown(voices, label="選擇語音", value="zh-CN-XiaoxiaoNeural")
|
| 88 |
rate_input = gr.Slider(-50, 50, value=0, step=1, label="語速調整 (%)")
|
| 89 |
pitch_input = gr.Slider(-50, 50, value=0, step=1, label="音高調整 (Hz)")
|
|
@@ -94,18 +108,32 @@ async def main():
|
|
| 94 |
inputs=[text_input, voice_input, rate_input, pitch_input],
|
| 95 |
outputs=audio_output
|
| 96 |
)
|
|
|
|
|
|
|
| 97 |
with gr.Tab("檢視已儲存語音"):
|
| 98 |
-
audio_files = list_saved_audios()
|
| 99 |
-
audio_dropdown = gr.Dropdown(audio_files, label="選擇已儲存語音檔案")
|
| 100 |
saved_audio_output = gr.Audio(type="filepath", label="播放已儲存語音")
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
inputs=audio_dropdown,
|
| 104 |
-
outputs=saved_audio_output
|
| 105 |
-
)
|
| 106 |
with gr.Tab("���客製作"):
|
| 107 |
-
gr.Markdown("###
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
voice_input2 = gr.Dropdown(voices, label="選擇語音", value="zh-CN-XiaoxiaoNeural")
|
| 110 |
rate_input2 = gr.Slider(-50, 50, value=0, step=1, label="語速調整 (%)")
|
| 111 |
pitch_input2 = gr.Slider(-50, 50, value=0, step=1, label="音高調整 (Hz)")
|
|
@@ -114,20 +142,19 @@ async def main():
|
|
| 114 |
podcast_desc = gr.Textbox(label="播客描述")
|
| 115 |
podcast_btn = gr.Button("生成播客")
|
| 116 |
podcast_output = gr.Audio(type="filepath", label="生成的播客音檔")
|
|
|
|
|
|
|
|
|
|
| 117 |
podcast_btn.click(
|
| 118 |
-
fn=podcast_produce,
|
| 119 |
-
inputs=[
|
| 120 |
outputs=podcast_output
|
| 121 |
)
|
|
|
|
| 122 |
with gr.Tab("檢視已儲存播客"):
|
| 123 |
-
podcast_files = list_saved_podcasts()
|
| 124 |
-
podcast_dropdown = gr.Dropdown(podcast_files, label="選擇已儲存播客檔案")
|
| 125 |
saved_podcast_output = gr.Audio(type="filepath", label="播放已儲存播客")
|
| 126 |
-
|
| 127 |
-
fn=play_saved_audio,
|
| 128 |
-
inputs=podcast_dropdown,
|
| 129 |
-
outputs=saved_podcast_output
|
| 130 |
-
)
|
| 131 |
|
| 132 |
demo.launch()
|
| 133 |
|
|
|
|
| 14 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
| 15 |
return os.path.join(folder, f"{prefix}_{timestamp}.{ext}")
|
| 16 |
|
| 17 |
+
# Edge TTS 語音合成
|
| 18 |
+
async def generate_speech(text, voice, rate, pitch, folder=AUDIO_DIR):
|
| 19 |
+
output_file = generate_unique_filename(folder)
|
| 20 |
communicate = edge_tts.Communicate(text, voice, rate=rate, pitch=pitch)
|
| 21 |
await communicate.save(output_file)
|
| 22 |
return output_file
|
|
|
|
| 26 |
voices = await edge_tts.list_voices()
|
| 27 |
return [voice["ShortName"] for voice in voices]
|
| 28 |
|
|
|
|
| 29 |
def list_saved_audios():
|
| 30 |
files = sorted(os.listdir(AUDIO_DIR), reverse=True)
|
| 31 |
return [os.path.join(AUDIO_DIR, f) for f in files if f.endswith(".mp3")]
|
| 32 |
|
|
|
|
| 33 |
def list_saved_podcasts():
|
| 34 |
files = sorted(os.listdir(PODCAST_DIR), reverse=True)
|
| 35 |
return [os.path.join(PODCAST_DIR, f) for f in files if f.endswith(".mp3")]
|
| 36 |
|
| 37 |
+
# 單段語音合成介面
|
| 38 |
async def tts_interface(text, voice, rate_percentage, pitch_hz):
|
| 39 |
rate = f"{'+' if rate_percentage >= 0 else ''}{rate_percentage}%"
|
| 40 |
pitch = f"{'+' if pitch_hz >= 0 else ''}{pitch_hz}Hz"
|
| 41 |
audio_path = await generate_speech(text, voice, rate, pitch)
|
| 42 |
return audio_path
|
| 43 |
|
|
|
|
| 44 |
def play_saved_audio(audio_file):
|
| 45 |
return audio_file
|
| 46 |
|
| 47 |
# 播客製作:多段腳本合成並拼接、可插入背景音樂
|
| 48 |
+
async def podcast_produce(script_list, voice, rate_percentage, pitch_hz, bgm_file, podcast_title, podcast_desc):
|
| 49 |
rate = f"{'+' if rate_percentage >= 0 else ''}{rate_percentage}%"
|
| 50 |
pitch = f"{'+' if pitch_hz >= 0 else ''}{pitch_hz}Hz"
|
|
|
|
| 51 |
audio_segments = []
|
| 52 |
+
for idx, text in enumerate(script_list):
|
| 53 |
if text.strip():
|
| 54 |
temp_audio = generate_unique_filename(PODCAST_DIR, prefix=f"segment{idx}")
|
| 55 |
communicate = edge_tts.Communicate(text, voice, rate=rate, pitch=pitch)
|
| 56 |
await communicate.save(temp_audio)
|
| 57 |
audio_segments.append(AudioSegment.from_file(temp_audio))
|
| 58 |
os.remove(temp_audio)
|
| 59 |
+
if not audio_segments:
|
| 60 |
+
return None
|
| 61 |
podcast_audio = sum(audio_segments)
|
| 62 |
# 插入背景音樂(可選)
|
| 63 |
if bgm_file is not None and os.path.isfile(bgm_file):
|
|
|
|
| 73 |
f.write(f"Title: {podcast_title}\nDescription: {podcast_desc}\n")
|
| 74 |
return podcast_file
|
| 75 |
|
| 76 |
+
# 動態段落管理
|
| 77 |
+
def add_paragraph(paragraphs):
|
| 78 |
+
paragraphs.append("")
|
| 79 |
+
return paragraphs
|
| 80 |
+
|
| 81 |
+
def remove_paragraph(paragraphs):
|
| 82 |
+
if len(paragraphs) > 1:
|
| 83 |
+
paragraphs.pop()
|
| 84 |
+
return paragraphs
|
| 85 |
+
|
| 86 |
+
def clear_paragraphs():
|
| 87 |
+
return [""]
|
| 88 |
+
|
| 89 |
+
def clear_textbox():
|
| 90 |
+
return ""
|
| 91 |
+
|
| 92 |
async def main():
|
| 93 |
voices = await get_voices()
|
| 94 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 95 |
+
gr.Markdown("## 🎙️ Edge TTS 語音合成與播客製作\n\n- 支援多段腳本、段落自由增減、內容一鍵清空\n- 介面直覺、操作友善、檔案自動管理")
|
| 96 |
|
|
|
|
|
|
|
| 97 |
with gr.Tab("語音合成"):
|
| 98 |
+
with gr.Row():
|
| 99 |
+
text_input = gr.Textbox(lines=5, label="輸入文本")
|
| 100 |
+
clear_btn = gr.Button("清空")
|
| 101 |
voice_input = gr.Dropdown(voices, label="選擇語音", value="zh-CN-XiaoxiaoNeural")
|
| 102 |
rate_input = gr.Slider(-50, 50, value=0, step=1, label="語速調整 (%)")
|
| 103 |
pitch_input = gr.Slider(-50, 50, value=0, step=1, label="音高調整 (Hz)")
|
|
|
|
| 108 |
inputs=[text_input, voice_input, rate_input, pitch_input],
|
| 109 |
outputs=audio_output
|
| 110 |
)
|
| 111 |
+
clear_btn.click(fn=clear_textbox, outputs=text_input)
|
| 112 |
+
|
| 113 |
with gr.Tab("檢視已儲存語音"):
|
| 114 |
+
audio_files = gr.Dropdown(list_saved_audios(), label="選擇已儲存語音檔案", interactive=True)
|
|
|
|
| 115 |
saved_audio_output = gr.Audio(type="filepath", label="播放已儲存語音")
|
| 116 |
+
audio_files.change(fn=play_saved_audio, inputs=audio_files, outputs=saved_audio_output)
|
| 117 |
+
|
|
|
|
|
|
|
|
|
|
| 118 |
with gr.Tab("���客製作"):
|
| 119 |
+
gr.Markdown("### 📝 多段腳本輸入(可自由增減段落)")
|
| 120 |
+
script_state = gr.State([""])
|
| 121 |
+
script_boxes = gr.Group()
|
| 122 |
+
def render_script_boxes(paragraphs):
|
| 123 |
+
return [gr.Textbox(value=p, label=f"段落{i+1}內容", lines=3) for i, p in enumerate(paragraphs)]
|
| 124 |
+
script_boxes.render = render_script_boxes
|
| 125 |
+
add_btn = gr.Button("新增段落")
|
| 126 |
+
remove_btn = gr.Button("刪除段落")
|
| 127 |
+
clear_all_btn = gr.Button("全部清空")
|
| 128 |
+
def update_script_boxes(paragraphs):
|
| 129 |
+
return gr.update(components=render_script_boxes(paragraphs))
|
| 130 |
+
add_btn.click(fn=add_paragraph, inputs=script_state, outputs=script_state).then(update_script_boxes, script_state, script_boxes)
|
| 131 |
+
remove_btn.click(fn=remove_paragraph, inputs=script_state, outputs=script_state).then(update_script_boxes, script_state, script_boxes)
|
| 132 |
+
clear_all_btn.click(fn=clear_paragraphs, outputs=script_state).then(update_script_boxes, script_state, script_boxes)
|
| 133 |
+
# 取得所有段落內容
|
| 134 |
+
def collect_scripts(*args):
|
| 135 |
+
return list(args)
|
| 136 |
+
# 參數設定
|
| 137 |
voice_input2 = gr.Dropdown(voices, label="選擇語音", value="zh-CN-XiaoxiaoNeural")
|
| 138 |
rate_input2 = gr.Slider(-50, 50, value=0, step=1, label="語速調整 (%)")
|
| 139 |
pitch_input2 = gr.Slider(-50, 50, value=0, step=1, label="音高調整 (Hz)")
|
|
|
|
| 142 |
podcast_desc = gr.Textbox(label="播客描述")
|
| 143 |
podcast_btn = gr.Button("生成播客")
|
| 144 |
podcast_output = gr.Audio(type="filepath", label="生成的播客音檔")
|
| 145 |
+
# 合成流程
|
| 146 |
+
def get_current_scripts(*args):
|
| 147 |
+
return [v for v in args if isinstance(v, str)]
|
| 148 |
podcast_btn.click(
|
| 149 |
+
fn=lambda *args: asyncio.run(podcast_produce(get_current_scripts(*args[:-6]), args[-6], args[-5], args[-4], args[-3], args[-2], args[-1])),
|
| 150 |
+
inputs=[*script_boxes, voice_input2, rate_input2, pitch_input2, bgm_input, podcast_title, podcast_desc],
|
| 151 |
outputs=podcast_output
|
| 152 |
)
|
| 153 |
+
|
| 154 |
with gr.Tab("檢視已儲存播客"):
|
| 155 |
+
podcast_files = gr.Dropdown(list_saved_podcasts(), label="選擇已儲存播客檔案", interactive=True)
|
|
|
|
| 156 |
saved_podcast_output = gr.Audio(type="filepath", label="播放已儲存播客")
|
| 157 |
+
podcast_files.change(fn=play_saved_audio, inputs=podcast_files, outputs=saved_podcast_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
demo.launch()
|
| 160 |
|