siddhartharya's picture
Update app.py
5f5ebb2 verified
raw
history blame
2.3 kB
import gradio as gr
from utils import generate_script, generate_audio, truncate_text, extract_text_from_url
from prompts import SYSTEM_PROMPT
from pydub import AudioSegment
import pypdf
import os
import tempfile
def generate_podcast(file, url, tone, length):
if file and url:
raise gr.Error("Please provide either a PDF file or a URL, not both.")
if file:
if not file.name.lower().endswith('.pdf'):
raise gr.Error("Please upload a PDF file.")
try:
pdf_reader = pypdf.PdfReader(file.name)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
except Exception as e:
raise gr.Error(f"Error reading the PDF file: {str(e)}")
elif url:
try:
text = extract_text_from_url(url)
except Exception as e:
raise gr.Error(f"Error extracting text from URL: {str(e)}")
else:
raise gr.Error("Please provide either a PDF file or a URL.")
truncated_text = truncate_text(text)
if len(truncated_text) < len(text):
print("Warning: The input text was truncated to fit within 2048 tokens.")
try:
script = generate_script(SYSTEM_PROMPT, truncated_text, tone, length)
except Exception as e:
raise gr.Error(f"Error generating script: {str(e)}")
audio_segments = []
transcript = ""
try:
for item in script.dialogue:
audio_file = generate_audio(item.text, item.speaker)
audio_segment = AudioSegment.from_wav(audio_file)
audio_segments.append(audio_segment)
transcript += f"**{item.speaker}**: {item.text}\n\n"
os.remove(audio_file) # Clean up temporary audio file
# Yield progress updates
yield None, f"Generating audio for {item.speaker}..."
except Exception as e:
raise gr.Error(f"Error generating audio: {str(e)}")
combined_audio = sum(audio_segments)
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
combined_audio.export(temp_audio.name, format="mp3")
temp_audio_path = temp_audio.name
return temp_audio_path, transcript
# The rest of the app.py file remains the same...