File size: 1,505 Bytes
0a46626
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""Quickstart against a running server (`tt serve changh95/Kokoro-82M`); standard library only.

    python3 examples/quickstart.py [--url http://127.0.0.1:20000] [--voice af_heart] [--text "..."]
"""
import argparse
import json
import urllib.request

ap = argparse.ArgumentParser()
ap.add_argument("--url", default="http://127.0.0.1:20000")
ap.add_argument("--voice", default="af_heart")
ap.add_argument("--text", default="Hello from a Tenstorrent p150. Kokoro speaks fifty four voices in nine languages.")
a = ap.parse_args()


def post(path, body):
    req = urllib.request.Request(a.url + path, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=300) as r:
        return r.read(), dict(r.headers)


# OpenAI-compatible route -> hello.wav
wav, h = post("/v1/audio/speech", {"model": "kokoro", "input": a.text, "voice": a.voice, "response_format": "wav"})
open("hello.wav", "wb").write(wav)
print("hello.wav", h.get("X-Audio-Seconds"), "s, timings", h.get("X-Timing-Ms"))
# native route: a weighted voice blend, slower speech, word timestamps (JSON body)
body, _ = post("/tts", {"text": "Two voices, blended.", "voice": "af_heart:0.6,am_michael:0.4", "speed": 0.9,
                        "timestamps": True, "json": True, "seed": 7})
d = json.loads(body)
print("blend:", d["seconds"], "s, words:", [(w["word"], w["start"], w["end"]) for w in d["chunks"][0]["words"]])