amasha03 commited on
Commit
a44a8cb
·
verified ·
1 Parent(s): 0d91b65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
app.py CHANGED
@@ -3,8 +3,7 @@ from TTS.api import TTS
3
  from TTS.tts.configs.vits_config import VitsConfig
4
  from huggingface_hub import hf_hub_download
5
  import os
6
- import json
7
- import gc
8
  from romanizer import sinhala_to_roman
9
 
10
  def load_my_model(repo_id):
@@ -21,56 +20,60 @@ def load_my_model(repo_id):
21
  if hasattr(config, "model_args"):
22
  config.model_args.num_chars = 137
23
 
24
- # Initialize TTS without the 'items_per_group' argument
25
  tts = TTS(gpu=False)
26
 
27
- # Load the model using the config object we just fixed
28
- tts.load_tts_model_by_path(
29
- checkpoint_path=model_path,
30
- config_path=config
31
- )
32
 
33
  gc.collect()
34
  return tts
35
 
36
- # Load All Models
37
  print("Initializing Models...")
38
  try:
39
- eng_tts = load_my_model("E-motionAssistant/text-to-speech-VITS-english")
40
- sin_tts = load_my_model("E-motionAssistant/text-to-speech-VITS-sinhala")
41
- tam_tts = load_my_model("E-motionAssistant/text-to-speech-VITS-tamil")
 
 
 
42
  except Exception as e:
43
  print(f"CRITICAL ERROR DURING LOADING: {e}")
 
44
 
45
  def generate_voice(text, language):
46
  try:
47
- if language == "English":
48
- engine = eng_tts
49
- processed_text = text
50
- elif language == "Sinhala":
51
- engine = sin_tts
52
- # Using your romanizer logic
 
53
  processed_text = sinhala_to_roman(text)
54
- else:
55
- engine = tam_tts
56
- processed_text = text
57
 
58
- output_path = "output.wav"
59
- engine.tts_to_file(text=processed_text, file_path=output_path)
 
60
  return output_path
61
  except Exception as e:
62
- print(f"Error: {e}")
63
  return None
64
 
65
  # Gradio Interface
66
  demo = gr.Interface(
67
  fn=generate_voice,
68
  inputs=[
69
- gr.Textbox(label="Input Text"),
70
  gr.Dropdown(["English", "Sinhala", "Tamil"], label="Select Language")
71
  ],
72
  outputs=gr.Audio(label="Synthesized Speech", type="filepath"),
73
  title="Multilingual VITS TTS"
74
  )
75
 
76
- demo.launch()
 
 
3
  from TTS.tts.configs.vits_config import VitsConfig
4
  from huggingface_hub import hf_hub_download
5
  import os
6
+ import gc
 
7
  from romanizer import sinhala_to_roman
8
 
9
  def load_my_model(repo_id):
 
20
  if hasattr(config, "model_args"):
21
  config.model_args.num_chars = 137
22
 
23
+ # Initialize TTS shell
24
  tts = TTS(gpu=False)
25
 
26
+ # IMPORTANT: We pass model_path and config POSITIONALLY.
27
+ # Some TTS versions use 'checkpoint_path', others use 'model_path'.
28
+ # Passing them in order (1st, 2nd) avoids that 'unexpected keyword' error.
29
+ tts.load_tts_model_by_path(model_path, config)
 
30
 
31
  gc.collect()
32
  return tts
33
 
34
+ # --- Loading Models ---
35
  print("Initializing Models...")
36
  try:
37
+ # We load them into a dictionary for cleaner access
38
+ engines = {
39
+ "English": load_my_model("E-motionAssistant/text-to-speech-VITS-english"),
40
+ "Sinhala": load_my_model("E-motionAssistant/text-to-speech-VITS-sinhala"),
41
+ "Tamil": load_my_model("E-motionAssistant/text-to-speech-VITS-tamil")
42
+ }
43
  except Exception as e:
44
  print(f"CRITICAL ERROR DURING LOADING: {e}")
45
+ engines = {}
46
 
47
  def generate_voice(text, language):
48
  try:
49
+ engine = engines.get(language)
50
+ if engine is None:
51
+ return None
52
+
53
+ processed_text = text
54
+ if language == "Sinhala":
55
+ # Call your romanizer
56
  processed_text = sinhala_to_roman(text)
57
+ print(f"Romanized: {processed_text}")
 
 
58
 
59
+ # Unique output name to prevent file conflicts
60
+ output_path = f"output_{language.lower()}.wav"
61
+ engine.tts_to_file(text=str(processed_text), file_path=output_path)
62
  return output_path
63
  except Exception as e:
64
+ print(f"Inference Error: {e}")
65
  return None
66
 
67
  # Gradio Interface
68
  demo = gr.Interface(
69
  fn=generate_voice,
70
  inputs=[
71
+ gr.Textbox(label="Input Text", placeholder="Type here..."),
72
  gr.Dropdown(["English", "Sinhala", "Tamil"], label="Select Language")
73
  ],
74
  outputs=gr.Audio(label="Synthesized Speech", type="filepath"),
75
  title="Multilingual VITS TTS"
76
  )
77
 
78
+ if __name__ == "__main__":
79
+ demo.launch()