yunuseduran commited on
Commit
f5e0882
1 Parent(s): 5717ba8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -36
app.py CHANGED
@@ -1,36 +1,32 @@
1
- import os
2
- from PyPDF2 import PdfReader
3
- from gtts import gTTS
4
- from pydub import AudioSegment
5
- from pydub.playback import play
6
- import gradio as gr
7
-
8
- def pdf_to_text(pdf_file):
9
- reader = PdfReader(pdf_file)
10
- text = ""
11
- for page in reader.pages:
12
- text += page.extract_text()
13
- return text
14
-
15
- def text_to_speech(text, lang='en'):
16
- tts = gTTS(text=text, lang=lang)
17
- tts.save("output.mp3")
18
- sound = AudioSegment.from_mp3("output.mp3")
19
- return sound
20
-
21
- def convert_pdf_to_speech(pdf_file):
22
- text = pdf_to_text(pdf_file)
23
- sound = text_to_speech(text)
24
- return sound
25
-
26
- def play_audio(audio_segment):
27
- play(audio_segment)
28
-
29
- # Gradio arayüzü
30
- def interface(pdf_file):
31
- audio_segment = convert_pdf_to_speech(pdf_file.name)
32
- play_audio(audio_segment)
33
- return "Seslendirme tamamlandı."
34
-
35
- iface = gr.Interface(fn=interface, inputs="file", outputs="text")
36
- iface.launch()
 
1
+ import os
2
+ from PyPDF2 import PdfReader
3
+ from gtts import gTTS
4
+ import gradio as gr
5
+
6
+ def pdf_to_text(pdf_file):
7
+ reader = PdfReader(pdf_file)
8
+ text = ""
9
+ for page in reader.pages:
10
+ text += page.extract_text()
11
+ return text
12
+
13
+ def text_to_speech(text, lang='en'):
14
+ tts = gTTS(text=text, lang=lang)
15
+ output_path = "output.mp3"
16
+ tts.save(output_path)
17
+ return output_path
18
+
19
+ def convert_pdf_to_speech(pdf_file):
20
+ text = pdf_to_text(pdf_file)
21
+ audio_file = text_to_speech(text)
22
+ return audio_file
23
+
24
+ # Gradio arayüzü
25
+ def interface(pdf_file):
26
+ audio_file = convert_pdf_to_speech(pdf_file.name)
27
+ return audio_file
28
+
29
+ iface = gr.Interface(fn=interface, inputs="file", outputs="file",
30
+ title="PDF to Speech Converter",
31
+ description="Upload a PDF file and convert it to MP3 format audio.")
32
+ iface.launch()