arad1367 commited on
Commit
f690e18
β€’
1 Parent(s): 104113f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -74
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 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()
 
 
 
 
 
 
 
 
 
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()