jaison2611 commited on
Commit
e2a73ce
·
verified ·
1 Parent(s): fe461c7

Update progress_callback.js

Browse files
Files changed (1) hide show
  1. progress_callback.js +21 -25
progress_callback.js CHANGED
@@ -1,12 +1,16 @@
1
- // We use the @huggingface/transformers (v3+) for better mobile & WebGPU support
 
2
  import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
3
 
4
- // --- THE FIX: MANDATORY CONFIG FOR SPACES ---
 
 
 
5
  env.allowRemoteModels = true;
6
  env.allowLocalModels = false;
7
- // This tells the library exactly how to build the URL to avoid the 401/Unauthorized error
8
- env.remotePathTemplate = '{model}/resolve/{revision}/';
9
- // ---------------------------------------------
10
 
11
  const output = document.getElementById('output');
12
  const btn = document.getElementById('process-btn');
@@ -19,41 +23,39 @@ let captioner = null;
19
  async function init() {
20
  try {
21
  progressContainer.style.display = 'block';
22
- output.textContent = "Connecting to AI Hub...";
23
 
24
- // Force 'wasm' for best stability on mobile unless WebGPU is 100% verified
25
- const device = navigator.gpu ? 'webgpu' : 'wasm';
 
26
 
27
- captioner = await pipeline('image-to-text', 'Xenova/blip-image-captioning-base', {
28
  device: device,
29
  progress_callback: (data) => {
30
  if (data.status === 'progress') {
31
  const pct = Math.round(data.progress);
32
  progressBar.style.width = pct + '%';
33
  output.textContent = `Downloading AI: ${pct}%`;
34
- } else if (data.status === 'ready') {
35
- output.textContent = "Analyzing...";
36
  }
37
  }
38
  });
39
 
40
  await runAnalysis();
41
  } catch (e) {
42
- // If the fetch still fails, we try one more fallback to the generic CDN
43
- output.innerHTML = `<span style="color:red">Security Block: ${e.message}</span><br>Try clearing browser data.`;
44
  btn.disabled = false;
45
  }
46
  }
47
 
48
  async function runAnalysis() {
49
- if (!captioner) return;
50
  try {
51
- btn.disabled = true;
52
  const result = await captioner(preview.src);
53
  output.innerHTML = `<strong>AI Result:</strong> ${result[0].generated_text}`;
54
  progressContainer.style.display = 'none';
55
  } catch (e) {
56
- output.textContent = "Analysis error: " + e.message;
57
  } finally {
58
  btn.disabled = false;
59
  btn.textContent = "Analyze Again";
@@ -65,15 +67,9 @@ btn.onclick = async () => {
65
  alert("Upload a photo first!");
66
  return;
67
  }
68
-
69
- if (!captioner) {
70
- if (confirm("Download ~90MB AI model?")) {
71
- btn.disabled = true;
72
- await init();
73
- }
74
- } else {
75
- await runAnalysis();
76
- }
77
  };
78
 
79
  document.getElementById('upload').onchange = (e) => {
 
1
+
2
+ // Upgrade to the official v3 library for 2026 stability
3
  import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
4
 
5
+ /**
6
+ * MANDATORY SECURITY BYPASS FOR HF SPACES
7
+ * This forces the browser to treat the request as a standard web fetch
8
+ */
9
  env.allowRemoteModels = true;
10
  env.allowLocalModels = false;
11
+
12
+ // This specific template bypasses the 401 Unauthorized error by using the direct resolve path
13
+ env.remotePathTemplate = '{model}/resolve/main/';
14
 
15
  const output = document.getElementById('output');
16
  const btn = document.getElementById('process-btn');
 
23
  async function init() {
24
  try {
25
  progressContainer.style.display = 'block';
26
+ output.textContent = "Bypassing security filters...";
27
 
28
+ // Force WASM for mobile; WebGPU often fails permission checks in iframes
29
+ const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
30
+ const device = (navigator.gpu && !isMobile) ? 'webgpu' : 'wasm';
31
 
32
+ captioner = await pipeline('image-to-text', 'onnx-community/blip-image-captioning-base', {
33
  device: device,
34
  progress_callback: (data) => {
35
  if (data.status === 'progress') {
36
  const pct = Math.round(data.progress);
37
  progressBar.style.width = pct + '%';
38
  output.textContent = `Downloading AI: ${pct}%`;
 
 
39
  }
40
  }
41
  });
42
 
43
  await runAnalysis();
44
  } catch (e) {
45
+ console.error(e);
46
+ output.innerHTML = `<b style="color:red">Security Block:</b> ${e.message}<br>Try opening in <b>Incognito Mode</b>.`;
47
  btn.disabled = false;
48
  }
49
  }
50
 
51
  async function runAnalysis() {
 
52
  try {
53
+ btn.textContent = "Analyzing...";
54
  const result = await captioner(preview.src);
55
  output.innerHTML = `<strong>AI Result:</strong> ${result[0].generated_text}`;
56
  progressContainer.style.display = 'none';
57
  } catch (e) {
58
+ output.textContent = "Error: " + e.message;
59
  } finally {
60
  btn.disabled = false;
61
  btn.textContent = "Analyze Again";
 
67
  alert("Upload a photo first!");
68
  return;
69
  }
70
+ btn.disabled = true;
71
+ if (!captioner) await init();
72
+ else await runAnalysis();
 
 
 
 
 
 
73
  };
74
 
75
  document.getElementById('upload').onchange = (e) => {