jaison2611 commited on
Commit
edda442
·
verified ·
1 Parent(s): 97854a8

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +81 -32
index.html CHANGED
@@ -1,32 +1,81 @@
1
- <div class="container">
2
- <h1>WebGPU AI Studio</h1>
3
- <p>Private AI processing on your local GPU.</p>
4
-
5
- <div id="progress-container" style="display: none; margin-bottom: 20px;">
6
- <div style="width: 100%; background: #333; height: 12px; border-radius: 6px; overflow: hidden;">
7
- <div id="progress-bar"
8
- role="progressbar"
9
- aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"
10
- style="width: 0%; background: linear-gradient(90deg, #00ffcc, #00aba9); height: 100%; transition: width 0.3s ease;">
11
- </div>
12
- </div>
13
- <div id="progress-text" style="font-size: 0.85em; color: #888; margin-top: 8px;">Initializing...</div>
14
- </div>
15
-
16
- <div style="margin-bottom: 20px;">
17
- <input type="file" id="upload" accept="image/*" style="display: none;">
18
- <label for="upload" style="background: #333; padding: 10px 15px; border-radius: 5px; cursor: pointer; border: 1px dashed #666;">
19
- 📁 Choose an Image
20
- </label>
21
- </div>
22
-
23
- <button id="process-btn" style="background: #00ffcc; border: none; padding: 12px 24px; border-radius: 8px; cursor: pointer; font-weight: bold; color: #121212; width: 100%; max-width: 300px;">
24
- Analyze Image
25
- </button>
26
-
27
- <div id="output" style="margin-top: 25px; font-size: 1.1em; min-height: 1.5em; color: #00ffcc;">
28
- Ready to analyze.
29
- </div>
30
-
31
- <img id="preview" style="max-width: 100%; margin-top: 20px; border-radius: 12px; border: 2px solid #333; display: none;">
32
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.1';
2
+
3
+ // 1. Select UI Elements
4
+ const progressContainer = document.getElementById('progress-container');
5
+ const progressBar = document.getElementById('progress-bar');
6
+ const progressText = document.getElementById('progress-text');
7
+ const output = document.getElementById('output');
8
+ const upload = document.getElementById('upload');
9
+ const preview = document.getElementById('preview');
10
+ const btn = document.getElementById('process-btn');
11
+
12
+ let classifier = null;
13
+
14
+ // 2. Initialize Model Function
15
+ async function initModel() {
16
+ try {
17
+ progressContainer.style.display = 'block';
18
+ output.textContent = "Loading AI engine...";
19
+
20
+ classifier = await pipeline('image-classification', 'Xenova/vit-gpt2-image-captioning', {
21
+ device: 'webgpu', // Forces local GPU usage
22
+ progress_callback: (data) => {
23
+ if (data.status === 'progress') {
24
+ const pct = Math.round(data.progress);
25
+ progressBar.style.width = pct + '%';
26
+ progressBar.setAttribute('aria-valuenow', pct);
27
+ progressText.textContent = `Downloading ${data.file}: ${pct}%`;
28
+ } else if (data.status === 'ready') {
29
+ progressText.textContent = "Model Ready!";
30
+ setTimeout(() => progressContainer.style.display = 'none', 1500);
31
+ }
32
+ }
33
+ });
34
+ return true;
35
+ } catch (err) {
36
+ console.error("WebGPU Initialization Error:", err);
37
+ output.textContent = "WebGPU Error: " + err.message;
38
+ progressText.textContent = "Try using Chrome or Edge with Hardware Acceleration ON.";
39
+ return false;
40
+ }
41
+ }
42
+
43
+ // 3. Handle Analysis
44
+ btn.onclick = async () => {
45
+ if (!upload.files[0]) {
46
+ output.textContent = "⚠️ Please select an image first.";
47
+ return;
48
+ }
49
+
50
+ // Load model if it's the first time clicking
51
+ if (!classifier) {
52
+ const ready = await initModel();
53
+ if (!ready) return;
54
+ }
55
+
56
+ try {
57
+ btn.disabled = true;
58
+ btn.textContent = "Processing...";
59
+ output.textContent = "Thinking...";
60
+
61
+ const result = await classifier(preview.src);
62
+
63
+ output.innerHTML = `<strong>Result:</strong> ${result[0].label}`;
64
+ } catch (err) {
65
+ output.textContent = "Analysis failed: " + err.message;
66
+ } finally {
67
+ btn.disabled = false;
68
+ btn.textContent = "Analyze Image";
69
+ }
70
+ };
71
+
72
+ // 4. Handle Image Preview
73
+ upload.onchange = (e) => {
74
+ const file = e.target.files[0];
75
+ if (file) {
76
+ const url = URL.createObjectURL(file);
77
+ preview.src = url;
78
+ preview.style.display = 'block';
79
+ output.textContent = "Image ready. Click Analyze.";
80
+ }
81
+ };