@spader/node-whisper-cpp 0.0.1 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +12 -0
- package/dist/index.js +23 -0
- package/dist/loader.d.ts +2 -0
- package/dist/loader.js +29 -0
- package/dist/platform.d.ts +17 -0
- package/dist/platform.js +79 -0
- package/dist/types.d.ts +26 -0
- package/dist/types.js +1 -0
- package/package.json +54 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ContextOptions, Segment, TranscribeOptions } from "./types.js";
|
|
2
|
+
export type { ContextOptions, Segment, TranscribeOptions };
|
|
3
|
+
export * as platform from "./platform.js";
|
|
4
|
+
export declare function version(): string;
|
|
5
|
+
export declare function systemInfo(): string;
|
|
6
|
+
export declare function createContext(options: ContextOptions): Context;
|
|
7
|
+
export declare class Context {
|
|
8
|
+
private readonly native;
|
|
9
|
+
constructor(options: ContextOptions);
|
|
10
|
+
transcribe(options: TranscribeOptions): Promise<Segment[]>;
|
|
11
|
+
free(): void;
|
|
12
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { loadAddon } from "./loader.js";
|
|
2
|
+
export * as platform from "./platform.js";
|
|
3
|
+
export function version() {
|
|
4
|
+
return loadAddon().version();
|
|
5
|
+
}
|
|
6
|
+
export function systemInfo() {
|
|
7
|
+
return loadAddon().systemInfo();
|
|
8
|
+
}
|
|
9
|
+
export function createContext(options) {
|
|
10
|
+
return new Context(options);
|
|
11
|
+
}
|
|
12
|
+
export class Context {
|
|
13
|
+
native;
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.native = new (loadAddon().WhisperContext)(options);
|
|
16
|
+
}
|
|
17
|
+
transcribe(options) {
|
|
18
|
+
return this.native.transcribe(options);
|
|
19
|
+
}
|
|
20
|
+
free() {
|
|
21
|
+
this.native.free();
|
|
22
|
+
}
|
|
23
|
+
}
|
package/dist/loader.d.ts
ADDED
package/dist/loader.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { detect } from "./platform.js";
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const currentDir = dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
function resolvePlatformPackageDir(name) {
|
|
8
|
+
try {
|
|
9
|
+
const entry = require.resolve(name);
|
|
10
|
+
return join(dirname(entry), "..");
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
const prefix = "@spader/node-whisper-cpp-";
|
|
14
|
+
return join(currentDir, "..", "packages", "platform", name.slice(prefix.length));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
let store = {
|
|
18
|
+
addon: null
|
|
19
|
+
};
|
|
20
|
+
export function loadAddon() {
|
|
21
|
+
if (store.addon != null)
|
|
22
|
+
return store.addon;
|
|
23
|
+
const triple = detect();
|
|
24
|
+
const name = `@spader/node-whisper-cpp-${triple}`;
|
|
25
|
+
const dir = resolvePlatformPackageDir(name);
|
|
26
|
+
const addonPath = join(dir, "bins", "whisper-addon.node");
|
|
27
|
+
store.addon = require(addonPath);
|
|
28
|
+
return store.addon;
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type Os = "darwin" | "linux";
|
|
2
|
+
export type Architecture = "arm64" | "x64";
|
|
3
|
+
export type Backend = "metal" | "cpu" | "cuda" | "vulkan";
|
|
4
|
+
export type Libc = "gnu" | "musl" | "apple";
|
|
5
|
+
export type Target = {
|
|
6
|
+
os: Os;
|
|
7
|
+
arch: Architecture;
|
|
8
|
+
backend: Backend;
|
|
9
|
+
libc: Libc;
|
|
10
|
+
};
|
|
11
|
+
declare const getPlatformId: (target: Target) => string;
|
|
12
|
+
declare const getPackageName: (target: Target) => string;
|
|
13
|
+
declare function detect(): string;
|
|
14
|
+
declare function detectTarget(): Target;
|
|
15
|
+
declare function resolve(backend?: Backend): string;
|
|
16
|
+
declare function resolveTarget(backend?: Backend): Target;
|
|
17
|
+
export { detect, detectTarget, resolve, resolveTarget, getPlatformId, getPackageName };
|
package/dist/platform.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
|
+
import { platform as os, arch } from "node:process";
|
|
3
|
+
function detectLibc() {
|
|
4
|
+
if (os == "darwin") {
|
|
5
|
+
return "apple";
|
|
6
|
+
}
|
|
7
|
+
else if (os == "linux") {
|
|
8
|
+
try {
|
|
9
|
+
const result = execSync("ldd --version 2>&1", { encoding: "utf8" });
|
|
10
|
+
return result.includes("musl") ? "musl" : "gnu";
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
return e.stdout?.includes("musl") ? "musl" : "gnu";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
throw new Error(`Unsupported OS: ${os}`);
|
|
17
|
+
}
|
|
18
|
+
const VALID_BACKENDS = ["metal", "cpu", "cuda", "vulkan"];
|
|
19
|
+
function detectBackend() {
|
|
20
|
+
const env = process.env.NODE_WHISPER_CPP_BACKEND;
|
|
21
|
+
if (env) {
|
|
22
|
+
if (!VALID_BACKENDS.includes(env)) {
|
|
23
|
+
throw new Error(`Invalid NODE_WHISPER_CPP_BACKEND: ${env} (expected one of ${VALID_BACKENDS.join(", ")})`);
|
|
24
|
+
}
|
|
25
|
+
return env;
|
|
26
|
+
}
|
|
27
|
+
if (os == "darwin") {
|
|
28
|
+
return "metal";
|
|
29
|
+
}
|
|
30
|
+
else if (os == "linux") {
|
|
31
|
+
try {
|
|
32
|
+
execSync("nvidia-smi", { stdio: "ignore" });
|
|
33
|
+
return "cuda";
|
|
34
|
+
}
|
|
35
|
+
catch { }
|
|
36
|
+
try {
|
|
37
|
+
execSync("vulkaninfo", { stdio: "ignore" });
|
|
38
|
+
return "vulkan";
|
|
39
|
+
}
|
|
40
|
+
catch { }
|
|
41
|
+
return "cpu";
|
|
42
|
+
}
|
|
43
|
+
throw new Error(`Unsupported OS: ${os}`);
|
|
44
|
+
}
|
|
45
|
+
const getPlatformId = (target) => {
|
|
46
|
+
const parts = [target.arch, target.os, target.backend];
|
|
47
|
+
if (os === "linux") {
|
|
48
|
+
parts.push(target.libc);
|
|
49
|
+
}
|
|
50
|
+
return parts.join("-");
|
|
51
|
+
};
|
|
52
|
+
const getPackageName = (target) => {
|
|
53
|
+
return `node-whisper-cpp-${getPlatformId(target)}`;
|
|
54
|
+
};
|
|
55
|
+
function detect() {
|
|
56
|
+
const target = detectTarget();
|
|
57
|
+
return getPlatformId(target);
|
|
58
|
+
}
|
|
59
|
+
function detectTarget() {
|
|
60
|
+
if (os !== "linux" && os !== "darwin") {
|
|
61
|
+
throw new Error(`Unsupported OS: ${os}`);
|
|
62
|
+
}
|
|
63
|
+
if (arch !== "x64" && arch !== "arm64") {
|
|
64
|
+
throw new Error(`Unsupported architecture: ${arch}`);
|
|
65
|
+
}
|
|
66
|
+
return { os, arch, backend: detectBackend(), libc: detectLibc() };
|
|
67
|
+
}
|
|
68
|
+
function resolve(backend) {
|
|
69
|
+
const target = resolveTarget(backend);
|
|
70
|
+
return getPlatformId(target);
|
|
71
|
+
}
|
|
72
|
+
function resolveTarget(backend) {
|
|
73
|
+
const detected = detectTarget();
|
|
74
|
+
return {
|
|
75
|
+
...detected,
|
|
76
|
+
backend: backend ?? detected.backend,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export { detect, detectTarget, resolve, resolveTarget, getPlatformId, getPackageName };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface ContextOptions {
|
|
2
|
+
model: string;
|
|
3
|
+
use_gpu?: boolean;
|
|
4
|
+
flash_attn?: boolean;
|
|
5
|
+
gpu_device?: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Segment {
|
|
8
|
+
t0: number;
|
|
9
|
+
t1: number;
|
|
10
|
+
text: string;
|
|
11
|
+
}
|
|
12
|
+
export interface TranscribeOptions {
|
|
13
|
+
pcm: Float32Array;
|
|
14
|
+
language?: string;
|
|
15
|
+
threads?: number;
|
|
16
|
+
onSegment?: (segment: Segment) => void;
|
|
17
|
+
}
|
|
18
|
+
export interface NativeContext {
|
|
19
|
+
transcribe(options: TranscribeOptions): Promise<Segment[]>;
|
|
20
|
+
free(): void;
|
|
21
|
+
}
|
|
22
|
+
export interface NativeAddon {
|
|
23
|
+
WhisperContext: new (options: ContextOptions) => NativeContext;
|
|
24
|
+
version(): string;
|
|
25
|
+
systemInfo(): string;
|
|
26
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spader/node-whisper-cpp",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.8",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"packageManager": "bun@1.3.9",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"imports": {
|
|
16
|
+
"#tools/build": "./tools/build.ts"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/",
|
|
20
|
+
"packages/platform/arm64-darwin-metal/",
|
|
21
|
+
"packages/platform/arm64-darwin-cpu/",
|
|
22
|
+
"packages/platform/x64-linux-cpu-gnu/",
|
|
23
|
+
"packages/platform/x64-linux-cuda-gnu/",
|
|
24
|
+
"package.json"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "bun run ./tools/build.ts all",
|
|
28
|
+
"install:ci": "bun run ./tools/ci.ts install",
|
|
29
|
+
"test": "bun test ./test/smoke.test.ts",
|
|
30
|
+
"clean": "bun run ./tools/clean.ts"
|
|
31
|
+
},
|
|
32
|
+
"optionalDependencies": {
|
|
33
|
+
"@spader/node-whisper-cpp-arm64-darwin-metal": "0.0.8",
|
|
34
|
+
"@spader/node-whisper-cpp-arm64-darwin-cpu": "0.0.8",
|
|
35
|
+
"@spader/node-whisper-cpp-x64-linux-cpu-gnu": "0.0.8",
|
|
36
|
+
"@spader/node-whisper-cpp-x64-linux-cuda-gnu": "0.0.8"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/tspader/node-whisper-cpp"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"node-addon-api": "^8.3.1"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@clack/prompts": "^1.0.0",
|
|
50
|
+
"@types/bun": "^1.2.21",
|
|
51
|
+
"@types/node": "^24.3.0",
|
|
52
|
+
"@types/yargs": "^17.0.35",
|
|
53
|
+
"cmake-js": "^7.3.0",
|
|
54
|
+
"typescript": "^5.9.3",
|
|
55
|
+
"yargs": "^18.0.0"
|
|
56
|
+
}
|
|
6
57
|
}
|