File size: 1,895 Bytes
f690e18 d38c319 f690e18 d38c319 f690e18 d38c319 f690e18 d38c319 f690e18 d38c319 f690e18 d38c319 f690e18 d38c319 f690e18 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import gradio as gr
from gtts import gTTS
from pdfplumber import open as pp_open
import os
def convert_pdf_to_speech(pdf, language):
"""
This function takes in a PDF file and converts it to speech.
Parameters:
pdf (str): The path to the PDF file.
language (str): The language of the text.
Returns:
A message stating that the PDF has been converted to speech and the path to the MP3 file.
"""
# Extract text from the PDF
pdf_content = ""
with pp_open(pdf) as pdf_file:
for page in pdf_file.pages:
pdf_content += page.extract_text()
# Define the output directory and ensure it exists
output_dir = "output"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Convert PDF to speech
tts = gTTS(text=pdf_content, lang=language)
filename = os.path.basename(pdf)
filename = f"{filename.split('.')[0]}.mp3"
# Save the file in the 'output' folder
output_path = os.path.join(output_dir, filename)
tts.save(output_path)
return output_path
demo = gr.Blocks(theme='gradio/soft')
with demo:
# App description
with gr.Column():
gr.Markdown("<b>PDF Text-to-Speech Converter</b>")
gr.Markdown("Convert your PDF files to audio books")
# Input for the PDF
pdf_input = gr.File(label="Select a PDF", type="filepath")
# Language selector
language_selector = gr.Dropdown(
label="Language",
value="en",
choices=["en", "es", "de", "it", "fr"],
interactive=True,
)
# Button to start the conversion process
button = gr.Button("Convert PDF to Speech")
# Output message
output = gr.File(label="Download MP3")
# Button click handler
button.click(convert_pdf_to_speech, inputs=[pdf_input, language_selector], outputs=output)
demo.launch()
|