VickyRDP commited on
Commit
379ab55
·
verified ·
1 Parent(s): c2f6821

Fix generation: remove fp16 I/O shim (config kv_cache_dtype handles it), pin transformers.js 3.8.1, refresh stale cached configs, restore WASM fallback with shader-f16 gate

Browse files
Files changed (1) hide show
  1. index.html +205 -480
index.html CHANGED
@@ -1,483 +1,208 @@
1
- <!DOCTYPE html>
2
-
3
- <html lang="en">
4
-
5
- <head>
6
-
7
- <meta charset="UTF-8">
8
-
9
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
10
-
11
- <title>GPU Mart Assistant — voice + text AI for RDP GPU Mart</title>
12
-
13
- <meta name="description" content="Ask anything about RDP GPU Mart (rdp.in/gpu-mart) — India's datacenter-grade GPU marketplace. A small fine-tuned model, running right in your browser, with voice and text.">
14
-
15
- <style>
16
-
17
- :root { --red:#d82228; --bg:#e8e8e8; --ink:#1a1a1a; --card:#ffffff; }
18
-
19
- * { box-sizing:border-box; margin:0; padding:0; }
20
-
21
- body { font-family:'Segoe UI',system-ui,-apple-system,sans-serif; background:var(--bg); color:var(--ink);
22
-
23
- display:flex; flex-direction:column; align-items:center; min-height:100vh; }
24
-
25
- header { width:100%; background:#fff; border-bottom:3px solid var(--red); padding:14px 20px; text-align:center; }
26
-
27
- header h1 { font-size:1.25rem; } header h1 span { color:var(--red); }
28
-
29
- header p { font-size:.85rem; color:#555; margin-top:2px; }
30
-
31
- #status { width:min(760px,94vw); margin:10px auto 0; font-size:.85rem; color:#444; background:#fff;
32
-
33
- border-radius:10px; padding:8px 14px; border-left:4px solid var(--red); }
34
-
35
- #bar { height:6px; background:#eee; border-radius:3px; margin-top:6px; overflow:hidden; display:none; }
36
-
37
- #bar > div { height:100%; width:0%; background:var(--red); transition:width .3s; }
38
-
39
- #chat { width:min(760px,94vw); flex:1; margin:12px auto; background:var(--card); border-radius:14px;
40
-
41
- padding:16px; overflow-y:auto; min-height:340px; max-height:56vh; box-shadow:0 1px 4px rgba(0,0,0,.08); }
42
-
43
- .msg { margin:8px 0; padding:10px 14px; border-radius:12px; max-width:85%; line-height:1.45; font-size:.95rem;
44
-
45
- white-space:pre-wrap; word-wrap:break-word; }
46
-
47
- .user { background:var(--red); color:#fff; margin-left:auto; }
48
-
49
- .bot { background:#f2f2f2; }
50
-
51
- #controls { width:min(760px,94vw); margin:0 auto 8px; display:flex; gap:8px; }
52
-
53
- #box { flex:1; padding:12px 14px; border:1px solid #ccc; border-radius:10px; font-size:1rem; outline:none; }
54
-
55
- #box:focus { border-color:var(--red); }
56
-
57
- button { border:none; border-radius:10px; padding:12px 18px; font-size:1rem; cursor:pointer; }
58
-
59
- #send { background:var(--red); color:#fff; }
60
-
61
- #mic { background:#fff; color:var(--red); border:2px solid var(--red); }
62
-
63
- #mic.listening { background:var(--red); color:#fff; animation:pulse 1.2s infinite; }
64
-
65
- @keyframes pulse { 50% { opacity:.6; } }
66
-
67
- #opts { width:min(760px,94vw); margin:0 auto 10px; display:flex; align-items:center; gap:14px; font-size:.85rem; color:#444; flex-wrap:wrap; }
68
-
69
- .chip { background:#fff; border:1px solid #ddd; border-radius:16px; padding:5px 12px; cursor:pointer; font-size:.8rem; }
70
-
71
- .chip:hover { border-color:var(--red); color:var(--red); }
72
-
73
- footer { width:100%; text-align:center; font-size:.75rem; color:#777; padding:10px; }
74
-
75
- footer a { color:var(--red); text-decoration:none; }
76
-
77
- </style>
78
-
79
- </head>
80
-
81
- <body>
82
-
83
- <header>
84
-
85
- <h1>🖥️ GPU Mart <span>Assistant</span></h1>
86
-
87
- <p>India's datacenter-grade GPU marketplace — ask by typing or speaking. Runs entirely in your browser.</p>
88
-
89
- </header>
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
- <div id="status">⏳ Loading the GPU Mart model…<div id="bar"><div></div></div></div>
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
- <div id="chat"></div>
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
- <div id="opts">
114
-
115
- <label><input type="checkbox" id="speakToggle" checked> 🔊 Speak replies</label>
116
-
117
- <span class="chip" data-q="What is GPU Mart?">What is GPU Mart?</span>
118
-
119
- <span class="chip" data-q="Explain CARINA, QUASAR and DRACO">CARINA / QUASAR / DRACO</span>
120
-
121
- <span class="chip" data-q="Price of the DRACO 8x H200 server?">DRACO 8× H200 price</span>
122
-
123
- <span class="chip" data-q="How do I get a quote?">Get a quote</span>
124
-
125
- </div>
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
- <div id="controls">
134
-
135
- <input id="box" type="text" placeholder="Ask about GPU Mart…" autocomplete="off">
136
-
137
- <button id="mic" title="Speak your question">🎤</button>
138
-
139
- <button id="send">Send</button>
140
-
141
- </div>
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
- <footer>
150
-
151
- Model: <a href="https://huggingface.co/VickyRDP/gpumart-qwen2.5-0.5b-instruct" target="_blank">gpumart fine-tune</a> ·
152
-
153
- Store: <a href="https://rdp.in/gpu-mart/" target="_blank">rdp.in/gpu-mart</a> ·
154
-
155
- Sales: +91&nbsp;720&nbsp;794&nbsp;8743
156
-
157
- </footer>
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
- <script type="module">
166
-
167
- const MODEL_ID = 'VickyRDP/gpumart-qwen2.5-0.5b-instruct';
168
-
169
- const SYSTEM = "You are the GPU Mart assistant for RDP GPU Mart (rdp.in/gpu-mart), India's datacenter-grade GPU marketplace by RDP Computers India Private Limited. Answer questions about GPU Mart products, series, pricing, policies and support accurately and concisely.";
170
-
171
-
172
-
173
-
174
-
175
- const chatEl = document.getElementById('chat');
176
-
177
- const statusEl = document.getElementById('status');
178
-
179
- const barWrap = document.getElementById('bar');
180
-
181
- const bar = barWrap.querySelector('div');
182
-
183
- const box = document.getElementById('box');
184
-
185
- const sendBtn = document.getElementById('send');
186
-
187
- const micBtn = document.getElementById('mic');
188
-
189
- const speakToggle = document.getElementById('speakToggle');
190
-
191
-
192
-
193
-
194
-
195
- const history = [];
196
-
197
- let generator = null, busy = false;
198
-
199
-
200
-
201
-
202
-
203
- function addMsg(text, cls) {
204
-
205
- const d = document.createElement('div');
206
-
207
- d.className = 'msg ' + cls;
208
-
209
- d.textContent = text;
210
-
211
- chatEl.appendChild(d);
212
-
213
- chatEl.scrollTop = chatEl.scrollHeight;
214
-
215
- return d;
216
-
217
- }
218
-
219
-
220
-
221
-
222
-
223
- function speak(text) {
224
-
225
- if (!speakToggle.checked || !window.speechSynthesis) return;
226
-
227
- speechSynthesis.cancel();
228
-
229
- const u = new SpeechSynthesisUtterance(text.slice(0, 800));
230
-
231
- u.lang = 'en-IN'; u.rate = 1.02;
232
-
233
- const vs = speechSynthesis.getVoices();
234
-
235
- u.voice = vs.find(v => v.lang === 'en-IN') || vs.find(v => v.lang.startsWith('en')) || null;
236
-
237
- speechSynthesis.speak(u);
238
-
239
- }
240
-
241
-
242
-
243
-
244
-
245
- async function loadModel() {
246
-
247
- try {
248
-
249
- const { pipeline, TextStreamer } = await import('https://cdn.jsdelivr.net/npm/@huggingface/transformers@3');
250
-
251
- window.__TextStreamer = TextStreamer;
252
-
253
- if (!navigator.gpu) {
254
-
255
- statusEl.firstChild.textContent = '🔴 GPU Mart requires WebGPU. Open this Space in the latest Chrome or Edge with WebGPU enabled.';
256
-
257
- return;
258
-
259
- }
260
-
261
- const adapter = await navigator.gpu.requestAdapter();
262
-
263
- if (!adapter) {
264
-
265
- statusEl.firstChild.textContent = '🔴 WebGPU is unavailable. Open this Space in the latest Chrome or Edge with WebGPU enabled.';
266
-
267
- return;
268
-
269
- }
270
-
271
- const device = 'webgpu';
272
-
273
- statusEl.firstChild.textContent = '⏳ Downloading the GPU Mart model (~1.2 GB, one-time, cached after) — WEBGPU mode…';
274
-
275
- barWrap.style.display = 'block';
276
-
277
- const files = {};
278
-
279
- generator = await pipeline('text-generation', MODEL_ID, {
280
-
281
- device, dtype: 'fp16',
282
-
283
- session_options: { graphOptimizationLevel: 'disabled' },
284
-
285
- progress_callback: p => {
286
-
287
- if (p.status === 'progress' && p.total) {
288
-
289
- files[p.file] = [p.loaded, p.total];
290
-
291
- let l = 0, t = 0; for (const k in files) { l += files[k][0]; t += files[k][1]; }
292
-
293
- bar.style.width = Math.round(100 * l / t) + '%';
294
-
295
- }
296
-
297
- }
298
-
299
- });
300
-
301
-
302
- // ---- fp16 I/O shim: cast float32 KV feeds -> float16, fp16 logits -> float32 ----
303
  try {
304
- const _b = new ArrayBuffer(4), _f = new Float32Array(_b), _u = new Uint32Array(_b);
305
- const f32h = v => { _f[0] = v; const x = _u[0]; const s = (x >>> 16) & 0x8000; let e = (x >>> 23) & 0xff, m = x & 0x7fffff;
306
- if (e === 0xff) return s | 0x7c00 | (m ? 0x200 : 0); let E = e - 112;
307
- if (E >= 0x1f) return s | 0x7c00;
308
- if (E <= 0) { if (E < -10) return s; m = (m | 0x800000) >>> (1 - E); if (m & 0x1000) m += 0x2000; return s | (m >>> 13); }
309
- if (m & 0x1000) { m += 0x2000; if (m & 0x800000) { m = 0; E += 1; if (E >= 0x1f) return s | 0x7c00; } }
310
- return s | (E << 10) | (m >>> 13); };
311
- const h2f = h => { const s = (h & 0x8000) << 16; let e = (h >>> 10) & 0x1f, m = h & 0x3ff;
312
- if (e === 0) { if (m === 0) { _u[0] = s; return _f[0]; } while (!(m & 0x400)) { m <<= 1; e -= 1; } e += 1; m &= ~0x400; }
313
- else if (e === 0x1f) { _u[0] = s | 0x7f800000 | (m << 13); return _f[0]; }
314
- _u[0] = s | ((e + 112) << 23) | (m << 13); return _f[0]; };
315
- const sessions = Object.values(generator.model.sessions || {});
316
- for (const sess of sessions) {
317
- const orig = sess.run.bind(sess);
318
- sess.run = async (feeds, opts) => {
319
- let Tctor = null;
320
- for (const k in feeds) { if (feeds[k] && feeds[k].constructor) { Tctor = feeds[k].constructor; break; } }
321
- for (const k in feeds) {
322
- const t2 = feeds[k];
323
- let wants16 = true;
324
- try { const md = sess.inputMetadata; if (md) { const e = Array.isArray(md) ? md.find(x=>x.name===k) : md[k]; if (e && e.type && e.type !== 'float16') wants16 = false; } } catch(e3) {}
325
- if (t2 && t2.type === 'float32' && k.startsWith('past_key_values') && wants16 && Tctor) {
326
- const src = t2.data; const dst = new Uint16Array(src.length);
327
- for (let i = 0; i < src.length; i++) dst[i] = f32h(src[i]);
328
- feeds[k] = new Tctor('float16', dst, t2.dims);
329
- }
330
- }
331
- const out = await orig(feeds, opts);
332
- try {
333
- const lg = out.logits;
334
- if (lg && lg.type === 'float16' && Tctor) {
335
- const src = lg.data; const dst = new Float32Array(src.length);
336
- for (let i = 0; i < src.length; i++) dst[i] = h2f(src[i]);
337
- out.logits = new Tctor('float32', dst, lg.dims);
338
- }
339
- } catch (e2) { console.warn('[GM] logits cast skipped', e2); }
340
- return out;
341
- };
342
  }
343
- console.log('[GM] fp16 I/O shim installed on', sessions.length, 'session(s)');
344
- } catch (e) { console.warn('[GM] fp16 shim install failed', e); }
345
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
  barWrap.style.display = 'none';
347
-
348
- statusEl.firstChild.textContent = '🟢 GPU Mart model loaded — ask away (type or tap the mic).';
349
-
350
- addMsg("Namaste! I'm the GPU Mart assistant — trained on RDP GPU Mart's catalog and policies. Ask me about products, CARINA/QUASAR/DRACO series, pricing, quotes, shipping, warranty or support.", 'bot');
351
-
352
- } catch (e) {
353
-
354
- console.error(e);
355
-
356
- statusEl.firstChild.textContent = '🔴 Could not load the model — it may still be uploading to Hugging Face. Please refresh in a few minutes. Meanwhile, visit rdp.in/gpu-mart or call +91 720 794 8743.';
357
-
358
- }
359
-
360
- }
361
-
362
-
363
-
364
-
365
-
366
- async function respond(text) {
367
-
368
- if (!text.trim() || busy) return;
369
-
370
- box.value = '';
371
-
372
- addMsg(text, 'user');
373
-
374
- if (!generator) { addMsg('The model is still loading — one moment please…', 'bot'); return; }
375
-
376
- busy = true; sendBtn.disabled = true;
377
-
378
- const botEl = addMsg('…', 'bot');
379
-
380
- try {
381
-
382
- const msgs = [{ role: 'system', content: SYSTEM }, ...history.slice(-6), { role: 'user', content: text }];
383
-
384
- let out = '';
385
-
386
- const streamer = new window.__TextStreamer(generator.tokenizer, {
387
-
388
- skip_prompt: true, skip_special_tokens: true,
389
-
390
- callback_function: t => { out += t; botEl.textContent = out; chatEl.scrollTop = chatEl.scrollHeight; }
391
-
392
- });
393
-
394
- const res = await generator(msgs, { max_new_tokens: 220, do_sample: true, temperature: 0.3, top_p: 0.9, repetition_penalty: 1.1, streamer });
395
-
396
- const full = (res[0].generated_text.at(-1).content || out).trim();
397
-
398
- botEl.textContent = full;
399
-
400
- history.push({ role: 'user', content: text }, { role: 'assistant', content: full });
401
-
402
- speak(full);
403
-
404
- } catch (e) {
405
-
406
- console.error(e);
407
-
408
- botEl.textContent = 'Sorry, something went wrong generating a reply. Please try again.';
409
-
410
- }
411
-
412
- busy = false; sendBtn.disabled = false;
413
-
414
- }
415
-
416
-
417
-
418
-
419
-
420
- sendBtn.onclick = () => respond(box.value);
421
-
422
- box.addEventListener('keydown', e => { if (e.key === 'Enter') respond(box.value); });
423
-
424
- document.querySelectorAll('.chip').forEach(c => c.onclick = () => respond(c.dataset.q));
425
-
426
-
427
-
428
-
429
-
430
- // ---- Voice input (Web Speech API) ----
431
-
432
- const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
433
-
434
- if (!SR) { micBtn.disabled = true; micBtn.title = 'Voice input not supported in this browser'; }
435
-
436
- let rec = null, listening = false;
437
-
438
- micBtn.onclick = () => {
439
-
440
- if (!SR) return;
441
-
442
- if (listening) { rec && rec.stop(); return; }
443
-
444
- rec = new SR();
445
-
446
- rec.lang = 'en-IN'; rec.interimResults = true;
447
-
448
- let finalTxt = '';
449
-
450
- rec.onresult = e => { let t = ''; for (const r of e.results) t += r[0].transcript; box.value = t;
451
-
452
- if (e.results[e.results.length - 1].isFinal) finalTxt = t; };
453
-
454
- rec.onend = () => { listening = false; micBtn.classList.remove('listening'); micBtn.textContent = '🎤';
455
-
456
- if (finalTxt.trim()) respond(finalTxt); };
457
-
458
- rec.onerror = () => { listening = false; micBtn.classList.remove('listening'); micBtn.textContent = '🎤'; };
459
-
460
- listening = true; micBtn.classList.add('listening'); micBtn.textContent = '🔴';
461
-
462
- rec.start();
463
-
464
- };
465
-
466
- if (window.speechSynthesis) speechSynthesis.getVoices();
467
-
468
-
469
-
470
-
471
-
472
- loadModel();
473
-
474
- </script>
475
-
476
- </body>
477
-
478
- </html>
479
-
480
-
481
-
482
-
483
-
 
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>GPU Mart Assistant — voice + text AI for RDP GPU Mart</title>
7
+ <meta name="description" content="Ask anything about RDP GPU Mart (rdp.in/gpu-mart) — India's datacenter-grade GPU marketplace. A small fine-tuned model, running right in your browser, with voice and text.">
8
+ <style>
9
+ :root { --red:#d82228; --bg:#e8e8e8; --ink:#1a1a1a; --card:#ffffff; }
10
+ * { box-sizing:border-box; margin:0; padding:0; }
11
+ body { font-family:'Segoe UI',system-ui,-apple-system,sans-serif; background:var(--bg); color:var(--ink);
12
+ display:flex; flex-direction:column; align-items:center; min-height:100vh; }
13
+ header { width:100%; background:#fff; border-bottom:3px solid var(--red); padding:14px 20px; text-align:center; }
14
+ header h1 { font-size:1.25rem; } header h1 span { color:var(--red); }
15
+ header p { font-size:.85rem; color:#555; margin-top:2px; }
16
+ #status { width:min(760px,94vw); margin:10px auto 0; font-size:.85rem; color:#444; background:#fff;
17
+ border-radius:10px; padding:8px 14px; border-left:4px solid var(--red); }
18
+ #bar { height:6px; background:#eee; border-radius:3px; margin-top:6px; overflow:hidden; display:none; }
19
+ #bar > div { height:100%; width:0%; background:var(--red); transition:width .3s; }
20
+ #chat { width:min(760px,94vw); flex:1; margin:12px auto; background:var(--card); border-radius:14px;
21
+ padding:16px; overflow-y:auto; min-height:340px; max-height:56vh; box-shadow:0 1px 4px rgba(0,0,0,.08); }
22
+ .msg { margin:8px 0; padding:10px 14px; border-radius:12px; max-width:85%; line-height:1.45; font-size:.95rem;
23
+ white-space:pre-wrap; word-wrap:break-word; }
24
+ .user { background:var(--red); color:#fff; margin-left:auto; }
25
+ .bot { background:#f2f2f2; }
26
+ #controls { width:min(760px,94vw); margin:0 auto 8px; display:flex; gap:8px; }
27
+ #box { flex:1; padding:12px 14px; border:1px solid #ccc; border-radius:10px; font-size:1rem; outline:none; }
28
+ #box:focus { border-color:var(--red); }
29
+ button { border:none; border-radius:10px; padding:12px 18px; font-size:1rem; cursor:pointer; }
30
+ #send { background:var(--red); color:#fff; }
31
+ #mic { background:#fff; color:var(--red); border:2px solid var(--red); }
32
+ #mic.listening { background:var(--red); color:#fff; animation:pulse 1.2s infinite; }
33
+ @keyframes pulse { 50% { opacity:.6; } }
34
+ #opts { width:min(760px,94vw); margin:0 auto 10px; display:flex; align-items:center; gap:14px; font-size:.85rem; color:#444; flex-wrap:wrap; }
35
+ .chip { background:#fff; border:1px solid #ddd; border-radius:16px; padding:5px 12px; cursor:pointer; font-size:.8rem; }
36
+ .chip:hover { border-color:var(--red); color:var(--red); }
37
+ footer { width:100%; text-align:center; font-size:.75rem; color:#777; padding:10px; }
38
+ footer a { color:var(--red); text-decoration:none; }
39
+ </style>
40
+ </head>
41
+ <body>
42
+ <header>
43
+ <h1>🖥️ GPU Mart <span>Assistant</span></h1>
44
+ <p>India's datacenter-grade GPU marketplace — ask by typing or speaking. Runs entirely in your browser.</p>
45
+ </header>
46
+
47
+ <div id="status">⏳ Loading the GPU Mart model…<div id="bar"><div></div></div></div>
48
+
49
+ <div id="chat"></div>
50
+ <div id="opts">
51
+ <label><input type="checkbox" id="speakToggle" checked> 🔊 Speak replies</label>
52
+ <span class="chip" data-q="What is GPU Mart?">What is GPU Mart?</span>
53
+ <span class="chip" data-q="Explain CARINA, QUASAR and DRACO">CARINA / QUASAR / DRACO</span>
54
+ <span class="chip" data-q="Price of the DRACO 8x H200 server?">DRACO 8× H200 price</span>
55
+ <span class="chip" data-q="How do I get a quote?">Get a quote</span>
56
+ </div>
57
+
58
+ <div id="controls">
59
+ <input id="box" type="text" placeholder="Ask about GPU Mart…" autocomplete="off">
60
+ <button id="mic" title="Speak your question">🎤</button>
61
+ <button id="send">Send</button>
62
+ </div>
63
+
64
+ <footer>
65
+ Model: <a href="https://huggingface.co/VickyRDP/gpumart-qwen2.5-0.5b-instruct" target="_blank">gpumart fine-tune</a> ·
66
+ Store: <a href="https://rdp.in/gpu-mart/" target="_blank">rdp.in/gpu-mart</a> ·
67
+ Sales: +91&nbsp;720&nbsp;794&nbsp;8743
68
+ </footer>
69
+
70
+ <script type="module">
71
+ const MODEL_ID = 'VickyRDP/gpumart-qwen2.5-0.5b-instruct';
72
+ const SYSTEM = "You are the GPU Mart assistant for RDP GPU Mart (rdp.in/gpu-mart), India's datacenter-grade GPU marketplace by RDP Computers India Private Limited. Answer questions about GPU Mart products, series, pricing, policies and support accurately and concisely.";
73
+
74
+ const chatEl = document.getElementById('chat');
75
+ const statusEl = document.getElementById('status');
76
+ const barWrap = document.getElementById('bar');
77
+ const bar = barWrap.querySelector('div');
78
+ const box = document.getElementById('box');
79
+ const sendBtn = document.getElementById('send');
80
+ const micBtn = document.getElementById('mic');
81
+ const speakToggle = document.getElementById('speakToggle');
82
+
83
+ const history = [];
84
+ let generator = null, busy = false;
85
+
86
+ function addMsg(text, cls) {
87
+ const d = document.createElement('div');
88
+ d.className = 'msg ' + cls;
89
+ d.textContent = text;
90
+ chatEl.appendChild(d);
91
+ chatEl.scrollTop = chatEl.scrollHeight;
92
+ return d;
93
+ }
94
+
95
+ function speak(text) {
96
+ if (!speakToggle.checked || !window.speechSynthesis) return;
97
+ speechSynnthesis.cancel();
98
+ const u = new SpeechSynthesisUtterance(text.slice(0, 800));
99
+ u.lang = 'en-IN'; u.rate = 1.02;
100
+ const vs = speechSynthesis.getVoices();
101
+ u.voice = vs.find(v => v.lang === 'en-IN') || vs.find(v => v.lang.startsWith('en')) || null;
102
+ speechSynthesis.speak(u);
103
+ }
104
+
105
+ async function loadModel() {
106
+ try {
107
+ const { pipeline, TextStreamer } = await import('https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1');
108
+ window.__TextStreamer = TextStreamer;
109
+
110
+ // Refresh cached small config files (config/tokenizer/generation) so config fixes
111
+ // reach returning visitors; the big ONNX weights stay cached.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  try {
113
+ const c = await caches.open('transformers-cache');
114
+ for (const req of await c.keys()) {
115
+ if (req.url.includes('gpumart-qwen2.5-0.5b-instruct') && /(config\.json|generation_config\.json|tokenizer_config\.json)$/.test(req.url)) {
116
+ await c.delete(req);
117
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  }
119
+ } catch (e) {}
120
+
121
+ // Prefer WebGPU (needs shader-f16 for this fp16 model); otherwise fall back to
122
+ // WASM on CPU — slower, but verified to work with fp16 + float16 KV cache.
123
+ let device = 'wasm';
124
+ if (navigator.gpu) {
125
+ try {
126
+ const adapter = await navigator.gpu.requestAdapter();
127
+ if (adapter && adapter.features.has('shader-f16')) device = 'webgpu';
128
+ } catch (e) {}
129
+ }
130
+ statusEl.firstChild.textContent = `⏳ Downloading the GPU Mart model (~1.2 GB, one-time, cached after) — ${device.toUpperCase()} mode…`;
131
+ barWrap.style.display = 'block';
132
+ const files = {};
133
+ generator = await pipeline('text-generation', MODEL_ID, {
134
+ device, dtype: 'fp16',
135
+ session_options: { graphOptimizationLevel: 'disabled' },
136
+ progress_callback: p => {
137
+ if (p.status === 'progress' && p.total) {
138
+ files[p.file] = [p.loaded, p.total];
139
+ let l = 0, t = 0; for (const k in files) { l += files[k][0]; t += files[k][1]; }
140
+ bar.style.width = Math.round(100 * l / t) + '%';
141
+ }
142
+ }
143
+ });
144
  barWrap.style.display = 'none';
145
+ statusEl.firstChild.textContent = device === 'webgpu'
146
+ ? '🟢 GPU Mart model loaded (WebGPU) — ask away (type or tap the mic).'
147
+ : '🟢 GPU Mart model loaded (CPU mode — replies may take a little longer). Ask away!';
148
+ addMsg("Namaste! I'm the GPU Mart assistant — trained on RDP GPU Mart's catalog and policies. Ask me about products, CARINA/QUASAR/DRACO series, pricing, quotes, shipping, warranty or support.", 'bot');
149
+ } catch (e) {
150
+ console.error(e);
151
+ statusEl.firstChild.textContent = '🔴 Could not load the model in this browser. Please use the latest Chrome or Edge. Meanwhile, visit rdp.in/gpu-mart or call +91 720 794 8743.';
152
+ }
153
+ }
154
+
155
+ async function respond(text) {
156
+ if (!text.trim() || busy) return;
157
+ box.value = '';
158
+ addMsg(text, 'user');
159
+ if (!generator) { addMsg('The model is still loading — one moment please…', 'bot'); return; }
160
+ busy = true; sendBtn.disabled = true;
161
+ const botEl = addMsg('…', 'bot');
162
+ try {
163
+ const msgs = [{ role: 'system', content: SYSTEM }, ...history.slice(-6), { role: 'user', content: text }];
164
+ let out = '';
165
+ const streamer = new window.__TextStreamer(generator.tokenizer, {
166
+ skip_prompt: true, skip_special_tokens: true,
167
+ callback_function: t => { out += t; botEl.textContent = out; chatEl.scrollTop = chatEl.scrollHeight; }
168
+ });
169
+ const res = await generator(msgs, { max_new_tokens: 220, do_sample: true, temperature: 0.3, top_p: 0.9, repetition_penalty: 1.1, streamer });
170
+ const full = (res[0].generated_text.at(-1).content || out).trim();
171
+ botEl.textContent = full;
172
+ history.push({ role: 'user', content: text }, { role: 'assistant', content: full });
173
+ speak(full);
174
+ } catch (e) {
175
+ console.error(e);
176
+ botEl.textContent = 'Sorry, something went wrong generating a reply. Please try again.';
177
+ }
178
+ busy = false; sendBtn.disabled = false;
179
+ }
180
+
181
+ sendBtn.onclick = () => respond(box.value);
182
+ box.addEventListener('keydown', e => { if (e.key === 'Enter') respond(box.value); });
183
+ document.querySelectorAll('.chip').forEach(c => c.onclick = () => respond(c.dataset.q));
184
+
185
+ // ---- Voice input (Web Speech API) ----
186
+ const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
187
+ if (!SR) { micBtn.disabled = true; micBtn.title = 'Voice input not supported in this browser'; }
188
+ let rec = null, listening = false;
189
+ micBtn.onclick = () => {
190
+ if (!SR) return;
191
+ if (listening) { rec && rec.stop(); return; }
192
+ rec = new SR();
193
+ rec.lang = 'en-IN'; rec.interimResults = true;
194
+ let finalTxt = '';
195
+ rec.onresult = e => { let t = ''; for (const r of e.results) t += r[0].transcript; box.value = t;
196
+ if (e.results[e.results.length - 1].isFinal) finalTxt = t; };
197
+ rec.onend = () => { listening = false; micBtn.classList.remove('listening'); micBtn.textContent = '🎤';
198
+ if (finalTxt.trim()) respond(finalTxt); };
199
+ rec.onerror = () => { listening = false; micBtn.classList.remove('listening'); micBtn.textContent = '🎤'; };
200
+ listening = true; micBtn.classList.add('listening'); micBtn.textContent = '🔴';
201
+ rec.start();
202
+ };
203
+ if (window.speechSynthesis) speechSynthesis.getVoices();
204
+
205
+ loadModel();
206
+ </script>
207
+ </body>
208
+ </html>