Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/avenkatesh8001_takeback.json
T

1 line
7.6 KiB
JSON

{"repo": "avenkatesh8001/takeback", "files": {"predictive-mute-extension/intent-detector.js": {"50955dcbffcc7a86dc914786887b9325c3a6af92": {"t": 1785595787.5309112, "n": 7335, "h": "92805f94f4462e0f", "c": "// ONNX-based Intent Detection for Predictive Muting\n// Uses fine-tuned DeBERTa model running locally in browser via onnxruntime-web\n// Detects PII leak-intent BEFORE sensitive words are spoken\n\n(function() {\n 'use strict';\n\n // Import onnxruntime-web and transformers.js from CDN\n const ONNX_RUNTIME_CDN = 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.16.3/dist/ort.min.js';\n const TRANSFORMERS_CDN = 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.11.0';\n\n class IntentDetector {\n constructor() {\n this.session = null;\n this.tokenizer = null;\n this.ready = false;\n this.loading = false;\n this.lastInferenceTime = 0;\n this.inferenceThrottle = 300; // Only run every 300ms\n this.ort = null; // ONNX Runtime\n }\n\n async initialize() {\n if (this.ready || this.loading) return;\n\n this.loading = true;\n console.log('[IntentDetector] Initializing ONNX Runtime and Transformers.js...');\n\n try {\n // Load ONNX Runtime and Transformers.js in parallel\n await Promise.all([\n this.loadScript('onnxruntime', ONNX_RUNTIME_CDN),\n this.loadScript('transformers', TRANSFORMERS_CDN)\n ]);\n\n this.ort = window.ort;\n const { AutoTokenizer } = window.Transformers;\n\n // Configure ONNX Runtime for performance\n this.ort.env.wasm.numThreads = 2;\n this.ort.env.wasm.simd = true;\n\n // Load model and tokenizer files\n const modelUrl = 'https://huggingface.co/pavidu/piiranha-v1-detect-personal-information-onnx/resolve/main/model.onnx';\n const tokenizerUrl = 'iiiorg/piiranha-v1-detect-personal-information';\n\n console.log('[IntentDetector] Loading model and tokenizer...');\n\n // Load tokenizer and create inference session in parallel\n const [tokenizer, session] = await Promise.all([\n AutoTokenizer.from_pretrained(tokenizerUrl),\n this.ort.InferenceSession.create(modelUrl, {\n executionProviders: ['wasm'],\n graphOptimizationLevel: 'all',\n enableCpuMemArena: true,\n enableMemPattern: true\n })\n ]);\n\n this.tokenizer = tokenizer;\n this.session = session;\n this.ready = true;\n this.loading = false;\n\n const inputNames = this.session.inputNames;\n const outputNames = this.session.outputNames;\n\n console.log('[IntentDetector] ✓ Model loaded successfully');\n console.log('[IntentDetector] Inputs:', inputNames);\n console.log('[IntentDetector] Outputs:', outputNames);\n console.log('[IntentDetector] Tokenizer:', tokenizer.constructor.name);\n\n } catch (error) {\n console.error('[IntentDetector] Failed to load model:', error);\n this.loading = false;\n this.ready = false;\n throw error;\n }\n }\n\n async loadScript(id, src) {\n return new Promise((resolve, reject) => {\n if (document.getElementById(id)) {\n resolve();\n return;\n }\n const script = document.createElement('script');\n script.id = id;\n script.src = src;\n script.onload = resolve;\n script.onerror = reject;\n document.head.appendChild(script);\n });\n }\n\n // Tokenizer using transformers.js\n async tokenize(text) {\n if (!this.tokenizer) {\n throw new Error('Tokenizer not initialized');\n }\n return this.tokenizer(text, {\n padding: 'max_length',\n truncation: true,\n max_length: 256,\n return_tensors: 'ort'\n });\n }\n\n // Run inference on text\n async predict(text) {\n if (!this.ready) {\n console.warn('[IntentDetector] Model not ready, skipping inference');\n return null;\n }\n\n // Throttle inference to avoid performance issues\n const now = Date.now();\n if (now - this.lastInferenceTime < this.inferenceThrottle) {\n return null; // Skip this inference\n }\n this.lastInferenceTime = now;\n\n try {\n const startTime = performance.now();\n\n // Tokenize input\n const model_inputs = await this.tokenize(text);\n\n // Run inference\n const results = await this.session.run(model_inputs);\n const logits = results.logits;\n \n const predictions = [];\n for (let i = 0; i < logits.dims[1]; ++i) {\n const tokenLogits = Array.from(logits.data.slice(i * logits.dims[2], (i + 1) * logits.dims[2]));\n const labelIndex = tokenLogits.indexOf(Math.max(...tokenLogits));\n predictions.push(this.tokenizer.model.config.id2label[labelIndex]);\n }\n\n const latency = performance.now() - startTime;\n\n const hasPII = predictions.some(p => p !== 'O');\n const result = {\n hasPII: hasPII,\n predictions: predictions,\n latency: latency,\n label: hasPII ? 'LEAK_INTENT' : 'SAFE',\n leak: hasPII ? 1 : 0,\n };\n\n console.log(\n `[IntentDetector] \"${text.substring(0, 40)}...\" → ${result.label} ` +\n `(${(result.leak * 100).toFixed(1)}%) [${latency.toFixed(1)}ms]`\n );\n\n return result;\n\n } catch (error) {\n console.error('[IntentDetector] Inference error:', error);\n return null;\n }\n }\n\n // Softmax function\n softmax(logits) {\n const maxLogit = Math.max(...logits);\n const expValues = logits.map(l => Math.exp(l - maxLogit));\n const sumExp = expValues.reduce((a, b) => a + b);\n return expValues.map(e => e / sumExp);\n }\n\n // Check if should mute based on prediction\n shouldMute(prediction, text, threshold = 0.7) {\n if (!prediction) return false;\n\n // Basic check for leak intent\n if (prediction.label !== 'LEAK_INTENT') return false;\n\n // More sophisticated heuristics\n const textLength = text.length;\n let dynamicThreshold = threshold;\n\n // Lower the threshold for longer text, as we are more confident\n if (textLength > 50) {\n dynamicThreshold = threshold * 0.8;\n } else if (textLength > 100) {\n dynamicThreshold = threshold * 0.6;\n }\n\n // Check for specific PII types that should always be muted\n const highConfidencePII = ['CREDIT_CARD_NUMBER', 'SOCIAL_SECURITY_NUMBER', 'PASSWORD', 'API_KEY'];\n const hasHighConfidencePII = prediction.predictions.some(p => highConfidencePII.includes(p));\n\n if (hasHighConfidencePII) {\n return true;\n }\n\n return prediction.leak >= dynamicThreshold;\n }\n\n // Get status for debugging\n getStatus() {\n return {\n ready: this.ready,\n loading: this.loading,\n modelLoaded: this.session !== null,\n tokenizerLoaded: this.tokenizer !== null\n };\n }\n }\n\n // Export singleton instance\n window.IntentDetector = IntentDetector;\n\n // Create global instance\n window.intentDetector = new IntentDetector();\n\n // Auto-load model (async, non-blocking)\n window.intentDetector.initialize().then(() => {\n console.log('[IntentDetector] Ready for predictions');\n }).catch(err => {\n console.warn('[IntentDetector] Auto-load failed:', err);\n console.warn('[IntentDetector] Will fall back to keyword-only detection');\n });\n\n console.log('[IntentDetector] Module loaded');\n\n})();"}}}}