Sambhavnoobcoder commited on
Commit
9a433d2
·
1 Parent(s): 741c4ec

another page rendering but no speaker options

Browse files
Files changed (1) hide show
  1. app.py +11 -32
app.py CHANGED
@@ -1,32 +1,29 @@
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 set_model(model_name_str):
17
- """
18
- Gets value from `model_name`, loads the model,
19
- re-initializes the tts object, and gets a list of
20
- speakers that the model supports and sets them to `speaker`.
21
- """
22
  model_path = hf_hub_download(repo_id="balacoon/tts", filename=model_name_str)
23
  global tts
24
  tts = TTS(model_path)
25
  speakers = tts.get_speakers()
26
- speaker_dropdown.update(choices=speakers, value=speakers[0] if speakers else None)
 
 
 
 
 
 
 
 
27
 
28
  def main():
29
- logging.basicConfig(level=logging.INFO)
30
  repo_files = list_repo_files(repo_id="balacoon/tts")
31
  model_files = [x for x in repo_files if x.endswith("_cpu.addon")]
32
  model_name_dropdown = gr.inputs.Dropdown(label="Model", choices=model_files)
@@ -35,25 +32,7 @@ def main():
35
  file_input = gr.inputs.File(label="Select a PDF File", type="file")
36
  text = gr.outputs.Textbox()
37
 
38
- def synthesize_audio(file, model_name_str, speaker_str):
39
- """
40
- Gets the selected PDF `file`, model name from `model_name`,
41
- and speaker name from `speaker`. Synthesizes the audio waveform
42
- from the text extracted from the PDF and returns it.
43
- """
44
- if file is None or file.name == "":
45
- logging.info("No file selected.")
46
- return None
47
-
48
- text_str = read_pdf(file)
49
- if len(text_str) > 1024:
50
- text_str = text_str[:1024]
51
-
52
- global tts
53
- samples = cast(TTS, tts).synthesize(text_str, speaker_str)
54
- return (cast(TTS, tts).get_sampling_rate(), samples)
55
-
56
- audio = gr.outputs.Audio(label="Generated Audio", type="numpy")
57
 
58
  iface = gr.Interface(
59
  fn=synthesize_audio,
@@ -64,7 +43,7 @@ def main():
64
  debug=True
65
  )
66
 
67
- model_name_dropdown.action = set_model
68
 
69
  iface.launch()
70
 
 
1
  import gradio as gr
2
  from pdfminer.high_level import extract_text
 
 
3
  from balacoon_tts import TTS
4
  from huggingface_hub import hf_hub_download, list_repo_files
5
 
 
 
 
6
  def read_pdf(file):
7
  with open(file.name, "rb") as f:
8
  text = extract_text(f)
9
  return text
10
 
11
  def set_model(model_name_str):
 
 
 
 
 
12
  model_path = hf_hub_download(repo_id="balacoon/tts", filename=model_name_str)
13
  global tts
14
  tts = TTS(model_path)
15
  speakers = tts.get_speakers()
16
+ speaker_dropdown.choices = speakers
17
+ speaker_dropdown.update(default=speakers[0] if speakers else None)
18
+
19
+ def synthesize_audio(file, model_name_str, speaker_str):
20
+ text_str = read_pdf(file)
21
+ if len(text_str) > 1024:
22
+ text_str = text_str[:1024]
23
+ samples = tts.synthesize(text_str, speaker_str)
24
+ return (tts.get_sampling_rate(), samples)
25
 
26
  def main():
 
27
  repo_files = list_repo_files(repo_id="balacoon/tts")
28
  model_files = [x for x in repo_files if x.endswith("_cpu.addon")]
29
  model_name_dropdown = gr.inputs.Dropdown(label="Model", choices=model_files)
 
32
  file_input = gr.inputs.File(label="Select a PDF File", type="file")
33
  text = gr.outputs.Textbox()
34
 
35
+ audio = gr.outputs.Audio(label="Generated Audio")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  iface = gr.Interface(
38
  fn=synthesize_audio,
 
43
  debug=True
44
  )
45
 
46
+ model_name_dropdown.set_action(set_model)
47
 
48
  iface.launch()
49