Sambhavnoobcoder commited on
Commit
0dddfdb
·
1 Parent(s): 5b9d837

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -39
app.py CHANGED
@@ -1,46 +1,26 @@
1
- import logging
2
- from pdfminer.high_level import extract_text
3
- from huggingface_hub import hf_hub_download
4
- from balacoon_tts import TTS
5
  import gradio as gr
6
-
7
- # Global TTS module, initialized from a selected model
8
- tts = None
9
-
10
- def read_pdf(file):
11
- text = extract_text(file.name)
12
- return text
13
-
14
- def synthesize_audio(file):
15
- text_str = read_pdf(file)
16
- if len(text_str) > 1024:
17
- text_str = text_str[:1024]
18
- samples = tts.synthesize(text_str)
19
- return (samples, tts.get_sampling_rate())
 
 
20
 
21
  def main():
22
- logging.basicConfig(level=logging.INFO)
23
- file_input = gr.inputs.File(label="Upload PDF")
24
-
25
- model_name = "balacoon/tts" # Set the desired model name here
26
- model_path = hf_hub_download(repo_id=model_name, filename="model.pt")
27
- global tts
28
- tts = TTS(model_path)
29
-
30
- audio = gr.outputs.Audio(label="Generated Audio", type="numpy")
31
-
32
- def generate_audio(file):
33
- return synthesize_audio(file)
34
-
35
- iface = gr.Interface(
36
- fn=generate_audio,
37
- inputs=file_input,
38
- outputs=audio,
39
- title="PDF TO SPEECH CONVERTER",
40
- layout="vertical",
41
- debug=True
42
- )
43
 
 
44
  iface.launch()
45
 
46
  if __name__ == "__main__":
 
 
 
 
 
1
  import gradio as gr
2
+ import pdf2speech
3
+ from gtts import gTTS
4
+ from tempfile import NamedTemporaryFile
5
+ import os
6
+
7
+ def convert_pdf_to_speech(pdf_file):
8
+ text = pdf2speech.extract_text_from_pdf(pdf_file.name)
9
+ tts = gTTS(text=text, lang='en')
10
+ audio_file = NamedTemporaryFile(suffix=".mp3", delete=False)
11
+ tts.save(audio_file.name)
12
+ audio_file.close()
13
+ return audio_file.name
14
+
15
+ def pdf_to_speech(pdf_file):
16
+ audio_file_path = convert_pdf_to_speech(pdf_file)
17
+ return audio_file_path
18
 
19
  def main():
20
+ pdf_input = gr.inputs.File(label="Upload PDF", type="file")
21
+ audio_output = gr.outputs.Audio(label="Generated Audio")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ iface = gr.Interface(fn=pdf_to_speech, inputs=pdf_input, outputs=audio_output, title="PDF to Speech Converter")
24
  iface.launch()
25
 
26
  if __name__ == "__main__":