Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,19 @@ The plugin form mirrors the URL namespace: `c0mpute.com/transcode`,
### CPU speech inference

TTS (`tts.synthesize`) supports Kokoro and Piper on CPU. STT
(`stt.transcribe`) uses multilingual faster-whisper `base` with CPU int8. Install the
models once on each worker, then start it with the role it should advertise:
(`stt.transcribe`) uses multilingual faster-whisper `base` with CPU int8. A normal
worker start installs both speech engines and STT on first run, then advertises
both roles automatically:

```sh
c0mpute tts setup --engine all
c0mpute stt setup
c0mpute worker start --roles tts,stt
c0mpute worker start
```

Pass `--roles` to choose an explicit role set. For example,
`c0mpute worker start --roles storage` skips speech setup. Speech inference
limits ONNX and Whisper CPU threads to the node's container quota when one is
available.

The Kokoro model files are Apache-2.0. Piper's runtime is GPL-3.0 and its
individual voice files carry their own licenses; the bundled Joe voice is
CC0 and Norman is public domain. faster-whisper is MIT. Agent voice presets
Expand Down
22 changes: 22 additions & 0 deletions node/crates/c0mpute-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,34 @@ async fn run_worker(cmd: WorkerCmd, config_path: &std::path::Path) -> Result<()>
daemon: _,
} => {
let mut cfg = Config::load_or_default(config_path)?;
let default_speech_roles = roles.is_none();
if let Some(rs) = roles {
cfg.roles = rs.iter().filter_map(|s| parse_role(s)).collect();
} else {
for role in [Role::Tts, Role::Stt] {
if !cfg.roles.contains(&role) {
cfg.roles.push(role);
}
}
}
if gpu && !cfg.roles.contains(&Role::Transcode) {
cfg.roles.push(Role::Transcode);
}
if cfg.roles.contains(&Role::Tts) {
c0mpute_core::speech::setup_tts("all")
.await
.context("install the default CPU TTS engines before starting the worker")?;
}
if cfg.roles.contains(&Role::Stt) {
c0mpute_core::speech::setup_stt()
.await
.context("install the default CPU STT model before starting the worker")?;
}
if default_speech_roles {
eprintln!(
"TTS and STT are enabled by default; pass --roles to choose a specific role set"
);
}
// Only gateway workers run the Infernet provider bootstrap. A
// speech-only or storage-only worker should not start an unrelated
// inference agent as a side effect of joining c0mpute.
Expand Down
8 changes: 7 additions & 1 deletion node/crates/c0mpute-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ impl Default for Config {
api: ApiConfig::default(),
storage: StorageConfig::default(),
gateway: GatewayConfig::default(),
roles: vec![Role::Storage, Role::Gateway, Role::Verifier],
roles: vec![
Role::Storage,
Role::Gateway,
Role::Verifier,
Role::Tts,
Role::Stt,
],
update_channel: default_update_channel(),
update_auto: default_auto_update(),
update_interval_secs: default_update_interval_secs(),
Expand Down
29 changes: 25 additions & 4 deletions node/crates/c0mpute-core/src/speech.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,32 @@ pub fn available_tts_engines() -> Vec<&'static str> {
engines
}

fn piper_ready() -> bool {
PIPER_VOICES
.iter()
.all(|voice| tts_voice_available("piper", voice))
}

fn kokoro_ready() -> bool {
KOKORO_VOICES
.iter()
.all(|voice| tts_voice_available("kokoro", voice))
}

pub async fn setup_tts(engine: &str) -> Result<()> {
if engine != "piper" && engine != "kokoro" && engine != "all" {
bail!("engine must be piper, kokoro, or all");
}
let install_piper = (engine == "piper" || engine == "all") && !piper_ready();
let install_kokoro = (engine == "kokoro" || engine == "all") && !kokoro_ready();
if !install_piper && !install_kokoro {
return Ok(());
}

ensure_venv().await?;
let root = speech_root();
tokio::fs::create_dir_all(root.join("models/piper")).await?;
tokio::fs::create_dir_all(root.join("models/kokoro")).await?;
if engine == "piper" || engine == "all" {
if install_piper {
tokio::fs::create_dir_all(root.join("models/piper")).await?;
pip_install(&["piper-tts==1.8.0"]).await?;
let mut command = Command::new(python_path());
command
Expand All @@ -101,7 +118,8 @@ pub async fn setup_tts(engine: &str) -> Result<()> {
run_checked(command, "download Piper voices").await?;
tokio::fs::write(root.join(".piper-ready"), b"piper-tts 1.8.0\n").await?;
}
if engine == "kokoro" || engine == "all" {
if install_kokoro {
tokio::fs::create_dir_all(root.join("models/kokoro")).await?;
pip_install(&["kokoro-onnx==0.6.1", "soundfile==0.13.1"]).await?;
download_verified(
KOKORO_MODEL_URL,
Expand All @@ -121,6 +139,9 @@ pub async fn setup_tts(engine: &str) -> Result<()> {
}

pub async fn setup_stt() -> Result<()> {
if stt_ready() {
return Ok(());
}
ensure_venv().await?;
// faster-whisper 1.2.1 still passes `metadata_errors` to av.open;
// PyAV 19 removed that keyword, so keep the runtime on the compatible API.
Expand Down
4 changes: 2 additions & 2 deletions node/crates/c0mpute-core/src/speech_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ async fn run_speech_auction(
.with_required_capabilities(required)
.with_max_price_usd(state.max_price_usd)
.with_bid_window(Duration::from_secs(4))
.with_deadline(Duration::from_secs(5 * 60))
.with_receipt_timeout(Duration::from_secs(4 * 60));
.with_deadline(Duration::from_secs(20 * 60))
.with_receipt_timeout(Duration::from_secs(15 * 60));
run_auction(state.net.clone(), auction)
.await
.map_err(|error| {
Expand Down
71 changes: 68 additions & 3 deletions node/crates/c0mpute-core/src/speech_worker.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,90 @@
"""Small CPU speech worker embedded into the c0mpute binary."""
import json
import math
import os
import sys
import wave
from pathlib import Path


def cpu_thread_count() -> int:
configured = os.environ.get("C0MPUTE_SPEECH_CPU_THREADS")
if configured:
try:
return max(1, int(configured))
except ValueError:
pass

# Docker and most Linux services expose their CPU quota through cgroups.
# Respect it because os.cpu_count() can report the whole host, which makes
# ONNX Runtime create far too many threads inside a small container.
for quota_path, period_path in [
(Path("/sys/fs/cgroup/cpu.max"), None),
(Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us"), Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us")),
(Path("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us"), Path("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us")),
]:
try:
if period_path is None:
quota, period = quota_path.read_text().split()
if quota == "max":
continue
quota_us, period_us = int(quota), int(period)
else:
quota_us = int(quota_path.read_text())
period_us = int(period_path.read_text())
if quota_us > 0 and period_us > 0:
return max(1, math.ceil(quota_us / period_us))
except (OSError, ValueError):
continue

try:
return max(1, len(os.sched_getaffinity(0)))
except AttributeError:
return max(1, os.cpu_count() or 1)


def session_options():
import onnxruntime as ort

options = ort.SessionOptions()
options.intra_op_num_threads = cpu_thread_count()
options.inter_op_num_threads = 1
return options


def tts(root: Path, output: Path, request: dict) -> None:
text = request["text"]
engine = request["engine"]
voice = request["voice"]
if engine == "piper":
from piper import PiperVoice
import onnxruntime as ort
from piper.config import PiperConfig
from piper.voice import PiperVoice

model = root / "models" / "piper" / f"{voice}.onnx"
piper_voice = PiperVoice.load(model)
config_path = model.with_suffix(model.suffix + ".json")
piper_config = PiperConfig.from_dict(json.loads(config_path.read_text(encoding="utf-8")))
session = ort.InferenceSession(
str(model), sess_options=session_options(), providers=["CPUExecutionProvider"]
)
piper_voice = PiperVoice(session=session, config=piper_config)
with wave.open(str(output), "wb") as wav_file:
piper_voice.synthesize_wav(text, wav_file)
return
if engine == "kokoro":
import soundfile as sf
import onnxruntime as ort
import kokoro_onnx
from kokoro_onnx import Kokoro

# kokoro-onnx does not expose ONNX SessionOptions publicly. Replace
# its small session factory so CPU jobs use the same quota-aware pool.
kokoro_onnx.create_session = lambda path: ort.InferenceSession(
path,
sess_options=session_options(),
providers=["CPUExecutionProvider"],
)

model_root = root / "models" / "kokoro"
kokoro = Kokoro(str(model_root / "kokoro-v1.0.int8.onnx"), str(model_root / "voices-v1.0.bin"))
try:
Expand All @@ -41,10 +105,11 @@ def stt(root: Path, audio: Path, output: Path, request: dict) -> None:
model_name = request.get("model", "base")
model = WhisperModel(
model_name, device="cpu", compute_type="int8",
cpu_threads=cpu_thread_count(), num_workers=1,
download_root=str(root / "models" / "whisper"),
)
segments, info = model.transcribe(
str(audio), beam_size=5,
str(audio), beam_size=1,
language=request.get("language") or None,
vad_filter=True,
)
Expand Down
Loading