Delete app.py
Browse files
app.py
DELETED
@@ -1,205 +0,0 @@
|
|
1 |
-
import concurrent.futures as cf
|
2 |
-
import glob
|
3 |
-
import io
|
4 |
-
import os
|
5 |
-
import time
|
6 |
-
from pathlib import Path
|
7 |
-
from tempfile import NamedTemporaryFile
|
8 |
-
from typing import List, Literal
|
9 |
-
import re
|
10 |
-
import gradio as gr
|
11 |
-
import PyPDF2 # <-- Make sure this is included
|
12 |
-
from transformers import pipeline
|
13 |
-
from pydantic import BaseModel
|
14 |
-
|
15 |
-
# Initialize Hugging Face text generation model
|
16 |
-
text_generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B')
|
17 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
18 |
-
|
19 |
-
# Instruction templates
|
20 |
-
INSTRUCTION_TEMPLATES = {
|
21 |
-
"podcast": {
|
22 |
-
"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...""",
|
23 |
-
"text_instructions": "First, carefully read through the input text...",
|
24 |
-
"scratch_pad": """Brainstorm creative ways to discuss the main topics...""",
|
25 |
-
"prelude": """Now that you have brainstormed ideas and created a rough outline...""",
|
26 |
-
"dialog": """Write a very long, engaging, informative podcast dialogue..."""
|
27 |
-
}
|
28 |
-
}
|
29 |
-
|
30 |
-
# Function to update instruction fields based on template selection
|
31 |
-
def update_instructions(template):
|
32 |
-
return (
|
33 |
-
INSTRUCTION_TEMPLATES[template]["intro"],
|
34 |
-
INSTRUCTION_TEMPLATES[template]["text_instructions"],
|
35 |
-
INSTRUCTION_TEMPLATES[template]["scratch_pad"],
|
36 |
-
INSTRUCTION_TEMPLATES[template]["prelude"],
|
37 |
-
INSTRUCTION_TEMPLATES[template]["dialog"]
|
38 |
-
)
|
39 |
-
|
40 |
-
# Define the structure of dialogue
|
41 |
-
class DialogueItem(BaseModel):
|
42 |
-
text: str
|
43 |
-
speaker: Literal["speaker-1", "speaker-2"]
|
44 |
-
|
45 |
-
class Dialogue(BaseModel):
|
46 |
-
scratchpad: str
|
47 |
-
dialogue: List[DialogueItem]
|
48 |
-
|
49 |
-
# Function to read README.md
|
50 |
-
def read_readme():
|
51 |
-
readme_path = Path("README.md")
|
52 |
-
if readme_path.exists():
|
53 |
-
with open(readme_path, "r") as file:
|
54 |
-
content = file.read()
|
55 |
-
content = re.sub(r'--.*?--', '', content, flags=re.DOTALL)
|
56 |
-
return content
|
57 |
-
else:
|
58 |
-
return "README.md not found. Please check the repository for more information."
|
59 |
-
|
60 |
-
# Hugging Face-based dialogue generation function
|
61 |
-
def generate_dialogue(text: str, intro_instructions: str, text_instructions: str,
|
62 |
-
scratch_pad_instructions: str, prelude_dialog: str,
|
63 |
-
podcast_dialog_instructions: str, edited_transcript: str = None,
|
64 |
-
user_feedback: str = None) -> str:
|
65 |
-
# Combine instructions and text into a prompt
|
66 |
-
full_prompt = f"""
|
67 |
-
{intro_instructions}
|
68 |
-
|
69 |
-
Original text:
|
70 |
-
{text}
|
71 |
-
|
72 |
-
{text_instructions}
|
73 |
-
|
74 |
-
Brainstorming:
|
75 |
-
{scratch_pad_instructions}
|
76 |
-
|
77 |
-
Prelude:
|
78 |
-
{prelude_dialog}
|
79 |
-
|
80 |
-
Dialogue:
|
81 |
-
{podcast_dialog_instructions}
|
82 |
-
|
83 |
-
{edited_transcript if edited_transcript else ""}
|
84 |
-
{user_feedback if user_feedback else ""}
|
85 |
-
"""
|
86 |
-
|
87 |
-
# Generate text using Hugging Face model
|
88 |
-
generated = text_generator(full_prompt, max_length=1000) # Adjust max_length as needed
|
89 |
-
return generated[0]['generated_text'] # Extract generated text from the response
|
90 |
-
|
91 |
-
# Function to handle audio generation (could be expanded later)
|
92 |
-
def get_mp3(text: str, voice: str, audio_model: str) -> bytes:
|
93 |
-
# Placeholder for audio generation; currently not implemented
|
94 |
-
# You can use text-to-speech services or local TTS engines
|
95 |
-
return b""
|
96 |
-
|
97 |
-
# Main audio generation function (adapted for Hugging Face text generation)
|
98 |
-
def generate_audio(
|
99 |
-
file: str,
|
100 |
-
text_model: str = "EleutherAI/gpt-neo-2.7B",
|
101 |
-
audio_model: str = "tts-1",
|
102 |
-
speaker_1_voice: str = "alloy",
|
103 |
-
speaker_2_voice: str = "echo",
|
104 |
-
intro_instructions: str = '',
|
105 |
-
text_instructions: str = '',
|
106 |
-
scratch_pad_instructions: str = '',
|
107 |
-
prelude_dialog: str = '',
|
108 |
-
podcast_dialog_instructions: str = '',
|
109 |
-
edited_transcript: str = None,
|
110 |
-
user_feedback: str = None,
|
111 |
-
original_text: str = None,
|
112 |
-
debug = False,
|
113 |
-
) -> tuple:
|
114 |
-
|
115 |
-
# Combine input text from the single file
|
116 |
-
combined_text = original_text or ""
|
117 |
-
if not combined_text:
|
118 |
-
with Path(file).open("rb") as f:
|
119 |
-
text = f.read().decode('utf-8') # Assuming the PDF text is extracted as UTF-8
|
120 |
-
combined_text += text + "\n\n"
|
121 |
-
|
122 |
-
# Generate the dialogue using Hugging Face
|
123 |
-
llm_output = generate_dialogue(
|
124 |
-
combined_text,
|
125 |
-
intro_instructions=intro_instructions,
|
126 |
-
text_instructions=text_instructions,
|
127 |
-
scratch_pad_instructions=scratch_pad_instructions,
|
128 |
-
prelude_dialog=prelude_dialog,
|
129 |
-
podcast_dialog_instructions=podcast_dialog_instructions,
|
130 |
-
edited_transcript=edited_transcript,
|
131 |
-
user_feedback=user_feedback
|
132 |
-
)
|
133 |
-
|
134 |
-
# Placeholder for audio (since TTS implementation is omitted)
|
135 |
-
audio = b""
|
136 |
-
transcript = llm_output
|
137 |
-
characters = len(llm_output)
|
138 |
-
|
139 |
-
# Generating audio (placeholder logic)
|
140 |
-
with cf.ThreadPoolExecutor() as executor:
|
141 |
-
futures = []
|
142 |
-
for line in llm_output.split('\n'):
|
143 |
-
future = executor.submit(get_mp3, line, speaker_1_voice, audio_model)
|
144 |
-
futures.append(future)
|
145 |
-
characters += len(line)
|
146 |
-
|
147 |
-
for future in futures:
|
148 |
-
audio_chunk = future.result()
|
149 |
-
audio += audio_chunk
|
150 |
-
|
151 |
-
temporary_directory = "./tmp/"
|
152 |
-
os.makedirs(temporary_directory, exist_ok=True)
|
153 |
-
|
154 |
-
# Save audio to a temporary file
|
155 |
-
temporary_file = NamedTemporaryFile(dir=temporary_directory, delete=False, suffix=".mp3")
|
156 |
-
temporary_file.write(audio)
|
157 |
-
temporary_file.close()
|
158 |
-
|
159 |
-
return temporary_file.name, transcript, combined_text
|
160 |
-
|
161 |
-
# Gradio Interface (for single file upload)
|
162 |
-
def generate_podcast_interface(file, intro_instructions, text_instructions, scratch_pad_instructions, prelude_dialog, podcast_dialog_instructions):
|
163 |
-
# Handle a single PDF file
|
164 |
-
combined_text = ""
|
165 |
-
file_path = file.name
|
166 |
-
with open(file_path, "rb") as f:
|
167 |
-
reader = PyPDF2.PdfReader(f)
|
168 |
-
for page in reader.pages:
|
169 |
-
combined_text += page.extract_text() + "\n\n"
|
170 |
-
|
171 |
-
# Generate podcast dialogue
|
172 |
-
podcast_dialogue = generate_dialogue(combined_text, intro_instructions, text_instructions, scratch_pad_instructions, prelude_dialog, podcast_dialog_instructions)
|
173 |
-
|
174 |
-
return podcast_dialogue
|
175 |
-
|
176 |
-
def interface():
|
177 |
-
with gr.Blocks() as demo:
|
178 |
-
gr.Markdown("# Podcast Generator from PDF File")
|
179 |
-
gr.Markdown("Upload a PDF file, input instructions, and generate podcast dialogues using Hugging Face models.")
|
180 |
-
|
181 |
-
with gr.Row():
|
182 |
-
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
183 |
-
|
184 |
-
with gr.Row():
|
185 |
-
intro_input = gr.Textbox(label="Intro Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["intro"], lines=5)
|
186 |
-
text_instructions_input = gr.Textbox(label="Text Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["text_instructions"], lines=5)
|
187 |
-
scratch_pad_input = gr.Textbox(label="Scratch Pad Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["scratch_pad"], lines=5)
|
188 |
-
prelude_input = gr.Textbox(label="Prelude", value=INSTRUCTION_TEMPLATES["podcast"]["prelude"], lines=5)
|
189 |
-
dialog_input = gr.Textbox(label="Podcast Dialogue Instructions", value=INSTRUCTION_TEMPLATES["podcast"]["dialog"], lines=5)
|
190 |
-
|
191 |
-
generate_btn = gr.Button("Generate Podcast Dialogue")
|
192 |
-
|
193 |
-
output_text = gr.Textbox(label="Generated Podcast Dialogue", lines=10)
|
194 |
-
|
195 |
-
generate_btn.click(
|
196 |
-
fn=generate_podcast_interface,
|
197 |
-
inputs=[pdf_input, intro_input, text_instructions_input, scratch_pad_input, prelude_input, dialog_input],
|
198 |
-
outputs=output_text
|
199 |
-
)
|
200 |
-
|
201 |
-
return demo
|
202 |
-
|
203 |
-
if __name__ == "__main__":
|
204 |
-
demo = interface()
|
205 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|