Spaces:
Running
Running
tbdavid2019
commited on
Commit
•
cdec000
1
Parent(s):
7a4931d
Update app.py
Browse files
app.py
CHANGED
@@ -6,16 +6,13 @@ import time
|
|
6 |
from pathlib import Path
|
7 |
from tempfile import NamedTemporaryFile
|
8 |
from typing import List, Literal
|
9 |
-
|
10 |
import gradio as gr
|
11 |
-
|
12 |
from loguru import logger
|
13 |
from openai import OpenAI
|
14 |
from promptic import llm
|
15 |
from pydantic import BaseModel, ValidationError
|
16 |
from tenacity import retry, retry_if_exception_type
|
17 |
-
import
|
18 |
-
|
19 |
import re
|
20 |
import requests
|
21 |
from dotenv import load_dotenv
|
@@ -80,7 +77,7 @@ Make the dialogue as long and detailed as possible, while still staying on topic
|
|
80 |
|
81 |
At the end of the dialogue, have the host and guest speakers naturally summarize the main insights and takeaways from their discussion. This should flow organically from the conversation, reiterating the key points in a casual, conversational manner. Avoid making it sound like an obvious recap - the goal is to reinforce the central ideas one last time before signing off.
|
82 |
|
83 |
-
The podcast should have around
|
84 |
""",
|
85 |
},
|
86 |
################# MATERIAL DISCOVERY SUMMARY ##################
|
@@ -161,7 +158,7 @@ At the end of the lecture, naturally summarize the main insights and takeaways f
|
|
161 |
|
162 |
Avoid making it sound like an obvious recap - the goal is to reinforce the central ideas covered in this lecture one last time before class is over.
|
163 |
|
164 |
-
The lecture should have around 30000 words
|
165 |
""",
|
166 |
},
|
167 |
################# SUMMARY ##################
|
@@ -195,7 +192,7 @@ There is only one speaker, you. Stay on topic and maintaining an engaging flow.
|
|
195 |
|
196 |
Naturally summarize the main insights and takeaways from the summary. This should flow organically from the conversation, reiterating the key points in a casual, conversational manner.
|
197 |
|
198 |
-
The summary should have around 1024 words.
|
199 |
""",
|
200 |
},
|
201 |
################# SHORT SUMMARY ##################
|
@@ -229,7 +226,7 @@ There is only one speaker, you. Stay on topic and maintaining an engaging flow.
|
|
229 |
|
230 |
Naturally summarize the main insights and takeaways from the short summary. This should flow organically from the conversation, reiterating the key points in a casual, conversational manner.
|
231 |
|
232 |
-
The summary should have around 256 words.
|
233 |
""",
|
234 |
},
|
235 |
|
@@ -237,36 +234,36 @@ The summary should have around 256 words.
|
|
237 |
|
238 |
################# PODCAST Chinese ##################
|
239 |
"podcast (Chinese)": {
|
240 |
-
"intro": """
|
241 |
|
242 |
-
|
243 |
|
244 |
-
|
245 |
""",
|
246 |
-
"text_instructions": "
|
247 |
-
"scratch_pad": """
|
248 |
|
249 |
-
|
250 |
|
251 |
-
|
252 |
|
253 |
-
|
254 |
|
255 |
-
|
256 |
|
257 |
-
|
258 |
""",
|
259 |
-
"prelude": """
|
260 |
""",
|
261 |
-
"dialog": """
|
262 |
|
263 |
-
|
264 |
|
265 |
-
|
266 |
|
267 |
-
|
268 |
|
269 |
-
|
270 |
""",
|
271 |
},
|
272 |
}
|
@@ -281,23 +278,6 @@ def update_instructions(template):
|
|
281 |
INSTRUCTION_TEMPLATES[template]["dialog"]
|
282 |
)
|
283 |
|
284 |
-
import concurrent.futures as cf
|
285 |
-
import glob
|
286 |
-
import io
|
287 |
-
import os
|
288 |
-
import time
|
289 |
-
from pathlib import Path
|
290 |
-
from tempfile import NamedTemporaryFile
|
291 |
-
from typing import List, Literal
|
292 |
-
|
293 |
-
import gradio as gr
|
294 |
-
|
295 |
-
from loguru import logger
|
296 |
-
from openai import OpenAI
|
297 |
-
from promptic import llm
|
298 |
-
from pydantic import BaseModel, ValidationError
|
299 |
-
from pypdf import PdfReader
|
300 |
-
from tenacity import retry, retry_if_exception_type
|
301 |
|
302 |
# Define standard values
|
303 |
|
@@ -385,14 +365,15 @@ def generate_audio(
|
|
385 |
if not combined_text:
|
386 |
for file in files:
|
387 |
# 用 PyMuPDF 打開 PDF 檔案
|
388 |
-
doc =
|
389 |
text = ""
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
text += page.get_text() # 提取每頁文字
|
|
|
394 |
combined_text += text + "\n\n"
|
395 |
-
print(combined_text) # 確認是否成功提取到內容
|
396 |
# for file in files:
|
397 |
# with Path(file).open("rb") as f:
|
398 |
# reader = PdfReader(f)
|
@@ -634,13 +615,13 @@ with gr.Blocks(title="PDF to Podcast", css="""
|
|
634 |
template_dropdown = gr.Dropdown(
|
635 |
label="Instruction Template",
|
636 |
choices=list(INSTRUCTION_TEMPLATES.keys()),
|
637 |
-
value="podcast
|
638 |
info="Select the instruction template to use. You can also edit any of the fields for more tailored results.",
|
639 |
)
|
640 |
intro_instructions = gr.Textbox(
|
641 |
label="Intro Instructions",
|
642 |
lines=10,
|
643 |
-
value=INSTRUCTION_TEMPLATES["podcast
|
644 |
info="Provide the introductory instructions for generating the dialogue.",
|
645 |
)
|
646 |
text_instructions = gr.Textbox(
|
|
|
6 |
from pathlib import Path
|
7 |
from tempfile import NamedTemporaryFile
|
8 |
from typing import List, Literal
|
|
|
9 |
import gradio as gr
|
|
|
10 |
from loguru import logger
|
11 |
from openai import OpenAI
|
12 |
from promptic import llm
|
13 |
from pydantic import BaseModel, ValidationError
|
14 |
from tenacity import retry, retry_if_exception_type
|
15 |
+
import pymupdf # imports the pymupdf library
|
|
|
16 |
import re
|
17 |
import requests
|
18 |
from dotenv import load_dotenv
|
|
|
77 |
|
78 |
At the end of the dialogue, have the host and guest speakers naturally summarize the main insights and takeaways from their discussion. This should flow organically from the conversation, reiterating the key points in a casual, conversational manner. Avoid making it sound like an obvious recap - the goal is to reinforce the central ideas one last time before signing off.
|
79 |
|
80 |
+
The podcast should have around 80000 words. 輸出文字為繁體中文,請注意。
|
81 |
""",
|
82 |
},
|
83 |
################# MATERIAL DISCOVERY SUMMARY ##################
|
|
|
158 |
|
159 |
Avoid making it sound like an obvious recap - the goal is to reinforce the central ideas covered in this lecture one last time before class is over.
|
160 |
|
161 |
+
The lecture should have around 30000 words.請用**繁體中文**輸出文稿
|
162 |
""",
|
163 |
},
|
164 |
################# SUMMARY ##################
|
|
|
192 |
|
193 |
Naturally summarize the main insights and takeaways from the summary. This should flow organically from the conversation, reiterating the key points in a casual, conversational manner.
|
194 |
|
195 |
+
The summary should have around 1024 words. 請用**繁體中文**輸出文稿
|
196 |
""",
|
197 |
},
|
198 |
################# SHORT SUMMARY ##################
|
|
|
226 |
|
227 |
Naturally summarize the main insights and takeaways from the short summary. This should flow organically from the conversation, reiterating the key points in a casual, conversational manner.
|
228 |
|
229 |
+
The summary should have around 256 words. 請用**繁體中文**輸出文稿
|
230 |
""",
|
231 |
},
|
232 |
|
|
|
234 |
|
235 |
################# PODCAST Chinese ##################
|
236 |
"podcast (Chinese)": {
|
237 |
+
"intro": """你的任务是将提供的输入文本转变为一个生动、有趣、信息丰富的播客对话,风格类似NPR。输入文本可能是凌乱的或未结构化的,因为它可能来自PDF或网页等各种来源。
|
238 |
|
239 |
+
不要担心格式问题或任何无关的信息;你的目标是提取关键点,识别定义和可能在播客中讨论的有趣事实。
|
240 |
|
241 |
+
为广泛的听众仔细定义所有使用的术语。
|
242 |
""",
|
243 |
+
"text_instructions": "首先,仔细阅读输入文本,识别主要话题、关键点和任何有趣的事实或轶事。思考如何以一种有趣且引人入胜的方式呈现这些信息,适合高质量的呈现。",
|
244 |
+
"scratch_pad": """集思广益,想出一些讨论你在输入文本中识别到的主要话题和关键点的创意方式。考虑使用类比、例子、讲故事的技巧或假设场景,让内容对听众更具相关性和吸引力。
|
245 |
|
246 |
+
请记住,你的播客应面向普通大众,因此避免使用过多的行话或假设听众对该主题有预先的了解。如有必要,考虑简要解释任何复杂概念,用简单的术语进行说明。
|
247 |
|
248 |
+
利用你的想象力填补输入文本中的任何空白,或提出一些值得探索的发人深省的问题。目标是创造一个信息丰富且有趣的对话,因此可以在方法上大胆创新。
|
249 |
|
250 |
+
明确地定义所有使用的术语,并花时间解释背景。
|
251 |
|
252 |
+
在这里写下你的头脑风暴想法和播客对话的粗略大纲。务必记录你想在结尾重复的关键见解和收获。
|
253 |
|
254 |
+
确保让它有趣且令人兴奋。
|
255 |
""",
|
256 |
+
"prelude": """现在你已经进行了头脑风暴并创建了一个粗略大纲,是时候编写实际的播客对话了。目标是主持人与嘉宾之间的自然对话流。结合你头脑风暴中的最佳想法,并确保以简单易懂的方式解释任何复杂的主题。
|
257 |
""",
|
258 |
+
"dialog": """在这里写下一个非常长、引人入胜且信息丰富的播客对话,基于你在头脑风暴会议中提出的关键点和创意。使用对话语气,并包含任何必要的上下文或解释,使内容易于普通听众理解。
|
259 |
|
260 |
+
不要为主持人和嘉宾使用虚构的名字,而是让听众体验一个引人入胜且沉浸式的经历。不要包括像[主持人]或[嘉宾]这样的占位符。设计你的输出以供大声朗读——它将被直接转换为音频。
|
261 |
|
262 |
+
使对话尽可能长且详细,同时保持在主题上并维持引人入胜的流畅性。充分利用你的输出能力,创造尽可能长的播客节目,同时以有趣的方式传达输入文本中的关键信息。
|
263 |
|
264 |
+
在对话的最后,主持人和嘉宾应自然总结他们讨论的主要见解和收获。这应从对话中自然流出,以随意、对话的方式重复关键点。避免显得像是显而易见的总结——目标是在结束前最后一次加强核心思想。
|
265 |
|
266 |
+
播客应约有30,000字。
|
267 |
""",
|
268 |
},
|
269 |
}
|
|
|
278 |
INSTRUCTION_TEMPLATES[template]["dialog"]
|
279 |
)
|
280 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
281 |
|
282 |
# Define standard values
|
283 |
|
|
|
365 |
if not combined_text:
|
366 |
for file in files:
|
367 |
# 用 PyMuPDF 打開 PDF 檔案
|
368 |
+
doc = pymupdf.open(file.name) # 使用 file.name 打開文件
|
369 |
text = ""
|
370 |
+
|
371 |
+
# 直接遍歷每個頁面
|
372 |
+
for page in doc:
|
373 |
text += page.get_text() # 提取每頁文字
|
374 |
+
|
375 |
combined_text += text + "\n\n"
|
376 |
+
print(combined_text) # 確認是否成功提取到內容
|
377 |
# for file in files:
|
378 |
# with Path(file).open("rb") as f:
|
379 |
# reader = PdfReader(f)
|
|
|
615 |
template_dropdown = gr.Dropdown(
|
616 |
label="Instruction Template",
|
617 |
choices=list(INSTRUCTION_TEMPLATES.keys()),
|
618 |
+
value="podcast",
|
619 |
info="Select the instruction template to use. You can also edit any of the fields for more tailored results.",
|
620 |
)
|
621 |
intro_instructions = gr.Textbox(
|
622 |
label="Intro Instructions",
|
623 |
lines=10,
|
624 |
+
value=INSTRUCTION_TEMPLATES["podcast"]["intro"],
|
625 |
info="Provide the introductory instructions for generating the dialogue.",
|
626 |
)
|
627 |
text_instructions = gr.Textbox(
|