Upload inference.py with huggingface_hub
Browse files- inference.py +61 -0
inference.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Thorsten-Voice/Kokoro - German TTS inference example.
|
| 3 |
+
|
| 4 |
+
Downloads model, config and voicepack from the Hugging Face Hub
|
| 5 |
+
(no local files required) and synthesizes German speech in Thorsten's voice.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python inference.py "Hallo, hier spricht Thorsten." output.wav
|
| 9 |
+
"""
|
| 10 |
+
import sys
|
| 11 |
+
|
| 12 |
+
import numpy as np
|
| 13 |
+
import soundfile as sf
|
| 14 |
+
import torch
|
| 15 |
+
from huggingface_hub import hf_hub_download
|
| 16 |
+
|
| 17 |
+
REPO_ID = "Thorsten-Voice/Kokoro"
|
| 18 |
+
BASE_REPO_ID = "hexgrad/Kokoro-82M" # architecture reference only, no download from here
|
| 19 |
+
SAMPLE_RATE = 24000
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def load_pipeline(device: str | None = None):
|
| 23 |
+
from kokoro import KModel, KPipeline
|
| 24 |
+
|
| 25 |
+
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
| 26 |
+
|
| 27 |
+
config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
|
| 28 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename="model.pth")
|
| 29 |
+
voice_path = hf_hub_download(repo_id=REPO_ID, filename="voices/thorsten.pt")
|
| 30 |
+
|
| 31 |
+
kmodel = KModel(repo_id=BASE_REPO_ID, config=config_path, model=model_path)
|
| 32 |
+
kmodel = kmodel.to(device).eval()
|
| 33 |
+
|
| 34 |
+
pipeline = KPipeline(lang_code="d", repo_id=BASE_REPO_ID, model=kmodel)
|
| 35 |
+
voice = torch.load(voice_path, map_location="cpu", weights_only=True)
|
| 36 |
+
|
| 37 |
+
return pipeline, voice, device
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def synthesize(text: str, output_path: str = "output.wav", speed: float = 1.0) -> None:
|
| 41 |
+
pipeline, voice, device = load_pipeline()
|
| 42 |
+
print(f"Using device: {device}")
|
| 43 |
+
|
| 44 |
+
audio_chunks = []
|
| 45 |
+
for _, phonemes, audio in pipeline(text, voice=voice, speed=speed):
|
| 46 |
+
print(f"Phonemes: {phonemes}")
|
| 47 |
+
audio_chunks.append(audio)
|
| 48 |
+
|
| 49 |
+
if not audio_chunks:
|
| 50 |
+
print("WARNING: no audio was generated")
|
| 51 |
+
return
|
| 52 |
+
|
| 53 |
+
combined = np.concatenate(audio_chunks)
|
| 54 |
+
sf.write(output_path, combined, SAMPLE_RATE)
|
| 55 |
+
print(f"Saved: {output_path} ({len(combined) / SAMPLE_RATE:.1f}s)")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
input_text = sys.argv[1] if len(sys.argv) > 1 else "Hallo, hier spricht Thorsten."
|
| 60 |
+
output_file = sys.argv[2] if len(sys.argv) > 2 else "output.wav"
|
| 61 |
+
synthesize(input_text, output_file)
|