Sambhavnoobcoder commited on
Commit
6fa7850
·
1 Parent(s): 1a95e11

this is chatgpt modificaion completely . so if things go to shit , revert back immediately

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