Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -19,23 +19,23 @@ class PodcastGenerator:
|
|
| 19 |
|
| 20 |
async def generate_script(self, prompt: str, language: str, api_key: str) -> Dict:
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
prompt (str):
|
| 26 |
-
language (str):
|
| 27 |
-
api_key (str): 用於訪問 Gemini AI 服務的 API
|
| 28 |
|
| 29 |
返回:
|
| 30 |
-
Dict: 包含以 JSON
|
| 31 |
|
| 32 |
異常:
|
| 33 |
-
gr.Error: 如果 API
|
| 34 |
|
| 35 |
-
此方法使用 Gemini AI
|
| 36 |
-
|
| 37 |
"""
|
| 38 |
-
# 定義一個示例JSON結構,用於指導AI生成類似格式的Podcast
|
| 39 |
example = """
|
| 40 |
{
|
| 41 |
"topic": "AGI",
|
|
@@ -244,7 +244,7 @@ class PodcastGenerator:
|
|
| 244 |
- The podcast should be interesting, lively, and engaging, and hook the listener from the start.
|
| 245 |
- The input text might be disorganized or unformatted, originating from sources like PDFs or text files. Ignore any formatting inconsistencies or irrelevant details; your task is to distill the essential points, identify key definitions, and highlight intriguing facts that would be suitable for discussion in a podcast.
|
| 246 |
- The script must be in JSON format.
|
| 247 |
-
Follow this example structure
|
| 248 |
{example}
|
| 249 |
"""
|
| 250 |
|
|
@@ -301,8 +301,8 @@ class PodcastGenerator:
|
|
| 301 |
"""
|
| 302 |
非同步生成文字轉語音音訊檔案。
|
| 303 |
|
| 304 |
-
|
| 305 |
-
text (str):
|
| 306 |
speaker (int): 說話者的編號(1 或 2)。
|
| 307 |
speaker1 (str): 第一位說話者的語音設定。
|
| 308 |
speaker2 (str): 第二位說話者的語音設定。
|
|
@@ -310,7 +310,7 @@ class PodcastGenerator:
|
|
| 310 |
返回:
|
| 311 |
str: 生成的臨時音訊檔案的檔名。
|
| 312 |
|
| 313 |
-
此方法使用 Edge TTS
|
| 314 |
根據指定的說話者編號選擇相應的語音設定。
|
| 315 |
"""
|
| 316 |
# 根據說話者選擇語音
|
|
@@ -334,7 +334,7 @@ class PodcastGenerator:
|
|
| 334 |
"""
|
| 335 |
非同步合併音訊檔案。
|
| 336 |
|
| 337 |
-
|
| 338 |
audio_files (List[str]): 包含音訊檔案路徑的列表。
|
| 339 |
|
| 340 |
返回:
|
|
@@ -355,26 +355,26 @@ class PodcastGenerator:
|
|
| 355 |
|
| 356 |
async def generate_podcast(self, input_text: str, language: str, speaker1: str, speaker2: str, api_key: str) -> str:
|
| 357 |
"""
|
| 358 |
-
|
| 359 |
|
| 360 |
-
|
| 361 |
-
input_text (str):
|
| 362 |
-
language (str):
|
| 363 |
speaker1 (str): 第一位說話者的語音設定。
|
| 364 |
speaker2 (str): 第二位說話者的語音設定。
|
| 365 |
-
api_key (str): 用於訪問 Gemini AI 服務的 API
|
| 366 |
|
| 367 |
返回:
|
| 368 |
-
str:
|
| 369 |
|
| 370 |
此方法執行以下步驟:
|
| 371 |
-
1. 使用 generate_script
|
| 372 |
-
2. 使用 tts_generate
|
| 373 |
-
3. 使用 combine_audio_files
|
| 374 |
|
| 375 |
整個過程是非同步的,以提高效率。方法還會記錄並顯示每個步驟的執行時間。
|
| 376 |
"""
|
| 377 |
-
# 生成Podcast
|
| 378 |
gr.Info("Generating podcast script...")
|
| 379 |
start_time = time.time()
|
| 380 |
podcast_json = await self.generate_script(input_text, language, api_key)
|
|
@@ -420,21 +420,21 @@ class TextExtractor:
|
|
| 420 |
|
| 421 |
async def process_input(input_text: str, input_file, language: str, speaker1: str, speaker2: str, api_key: str = "") -> str:
|
| 422 |
"""
|
| 423 |
-
|
| 424 |
|
| 425 |
-
|
| 426 |
-
input_text (str):
|
| 427 |
-
input_file:
|
| 428 |
language (str): 選擇的語言。
|
| 429 |
speaker1 (str): 第一位說話者的語音選擇。
|
| 430 |
speaker2 (str): 第二位說話者的語音選擇。
|
| 431 |
-
api_key (str): 用於生成 AI 的 API
|
| 432 |
|
| 433 |
返回:
|
| 434 |
-
str:
|
| 435 |
|
| 436 |
-
|
| 437 |
-
|
| 438 |
"""
|
| 439 |
# 開始生成Podcast
|
| 440 |
gr.Info("Starting podcast generation...")
|
|
@@ -462,7 +462,7 @@ async def process_input(input_text: str, input_file, language: str, speaker1: st
|
|
| 462 |
|
| 463 |
# 如果沒有提供API金鑰,則使用環境變數中的金鑰
|
| 464 |
if not api_key:
|
| 465 |
-
api_key = os.getenv("
|
| 466 |
|
| 467 |
# 建立PodcastGenerator實例並生成Podcast
|
| 468 |
podcast_generator = PodcastGenerator()
|
|
@@ -524,7 +524,7 @@ iface = gr.Interface(
|
|
| 524 |
outputs=[
|
| 525 |
gr.Audio(label="Generated Podcast Audio")
|
| 526 |
],
|
| 527 |
-
title="
|
| 528 |
description="Generate a 2-speaker podcast from text input or documents!",
|
| 529 |
allow_flagging="never"
|
| 530 |
)
|
|
@@ -532,4 +532,3 @@ iface = gr.Interface(
|
|
| 532 |
if __name__ == "__main__":
|
| 533 |
iface.launch()
|
| 534 |
|
| 535 |
-
|
|
|
|
| 19 |
|
| 20 |
async def generate_script(self, prompt: str, language: str, api_key: str) -> Dict:
|
| 21 |
"""
|
| 22 |
+
異步生成基於給定提示和語言的播客腳本。
|
| 23 |
|
| 24 |
+
參數:
|
| 25 |
+
prompt (str): 用於生成播客腳本的用戶輸入文本。
|
| 26 |
+
language (str): 播客腳本所需的語言。
|
| 27 |
+
api_key (str): 用於訪問 Gemini AI 服務的 API 密鑰。
|
| 28 |
|
| 29 |
返回:
|
| 30 |
+
Dict: 包含以 JSON 格式生成的播客腳本的字典。
|
| 31 |
|
| 32 |
異常:
|
| 33 |
+
gr.Error: 如果 API 密鑰或速率限制出現問題。
|
| 34 |
|
| 35 |
+
此方法使用 Gemini AI 模型根據用戶的輸入生成播客腳本。
|
| 36 |
+
它處理語言選擇,使用適當的配置設置 AI 模型,並處理生成的響應。
|
| 37 |
"""
|
| 38 |
+
# 定義一個示例JSON結構,用於指導AI生成類似格式的Podcast指令碼
|
| 39 |
example = """
|
| 40 |
{
|
| 41 |
"topic": "AGI",
|
|
|
|
| 244 |
- The podcast should be interesting, lively, and engaging, and hook the listener from the start.
|
| 245 |
- The input text might be disorganized or unformatted, originating from sources like PDFs or text files. Ignore any formatting inconsistencies or irrelevant details; your task is to distill the essential points, identify key definitions, and highlight intriguing facts that would be suitable for discussion in a podcast.
|
| 246 |
- The script must be in JSON format.
|
| 247 |
+
Follow this example structure:
|
| 248 |
{example}
|
| 249 |
"""
|
| 250 |
|
|
|
|
| 301 |
"""
|
| 302 |
非同步生成文字轉語音音訊檔案。
|
| 303 |
|
| 304 |
+
參數:
|
| 305 |
+
text (str): 要轉換為語音的文字內容。
|
| 306 |
speaker (int): 說話者的編號(1 或 2)。
|
| 307 |
speaker1 (str): 第一位說話者的語音設定。
|
| 308 |
speaker2 (str): 第二位說話者的語音設定。
|
|
|
|
| 310 |
返回:
|
| 311 |
str: 生成的臨時音訊檔案的檔名。
|
| 312 |
|
| 313 |
+
此方法使用 Edge TTS 將文字轉換為語音,並將結果保存為臨時音訊檔案。
|
| 314 |
根據指定的說話者編號選擇相應的語音設定。
|
| 315 |
"""
|
| 316 |
# 根據說話者選擇語音
|
|
|
|
| 334 |
"""
|
| 335 |
非同步合併音訊檔案。
|
| 336 |
|
| 337 |
+
參數:
|
| 338 |
audio_files (List[str]): 包含音訊檔案路徑的列表。
|
| 339 |
|
| 340 |
返回:
|
|
|
|
| 355 |
|
| 356 |
async def generate_podcast(self, input_text: str, language: str, speaker1: str, speaker2: str, api_key: str) -> str:
|
| 357 |
"""
|
| 358 |
+
非同步生成播客音訊檔案。
|
| 359 |
|
| 360 |
+
參數:
|
| 361 |
+
input_text (str): 用於生成播客腳本的輸入文本。
|
| 362 |
+
language (str): 播客使用的語言。
|
| 363 |
speaker1 (str): 第一位說話者的語音設定。
|
| 364 |
speaker2 (str): 第二位說話者的語音設定。
|
| 365 |
+
api_key (str): 用於訪問 Gemini AI 服務的 API 密鑰。
|
| 366 |
|
| 367 |
返回:
|
| 368 |
+
str: 生成的播客音訊檔案的檔名。
|
| 369 |
|
| 370 |
此方法執行以下步驟:
|
| 371 |
+
1. 使用 generate_script 方法生成播客腳本。
|
| 372 |
+
2. 使用 tts_generate 方法為每個對話行生成音訊檔案。
|
| 373 |
+
3. 使用 combine_audio_files 方法將所有音訊檔案合併為一個完整的播客。
|
| 374 |
|
| 375 |
整個過程是非同步的,以提高效率。方法還會記錄並顯示每個步驟的執行時間。
|
| 376 |
"""
|
| 377 |
+
# 生成Podcast指令碼
|
| 378 |
gr.Info("Generating podcast script...")
|
| 379 |
start_time = time.time()
|
| 380 |
podcast_json = await self.generate_script(input_text, language, api_key)
|
|
|
|
| 420 |
|
| 421 |
async def process_input(input_text: str, input_file, language: str, speaker1: str, speaker2: str, api_key: str = "") -> str:
|
| 422 |
"""
|
| 423 |
+
處理輸入並生成播客的非同步函數。
|
| 424 |
|
| 425 |
+
參數:
|
| 426 |
+
input_text (str): 用戶輸入的文本內容。
|
| 427 |
+
input_file: 用戶上傳的文件(可以是 PDF 或 TXT)。
|
| 428 |
language (str): 選擇的語言。
|
| 429 |
speaker1 (str): 第一位說話者的語音選擇。
|
| 430 |
speaker2 (str): 第二位說話者的語音選擇。
|
| 431 |
+
api_key (str): 用於生成 AI 的 API 金鑰,預設為空字串。
|
| 432 |
|
| 433 |
返回:
|
| 434 |
+
str: 生成的播客音頻文件路徑。
|
| 435 |
|
| 436 |
+
此函數協調整個播客生成過程,包括文本提取、腳本生成和音頻合成。
|
| 437 |
+
它處理不同的輸入類型(文本或文件),並使用指定的語音和語言設置來創建最終的播客。
|
| 438 |
"""
|
| 439 |
# 開始生成Podcast
|
| 440 |
gr.Info("Starting podcast generation...")
|
|
|
|
| 462 |
|
| 463 |
# 如果沒有提供API金鑰,則使用環境變數中的金鑰
|
| 464 |
if not api_key:
|
| 465 |
+
api_key = os.getenv("GENAI_API_KEY")
|
| 466 |
|
| 467 |
# 建立PodcastGenerator實例並生成Podcast
|
| 468 |
podcast_generator = PodcastGenerator()
|
|
|
|
| 524 |
outputs=[
|
| 525 |
gr.Audio(label="Generated Podcast Audio")
|
| 526 |
],
|
| 527 |
+
title="PodcastGen 🎙️",
|
| 528 |
description="Generate a 2-speaker podcast from text input or documents!",
|
| 529 |
allow_flagging="never"
|
| 530 |
)
|
|
|
|
| 532 |
if __name__ == "__main__":
|
| 533 |
iface.launch()
|
| 534 |
|
|
|