Views
No views yet

Caro_fastpitch.nemo - FastPitch model in NEMO formatCaro_hifigan.nemo - HiFi-GAN vocoder in NEMO formatCaro_fastpitch.onnx - FastPitch model in ONNX formatCaro_hifigan.onnx - HiFi-GAN vocoder in ONNX formatCaro_fastpitch_encoder.pt2 - FastPitch-Encoder compiled with PyTorch Inductor (for CUDA/Zero GPU)Caro_fastpitch_decoder.pt2 - FastPitch-Decoder compiled with PyTorch Inductor (for CUDA/Zero GPU)Caro_hifigan.pt2 - HiFi-GAN compiled with PyTorch Inductor (for CUDA/Zero GPU)1import numpy as np
2import onnxruntime as ort
3import soundfile as sf
4
5# Tokenization function
6def normalize_unicode_text(text: str) -> str:
7 import unicodedata
8 if not unicodedata.is_normalized("NFC", text):
9 text = unicodedata.normalize("NFC", text)
10 return text
11
12def any_locale_text_preprocessing(text: str) -> str:
13 res = []
14 for c in normalize_unicode_text(text):
15 if c in ["'"]:
16 res.append("'")
17 else:
18 res.append(c)
19 return "".join(res)
20
21def tokenize_german(text: str, punct: bool = True, apostrophe: bool = True,
22 pad_with_space: bool = True) -> list[int]:
23 """Tokenize German text into a list of integer token IDs."""
24
25 _CHARSET_STR = "ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜẞabcdefghijklmnopqrstuvwxyzäöüß"
26 _PUNCT_LIST = [
27 "!", '"', "(", ")", ",", "-", ".", "/", ":", ";", "?", "[", "]",
28 "{", "}", "«", "»", "‒", "–", "—", "'", "‚", '"', "„", "‹", "›",
29 ]
30
31 tokens = [" "] # Space at index 0
32 tokens.extend(_CHARSET_STR)
33 if apostrophe:
34 tokens.append("'")
35 if punct:
36 tokens.extend(_PUNCT_LIST)
37 tokens.extend(["<pad>", "<blank>", "<oov>"])
38
39 token2id = {token: i for i, token in enumerate(tokens)}
40 space = " "
41
42 text = any_locale_text_preprocessing(text)
43
44 # Encode
45 cs = []
46 tokens_set = set(tokens)
47
48 for c in text:
49 if ((c == space and len(cs) > 0 and cs[-1] != space) or
50 ((c.isalnum() or c == "'") and c in tokens_set) or
51 (c in _PUNCT_LIST and punct)):
52 cs.append(c)
53
54 if cs:
55 while cs and cs[-1] == space:
56 cs.pop()
57
58 if pad_with_space:
59 cs = [space] + cs + [space]
60
61 return [token2id[p] for p in cs]
62
63# Load ONNX models
64fastpitch_session = ort.InferenceSession("Caro_fastpitch.onnx")
65hifigan_session = ort.InferenceSession("Caro_hifigan.onnx")
66
67# Prepare text
68text = "Hallo, ich bin CaroTTS, ein deutsches Text-zu-Sprache-System."
69tokens = tokenize_german(text)
70
71# Prepare inputs
72paces = np.ones(len(tokens), dtype=np.float32)
73pitches = np.zeros(len(tokens), dtype=np.float32)
74
75inputs = {
76 "text": np.array([tokens], dtype=np.int64),
77 "pace": np.array([paces], dtype=np.float32),
78 "pitch": np.array([pitches], dtype=np.float32),
79}
80
81# Generate spectrogram
82spec = fastpitch_session.run(None, inputs)[0]
83
84# Generate audio
85audio = hifigan_session.run(None, {"spec": spec})[0]
86
87# Save audio (44.1kHz sample rate)
88sf.write("output.wav", audio.squeeze(), 44100)pip install nemo-toolkit[tts])and want to work with the original .nemo checkpoints:1import torch
2import soundfile as sf
3from nemo.collections.tts.models.fastpitch import FastPitchModel
4from nemo.collections.tts.models.hifigan import HifiGanModel
5
6# Load models
7device = "cuda" if torch.cuda.is_available() else "cpu"
8fastpitch = FastPitchModel.restore_from("Caro_fastpitch.nemo", map_location=device).eval()
9hifigan = HifiGanModel.restore_from("Caro_hifigan.nemo", map_location=device).eval()
10
11# Prepare text
12text = "Guten Tag. Herzlich Willkommen zu dieser Demonstration."
13
14with torch.inference_mode():
15 # Parse and generate
16 parsed_text = fastpitch.parse(text)
17 spec = fastpitch.generate_spectrogram(tokens=parsed_text)
18 audio = hifigan.convert_spectrogram_to_audio(spec=spec)
19
20 # Save audio (44.1kHz sample rate)
21 sf.write("output.wav", audio.squeeze().cpu().numpy(), 44100)1@misc{carotts2024,
2 title={CaroTTS: Fast Lightweight German Text-to-Speech},
3 author={Holtzwart, Tassilo},
4 year={2024},
5 url={https://github.com/TassiloHo/CaroTTS}
6}