Skip to content

armgit / bygga

Public Imported from GitHub
0

IKEA assembly, step by step: animated, manual-style 3D guides. Scan any paper manual with Together AI.

TypeScript
1.3 KB
Raw Blame
import { NAMES as SPELLED } from "./names";

/**
 * The hands-free voice, on Together AI: one model and voice for the
 * pre-recorded clips (scripts/voice/record.ts) and for lines recorded live
 * (/api/voice), so a step never mixes two voices.
 */
export const TTS = {
  model: "hexgrad/Kokoro-82M",
  voice: "af_heart",
  name: "Heart",
  label: "Kokoro on Together AI",
  usdPer1K: 0.004,
};

const NAMES = Object.entries(SPELLED);

/** The line as the voice should read it: product names get their phonetic spelling (scripts/voice/names.ts). */
export function forTTS(text: string) {
  let t = text;
  for (const [name, ipa] of NAMES) if (t.includes(name)) t = t.replace(new RegExp(`(?<!\\p{L})${name}(?!\\p{L})`, "gu"), `[${name}](/${ipa}/)`);
  return t;
}

/** Renders one line to mp3: on the person's own key live, on the recording script's key for the clips. */
export async function speak(text: string, key: string) {
  const r = await fetch("https://api.together.ai/v1/audio/speech", {
    method: "POST",
    headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" },
    body: JSON.stringify({ model: TTS.model, voice: TTS.voice, input: text, language: "en", response_format: "mp3", stream: false }),
  });
  if (!r.ok) throw new Error(`together tts ${r.status}: ${(await r.text()).slice(0, 200)}`);
  return r.arrayBuffer();
}