@masteryhub-its/speakout-local-client-model 0.0.1

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.
@@ -0,0 +1,35 @@
1
+ export declare const ONNX_CONFIG: {
2
+ EXECUTION_PROVIDER_WASM: string;
3
+ GRAPH_OPTIMIZATION_LEVEL: string;
4
+ TENSOR_TYPE_INT64: string;
5
+ DEFAULT_PROVIDER: string;
6
+ WASM_NUM_THREADS: number;
7
+ };
8
+ export declare const MODEL_PATH: string;
9
+ export declare const TOKENIZER_PATH: string;
10
+ export declare const DEFAULTS: {
11
+ THRESHOLD: number;
12
+ MAX_LENGTH: number;
13
+ };
14
+ export declare const ERROR_MESSAGES: {
15
+ ONNX_RUNTIME_NOT_AVAILABLE: string;
16
+ TOKENIZER_INIT_FAILED: string;
17
+ SESSION_NOT_INITIALIZED: string;
18
+ TOKENIZER_NOT_INITIALIZED: string;
19
+ };
20
+ export declare const MODEL_NAME = "asafaya/bert-mini-arabic";
21
+ export declare const MAX_LENGTH = 128;
22
+ export declare const NUM_LABELS = 2;
23
+ export declare const SPECIAL_TOKENS: {
24
+ PAD: string;
25
+ CLS: string;
26
+ SEP: string;
27
+ UNK: string;
28
+ SUBWORD_PREFIX: string;
29
+ };
30
+ export declare const DEFAULT_TOKEN_IDS: {
31
+ PAD: number;
32
+ UNK: number;
33
+ CLS: number;
34
+ SEP: number;
35
+ };
@@ -0,0 +1,59 @@
1
+ export const ONNX_CONFIG = {
2
+ EXECUTION_PROVIDER_WASM: "wasm",
3
+ GRAPH_OPTIMIZATION_LEVEL: "all",
4
+ TENSOR_TYPE_INT64: "int64",
5
+ DEFAULT_PROVIDER: "cpu",
6
+ WASM_NUM_THREADS: 1,
7
+ };
8
+ // Model file paths - resolved at runtime relative to package location
9
+ const packageName = '@masteryhub-its/speakout-local-client-model';
10
+ function getModelPath(filename) {
11
+ const basePath = `/node_modules/${packageName}/models/bert-mini-moderation-output/${filename}`;
12
+ if (typeof window !== 'undefined' && window.location) {
13
+ return new URL(basePath, window.location.origin).toString();
14
+ }
15
+ try {
16
+ if (typeof import.meta !== 'undefined' && import.meta.url) {
17
+ const currentUrl = new URL(import.meta.url);
18
+ const pathname = currentUrl.pathname;
19
+ const packageIndex = pathname.indexOf(packageName);
20
+ if (packageIndex !== -1) {
21
+ const packageBasePath = pathname.substring(0, packageIndex + packageName.length);
22
+ const packageBaseUrl = new URL(packageBasePath + '/', currentUrl.origin);
23
+ return new URL(`models/bert-mini-moderation-output/${filename}`, packageBaseUrl).toString();
24
+ }
25
+ }
26
+ }
27
+ catch {
28
+ // Fall through to return basePath
29
+ }
30
+ return basePath;
31
+ }
32
+ export const MODEL_PATH = getModelPath("model.int8.onnx");
33
+ export const TOKENIZER_PATH = getModelPath("tokenizer.json");
34
+ export const DEFAULTS = {
35
+ THRESHOLD: 0.5,
36
+ MAX_LENGTH: 128,
37
+ };
38
+ export const ERROR_MESSAGES = {
39
+ ONNX_RUNTIME_NOT_AVAILABLE: "ONNX Runtime not available. Please ensure onnxruntime-web is properly installed.",
40
+ TOKENIZER_INIT_FAILED: "Failed to initialize tokenizer. Please provide valid tokenizer URL (e.g., '/models/tokenizer.json').",
41
+ SESSION_NOT_INITIALIZED: "Model session is not initialized. Please call initialize() first.",
42
+ TOKENIZER_NOT_INITIALIZED: "Tokenizer is not initialized. Please call initialize() first.",
43
+ };
44
+ export const MODEL_NAME = "asafaya/bert-mini-arabic";
45
+ export const MAX_LENGTH = 128;
46
+ export const NUM_LABELS = 2;
47
+ export const SPECIAL_TOKENS = {
48
+ PAD: "[PAD]",
49
+ CLS: "[CLS]",
50
+ SEP: "[SEP]",
51
+ UNK: "[UNK]",
52
+ SUBWORD_PREFIX: "##",
53
+ };
54
+ export const DEFAULT_TOKEN_IDS = {
55
+ PAD: 0,
56
+ UNK: 1,
57
+ CLS: 2,
58
+ SEP: 3,
59
+ };
@@ -0,0 +1,9 @@
1
+ export interface PathResolutionResult {
2
+ url: string;
3
+ method: 'relative' | 'node_modules' | 'extracted' | 'cached';
4
+ index: number;
5
+ }
6
+ export declare function setDebugLogging(enabled: boolean): void;
7
+ export declare function resolveModelPath(fileName: string, expectedType: 'json' | 'binary'): Promise<PathResolutionResult>;
8
+ export declare function clearPathCache(): void;
9
+ export declare function getCachedPaths(): Map<string, string>;
@@ -0,0 +1,94 @@
1
+ const PACKAGE_NAME = '@reham_h/speakout-local-client-model';
2
+ const MODEL_SUBPATH = 'bert-mini-moderation-output';
3
+ const pathCache = new Map();
4
+ let debugEnabled = false;
5
+ export function setDebugLogging(enabled) {
6
+ debugEnabled = enabled;
7
+ }
8
+ function debugLog(message, ...args) {
9
+ if (debugEnabled)
10
+ console.log(`[PathResolver] ${message}`, ...args);
11
+ }
12
+ function buildPossibleUrls(fileName) {
13
+ const baseUrl = import.meta.url;
14
+ const urls = [];
15
+ for (let depth = 1; depth <= 6; depth++) {
16
+ try {
17
+ urls.push(new URL('../'.repeat(depth) + `models/${MODEL_SUBPATH}/${fileName}`, baseUrl).toString());
18
+ }
19
+ catch { }
20
+ }
21
+ try {
22
+ const baseUrlObj = new URL(baseUrl);
23
+ const origin = baseUrlObj.origin;
24
+ urls.push(new URL(`/node_modules/${PACKAGE_NAME}/models/${MODEL_SUBPATH}/${fileName}`, origin).toString());
25
+ const urlPath = baseUrlObj.pathname;
26
+ if (urlPath.includes('node_modules')) {
27
+ const parts = urlPath.substring(urlPath.indexOf('node_modules')).split('/').filter(Boolean);
28
+ if (parts.length >= 3 && parts[0] === 'node_modules') {
29
+ urls.push(new URL(`/${parts[0]}/${parts[1]}/${parts[2]}/models/${MODEL_SUBPATH}/${fileName}`, origin).toString());
30
+ }
31
+ }
32
+ }
33
+ catch { }
34
+ return urls;
35
+ }
36
+ async function verifyFileContent(url, expectedType) {
37
+ try {
38
+ const response = await fetch(url);
39
+ if (!response.ok)
40
+ return false;
41
+ const contentType = response.headers.get('content-type') || '';
42
+ if (expectedType === 'json') {
43
+ if (!contentType.includes('application/json') && !contentType.includes('text/json')) {
44
+ if (contentType.includes('text/html'))
45
+ return false;
46
+ const text = await response.clone().text();
47
+ const lower = text.trim().toLowerCase();
48
+ if (lower.startsWith('<!doctype') || lower.startsWith('<html'))
49
+ return false;
50
+ }
51
+ return true;
52
+ }
53
+ else {
54
+ if (contentType.includes('text/html'))
55
+ return false;
56
+ const bytes = new Uint8Array((await response.clone().arrayBuffer()).slice(0, 100));
57
+ return !(bytes.length > 0 && bytes[0] === 0x3C);
58
+ }
59
+ }
60
+ catch {
61
+ return false;
62
+ }
63
+ }
64
+ export async function resolveModelPath(fileName, expectedType) {
65
+ const cacheKey = `${fileName}:${expectedType}`;
66
+ const cached = pathCache.get(cacheKey);
67
+ if (cached) {
68
+ debugLog(`Using cached path for ${fileName}: ${cached}`);
69
+ return { url: cached, method: 'cached', index: -1 };
70
+ }
71
+ const possibleUrls = buildPossibleUrls(fileName);
72
+ debugLog(`Resolving path for ${fileName}, trying ${possibleUrls.length} possible URLs`);
73
+ for (let i = 0; i < possibleUrls.length; i++) {
74
+ const url = possibleUrls[i];
75
+ debugLog(` [${i + 1}/${possibleUrls.length}] Trying: ${url}`);
76
+ if (await verifyFileContent(url, expectedType)) {
77
+ pathCache.set(cacheKey, url);
78
+ const method = url.includes('/node_modules/')
79
+ ? (url.includes(PACKAGE_NAME) ? 'node_modules' : 'extracted')
80
+ : 'relative';
81
+ debugLog(`✓ Successfully resolved ${fileName} to: ${url} (method: ${method})`);
82
+ return { url, method, index: i };
83
+ }
84
+ }
85
+ throw new Error(`Failed to resolve path for ${fileName}. Tried ${possibleUrls.length} different paths. ` +
86
+ `Last attempted: ${possibleUrls[possibleUrls.length - 1] || 'none'}`);
87
+ }
88
+ export function clearPathCache() {
89
+ pathCache.clear();
90
+ debugLog('Path cache cleared');
91
+ }
92
+ export function getCachedPaths() {
93
+ return new Map(pathCache);
94
+ }