Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from extract_text_from_pdf import PDFTextExtractor
|
| 5 |
+
from generate_transcript import TranscriptProcessor
|
| 6 |
+
from generate_audio import TTSGenerator
|
| 7 |
+
import pickle
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Define paths
|
| 11 |
+
pdf_path = './resources/uploaded_pdf.pdf'
|
| 12 |
+
clean_text_path = './resources/clean_text.txt'
|
| 13 |
+
transcript_path = './resources/data.pkl'
|
| 14 |
+
tts_ready_path = './resources/podcast_ready_data.pkl'
|
| 15 |
+
audio_output_path = 'final_podcast_audio.mp3'
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Combined function to perform all steps sequentially
|
| 19 |
+
def process_pdf_to_podcast(pdf_file):
|
| 20 |
+
# Step 1: Extract Text from PDF
|
| 21 |
+
with open(pdf_path, 'wb') as f:
|
| 22 |
+
f.write(pdf_file.read())
|
| 23 |
+
|
| 24 |
+
extractor = PDFTextExtractor(pdf_path)
|
| 25 |
+
clean_text_path = extractor.clean_and_save_text()
|
| 26 |
+
|
| 27 |
+
# Display a preview of extracted text
|
| 28 |
+
with open(clean_text_path, 'r', encoding='utf-8') as file:
|
| 29 |
+
text_preview = file.read(500)
|
| 30 |
+
|
| 31 |
+
# Step 2: Generate Transcript
|
| 32 |
+
processor = TranscriptProcessor(clean_text_path)
|
| 33 |
+
transcript_path = processor.generate_transcript()
|
| 34 |
+
|
| 35 |
+
# Load the generated transcript for preview
|
| 36 |
+
with open(transcript_path, 'rb') as f:
|
| 37 |
+
transcript_preview = pickle.load(f)
|
| 38 |
+
|
| 39 |
+
# Step 3: Rewrite Transcript for TTS
|
| 40 |
+
tts_ready_path = processor.rewrite_transcript()
|
| 41 |
+
|
| 42 |
+
# Load the rewritten transcript for preview and editing
|
| 43 |
+
with open(tts_ready_path, 'rb') as f:
|
| 44 |
+
tts_ready_preview = pickle.load(f)
|
| 45 |
+
|
| 46 |
+
return (
|
| 47 |
+
f"Steps 1-3 completed. Preview and adjust the rewritten transcript if needed.",
|
| 48 |
+
text_preview,
|
| 49 |
+
transcript_preview,
|
| 50 |
+
tts_ready_preview
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Final Step: Generate Audio after optional adjustments
|
| 55 |
+
def generate_audio_from_modified_text(tts_ready_text):
|
| 56 |
+
# Save any modified TTS-ready transcript
|
| 57 |
+
with open(tts_ready_path, 'wb') as f:
|
| 58 |
+
pickle.dump(tts_ready_text, f)
|
| 59 |
+
|
| 60 |
+
# Generate audio from the TTS-ready transcript
|
| 61 |
+
tts_gen = TTSGenerator(tts_ready_path)
|
| 62 |
+
audio_path = tts_gen.generate_audio()
|
| 63 |
+
|
| 64 |
+
return f"Step 4 complete. Audio saved to {audio_path}.", audio_path
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# Gradio Interface
|
| 68 |
+
with gr.Blocks() as app:
|
| 69 |
+
gr.Markdown("# PDF to Podcast Conversion Application")
|
| 70 |
+
|
| 71 |
+
# Single-click initiation of Steps 1-3
|
| 72 |
+
with gr.Row():
|
| 73 |
+
pdf_input = gr.File(label="Upload PDF")
|
| 74 |
+
run_all_button = gr.Button("Run All Steps (1-3)")
|
| 75 |
+
output_status = gr.Textbox(label="Status")
|
| 76 |
+
|
| 77 |
+
# Step 1 Preview of Extracted Text
|
| 78 |
+
extracted_text_preview = gr.Textbox(label="Extracted Text Preview (First 500 Characters)", interactive=False)
|
| 79 |
+
|
| 80 |
+
# Step 2 Preview of Generated Transcript
|
| 81 |
+
transcript_preview = gr.Textbox(label="Generated Transcript Preview", interactive=False)
|
| 82 |
+
|
| 83 |
+
# Step 3 Editable Rewritten Transcript for TTS
|
| 84 |
+
tts_ready_preview = gr.Textbox(label="Editable Rewritten Transcript for TTS", interactive=True)
|
| 85 |
+
|
| 86 |
+
# Button for generating audio with editable transcript
|
| 87 |
+
generate_audio_button = gr.Button("Generate Audio from Edited Transcript")
|
| 88 |
+
final_audio_output = gr.Audio(label="Generated Podcast Audio")
|
| 89 |
+
|
| 90 |
+
# Step 1-3 execution
|
| 91 |
+
run_all_button.click(
|
| 92 |
+
process_pdf_to_podcast,
|
| 93 |
+
inputs=pdf_input,
|
| 94 |
+
outputs=[output_status, extracted_text_preview, transcript_preview, tts_ready_preview]
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Final step: Generate Audio from modified TTS-ready transcript
|
| 98 |
+
generate_audio_button.click(
|
| 99 |
+
generate_audio_from_modified_text,
|
| 100 |
+
inputs=tts_ready_preview,
|
| 101 |
+
outputs=[output_status, final_audio_output]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
app.launch()
|