Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from TTS.utils.synthesizer import Synthesizer
|
|
|
|
|
|
|
| 3 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
-
import gc
|
| 7 |
|
| 8 |
def load_eng_model():
|
| 9 |
repo_id = "E-motionAssistant/text-to-speech-VITS-english"
|
| 10 |
print(f"--- Downloading English Model ---")
|
| 11 |
|
| 12 |
-
# 1. Download original files
|
| 13 |
model_path = hf_hub_download(repo_id=repo_id, filename="best_model.pth")
|
| 14 |
config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
# 3.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# 4.
|
| 29 |
-
print("Building Synthesizer with fixed_config.json path...")
|
| 30 |
syn = Synthesizer(
|
| 31 |
tts_checkpoint=model_path,
|
| 32 |
-
tts_config_path=
|
| 33 |
use_cuda=False
|
| 34 |
)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
gc.collect()
|
| 37 |
return syn
|
| 38 |
|
| 39 |
-
# ---
|
| 40 |
print("Starting English TTS Startup...")
|
| 41 |
try:
|
| 42 |
eng_tts = load_eng_model()
|
| 43 |
-
print("--- SUCCESS:
|
| 44 |
except Exception as e:
|
| 45 |
print(f"CRITICAL ERROR: {e}")
|
| 46 |
eng_tts = None
|
|
@@ -50,9 +56,7 @@ def generate_voice(text):
|
|
| 50 |
return None
|
| 51 |
try:
|
| 52 |
output_path = os.path.join(os.getcwd(), "output.wav")
|
| 53 |
-
# Generate the audio samples
|
| 54 |
wav = eng_tts.tts(text=str(text))
|
| 55 |
-
# Save samples to a .wav file
|
| 56 |
eng_tts.save_wav(wav, output_path)
|
| 57 |
return output_path
|
| 58 |
except Exception as e:
|
|
@@ -62,9 +66,9 @@ def generate_voice(text):
|
|
| 62 |
# Gradio Interface
|
| 63 |
demo = gr.Interface(
|
| 64 |
fn=generate_voice,
|
| 65 |
-
inputs=gr.Textbox(label="Input English Text"
|
| 66 |
-
outputs=gr.Audio(label="
|
| 67 |
-
title="English VITS TTS"
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from TTS.utils.synthesizer import Synthesizer
|
| 3 |
+
from TTS.tts.models.vits import Vits
|
| 4 |
+
from TTS.tts.configs.vits_config import VitsConfig
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
+
import torch
|
| 7 |
import os
|
| 8 |
import json
|
|
|
|
| 9 |
|
| 10 |
def load_eng_model():
|
| 11 |
repo_id = "E-motionAssistant/text-to-speech-VITS-english"
|
| 12 |
print(f"--- Downloading English Model ---")
|
| 13 |
|
|
|
|
| 14 |
model_path = hf_hub_download(repo_id=repo_id, filename="best_model.pth")
|
| 15 |
config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
|
| 16 |
|
| 17 |
+
# 1. Load and fix config
|
| 18 |
+
config = VitsConfig()
|
| 19 |
+
config.load_json(config_path)
|
| 20 |
+
config.model_args.num_chars = 137 # The magic number
|
| 21 |
|
| 22 |
+
# 2. Build the Model Architecture Manually
|
| 23 |
+
print("Building model architecture...")
|
| 24 |
+
model = Vits.init_from_config(config)
|
| 25 |
|
| 26 |
+
# 3. Load the checkpoint weights
|
| 27 |
+
print("Loading weights (using non-strict mode to bypass mismatch)...")
|
| 28 |
+
checkpoint = torch.load(model_path, map_location="cpu")
|
| 29 |
+
|
| 30 |
+
# This is the line that solves the "Size Mismatch" crash
|
| 31 |
+
model.load_state_dict(checkpoint["model"], strict=False)
|
| 32 |
+
model.eval()
|
| 33 |
|
| 34 |
+
# 4. Wrap it in a Synthesizer
|
|
|
|
| 35 |
syn = Synthesizer(
|
| 36 |
tts_checkpoint=model_path,
|
| 37 |
+
tts_config_path=config_path,
|
| 38 |
use_cuda=False
|
| 39 |
)
|
| 40 |
+
# Overwrite the internal model with our forced one
|
| 41 |
+
syn.tts_model = model
|
| 42 |
|
|
|
|
| 43 |
return syn
|
| 44 |
|
| 45 |
+
# --- Initialization ---
|
| 46 |
print("Starting English TTS Startup...")
|
| 47 |
try:
|
| 48 |
eng_tts = load_eng_model()
|
| 49 |
+
print("--- SUCCESS: SYSTEM READY ---")
|
| 50 |
except Exception as e:
|
| 51 |
print(f"CRITICAL ERROR: {e}")
|
| 52 |
eng_tts = None
|
|
|
|
| 56 |
return None
|
| 57 |
try:
|
| 58 |
output_path = os.path.join(os.getcwd(), "output.wav")
|
|
|
|
| 59 |
wav = eng_tts.tts(text=str(text))
|
|
|
|
| 60 |
eng_tts.save_wav(wav, output_path)
|
| 61 |
return output_path
|
| 62 |
except Exception as e:
|
|
|
|
| 66 |
# Gradio Interface
|
| 67 |
demo = gr.Interface(
|
| 68 |
fn=generate_voice,
|
| 69 |
+
inputs=gr.Textbox(label="Input English Text"),
|
| 70 |
+
outputs=gr.Audio(label="Audio Output", type="filepath"),
|
| 71 |
+
title="English VITS TTS (Forced Load)"
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|