jaison2611 commited on
Commit
26a2425
·
verified ·
1 Parent(s): 9b81179

Update progress_callback.js

Browse files
Files changed (1) hide show
  1. progress_callback.js +82 -24
progress_callback.js CHANGED
@@ -1,31 +1,89 @@
1
- // --- FIX: Bulletproof Image Upload ---
2
- upload.onchange = (e) => {
3
- const file = e.target.files[0];
4
- if (!file) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- // Show a loading state in the output
7
- output.textContent = "Loading image...";
 
 
 
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  const reader = new FileReader();
10
-
11
- reader.onload = (event) => {
12
- // 1. Set the preview source to the Base64 string
13
- preview.src = event.target.result;
14
-
15
- // 2. Make sure it's visible
16
  preview.style.display = 'block';
17
-
18
- // 3. Clear the output text
19
- output.textContent = "Image loaded. Ready to analyze!";
20
-
21
- console.log("Image successfully converted to Base64");
22
- };
23
-
24
- reader.onerror = (err) => {
25
- console.error("FileReader Error: ", err);
26
- output.textContent = "Error: Could not read the file.";
27
  };
28
-
29
- // This converts the image to a Data URL (Base64)
30
  reader.readAsDataURL(file);
31
  };
 
1
+ import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.1';
2
+
3
+ // Mandatory for Hugging Face Spaces to prevent "Unauthorized" errors
4
+ env.allowRemoteModels = true;
5
+ env.allowLocalModels = false;
6
+
7
+ const output = document.getElementById('output');
8
+ const btn = document.getElementById('process-btn');
9
+ const preview = document.getElementById('preview');
10
+ const progressContainer = document.getElementById('progress-container');
11
+ const progressBar = document.getElementById('progress-bar');
12
+
13
+ let captioner = null;
14
+
15
+ // Helper to update the UI
16
+ function setStatus(msg) {
17
+ output.textContent = msg;
18
+ console.log(msg);
19
+ }
20
+
21
+ async function init() {
22
+ try {
23
+ progressContainer.style.display = 'block';
24
+ setStatus("Downloading AI model...");
25
+
26
+ // BLIP is the most stable 'vision' model for mobile/web
27
+ captioner = await pipeline('image-to-text', 'Xenova/blip-image-captioning-base', {
28
+ // Mobile safe: checks if WebGPU exists, otherwise uses CPU
29
+ device: navigator.gpu ? 'webgpu' : 'wasm',
30
+ progress_callback: (data) => {
31
+ if (data.status === 'progress') {
32
+ const pct = Math.round(data.progress);
33
+ progressBar.style.width = pct + '%';
34
+ }
35
+ }
36
+ });
37
+
38
+ setStatus("Model Loaded! Analyzing...");
39
+ await runAnalysis();
40
+ } catch (e) {
41
+ setStatus("Error: " + e.message);
42
+ btn.disabled = false;
43
+ }
44
+ }
45
 
46
+ async function runAnalysis() {
47
+ if (!captioner) return;
48
+ try {
49
+ const result = await captioner(preview.src);
50
+ output.innerHTML = `<strong>Result:</strong> ${result[0].generated_text}`;
51
+ progressContainer.style.display = 'none';
52
+ } catch (e) {
53
+ setStatus("Analysis Error: " + e.message);
54
+ } finally {
55
+ btn.disabled = false;
56
+ btn.textContent = "Analyze Again";
57
+ }
58
+ }
59
 
60
+ btn.onclick = async () => {
61
+ // Check if image was uploaded
62
+ if (!preview.src || preview.style.display === 'none') {
63
+ alert("Please upload an image first!");
64
+ return;
65
+ }
66
+
67
+ btn.disabled = true;
68
+ btn.textContent = "Processing...";
69
+
70
+ if (!captioner) {
71
+ // First time click: download then run
72
+ await init();
73
+ } else {
74
+ // Second time click: just run
75
+ await runAnalysis();
76
+ }
77
+ };
78
+
79
+ // Standard upload logic
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
+ setStatus("Image ready.");
 
 
 
 
 
 
 
 
 
87
  };
 
 
88
  reader.readAsDataURL(file);
89
  };