Overview

What I am building and thinking, in one place.

Featured tool
Recent snippetsAll snippets →
python
import numpy as np
from sklearn.cluster import SpectralClustering


def estimate_speakers(embeddings: np.ndarray, max_k: int = 8):
    # L2-normalize so the dot product is cosine similarity.
    x = embeddings / np.linalg.norm(embeddings, axis=1, keepdims=True)
    sim = x @ x.T

    # CLAMP negatives to 0. Do NOT rescale [-1, 1] to [0, 1]:
    # rescaling maps near-orthogonal (different-speaker) pairs at ~0
    # up to 0.5, inflating cross-speaker edges and washing out the
    # eigengap. Clamping leaves different-speaker pairs near 0.
    affinity = np.clip(sim, 0.0, 1.0)
    np.fill_diagonal(affinity, 1.0)

    # Normalized (symmetric) graph Laplacian: L = I - D^-1/2 A D^-1/2.
    deg = affinity.sum(axis=1)
    d_inv_sqrt = 1.0 / np.sqrt(np.maximum(deg, 1e-12))
    lap = np.eye(affinity.shape[0]) - (
        affinity * d_inv_sqrt[:, None] * d_inv_sqrt[None, :]
    )

    eigvals = np.sort(np.linalg.eigvalsh(lap))
    upper = min(max_k, len(eigvals) - 1)
    gaps = np.diff(eigvals[: upper + 1])
    k = int(np.argmax(gaps)) + 1

    if k < 2:
        return 1, np.zeros(affinity.shape[0], dtype=int)

    labels = SpectralClustering(
        n_clusters=k, affinity="precomputed", random_state=0
    ).fit_predict(affinity)
    return k, labels
ts
import { execFile } from "node:child_process";
import { promisify } from "node:util";

const run = promisify(execFile);

interface SshTarget {
  keyPath: string;
  user: string;
  host: string;
}

export async function dokku(t: SshTarget, args: string[]): Promise<string> {
  const { stdout } = await run(
    "ssh",
    [
      "-i",
      t.keyPath,
      "-o",
      "StrictHostKeyChecking=accept-new",
      `${t.user}@${t.host}`,
      "dokku",
      ...args,
    ],
    { maxBuffer: 5 * 1024 * 1024, timeout: 60_000 },
  );
  return stdout;
}

// Want structured data out of a REPL? Make the REPL emit JSON.
export async function mongoJson(uri: string, expr: string): Promise<unknown> {
  const { stdout } = await run(
    "mongosh",
    [uri, "--quiet", "--eval", `JSON.stringify(${expr})`],
    { maxBuffer: 5 * 1024 * 1024, timeout: 60_000 },
  );
  return JSON.parse(stdout);
}
ts
import sodium from "libsodium-wrappers";

interface PublicKey {
  key: string; // base64
  key_id: string;
}

export async function setSecret(opts: {
  owner: string;
  repo: string;
  name: string;
  value: string;
  token: string;
}): Promise<void> {
  const api = `https://api.github.com/repos/${opts.owner}/${opts.repo}/actions/secrets`;
  const headers = {
    Authorization: `Bearer ${opts.token}`,
    Accept: "application/vnd.github+json",
    "X-GitHub-Api-Version": "2022-11-28",
  };

  const keyRes = await fetch(`${api}/public-key`, { headers });
  if (!keyRes.ok) throw new Error(`public-key: ${keyRes.status}`);
  const pk = (await keyRes.json()) as PublicKey;

  await sodium.ready;
  const sealed = sodium.crypto_box_seal(
    sodium.from_string(opts.value),
    sodium.from_base64(pk.key, sodium.base64_variants.ORIGINAL),
  );
  const encrypted_value = sodium.to_base64(
    sealed,
    sodium.base64_variants.ORIGINAL,
  );

  const putRes = await fetch(`${api}/${opts.name}`, {
    method: "PUT",
    headers: { ...headers, "Content-Type": "application/json" },
    body: JSON.stringify({ encrypted_value, key_id: pk.key_id }),
  });
  if (!putRes.ok && putRes.status !== 201 && putRes.status !== 204) {
    throw new Error(`put secret: ${putRes.status}`);
  }
}
Now
now runningwhisper_scheduleopen