jaison2611 commited on
Commit
39bde65
·
verified ·
1 Parent(s): 7958d6d

Update progress_callback.js

Browse files
Files changed (1) hide show
  1. progress_callback.js +13 -43
progress_callback.js CHANGED
@@ -1,29 +1,27 @@
1
  import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
2
 
3
- // 🛡️ HARDENED MEMORY & SECURITY SETTINGS
4
  env.allowRemoteModels = true;
5
  env.allowLocalModels = false;
6
  env.remotePathTemplate = '{model}/resolve/main/';
7
 
8
- // CRITICAL: Disable heavy features to save RAM
9
  env.backends.onnx.wasm.numThreads = 1;
 
10
 
11
- const chatBox = document.getElementById('chat-box');
12
- const input = document.getElementById('chat-input');
13
- const btn = document.getElementById('main-btn');
14
  const status = document.getElementById('status');
15
-
16
  let generator = null;
17
 
18
  async function init() {
19
  try {
20
  btn.disabled = true;
21
- status.textContent = "Loading Ultra-Light AI...";
22
 
23
- // Qwen2.5-0.5B-Instruct is the best "Tiny" model for 2026
24
- generator = await pipeline('text-generation', 'onnx-community/Qwen2.5-0.5B-Instruct', {
25
- device: 'wasm',
26
- dtype: 'q4', // 4-bit quantization keeps RAM usage ~160MB
27
  progress_callback: (d) => {
28
  if (d.status === 'progress') {
29
  status.textContent = `Downloading: ${Math.round(d.progress)}%`;
@@ -31,40 +29,12 @@ async function init() {
31
  }
32
  });
33
 
34
- status.textContent = "Ready! (Under 200MB)";
35
  btn.disabled = false;
36
  btn.textContent = "Send";
37
- input.disabled = false;
38
- } catch (e) {
39
- status.innerHTML = `<span style="color:red">Memory/Security Error.</span> <button onclick="window.open(window.location.href, '_blank')">Try Full Window</button>`;
40
- }
41
- }
42
-
43
- async function handleChat() {
44
- const text = input.value.trim();
45
- if (!text || btn.disabled) return;
46
-
47
- chatBox.innerHTML += `<div class="msg"><span class="user">You:</span> ${text}</div>`;
48
- input.value = "";
49
- btn.disabled = true;
50
- status.textContent = "AI thinking...";
51
-
52
- try {
53
- const output = await generator(text, {
54
- max_new_tokens: 40, // Keep responses short to prevent RAM spikes
55
- temperature: 0.6,
56
- do_sample: true
57
- });
58
-
59
- const reply = output[0].generated_text.replace(text, "").trim();
60
- chatBox.innerHTML += `<div class="msg"><span class="ai">AI:</span> ${reply}</div>`;
61
- chatBox.scrollTop = chatBox.scrollHeight;
62
  } catch (e) {
63
- status.textContent = "Crash prevented: " + e.message;
64
- } finally {
65
- btn.disabled = false;
66
- status.textContent = "Ready.";
67
  }
68
  }
69
-
70
- btn.onclick = () => { if (!generator) init(); else handleChat(); };
 
1
  import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
2
 
3
+ // 🛡️ MEMORY CRANK-DOWN
4
  env.allowRemoteModels = true;
5
  env.allowLocalModels = false;
6
  env.remotePathTemplate = '{model}/resolve/main/';
7
 
8
+ // CRITICAL: Disable multi-threading to prevent OOM (Out of Memory) crashes
9
  env.backends.onnx.wasm.numThreads = 1;
10
+ env.backends.onnx.wasm.proxy = false; // Run directly in main thread to save overhead
11
 
 
 
 
12
  const status = document.getElementById('status');
13
+ const btn = document.getElementById('main-btn');
14
  let generator = null;
15
 
16
  async function init() {
17
  try {
18
  btn.disabled = true;
19
+ status.textContent = "Loading Tiny Brain (Single-Thread)...";
20
 
21
+ // Switching to the absolute smallest stable model (~120MB)
22
+ generator = await pipeline('text-generation', 'onnx-community/phi-1_5_tiny-quantized', {
23
+ device: 'wasm',
24
+ dtype: 'q4',
25
  progress_callback: (d) => {
26
  if (d.status === 'progress') {
27
  status.textContent = `Downloading: ${Math.round(d.progress)}%`;
 
29
  }
30
  });
31
 
32
+ status.textContent = "Ready! RAM usage minimized.";
33
  btn.disabled = false;
34
  btn.textContent = "Send";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  } catch (e) {
36
+ status.innerHTML = `<b style="color:red">OOM Crash Prevented.</b><br>
37
+ <button onclick="window.open(window.location.href, '_blank')">Run Outside Iframe (Fixes Crash)</button>`;
 
 
38
  }
39
  }
40
+ // ... rest of your chat logic ...