Update audiobook.py
Browse files- audiobook.py +195 -188
audiobook.py
CHANGED
|
@@ -1,188 +1,195 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
-
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
-
from scipy.io.wavfile import write
|
| 6 |
-
from tts import commons
|
| 7 |
-
from tts import utils
|
| 8 |
-
from tts.models import SynthesizerTrn
|
| 9 |
-
from text.symbols import symbols
|
| 10 |
-
from text import text_to_sequence
|
| 11 |
-
from phonemizer.backend.espeak.wrapper import EspeakWrapper
|
| 12 |
-
from safetensors.torch import load_file
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
audio = (audio
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
text = text.replace("
|
| 51 |
-
text = text.replace("
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
audio_chunks.append(
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
from scipy.io.wavfile import write
|
| 6 |
+
from tts import commons
|
| 7 |
+
from tts import utils
|
| 8 |
+
from tts.models import SynthesizerTrn
|
| 9 |
+
from text.symbols import symbols
|
| 10 |
+
from text import text_to_sequence
|
| 11 |
+
from phonemizer.backend.espeak.wrapper import EspeakWrapper
|
| 12 |
+
from safetensors.torch import load_file
|
| 13 |
+
from huggingface_hub import hf_hub_download
|
| 14 |
+
|
| 15 |
+
_ESPEAK_LIBRARY = r"C:\Program Files\eSpeak NG\libespeak-ng.dll"
|
| 16 |
+
if os.path.exists(_ESPEAK_LIBRARY):
|
| 17 |
+
EspeakWrapper.set_library(_ESPEAK_LIBRARY)
|
| 18 |
+
print(f"β
Found eSpeak-ng: {_ESPEAK_LIBRARY}")
|
| 19 |
+
else:
|
| 20 |
+
print("β οΈ eSpeak-ng not found (ok if already working)")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
REPO_ID = "PatnaikAshish/Sonya-TTS"
|
| 24 |
+
MODEL_FILENAME = "sonya-tts.safetensors"
|
| 25 |
+
CONFIG_FILENAME = "config.json"
|
| 26 |
+
|
| 27 |
+
LOCAL_MODEL_PATH = "checkpoints/sonya-tts.safetensors"
|
| 28 |
+
LOCAL_CONFIG_PATH = "checkpoints/config.json"
|
| 29 |
+
OUTPUT_WAV_SHORT = "output.wav"
|
| 30 |
+
OUTPUT_WAV_LONG = "audiobook.wav"
|
| 31 |
+
|
| 32 |
+
USE_LONG_FORM = True # β change to False for short text
|
| 33 |
+
|
| 34 |
+
TEXT = """
|
| 35 |
+
A neural network or Artificial Neural Network is a computer system inspired by the human brain,
|
| 36 |
+
using interconnected nodes neurons in layers to recognize complex patterns in data for tasks like
|
| 37 |
+
image recognition, language processing, and prediction
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
def save_wav_int16(path, audio, sample_rate):
|
| 41 |
+
audio = np.clip(audio, -1.0, 1.0)
|
| 42 |
+
audio = (audio * 32767).astype(np.int16)
|
| 43 |
+
write(path, sample_rate, audio)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def clean_text_for_vits(text):
|
| 47 |
+
text = text.strip()
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
text = text.replace("β", "'")
|
| 51 |
+
text = text.replace("β", '"').replace("β", '"')
|
| 52 |
+
text = text.replace("β", "-").replace("β", "-")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
text = re.sub(r"[()\[\]{}<>]", "", text)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
text = re.sub(r"[^a-zA-Z0-9\s.,!?'\-]", "", text)
|
| 59 |
+
|
| 60 |
+
text = re.sub(r"\s+", " ", text)
|
| 61 |
+
|
| 62 |
+
return text
|
| 63 |
+
|
| 64 |
+
def get_text(text, hps):
|
| 65 |
+
text = clean_text_for_vits(text)
|
| 66 |
+
text_norm = text_to_sequence(text, hps.data.text_cleaners)
|
| 67 |
+
if hps.data.add_blank:
|
| 68 |
+
text_norm = commons.intersperse(text_norm, 0)
|
| 69 |
+
return torch.LongTensor(text_norm)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def split_sentences(text):
|
| 73 |
+
text = clean_text_for_vits(text)
|
| 74 |
+
if not text:
|
| 75 |
+
return []
|
| 76 |
+
return re.split(r'(?<=[.!?])\s+', text)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def generate_audiobook(
|
| 80 |
+
net_g,
|
| 81 |
+
hps,
|
| 82 |
+
text,
|
| 83 |
+
device,
|
| 84 |
+
output_file,
|
| 85 |
+
noise_scale=0.5,
|
| 86 |
+
noise_scale_w=0.6,
|
| 87 |
+
length_scale=1.0,
|
| 88 |
+
base_pause=0.4,
|
| 89 |
+
):
|
| 90 |
+
print("π Long-form audiobook mode enabled")
|
| 91 |
+
|
| 92 |
+
sentences = split_sentences(text)
|
| 93 |
+
print(f"πΉ Sentences: {len(sentences)}")
|
| 94 |
+
|
| 95 |
+
audio_chunks = []
|
| 96 |
+
|
| 97 |
+
for i, sent in enumerate(sentences):
|
| 98 |
+
sent = sent.strip()
|
| 99 |
+
if not sent:
|
| 100 |
+
continue
|
| 101 |
+
|
| 102 |
+
stn_tst = get_text(sent, hps)
|
| 103 |
+
|
| 104 |
+
with torch.no_grad():
|
| 105 |
+
x = stn_tst.to(device).unsqueeze(0)
|
| 106 |
+
x_len = torch.LongTensor([stn_tst.size(0)]).to(device)
|
| 107 |
+
|
| 108 |
+
audio = net_g.infer(
|
| 109 |
+
x,
|
| 110 |
+
x_len,
|
| 111 |
+
noise_scale=noise_scale,
|
| 112 |
+
noise_scale_w=noise_scale_w,
|
| 113 |
+
length_scale=length_scale,
|
| 114 |
+
)[0][0, 0].cpu().numpy()
|
| 115 |
+
|
| 116 |
+
if sent.endswith("?"):
|
| 117 |
+
pause = base_pause + 0.15
|
| 118 |
+
elif sent.endswith("!"):
|
| 119 |
+
pause = base_pause
|
| 120 |
+
else:
|
| 121 |
+
pause = base_pause + 0.05
|
| 122 |
+
|
| 123 |
+
silence = np.zeros(int(hps.data.sampling_rate * pause))
|
| 124 |
+
|
| 125 |
+
audio_chunks.append(audio)
|
| 126 |
+
audio_chunks.append(silence)
|
| 127 |
+
|
| 128 |
+
print(f" β
Sentence {i+1}/{len(sentences)} done")
|
| 129 |
+
|
| 130 |
+
final_audio = np.concatenate(audio_chunks)
|
| 131 |
+
save_wav_int16(output_file, final_audio, hps.data.sampling_rate)
|
| 132 |
+
|
| 133 |
+
print(f"π Audiobook saved: {os.path.abspath(output_file)}")
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def main():
|
| 137 |
+
if os.path.exists(LOCAL_MODEL_PATH) and os.path.exists(LOCAL_CONFIG_PATH):
|
| 138 |
+
print("β
Loading Sonya TTS from local checkpoints...")
|
| 139 |
+
model_path = LOCAL_MODEL_PATH
|
| 140 |
+
config_path = LOCAL_CONFIG_PATH
|
| 141 |
+
else:
|
| 142 |
+
print("π Downloading Sonya TTS from Hugging Face...")
|
| 143 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
|
| 144 |
+
config_path = hf_hub_download(repo_id=REPO_ID, filename=CONFIG_FILENAME)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
hps = utils.get_hparams_from_file(config_path)
|
| 148 |
+
|
| 149 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 150 |
+
print(f"π Using device: {device}")
|
| 151 |
+
|
| 152 |
+
# Load model
|
| 153 |
+
net_g = SynthesizerTrn(
|
| 154 |
+
len(symbols),
|
| 155 |
+
hps.data.filter_length // 2 + 1,
|
| 156 |
+
hps.train.segment_size // hps.data.hop_length,
|
| 157 |
+
**hps.model,
|
| 158 |
+
).to(device)
|
| 159 |
+
net_g.eval()
|
| 160 |
+
|
| 161 |
+
# Load checkpoint
|
| 162 |
+
state_dict = load_file(model_path)
|
| 163 |
+
net_g.load_state_dict(state_dict)
|
| 164 |
+
print(f"β
Loaded model: {model_path}")
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
if USE_LONG_FORM:
|
| 168 |
+
generate_audiobook(
|
| 169 |
+
net_g,
|
| 170 |
+
hps,
|
| 171 |
+
TEXT,
|
| 172 |
+
device,
|
| 173 |
+
OUTPUT_WAV_LONG,
|
| 174 |
+
)
|
| 175 |
+
else:
|
| 176 |
+
print("π£οΈ Short-text inference")
|
| 177 |
+
|
| 178 |
+
stn_tst = get_text(TEXT, hps)
|
| 179 |
+
with torch.no_grad():
|
| 180 |
+
x = stn_tst.to(device).unsqueeze(0)
|
| 181 |
+
x_len = torch.LongTensor([stn_tst.size(0)]).to(device)
|
| 182 |
+
|
| 183 |
+
audio = net_g.infer(
|
| 184 |
+
x,
|
| 185 |
+
x_len,
|
| 186 |
+
noise_scale=0.5,
|
| 187 |
+
noise_scale_w=0.6,
|
| 188 |
+
length_scale=1.0,
|
| 189 |
+
)[0][0, 0].cpu().numpy()
|
| 190 |
+
|
| 191 |
+
save_wav_int16(OUTPUT_WAV_SHORT, audio, hps.data.sampling_rate)
|
| 192 |
+
print(f"πΎ Saved audio: {os.path.abspath(OUTPUT_WAV_SHORT)}")
|
| 193 |
+
|
| 194 |
+
if __name__ == "__main__":
|
| 195 |
+
main()
|