jaison2611 commited on
Commit
d0ec19e
·
verified ·
1 Parent(s): 81f4b51

Create progress_callback.js

Browse files
Files changed (1) hide show
  1. progress_callback.js +45 -0
progress_callback.js ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script type="module">
2
+ const progressContainer = document.getElementById('progress-container');
3
+ const progressBar = document.getElementById('progress-bar');
4
+ const progressText = document.getElementById('progress-text');
5
+ const output = document.getElementById('output');
6
+ const upload = document.getElementById('upload');
7
+ const preview = document.getElementById('preview');
8
+ const btn = document.getElementById('process-btn');
9
+
10
+ let classifier;
11
+
12
+ async function init() {
13
+ // Show the progress UI
14
+ progressContainer.style.display = 'block';
15
+
16
+ classifier = await window.Transformers.pipeline('image-classification', 'Xenova/vit-gpt2-image-captioning', {
17
+ device: 'webgpu',
18
+ // This function runs every time a chunk of the model is downloaded
19
+ progress_callback: (data) => {
20
+ if (data.status === 'progress') {
21
+ const pct = Math.round(data.progress);
22
+ progressBar.style.width = pct + '%';
23
+ progressText.textContent = `Downloading ${data.file}: ${pct}%`;
24
+ } else if (data.status === 'ready') {
25
+ progressText.textContent = "Model Ready!";
26
+ setTimeout(() => progressContainer.style.display = 'none', 2000);
27
+ }
28
+ }
29
+ });
30
+ }
31
+
32
+ // Rest of your logic...
33
+ upload.onchange = (e) => {
34
+ const file = e.target.files[0];
35
+ preview.src = URL.createObjectURL(file);
36
+ preview.style.display = 'block';
37
+ };
38
+
39
+ btn.onclick = async () => {
40
+ if (!classifier) await init();
41
+ output.textContent = "Thinking...";
42
+ const result = await classifier(preview.src);
43
+ output.textContent = "Result: " + result[0].label;
44
+ };
45
+ </script>