DhanuakaDev commited on
Commit
ceb6264
·
1 Parent(s): 63ef4fe

changed3 app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -1,36 +1,38 @@
1
  import json
2
- import os
3
 
4
  import numpy as np
5
  import gradio as gr
6
- from TTS.utils.synthesizer import Synthesizer
7
 
8
  # ---------- Paths ----------
9
- MODEL_PATH = "checkpoint_80000.pth" # or "best_model_23206.pth"
10
  CONFIG_PATH = "config.json"
11
 
12
- # ---------- Load config to get sample rate ----------
13
- with open(CONFIG_PATH, "r", encoding="utf-8") as f:
14
- cfg = json.load(f)
15
 
16
- SAMPLE_RATE = cfg.get("audio", {}).get("sample_rate", 24000)
17
-
18
- # ---------- Load Coqui TTS Synthesizer ----------
19
- synthesizer = Synthesizer(
20
- tts_checkpoint=MODEL_PATH,
21
- tts_config_path=CONFIG_PATH,
22
- use_cuda=False, # Set True only if you enable GPU in the Space
23
  )
24
 
 
 
 
25
  # ---------- Inference function ----------
26
  def tts_generate(text: str):
27
- if not text.strip():
 
28
  return None
29
 
30
- # Whatever Coqui returns (list, numpy array, etc.)
31
- wav = synthesizer.tts(text)
32
 
33
- # Convert to a 1D numpy array for Gradio
34
  wav = np.asarray(wav, dtype="float32").flatten()
35
 
36
  # Gradio Audio(type="numpy") expects (sample_rate, np.ndarray)
@@ -41,15 +43,15 @@ demo = gr.Interface(
41
  fn=tts_generate,
42
  inputs=gr.Textbox(
43
  label="Input text",
44
- placeholder="Type Sinhala text here…(use Sinhala script, max 120 characters)",
45
  lines=3,
46
  ),
47
  outputs=gr.Audio(
48
  label="Generated speech",
49
  type="numpy",
50
  ),
51
- title="Sinhala TTS",
52
- description="Sinhala TTS model- Research-stage model",
53
  )
54
 
55
  if __name__ == "__main__":
 
1
  import json
2
+ from pathlib import Path
3
 
4
  import numpy as np
5
  import gradio as gr
6
+ from TTS.api import TTS
7
 
8
  # ---------- Paths ----------
9
+ MODEL_PATH = "checkpoint_80000.pth" # or checkpoint_70000.pth if that's the good one
10
  CONFIG_PATH = "config.json"
11
 
12
+ run_dir = Path(".")
 
 
13
 
14
+ # ---------- Load model ----------
15
+ # In Spaces we will force CPU (gpu=False)
16
+ tts = TTS(
17
+ model_path=str(run_dir / MODEL_PATH),
18
+ config_path=str(run_dir / CONFIG_PATH),
19
+ progress_bar=False,
20
+ gpu=False,
21
  )
22
 
23
+ # Sample rate from the loaded synthesizer
24
+ SAMPLE_RATE = tts.synthesizer.output_sample_rate
25
+
26
  # ---------- Inference function ----------
27
  def tts_generate(text: str):
28
+ text = text.strip()
29
+ if not text:
30
  return None
31
 
32
+ # This is the same as your local script
33
+ wav = tts.tts(text)
34
 
35
+ # Ensure numpy array (1D) for Gradio
36
  wav = np.asarray(wav, dtype="float32").flatten()
37
 
38
  # Gradio Audio(type="numpy") expects (sample_rate, np.ndarray)
 
43
  fn=tts_generate,
44
  inputs=gr.Textbox(
45
  label="Input text",
46
+ placeholder="Type Sinhala text here…",
47
  lines=3,
48
  ),
49
  outputs=gr.Audio(
50
  label="Generated speech",
51
  type="numpy",
52
  ),
53
+ title="Sinhala TTS ",
54
+ description="Sinhala TTS model research-stage model.",
55
  )
56
 
57
  if __name__ == "__main__":