yunuseduran commited on
Commit
4c6fdbe
1 Parent(s): 7497633

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -18
app.py CHANGED
@@ -1,32 +1,55 @@
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 Seslendirme",
31
- description="PDF dosyasını yükleyin ve onu MP3 formatındaki sese dönüştürün.")
32
- iface.launch()
 
 
 
 
 
 
 
 
 
1
  import os
2
+ from PyPDF2 import PdfReader, PdfReadError
3
  from gtts import gTTS
4
+ from pydub import AudioSegment
5
  import gradio as gr
6
 
7
  def pdf_to_text(pdf_file):
8
+ try:
9
+ reader = PdfReader(pdf_file)
10
+ text = ""
11
+ for page in reader.pages:
12
+ text += page.extract_text()
13
+ return text
14
+ except PdfReadError:
15
+ return None
16
 
17
+ def text_to_speech(text, lang='en', file_format='mp3'):
18
  tts = gTTS(text=text, lang=lang)
19
+ output_path = f"output.{file_format}"
20
+ tts.save("temp.mp3")
21
+
22
+ if file_format == 'mp3':
23
+ os.rename("temp.mp3", output_path)
24
+ elif file_format == 'wav':
25
+ sound = AudioSegment.from_mp3("temp.mp3")
26
+ sound.export(output_path, format="wav")
27
+ os.remove("temp.mp3")
28
+
29
  return output_path
30
 
31
+ def convert_pdf_to_speech(pdf_file, lang='en', file_format='mp3'):
32
  text = pdf_to_text(pdf_file)
33
+ if text:
34
+ audio_file = text_to_speech(text, lang=lang, file_format=file_format)
35
+ return audio_file
36
+ else:
37
+ return "Error: Could not read the PDF file. Please upload a valid PDF."
38
 
39
  # Gradio arayüzü
40
+ def interface(pdf_file, lang, file_format):
41
+ audio_file = convert_pdf_to_speech(pdf_file.name, lang=lang, file_format=file_format)
42
  return audio_file
43
 
44
+ iface = gr.Interface(
45
+ fn=interface,
46
+ inputs=[
47
+ gr.inputs.File(label="Upload PDF"),
48
+ gr.inputs.Dropdown(choices=["en", "es", "fr", "de", "it", "tr", "zh", "jp"], label="Select Language"),
49
+ gr.inputs.Radio(choices=["mp3", "wav"], label="Select Output Format")
50
+ ],
51
+ outputs="file",
52
+ title="PDF to Speech Converter",
53
+ description="Upload a PDF file, select the language, and choose the output format (MP3 or WAV)."
54
+ )
55
+ iface.launch(share=True)