amasha03 commited on
Commit
0861d22
·
verified ·
1 Parent(s): 8636826

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -48
app.py CHANGED
@@ -12,80 +12,55 @@ def load_eng_model():
12
  model_path = hf_hub_download(repo_id=repo_id, filename="best_model.pth")
13
  config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
14
 
15
- # --- THE FIX: MANUALLY OVERWRITE THE FILE ON DISK ---
16
  with open(config_path, 'r') as f:
17
- config_data = json.load(f)
18
 
19
- # Force the character count in the dictionary
20
- print("Modifying config file on disk to 137 characters...")
21
- config_data["model_args"]["num_chars"] = 137
22
 
23
- # Save it back to the same file
24
- with open(config_path, 'w') as f:
25
- json.dump(config_data, f)
26
- # ---------------------------------------------------
27
 
28
- # Now load using the paths normally. Since the file is
29
- # changed on disk, the model will read 137 correctly.
30
- tts = TTS(model_path=model_path, config_path=config_path, gpu=False)
 
31
 
32
  gc.collect()
33
  return tts
34
 
35
  # --- Global Initialization ---
36
- print("Starting English TTS...")
37
  try:
38
  eng_tts = load_eng_model()
39
- print("--- ENGLISH MODEL LOADED SUCCESSFULLY ---")
40
  except Exception as e:
41
  print(f"CRITICAL ERROR: {e}")
42
  eng_tts = None
43
 
44
  def generate_voice(text):
45
- print(f"\n>>> SYNTHESIS START: '{text}'")
46
  if eng_tts is None:
47
- print(">>> ERROR: eng_tts is None. Model did not load!")
48
  return None
49
-
50
  try:
51
- # Use an absolute path to ensure Gradio finds it
52
  output_path = os.path.join(os.getcwd(), "en_output.wav")
53
-
54
- # Clean up old file if it exists
55
- if os.path.exists(output_path):
56
- os.remove(output_path)
57
- print(">>> Cleaned old audio file.")
58
-
59
- # Synthesis
60
- print(">>> Running tts_to_file...")
61
  eng_tts.tts_to_file(text=str(text), file_path=output_path)
62
-
63
- # Check if the file actually exists and has data
64
- if os.path.exists(output_path):
65
- size = os.path.getsize(output_path)
66
- if size > 1000: # Files smaller than 1KB are usually silent/empty
67
- print(f">>> SUCCESS: Audio generated ({size} bytes)")
68
- return output_path
69
- else:
70
- print(f">>> ERROR: File created but it is TOO SMALL ({size} bytes).")
71
- else:
72
- print(">>> ERROR: tts_to_file finished but NO FILE was found on disk.")
73
-
74
  except Exception as e:
75
- print(f">>> GENERATION CRASHED: {e}")
76
- # If espeak is missing, the error will appear here!
77
-
78
- return None
79
 
80
- # Gradio Interface - Added interactive features to help debugging
81
  demo = gr.Interface(
82
  fn=generate_voice,
83
- inputs=gr.Textbox(label="Input English Text", value="Hello, the system is now testing audio output."),
84
- outputs=gr.Audio(label="Synthesized Speech", type="filepath", autoplay=True),
85
- title="English VITS TTS",
86
- allow_flagging="never"
87
  )
88
 
89
-
90
  if __name__ == "__main__":
91
  demo.launch()
 
12
  model_path = hf_hub_download(repo_id=repo_id, filename="best_model.pth")
13
  config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
14
 
15
+ # 1. Read the config file
16
  with open(config_path, 'r') as f:
17
+ config_dict = json.load(f)
18
 
19
+ # 2. Force the character count to 137 in the dictionary
20
+ print("Forcing 137 characters in config...")
21
+ config_dict["model_args"]["num_chars"] = 137
22
 
23
+ # 3. Save it back to a NEW local file to ensure it's fresh
24
+ local_config_path = os.path.join(os.getcwd(), "fixed_config.json")
25
+ with open(local_config_path, 'w') as f:
26
+ json.dump(config_dict, f)
27
 
28
+ # 4. Initialize TTS and load model
29
+ # Use the local_config_path we just created
30
+ print("Initializing TTS engine with fixed config...")
31
+ tts = TTS(model_path=model_path, config_path=local_config_path, gpu=False)
32
 
33
  gc.collect()
34
  return tts
35
 
36
  # --- Global Initialization ---
37
+ print("Starting English TTS Startup...")
38
  try:
39
  eng_tts = load_eng_model()
40
+ print("--- SUCCESS: ENGLISH MODEL LOADED ---")
41
  except Exception as e:
42
  print(f"CRITICAL ERROR: {e}")
43
  eng_tts = None
44
 
45
  def generate_voice(text):
 
46
  if eng_tts is None:
 
47
  return None
 
48
  try:
 
49
  output_path = os.path.join(os.getcwd(), "en_output.wav")
50
+ # Synthesize
 
 
 
 
 
 
 
51
  eng_tts.tts_to_file(text=str(text), file_path=output_path)
52
+ return output_path
 
 
 
 
 
 
 
 
 
 
 
53
  except Exception as e:
54
+ print(f"Generation Error: {e}")
55
+ return None
 
 
56
 
57
+ # Gradio Interface (Fixed: removed 'allow_flagging' which caused the crash)
58
  demo = gr.Interface(
59
  fn=generate_voice,
60
+ inputs=gr.Textbox(label="Input English Text"),
61
+ outputs=gr.Audio(label="Synthesized Speech", type="filepath"),
62
+ title="English VITS TTS"
 
63
  )
64
 
 
65
  if __name__ == "__main__":
66
  demo.launch()