Upload 2 files
Browse files- app.py +74 -0
- requirements.txt +66 -0
app.py
ADDED
@@ -0,0 +1,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 our 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 |
+
# Convert pdf to speech and make AudioBook!
|
26 |
+
tts = gTTS(text=pdf_content, lang=language)
|
27 |
+
filename = os.path.basename(pdf)
|
28 |
+
filename = f"{filename.split('.')[0]}.mp3"
|
29 |
+
tts.save(filename)
|
30 |
+
|
31 |
+
return f"Your PDF has been converted to speech. The MP3 file is saved as {os.path.abspath(filename)}"
|
32 |
+
|
33 |
+
demo = gr.Blocks(theme='gradio/soft')
|
34 |
+
|
35 |
+
with demo:
|
36 |
+
# App description
|
37 |
+
with gr.Column():
|
38 |
+
gr.Markdown("<b>PDF Text-to-Speech Converter</b>")
|
39 |
+
gr.Markdown("Convert your PDF files to audio books")
|
40 |
+
|
41 |
+
# Input for the PDF
|
42 |
+
pdf_input = gr.File(label="Select a PDF", type="filepath")
|
43 |
+
|
44 |
+
# Language selector
|
45 |
+
language_selector = gr.Dropdown(
|
46 |
+
label="Language",
|
47 |
+
value="en",
|
48 |
+
choices=["en", "es", "de", "it", "fr"],
|
49 |
+
interactive=True,
|
50 |
+
)
|
51 |
+
|
52 |
+
# Button to start the conversion process
|
53 |
+
button = gr.Button("Convert PDF to Speech")
|
54 |
+
|
55 |
+
# Output message
|
56 |
+
output = gr.Textbox(label="Output")
|
57 |
+
|
58 |
+
with gr.Column():
|
59 |
+
# Footer with links to LinkedIn, GitHub and Live demo of PhD defense
|
60 |
+
footer_html = """
|
61 |
+
<div style="text-align: center; margin-top: 20px;">
|
62 |
+
<a href="https://www.linkedin.com/in/pejman-ebrahimi-4a60151a7/" target="_blank">LinkedIn</a> |
|
63 |
+
<a href="https://github.com/arad1367" target="_blank">GitHub</a> |
|
64 |
+
<a href="https://arad1367.pythonanywhere.com/" target="_blank">Live demo of my PhD defense</a>
|
65 |
+
<br>
|
66 |
+
Made with π by Pejman Ebrahimi
|
67 |
+
</div>
|
68 |
+
"""
|
69 |
+
gr.HTML(footer_html)
|
70 |
+
|
71 |
+
# Layout the components
|
72 |
+
button.click(convert_pdf_to_speech, inputs=[pdf_input, language_selector], outputs=output)
|
73 |
+
|
74 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
annotated-types==0.7.0
|
3 |
+
anyio==4.4.0
|
4 |
+
certifi==2024.8.30
|
5 |
+
cffi==1.17.1
|
6 |
+
charset-normalizer==3.3.2
|
7 |
+
click==8.1.7
|
8 |
+
colorama==0.4.6
|
9 |
+
contourpy==1.3.0
|
10 |
+
cryptography==43.0.1
|
11 |
+
cycler==0.12.1
|
12 |
+
exceptiongroup==1.2.2
|
13 |
+
fastapi==0.114.2
|
14 |
+
ffmpy==0.4.0
|
15 |
+
filelock==3.16.0
|
16 |
+
fonttools==4.53.1
|
17 |
+
fsspec==2024.9.0
|
18 |
+
gradio==4.44.0
|
19 |
+
gradio_client==1.3.0
|
20 |
+
gTTS==2.5.3
|
21 |
+
h11==0.14.0
|
22 |
+
httpcore==1.0.5
|
23 |
+
httpx==0.27.2
|
24 |
+
huggingface-hub==0.24.7
|
25 |
+
idna==3.10
|
26 |
+
importlib_resources==6.4.5
|
27 |
+
Jinja2==3.1.4
|
28 |
+
kiwisolver==1.4.7
|
29 |
+
markdown-it-py==3.0.0
|
30 |
+
MarkupSafe==2.1.5
|
31 |
+
matplotlib==3.9.2
|
32 |
+
mdurl==0.1.2
|
33 |
+
numpy==2.1.1
|
34 |
+
orjson==3.10.7
|
35 |
+
packaging==24.1
|
36 |
+
pandas==2.2.2
|
37 |
+
pdfminer.six==20231228
|
38 |
+
pdfplumber==0.11.4
|
39 |
+
pillow==10.4.0
|
40 |
+
pycparser==2.22
|
41 |
+
pydantic==2.9.1
|
42 |
+
pydantic_core==2.23.3
|
43 |
+
pydub==0.25.1
|
44 |
+
Pygments==2.18.0
|
45 |
+
pyparsing==3.1.4
|
46 |
+
pypdfium2==4.30.0
|
47 |
+
python-dateutil==2.9.0.post0
|
48 |
+
python-multipart==0.0.9
|
49 |
+
pytz==2024.2
|
50 |
+
PyYAML==6.0.2
|
51 |
+
requests==2.32.3
|
52 |
+
rich==13.8.1
|
53 |
+
ruff==0.6.5
|
54 |
+
semantic-version==2.10.0
|
55 |
+
shellingham==1.5.4
|
56 |
+
six==1.16.0
|
57 |
+
sniffio==1.3.1
|
58 |
+
starlette==0.38.5
|
59 |
+
tomlkit==0.12.0
|
60 |
+
tqdm==4.66.5
|
61 |
+
typer==0.12.5
|
62 |
+
typing_extensions==4.12.2
|
63 |
+
tzdata==2024.1
|
64 |
+
urllib3==2.2.3
|
65 |
+
uvicorn==0.30.6
|
66 |
+
websockets==12.0
|