Files
smallest-ai-autoreg/waves.py
T

108 lines
4.4 KiB
Python

#!/usr/bin/env python
"""Cookie-session client for smallest.ai Waves (TTS + instant voice cloning).
Auth: accessToken/refreshToken cookies obtained by reg.py. No API key needed -
the dashboard itself calls /waves/v1/* with cookies + X-Organization-Id.
"""
import json, os, sys
import requests
BASE = "https://api.smallest.ai"
WAVES = BASE + "/waves/v1"
UA = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36")
HERE = os.path.dirname(os.path.abspath(__file__))
class Waves:
def __init__(self, rec):
self.rec = rec
s = requests.Session()
s.headers.update({"User-Agent": UA, "Origin": "https://app.smallest.ai",
"Referer": "https://app.smallest.ai/dashboard",
"X-Organization-Id": rec["orgId"],
"Accept": "application/json, text/plain, */*"})
for k, v in rec["cookies"].items():
s.cookies.set(k, v, domain="api.smallest.ai")
self.s = s
# ---- voices
def voices(self, model="lightning-v3.1"):
return self.s.get(WAVES + "/%s/get_voices" % model, timeout=30).json().get("voices", [])
def clone_models(self):
return self.s.get(WAVES + "/voice-cloning/models", timeout=30).json()["data"]
def clones(self):
return self.s.get(WAVES + "/voice-cloning", timeout=30).json().get("data", [])
# ---- tts
def tts(self, text, voice_id="lakshya", model="lightning-v3.1",
sample_rate=24000, fmt="wav", speed=1.0):
r = self.s.post(WAVES + "/%s/get_speech" % model,
json={"text": text, "voice_id": voice_id,
"sample_rate": sample_rate, "speed": speed,
"output_format": fmt}, timeout=180)
if r.status_code != 200:
raise RuntimeError("tts %s %s" % (r.status_code, r.text[:200]))
return r.content
# ---- cloning
def upload(self, path, model="lightning-v3.1-pro", language="en"):
with open(path, "rb") as fh:
files = {"file": (os.path.basename(path), fh, "audio/wav")}
r = self.s.post(WAVES + "/voice-cloning/upload-audio",
files=files, data={"model": model, "language": language},
timeout=240)
if r.status_code != 200:
raise RuntimeError("upload %s %s" % (r.status_code, r.text[:300]))
return r.json()["data"]
def create_clone(self, voice_id, display_name, description="", accent="American",
tags=None, language="en"):
body = {"voiceId": voice_id, "displayName": display_name,
"description": description, "accent": accent,
"tags": tags or [], "language": language}
r = self.s.post(WAVES + "/voice-cloning/create", json=body, timeout=180)
if r.status_code != 200:
raise RuntimeError("clone %s %s" % (r.status_code, r.text[:300]))
return r.json()["data"]
def delete_clone(self, clone_id):
return self.s.post(WAVES + "/voice-cloning/delete", json={"id": clone_id}, timeout=60). status_code
def credits(self):
return self.s.get(BASE + "/payment/v1/credits/balance", timeout=30).json()["data"]
def load_bank():
p = os.path.join(HERE, "bank.jsonl")
return [json.loads(l) for l in open(p, encoding="utf-8")]
if __name__ == "__main__":
cmd = sys.argv[1] if len(sys.argv) > 1 else "probe"
bank = load_bank()
rec = bank[0]
w = Waves(rec)
if cmd == "probe":
print("account", rec["email"], "credits", w.credits())
print("clone models", [m["model"] for m in w.clone_models()])
print("voices", [v["voiceId"] for v in w.voices()][:10])
print("clones", [(c["voiceId"], c["displayName"], c["status"]) for c in w.clones()])
elif cmd == "tts":
audio = w.tts(sys.argv[2], voice_id=sys.argv[3] if len(sys.argv) > 3 else "lakshya")
out = "out.wav"
open(out, "wb").write(audio)
print("wrote", out, len(audio), "bytes")
elif cmd == "clone":
sample = sys.argv[2]
name = sys.argv[3] if len(sys.argv) > 3 else "Clone 1"
up = w.upload(sample)
vid = up["voiceId"]
print("uploaded", vid)
c = w.create_clone(vid, name)
print("clone", c["_id"], c["status"])
print("credits", w.credits())
else:
print("usage: waves.py probe|tts <text> [voice]|clone <sample.wav> [name]")