Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,82 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from gtts import gTTS
|
3 |
-
from pdfplumber import open as pp_open
|
4 |
-
import os
|
5 |
-
|
6 |
-
def convert_pdf_to_speech(pdf, language):
|
7 |
-
"""
|
8 |
-
This function takes in a PDF file and converts it to speech.
|
9 |
-
|
10 |
-
Parameters:
|
11 |
-
pdf (str): The path to the PDF file.
|
12 |
-
language (str): The language of the text.
|
13 |
-
|
14 |
-
Returns:
|
15 |
-
A message stating that the PDF has been converted to speech.
|
16 |
-
"""
|
17 |
-
|
18 |
-
# Extract text from
|
19 |
-
pdf_content = ""
|
20 |
-
|
21 |
-
with pp_open(pdf) as pdf_file:
|
22 |
-
for page in pdf_file.pages:
|
23 |
-
pdf_content += page.extract_text()
|
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 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gtts import gTTS
|
3 |
+
from pdfplumber import open as pp_open
|
4 |
+
import os
|
5 |
+
|
6 |
+
def convert_pdf_to_speech(pdf, language):
|
7 |
+
"""
|
8 |
+
This function takes in a PDF file and converts it to speech.
|
9 |
+
|
10 |
+
Parameters:
|
11 |
+
pdf (str): The path to the PDF file.
|
12 |
+
language (str): The language of the text.
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
A message stating that the PDF has been converted to speech.
|
16 |
+
"""
|
17 |
+
|
18 |
+
# Extract text from the pdf
|
19 |
+
pdf_content = ""
|
20 |
+
|
21 |
+
with pp_open(pdf) as pdf_file:
|
22 |
+
for page in pdf_file.pages:
|
23 |
+
pdf_content += page.extract_text()
|
24 |
+
|
25 |
+
# Create output directory if it doesn't exist
|
26 |
+
output_dir = "output"
|
27 |
+
if not os.path.exists(output_dir):
|
28 |
+
os.makedirs(output_dir)
|
29 |
+
|
30 |
+
# Convert pdf to speech and make AudioBook!
|
31 |
+
tts = gTTS(text=pdf_content, lang=language)
|
32 |
+
filename = os.path.basename(pdf)
|
33 |
+
filename = f"{filename.split('.')[0]}.mp3"
|
34 |
+
|
35 |
+
# Save the file in the 'output' folder
|
36 |
+
output_path = os.path.join(output_dir, filename)
|
37 |
+
tts.save(output_path)
|
38 |
+
|
39 |
+
return f"Your PDF has been converted to speech. The MP3 file is saved as {os.path.abspath(output_path)}"
|
40 |
+
|
41 |
+
demo = gr.Blocks(theme='gradio/soft')
|
42 |
+
|
43 |
+
with demo:
|
44 |
+
# App description
|
45 |
+
with gr.Column():
|
46 |
+
gr.Markdown("<b>PDF Text-to-Speech Converter</b>")
|
47 |
+
gr.Markdown("Convert your PDF files to audio books")
|
48 |
+
|
49 |
+
# Input for the PDF
|
50 |
+
pdf_input = gr.File(label="Select a PDF", type="filepath")
|
51 |
+
|
52 |
+
# Language selector
|
53 |
+
language_selector = gr.Dropdown(
|
54 |
+
label="Language",
|
55 |
+
value="en",
|
56 |
+
choices=["en", "es", "de", "it", "fr"],
|
57 |
+
interactive=True,
|
58 |
+
)
|
59 |
+
|
60 |
+
# Button to start the conversion process
|
61 |
+
button = gr.Button("Convert PDF to Speech")
|
62 |
+
|
63 |
+
# Output message
|
64 |
+
output = gr.Textbox(label="Output")
|
65 |
+
|
66 |
+
with gr.Column():
|
67 |
+
# Footer with links to LinkedIn, GitHub and Live demo of PhD defense
|
68 |
+
footer_html = """
|
69 |
+
<div style="text-align: center; margin-top: 20px;">
|
70 |
+
<a href="https://www.linkedin.com/in/pejman-ebrahimi-4a60151a7/" target="_blank">LinkedIn</a> |
|
71 |
+
<a href="https://github.com/arad1367" target="_blank">GitHub</a> |
|
72 |
+
<a href="https://arad1367.pythonanywhere.com/" target="_blank">Live demo of my PhD defense</a>
|
73 |
+
<br>
|
74 |
+
Made with π by Pejman Ebrahimi
|
75 |
+
</div>
|
76 |
+
"""
|
77 |
+
gr.HTML(footer_html)
|
78 |
+
|
79 |
+
# Layout the components
|
80 |
+
button.click(convert_pdf_to_speech, inputs=[pdf_input, language_selector], outputs=output)
|
81 |
+
|
82 |
+
demo.launch()
|