mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
fix(cli): isolate language detection temporary files and sync skill hashes
This commit is contained in:
@@ -323,18 +323,17 @@ async function transcribeAudio(
|
||||
const { createRenderCancellationScope, stoppedByCancelSignal } =
|
||||
await import("../utils/renderCancellation.js");
|
||||
|
||||
// Only the engine pick reads auto as no language: the whisper model choice still needs "auto" (it drops .en).
|
||||
const spoken = requestedLanguage(opts.language);
|
||||
const routingLanguage = requestedLanguage(opts.language);
|
||||
const engine = (opts.engine ?? "auto").toLowerCase();
|
||||
if (engine !== "auto" && engine !== "parakeet" && engine !== "whisper") {
|
||||
failWith(`Unknown --engine: ${opts.engine}. Use auto, parakeet, or whisper.`, !!opts.json);
|
||||
}
|
||||
const unsupported = sherpaUnsupportedReason();
|
||||
const sherpaUsable = () => !unsupported && sherpaParakeetInstalled();
|
||||
let runner = pickRunner(engine, sherpaUsable, spoken);
|
||||
let runner = pickRunner(engine, sherpaUsable, routingLanguage);
|
||||
if (engine === "parakeet" && runner === "whisper") {
|
||||
failWith(
|
||||
!parakeetSpeaks(spoken)
|
||||
!parakeetSpeaks(routingLanguage)
|
||||
? `Parakeet does not transcribe --language ${opts.language}; it covers ${PARAKEET_LANGUAGES.split(" ").join(", ")}. Use --engine whisper.`
|
||||
: (unsupported ??
|
||||
`Parakeet is not installed. Install it with: ${PARAKEET_INSTALL_COMMAND} (or use --engine whisper)`),
|
||||
@@ -398,7 +397,7 @@ async function transcribeAudio(
|
||||
const reason = normalizeErrorMessage(err).replace(/\.+$/, "");
|
||||
const parakeetError = `Parakeet failed: ${reason}. To repair it, run: ${PARAKEET_INSTALL_COMMAND}`;
|
||||
if (!parakeetFallsBack(engine)) throw new Error(parakeetError);
|
||||
runner = pickRunner(engine, () => false, spoken);
|
||||
runner = pickRunner(engine, () => false, routingLanguage);
|
||||
spin?.clear();
|
||||
console.error(c.warn(`${parakeetError}. Using ${runner} for this run.`));
|
||||
spin?.start(`Transcribing with ${label(runner)}...`);
|
||||
@@ -484,7 +483,7 @@ async function transcribeAudio(
|
||||
if (isWhisperUnavailable(err)) {
|
||||
trackTranscribeUnavailable({ optional: opts.optional === true });
|
||||
const install =
|
||||
engine === "auto" && parakeetSpeaks(spoken) && !unsupported
|
||||
engine === "auto" && parakeetSpeaks(routingLanguage) && !unsupported
|
||||
? PARAKEET_INSTALL_COMMAND
|
||||
: undefined;
|
||||
if (opts.json) {
|
||||
|
||||
@@ -3,13 +3,9 @@ export const WINDOW_SECONDS = 30;
|
||||
/** A music or noise window comes back near p = 0.5; speech in a language comes back above 0.9. */
|
||||
const CONFIDENT = 0.7;
|
||||
|
||||
/** `--language auto`, in any case, asks for detection, as no language does. */
|
||||
export const requestedLanguage = (language?: string) =>
|
||||
language?.trim().toLowerCase() === "auto" ? undefined : language;
|
||||
|
||||
/** The starts of three windows spread over the clip after `onset`, so an intro alone cannot decide; they overlap
|
||||
* when the speech is short. None under 30 s of speech, or under a minute when no onset was found (a loud or long
|
||||
* intro hides it too): whisper's own pick stands then. */
|
||||
export function detectionWindows(seconds: number, onset: number | null): number[] {
|
||||
const from = onset ?? 0;
|
||||
const span = seconds - from;
|
||||
@@ -25,13 +21,11 @@ export interface LanguageVote {
|
||||
p: number;
|
||||
}
|
||||
|
||||
/** The language at least two confident windows agree on, else null (whisper's own pick stands). */
|
||||
export function pickLanguage(votes: readonly LanguageVote[]): string | null {
|
||||
const sure = votes.filter((vote) => vote.p >= CONFIDENT).map((vote) => vote.language);
|
||||
return sure.find((language, i) => sure.indexOf(language) !== i) ?? null;
|
||||
}
|
||||
|
||||
/** A detection's language when it is sure; a wrong label is worse than none. */
|
||||
export const confidentLanguage = (vote: LanguageVote | null) =>
|
||||
vote && vote.p >= CONFIDENT ? vote.language : null;
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import { findFFmpeg } from "../browser/ffmpeg.js";
|
||||
import { downloadFile } from "../utils/download.js";
|
||||
|
||||
const MODELS_DIR = join(homedir(), ".cache", "hyperframes", "whisper", "models");
|
||||
// Multilingual: an English-only default turned every other language into English text.
|
||||
const DEFAULT_MODEL = "small";
|
||||
|
||||
export type WhisperSource = "env" | "system" | "brew" | "build";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { existsSync, mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { dirname, join } from "node:path";
|
||||
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
||||
import { transcribe, type TranscribeProgress } from "./transcribe.js";
|
||||
import { readWav } from "./wav.js";
|
||||
@@ -367,3 +367,22 @@ it("--language auto with an English-only model detects with the multilingual one
|
||||
expect(result).toMatchObject({ model: "small", detectedLanguage: "es" });
|
||||
expect(languagePassed()).toEqual(["es"]);
|
||||
});
|
||||
|
||||
it("keeps detection WAVs in a private temporary directory and removes it after a failed vote", async () => {
|
||||
musicThenSpanish();
|
||||
const directories = new Map<string, number>();
|
||||
const previous = native.exec.getMockImplementation()!;
|
||||
native.exec.mockImplementation((command: string, args: string[]) => {
|
||||
if (!args.includes("--detect-language")) return previous(command, args);
|
||||
const directory = dirname(args.at(-1)!);
|
||||
directories.set(directory, statSync(directory).mode & 0o777);
|
||||
throw new Error("detection failed");
|
||||
});
|
||||
await transcribe(join(dir, "audio.wav"), dir, { model: "small" });
|
||||
expect(directories.size).toBe(1);
|
||||
for (const [directory, permissions] of directories) {
|
||||
expect(directory).not.toBe(tmpdir());
|
||||
if (process.platform !== "win32") expect(permissions).toBe(0o700);
|
||||
expect(existsSync(directory)).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,7 +3,15 @@ import { execFile, execFileSync } from "node:child_process";
|
||||
import { once } from "node:events";
|
||||
import { createInterface } from "node:readline";
|
||||
import type { Readable } from "node:stream";
|
||||
import { existsSync, readFileSync, mkdirSync, rmSync, statSync, unlinkSync } from "node:fs";
|
||||
import {
|
||||
existsSync,
|
||||
readFileSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
unlinkSync,
|
||||
} from "node:fs";
|
||||
import { join, extname } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { randomUUID } from "node:crypto";
|
||||
@@ -643,7 +651,8 @@ async function languageFromWindows(
|
||||
const starts = clip.seconds === null ? [] : detectionWindows(clip.seconds, clip.onset);
|
||||
if (starts.length === 0) return null;
|
||||
clip.onProgress?.("Detecting the language...");
|
||||
const window = tempWavPath();
|
||||
const windowDir = mkdtempSync(join(tmpdir(), "hyperframes-language-"));
|
||||
const window = join(windowDir, "window.wav");
|
||||
const votes: LanguageVote[] = [];
|
||||
try {
|
||||
for (const start of starts) {
|
||||
@@ -671,7 +680,7 @@ async function languageFromWindows(
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
rmSync(window, { force: true });
|
||||
rmSync(windowDir, { recursive: true, force: true });
|
||||
}
|
||||
return pickLanguage(votes);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
"files": 1
|
||||
},
|
||||
"media-use": {
|
||||
"hash": "9b98931fc057f125",
|
||||
"hash": "a10025845a222ac6",
|
||||
"files": 107
|
||||
},
|
||||
"motion-graphics": {
|
||||
|
||||
Reference in New Issue
Block a user