Sambhavnoobcoder commited on
Commit
527f66c
·
1 Parent(s): 25e677a

new version

Browse files
Files changed (1) hide show
  1. app.py +17 -96
app.py CHANGED
@@ -1,106 +1,27 @@
1
  import gradio as gr
2
- import pdfminer
3
- from pdfminer.high_level import extract_text
4
- import logging
5
- from typing import cast
6
 
7
- import gradio as gr
8
- from balacoon_tts import TTS
9
- from huggingface_hub import hf_hub_download, list_repo_files
10
-
11
- # global tts module, initialized from a model selected
12
- tts = None
13
-
14
- def read_pdf(file):
15
- with open(file.name, "rb") as f:
16
- text = extract_text(f)
17
- return text
18
-
19
- # iface = gr.Interface(
20
- # read_pdf,
21
- # gr.inputs.File(),
22
- # # gr.outputs.Textbox()
23
- # )
24
- # iface.launch()
25
-
26
-
27
- def main():
28
- logging.basicConfig(level=logging.INFO)
29
- with gr.Blocks() as demo:
30
- gr.Markdown(
31
- """
32
- <h1 align="center">PDF TO SPEECH CONVERTER</h1>
33
- 1. insert a pdf
34
- 2. Select the model to synthesize with
35
- 3. Select speaker
36
- 4. Hit "Generate" and listen to the result!
37
- When you select model for the first time,
38
- it will take a little time to download it.
39
- this project is designed to take the love
40
- of reading without the hassle of looking over.
41
- if you want an audio book , you now got it .
42
- """
43
- )
44
-
45
- with gr.Row(variant="panel"):
46
- f=gr.inputs.File("enter the file")
47
- text = read_pdf(f)
48
-
49
- with gr.Row():
50
- with gr.Column(variant="panel"):
51
- repo_files = list_repo_files(repo_id="balacoon/tts")
52
- model_files = [x for x in repo_files if x.endswith("_cpu.addon")]
53
- model_name = gr.Dropdown(
54
- label="Model",
55
- choices=model_files,
56
- )
57
- with gr.Column(variant="panel"):
58
- speaker = gr.Dropdown(label="Speaker", choices=[])
59
 
60
- def set_model(model_name_str: str):
61
- """
62
- gets value from `model_name`, loads model,
63
- re-initializes tts object, gets list of
64
- speakers that model supports and set them to `speaker`
65
- """
66
- model_path = hf_hub_download(
67
- repo_id="balacoon/tts", filename=model_name_str
68
- )
69
- global tts
70
- tts = TTS(model_path)
71
- speakers = tts.get_speakers()
72
- value = speakers[-1]
73
- return gr.Dropdown.update(
74
- choices=speakers, value=value, visible=True
75
- )
76
 
77
- model_name.change(set_model, inputs=model_name, outputs=speaker)
 
 
78
 
79
- with gr.Row(variant="panel"):
80
- generate = gr.Button("Generate")
81
- with gr.Row(variant="panel"):
82
- audio = gr.Audio()
83
 
84
- def synthesize_audio(text_str: str, speaker_str: str = ""):
85
- """
86
- gets utterance to synthesize from `text` Textbox
87
- and speaker name from `speaker` dropdown list.
88
- speaker name might be empty for single-speaker models.
89
- Synthesizes the waveform and updates `audio` with it.
90
- """
91
- if not text_str:
92
- logging.info("text or speaker are not provided")
93
- return None
94
- global tts
95
- if len(text_str) > 1024:
96
- text_str = text_str[:1024]
97
- samples = cast(TTS, tts).synthesize(text_str, speaker_str)
98
- return gr.Audio.update(value=(cast(TTS, tts).get_sampling_rate(), samples))
99
 
100
- generate.click(synthesize_audio, inputs=[text, speaker], outputs=audio)
 
 
101
 
102
- demo.launch()
103
 
 
104
 
105
- if __name__ == "__main__":
106
- main()
 
1
  import gradio as gr
2
+ import pyttsx3
3
+ import PyPDF2
 
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def pdf_to_audio(pdf_file):
7
+ pdf_reader = PyPDF2.PdfFileReader(pdf_file)
8
+ text = ""
9
+ for page in range(pdf_reader.numPages):
10
+ text += pdf_reader.getPage(page).extractText()
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ engine = pyttsx3.init()
13
+ engine.say(text)
14
+ engine.runAndWait()
15
 
 
 
 
 
16
 
17
+ demo = gr.Blocks()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ with demo:
20
+ pdf_file = gr.File(type="filepath")
21
+ text = gr.Textbox()
22
 
23
+ b1 = gr.Button("Convert PDF to Audio")
24
 
25
+ b1.click(pdf_to_audio, inputs=pdf_file, outputs=text)
26
 
27
+ demo.launch()