import gradio as gr from gtts import gTTS import pdfplumber import os def extract_text_from_pdf(pdf_file): """ Extract text from a PDF file using pdfplumber. Args: pdf_file: Uploaded PDF file. Returns: str: Extracted text from the PDF. """ try: with pdfplumber.open(pdf_file) as pdf: text = "" for page in pdf.pages: text += page.extract_text() or "" return text if text else "No text could be extracted from the PDF." except Exception as e: return f"Error extracting text: {str(e)}" def text_to_speech(pdf_file, lang="en"): """ Convert text from a PDF to speech using gTTS and return the audio file path. Args: pdf_file: Uploaded PDF file. lang (str): Language code (default is 'en' for English). Returns: str: Path to the generated audio file or error message. """ try: # Extract text from PDF text = extract_text_from_pdf(pdf_file) if "Error" in text: return text # Create gTTS object tts = gTTS(text=text, lang=lang, slow=False) # Save the audio file output_file = "output.mp3" tts.save(output_file) return output_file except Exception as e: return f"An error occurred: {str(e)}" # Define Gradio interface demo = gr.Interface( fn=text_to_speech, inputs=[ gr.File(label="Upload a PDF file", file_types=[".pdf"]), gr.Dropdown(choices=["en", "es", "fr"], label="Select Language", value="en") ], outputs=gr.Audio(label="Generated Speech"), title="PDF to Speech with gTTS", description="Upload a PDF file, select a language, and generate speech from the extracted text." ) # Launch the app if __name__ == "__main__": demo.launch()