Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,22 +12,22 @@ def convert_pdf_to_speech(pdf, language):
|
|
| 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
|
| 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 |
output_dir = "output"
|
| 27 |
if not os.path.exists(output_dir):
|
| 28 |
os.makedirs(output_dir)
|
| 29 |
|
| 30 |
-
# Convert
|
| 31 |
tts = gTTS(text=pdf_content, lang=language)
|
| 32 |
filename = os.path.basename(pdf)
|
| 33 |
filename = f"{filename.split('.')[0]}.mp3"
|
|
@@ -36,7 +36,7 @@ def convert_pdf_to_speech(pdf, language):
|
|
| 36 |
output_path = os.path.join(output_dir, filename)
|
| 37 |
tts.save(output_path)
|
| 38 |
|
| 39 |
-
return
|
| 40 |
|
| 41 |
demo = gr.Blocks(theme='gradio/soft')
|
| 42 |
|
|
@@ -61,22 +61,9 @@ with demo:
|
|
| 61 |
button = gr.Button("Convert PDF to Speech")
|
| 62 |
|
| 63 |
# Output message
|
| 64 |
-
output = gr.
|
| 65 |
|
| 66 |
-
|
| 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()
|
|
|
|
| 12 |
language (str): The language of the text.
|
| 13 |
|
| 14 |
Returns:
|
| 15 |
+
A message stating that the PDF has been converted to speech and the path to the MP3 file.
|
| 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 |
+
# Define the output directory and ensure it exists
|
| 26 |
output_dir = "output"
|
| 27 |
if not os.path.exists(output_dir):
|
| 28 |
os.makedirs(output_dir)
|
| 29 |
|
| 30 |
+
# Convert PDF to speech
|
| 31 |
tts = gTTS(text=pdf_content, lang=language)
|
| 32 |
filename = os.path.basename(pdf)
|
| 33 |
filename = f"{filename.split('.')[0]}.mp3"
|
|
|
|
| 36 |
output_path = os.path.join(output_dir, filename)
|
| 37 |
tts.save(output_path)
|
| 38 |
|
| 39 |
+
return output_path
|
| 40 |
|
| 41 |
demo = gr.Blocks(theme='gradio/soft')
|
| 42 |
|
|
|
|
| 61 |
button = gr.Button("Convert PDF to Speech")
|
| 62 |
|
| 63 |
# Output message
|
| 64 |
+
output = gr.File(label="Download MP3")
|
| 65 |
|
| 66 |
+
# Button click handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
button.click(convert_pdf_to_speech, inputs=[pdf_input, language_selector], outputs=output)
|
| 68 |
|
| 69 |
demo.launch()
|