jaison2611 commited on
Commit
f94b069
·
verified ·
1 Parent(s): 67a5156

Update progress_callback.js

Browse files
Files changed (1) hide show
  1. progress_callback.js +33 -63
progress_callback.js CHANGED
@@ -1,89 +1,59 @@
1
  import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
2
 
3
- /**
4
- * 🛡️ THE HARDENED SECURITY BYPASS
5
- * This configuration forces a direct, non-authenticated fetch.
6
- */
7
  env.allowRemoteModels = true;
8
  env.allowLocalModels = false;
9
- // Using the "onnx" folder specifically as it has the best CORS headers for 2026
10
- env.remotePathTemplate = '{model}/resolve/main/onnx/';
11
 
12
  const output = document.getElementById('output');
13
  const btn = document.getElementById('process-btn');
14
  const preview = document.getElementById('preview');
15
- const progressContainer = document.getElementById('progress-container');
16
- const progressBar = document.getElementById('progress-bar');
17
 
18
- let captioner = null;
19
 
20
  async function init() {
21
  try {
22
- progressContainer.style.display = 'block';
23
- output.textContent = "Attempting Secure Tunnel...";
24
-
25
- // Force WASM. WebGPU permission blocks often trigger 401 errors on mobile.
26
- const device = 'wasm';
27
-
28
- // Model: vit-gpt2 (~140MB quantized) - The safest choice for < 200MB.
29
- captioner = await pipeline('image-to-text', 'onnx-community/vit-gpt2-image-captioning', {
 
 
 
 
 
 
 
30
  device: device,
31
- dtype: 'q8',
32
- progress_callback: (data) => {
33
- if (data.status === 'progress') {
34
- const pct = Math.round(data.progress);
35
- progressBar.style.width = pct + '%';
36
- output.textContent = `Downloading: ${pct}%`;
37
  }
38
  }
39
  });
40
 
41
- await runAnalysis();
42
  } catch (e) {
43
- console.error("Critical Error:", e);
44
- // If it fails, we show a direct link button
45
- output.innerHTML = `
46
- <b style="color:red">Security Blocked by Browser.</b><br>
47
- <p style="font-size:0.8em">Mobile Chrome/Safari is blocking the AI download inside this frame.</p>
48
- <button onclick="window.open(window.location.href, '_blank')"
49
- style="background:#ff4b4b; color:white; padding:10px; border:none; border-radius:5px; margin-top:10px;">
50
- Open in Full Window (Fixes Error)
51
- </button>
52
- `;
53
  }
54
  }
55
 
56
- async function runAnalysis() {
57
- try {
58
- btn.textContent = "Processing...";
59
- const result = await captioner(preview.src, { max_new_tokens: 30 });
60
- output.innerHTML = `<strong>Result:</strong> ${result[0].generated_text}`;
61
- progressContainer.style.display = 'none';
62
- } catch (e) {
63
- output.textContent = "Error: " + e.message;
64
- } finally {
65
- btn.disabled = false;
66
- btn.textContent = "Analyze Again";
67
- }
68
  }
69
 
70
- btn.onclick = async () => {
71
- if (!preview.src || preview.style.display === 'none') {
72
- alert("Please upload a photo first!");
73
- return;
74
- }
75
  btn.disabled = true;
76
- if (!captioner) await init();
77
- else await runAnalysis();
78
  };
79
 
80
- document.getElementById('upload').onchange = (e) => {
81
- const file = e.target.files[0];
82
- const reader = new FileReader();
83
- reader.onload = (ev) => {
84
- preview.src = ev.target.result;
85
- preview.style.display = 'block';
86
- output.textContent = "Image ready.";
87
- };
88
- reader.readAsDataURL(file);
89
- };
 
1
  import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
2
 
3
+ // --- SECURITY BYPASS ---
 
 
 
4
  env.allowRemoteModels = true;
5
  env.allowLocalModels = false;
6
+ env.remotePathTemplate = '{model}/resolve/main/';
 
7
 
8
  const output = document.getElementById('output');
9
  const btn = document.getElementById('process-btn');
10
  const preview = document.getElementById('preview');
 
 
11
 
12
+ let pipe = null;
13
 
14
  async function init() {
15
  try {
16
+ output.textContent = "Initializing WebGPU Compatibility Mode...";
17
+
18
+ // NEW for 2026: Attempt to request an adapter with compatibility fallback
19
+ let adapter = null;
20
+ if (navigator.gpu) {
21
+ adapter = await navigator.gpu.requestAdapter({
22
+ featureLevel: "compatibility" // Essential for mobile stability
23
+ });
24
+ }
25
+
26
+ const device = adapter ? 'webgpu' : 'wasm';
27
+ output.textContent = `Loading Model on ${device.toUpperCase()}...`;
28
+
29
+ // Switch to a lighter, more modern community export
30
+ pipe = await pipeline('image-to-text', 'onnx-community/vit-gpt2-image-captioning', {
31
  device: device,
32
+ dtype: 'q8', // Strictly 8-bit to stay < 200MB
33
+ progress_callback: (d) => {
34
+ if (d.status === 'progress') {
35
+ document.getElementById('progress-bar').style.width = d.progress + '%';
 
 
36
  }
37
  }
38
  });
39
 
40
+ analyze();
41
  } catch (e) {
42
+ output.innerHTML = `<span style="color:red">WebGPU Blocked: ${e.message}</span><br>
43
+ <button onclick="window.open(window.location.href, '_blank')">Run in Full Window</button>`;
 
 
 
 
 
 
 
 
44
  }
45
  }
46
 
47
+ async function analyze() {
48
+ const result = await pipe(preview.src, { max_new_tokens: 20 });
49
+ output.textContent = result[0].generated_text;
50
+ btn.disabled = false;
 
 
 
 
 
 
 
 
51
  }
52
 
53
+ btn.onclick = () => {
 
 
 
 
54
  btn.disabled = true;
55
+ if (!pipe) init();
56
+ else analyze();
57
  };
58
 
59
+ // ... include your standard file upload logic here ...