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

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +178 -81
index.html CHANGED
@@ -1,81 +1,178 @@
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
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>WebGPU AI Studio</title>
7
+ <script src="https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.1"></script>
8
+ <style>
9
+ :root {
10
+ --primary: #00ffcc;
11
+ --bg: #121212;
12
+ --card: #1e1e1e;
13
+ --text: #ffffff;
14
+ }
15
+
16
+ body {
17
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
18
+ background-color: var(--bg);
19
+ color: var(--text);
20
+ margin: 0;
21
+ display: flex;
22
+ justify-content: center;
23
+ align-items: center;
24
+ min-height: 100vh;
25
+ }
26
+
27
+ .container {
28
+ width: 100%;
29
+ max-width: 500px;
30
+ background: var(--card);
31
+ padding: 40px;
32
+ border-radius: 20px;
33
+ box-shadow: 0 10px 30px rgba(0,0,0,0.5);
34
+ text-align: center;
35
+ }
36
+
37
+ h1 {
38
+ margin-top: 0;
39
+ color: var(--primary);
40
+ font-size: 1.8rem;
41
+ }
42
+
43
+ p {
44
+ color: #888;
45
+ font-size: 0.9rem;
46
+ margin-bottom: 30px;
47
+ }
48
+
49
+ /* Progress Bar Styling */
50
+ #progress-container {
51
+ margin-bottom: 25px;
52
+ text-align: left;
53
+ }
54
+
55
+ .progress-bg {
56
+ width: 100%;
57
+ background: #333;
58
+ height: 12px;
59
+ border-radius: 6px;
60
+ overflow: hidden;
61
+ }
62
+
63
+ #progress-bar {
64
+ width: 0%;
65
+ background: linear-gradient(90deg, var(--primary), #00aba9);
66
+ height: 100%;
67
+ transition: width 0.3s ease;
68
+ }
69
+
70
+ #progress-text {
71
+ font-size: 0.75rem;
72
+ color: #aaa;
73
+ margin-top: 8px;
74
+ }
75
+
76
+ /* File Upload Styling */
77
+ .upload-section {
78
+ margin-bottom: 25px;
79
+ }
80
+
81
+ .custom-file-upload {
82
+ display: inline-block;
83
+ padding: 12px 20px;
84
+ cursor: pointer;
85
+ background: #2a2a2a;
86
+ border: 1px dashed #555;
87
+ border-radius: 10px;
88
+ transition: all 0.2s;
89
+ }
90
+
91
+ .custom-file-upload:hover {
92
+ border-color: var(--primary);
93
+ background: #333;
94
+ }
95
+
96
+ input[type="file"] {
97
+ display: none;
98
+ }
99
+
100
+ /* Button Styling */
101
+ #process-btn {
102
+ background: var(--primary);
103
+ color: #121212;
104
+ border: none;
105
+ padding: 15px 30px;
106
+ border-radius: 12px;
107
+ font-weight: bold;
108
+ font-size: 1rem;
109
+ cursor: pointer;
110
+ width: 100%;
111
+ transition: transform 0.1s, opacity 0.2s;
112
+ }
113
+
114
+ #process-btn:active {
115
+ transform: scale(0.98);
116
+ }
117
+
118
+ #process-btn:disabled {
119
+ opacity: 0.5;
120
+ cursor: not-allowed;
121
+ }
122
+
123
+ /* Output & Image Preview */
124
+ #output {
125
+ margin-top: 30px;
126
+ padding: 15px;
127
+ border-radius: 10px;
128
+ background: rgba(0, 255, 204, 0.05);
129
+ border: 1px solid rgba(0, 255, 204, 0.1);
130
+ min-height: 20px;
131
+ font-style: italic;
132
+ }
133
+
134
+ #preview {
135
+ max-width: 100%;
136
+ margin-top: 25px;
137
+ border-radius: 15px;
138
+ border: 2px solid #333;
139
+ display: none;
140
+ }
141
+
142
+ .status-ready {
143
+ color: var(--primary);
144
+ font-weight: bold;
145
+ }
146
+ </style>
147
+ </head>
148
+ <body>
149
+
150
+ <div class="container">
151
+ <h1>WebGPU AI Studio</h1>
152
+ <p>Run private AI analysis on your local GPU.</p>
153
+
154
+ <div id="progress-container" style="display: none;">
155
+ <div class="progress-bg">
156
+ <div id="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
157
+ </div>
158
+ <div id="progress-text">Initializing AI models...</div>
159
+ </div>
160
+
161
+ <div class="upload-section">
162
+ <label for="upload" class="custom-file-upload">
163
+ 📁 Select Image to Analyze
164
+ </label>
165
+ <input type="file" id="upload" accept="image/*">
166
+ </div>
167
+
168
+ <button id="process-btn">Analyze Image</button>
169
+
170
+ <div id="output">Ready for input.</div>
171
+
172
+ <img id="preview" alt="Image Preview">
173
+ </div>
174
+
175
+ <script type="module" src="progress_callback.js"></script>
176
+
177
+ </body>
178
+ </html>