Update app.py
Browse files
app.py
CHANGED
@@ -7,15 +7,15 @@ from pathlib import Path
|
|
7 |
from tempfile import NamedTemporaryFile
|
8 |
from typing import List, Literal
|
9 |
import re
|
|
|
10 |
from transformers import pipeline
|
11 |
from pydantic import BaseModel
|
12 |
-
import gradio as gr
|
13 |
|
14 |
-
# Initialize Hugging Face
|
15 |
text_generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B')
|
16 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
17 |
|
18 |
-
# Instruction templates
|
19 |
INSTRUCTION_TEMPLATES = {
|
20 |
"podcast": {
|
21 |
"intro": """Your task is to take the input text provided and turn it into a lively, engaging, informative podcast dialogue, in the style of NPR...""",
|
@@ -95,7 +95,7 @@ def get_mp3(text: str, voice: str, audio_model: str) -> bytes:
|
|
95 |
|
96 |
# Main audio generation function (adapted for Hugging Face text generation)
|
97 |
def generate_audio(
|
98 |
-
|
99 |
text_model: str = "EleutherAI/gpt-neo-2.7B",
|
100 |
audio_model: str = "tts-1",
|
101 |
speaker_1_voice: str = "alloy",
|
@@ -111,13 +111,12 @@ def generate_audio(
|
|
111 |
debug = False,
|
112 |
) -> tuple:
|
113 |
|
114 |
-
# Combine input text from
|
115 |
combined_text = original_text or ""
|
116 |
if not combined_text:
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
combined_text += text + "\n\n"
|
121 |
|
122 |
# Generate the dialogue using Hugging Face
|
123 |
llm_output = generate_dialogue(
|
@@ -158,40 +157,48 @@ def generate_audio(
|
|
158 |
|
159 |
return temporary_file.name, transcript, combined_text
|
160 |
|
161 |
-
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
def interface():
|
163 |
with gr.Blocks() as demo:
|
164 |
-
gr.Markdown("# Podcast Generator from PDF
|
165 |
-
gr.Markdown("Upload a PDF file and generate
|
166 |
|
167 |
with gr.Row():
|
168 |
-
|
169 |
-
pdf_input = gr.File(label="Upload PDF(s)", file_types=[".pdf"], type="file", multiple=True)
|
170 |
|
171 |
with gr.Row():
|
172 |
-
# Instructions input
|
173 |
intro_input = gr.Textbox(label="Intro Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["intro"], lines=5)
|
174 |
text_instructions_input = gr.Textbox(label="Text Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["text_instructions"], lines=5)
|
175 |
-
scratch_pad_input = gr.Textbox(label="Scratch Pad", value=INSTRUCTION_TEMPLATES["podcast"]["scratch_pad"], lines=5)
|
176 |
-
prelude_input = gr.Textbox(label="Prelude
|
177 |
dialog_input = gr.Textbox(label="Podcast Dialogue Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["dialog"], lines=5)
|
178 |
|
179 |
-
# Generate button
|
180 |
generate_btn = gr.Button("Generate Podcast Dialogue")
|
181 |
|
182 |
-
# Output
|
183 |
output_text = gr.Textbox(label="Generated Podcast Dialogue", lines=10)
|
184 |
|
185 |
-
# Generate button action
|
186 |
generate_btn.click(
|
187 |
-
fn=
|
188 |
inputs=[pdf_input, intro_input, text_instructions_input, scratch_pad_input, prelude_input, dialog_input],
|
189 |
-
outputs=
|
190 |
)
|
191 |
|
192 |
return demo
|
193 |
|
194 |
-
# Launch the Gradio interface
|
195 |
if __name__ == "__main__":
|
196 |
demo = interface()
|
197 |
demo.launch()
|
|
|
7 |
from tempfile import NamedTemporaryFile
|
8 |
from typing import List, Literal
|
9 |
import re
|
10 |
+
import gradio as gr
|
11 |
from transformers import pipeline
|
12 |
from pydantic import BaseModel
|
|
|
13 |
|
14 |
+
# Initialize Hugging Face text generation model
|
15 |
text_generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B')
|
16 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
17 |
|
18 |
+
# Instruction templates
|
19 |
INSTRUCTION_TEMPLATES = {
|
20 |
"podcast": {
|
21 |
"intro": """Your task is to take the input text provided and turn it into a lively, engaging, informative podcast dialogue, in the style of NPR...""",
|
|
|
95 |
|
96 |
# Main audio generation function (adapted for Hugging Face text generation)
|
97 |
def generate_audio(
|
98 |
+
file: str,
|
99 |
text_model: str = "EleutherAI/gpt-neo-2.7B",
|
100 |
audio_model: str = "tts-1",
|
101 |
speaker_1_voice: str = "alloy",
|
|
|
111 |
debug = False,
|
112 |
) -> tuple:
|
113 |
|
114 |
+
# Combine input text from the single file
|
115 |
combined_text = original_text or ""
|
116 |
if not combined_text:
|
117 |
+
with Path(file).open("rb") as f:
|
118 |
+
text = f.read().decode('utf-8') # Assuming the PDF text is extracted as UTF-8
|
119 |
+
combined_text += text + "\n\n"
|
|
|
120 |
|
121 |
# Generate the dialogue using Hugging Face
|
122 |
llm_output = generate_dialogue(
|
|
|
157 |
|
158 |
return temporary_file.name, transcript, combined_text
|
159 |
|
160 |
+
# Gradio Interface (for single file upload)
|
161 |
+
def generate_podcast_interface(file, intro_instructions, text_instructions, scratch_pad_instructions, prelude_dialog, podcast_dialog_instructions):
|
162 |
+
# Handle a single PDF file
|
163 |
+
combined_text = ""
|
164 |
+
file_path = file.name
|
165 |
+
with open(file_path, "rb") as f:
|
166 |
+
reader = PyPDF2.PdfReader(f)
|
167 |
+
for page in reader.pages:
|
168 |
+
combined_text += page.extract_text() + "\n\n"
|
169 |
+
|
170 |
+
# Generate podcast dialogue
|
171 |
+
podcast_dialogue = generate_dialogue(combined_text, intro_instructions, text_instructions, scratch_pad_instructions, prelude_dialog, podcast_dialog_instructions)
|
172 |
+
|
173 |
+
return podcast_dialogue
|
174 |
+
|
175 |
def interface():
|
176 |
with gr.Blocks() as demo:
|
177 |
+
gr.Markdown("# Podcast Generator from PDF File")
|
178 |
+
gr.Markdown("Upload a PDF file, input instructions, and generate podcast dialogues using Hugging Face models.")
|
179 |
|
180 |
with gr.Row():
|
181 |
+
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
|
|
182 |
|
183 |
with gr.Row():
|
|
|
184 |
intro_input = gr.Textbox(label="Intro Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["intro"], lines=5)
|
185 |
text_instructions_input = gr.Textbox(label="Text Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["text_instructions"], lines=5)
|
186 |
+
scratch_pad_input = gr.Textbox(label="Scratch Pad Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["scratch_pad"], lines=5)
|
187 |
+
prelude_input = gr.Textbox(label="Prelude", value=INSTRUCTION_TEMPLATES["podcast"]["prelude"], lines=5)
|
188 |
dialog_input = gr.Textbox(label="Podcast Dialogue Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["dialog"], lines=5)
|
189 |
|
|
|
190 |
generate_btn = gr.Button("Generate Podcast Dialogue")
|
191 |
|
|
|
192 |
output_text = gr.Textbox(label="Generated Podcast Dialogue", lines=10)
|
193 |
|
|
|
194 |
generate_btn.click(
|
195 |
+
fn=generate_podcast_interface,
|
196 |
inputs=[pdf_input, intro_input, text_instructions_input, scratch_pad_input, prelude_input, dialog_input],
|
197 |
+
outputs=output_text
|
198 |
)
|
199 |
|
200 |
return demo
|
201 |
|
|
|
202 |
if __name__ == "__main__":
|
203 |
demo = interface()
|
204 |
demo.launch()
|