Download src/clip/transcript.ts from abc1181/Whisper: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/spaces/abc1181/Whisper/resolve/main/src/clip/transcript.ts
- Command line
-
hf download hf://spaces/abc1181/Whisper/src/clip/transcript.ts
-
curl -L -o transcript.ts https://huggingface.co/spaces/abc1181/Whisper/resolve/main/src/clip/transcript.ts
1.96 kB
| export interface TranscriptWord { | |
| word: string; | |
| start: number; | |
| end: number; | |
| } | |
| export interface Transcript { | |
| words: TranscriptWord[]; | |
| text: string; | |
| /** Detected language, when the transcriber reports it. */ | |
| language?: string; | |
| } | |
| /** The narration inside a window, with normal spacing. */ | |
| export function textForRange(words: TranscriptWord[], start: number, end: number): string { | |
| return words | |
| .filter((w) => w.end > start && w.start < end) | |
| .map((w) => w.word.trim()) | |
| .filter(Boolean) | |
| .join(" ") | |
| .replace(/\s+([,.!?;:])/g, "$1"); | |
| } | |
| /** | |
| * Round a cut point to the nearest word boundary. | |
| * | |
| * A cut in the middle of a word is the most obvious sign that a clip was | |
| * produced by a machine: the first syllable of the first word is missing, or | |
| * the last one is cut off. `mode: "start"` moves forward to the next word | |
| * start, `mode: "end"` moves back to the previous word end, and both fall back | |
| * to the original time when there is no word within the tolerance. | |
| */ | |
| export function snapToWord( | |
| words: TranscriptWord[], | |
| time: number, | |
| mode: "start" | "end", | |
| tolerance = 1.5, | |
| ): number { | |
| if (words.length === 0) return time; | |
| let best = time; | |
| let bestDistance = Number.POSITIVE_INFINITY; | |
| for (const word of words) { | |
| const point = mode === "start" ? word.start : word.end; | |
| const distance = Math.abs(point - time); | |
| if (distance > tolerance) continue; | |
| const acceptable = mode === "start" ? point >= time - 0.15 : point <= time + 0.15; | |
| if (!acceptable) continue; | |
| if (distance < bestDistance) { | |
| best = point; | |
| bestDistance = distance; | |
| } | |
| } | |
| return best; | |
| } | |
| /** Words per second of actual speech, used to rank fallback windows. */ | |
| export function speechDensity(words: TranscriptWord[], start: number, end: number): number { | |
| if (end <= start) return 0; | |
| const inside = words.filter((w) => w.start >= start && w.end <= end); | |
| return inside.length / (end - start); | |
| } | |