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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
| 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" />
|
| 6 |
+
<title>Qwen2.5‑Coder‑3B‑Instruct — WebGPU (ONNX)</title>
|
| 7 |
+
<style>
|
| 8 |
+
:root{--bg:#0b1021;--panel:#0e1430;--text:#e9eeff;--muted:#9fb3ff;--accent:#7aa2ff;--ok:#2ecc71;--warn:#f39c12;--err:#e74c3c;--radius:14px;--border:1px solid rgba(255,255,255,.08)}
|
| 9 |
+
*{box-sizing:border-box}body{margin:0;background:radial-gradient(1200px 800px at 10% -10%,#1a2252 0%,transparent 60%),radial-gradient(1200px 800px at 110% 10%,#1b2d61 0%,transparent 60%),var(--bg);color:var(--text);font:16px/1.6 system-ui,-apple-system,Segoe UI,Roboto,Inter,Arial,sans-serif;padding:20px}
|
| 10 |
+
header{display:flex;gap:16px;align-items:center;justify-content:space-between;padding:14px 16px;border-radius:var(--radius);background:linear-gradient(180deg,rgba(255,255,255,.06),rgba(255,255,255,.02));border:var(--border)}
|
| 11 |
+
h1{margin:0;font-size:22px}
|
| 12 |
+
.muted{color:var(--muted);font-size:12px}
|
| 13 |
+
.row{display:flex;align-items:center;gap:8px;flex-wrap:wrap}
|
| 14 |
+
.card{background:linear-gradient(180deg,rgba(255,255,255,.06),rgba(255,255,255,.02));border:var(--border);border-radius:var(--radius);padding:14px;margin-top:16px}
|
| 15 |
+
textarea{width:100%;height:180px;background:var(--panel);border:var(--border);border-radius:12px;color:var(--text);padding:10px}
|
| 16 |
+
input[type="text"]{width:100%;background:var(--panel);border:var(--border);border-radius:12px;color:var(--text);padding:10px}
|
| 17 |
+
button{appearance:none;border:0;border-radius:12px;padding:10px 14px;cursor:pointer;font-weight:600;background:linear-gradient(180deg,#7aa2ff,#4e77ff);color:white}
|
| 18 |
+
button.secondary{background:linear-gradient(180deg,#7780a6,#5a6284)}
|
| 19 |
+
button.ghost{background:transparent;border:var(--border)}
|
| 20 |
+
#chat{display:flex;flex-direction:column;gap:8px;height:280px;overflow:auto;border-radius:12px;background:var(--panel);border:var(--border);padding:10px}
|
| 21 |
+
.msg{padding:10px;border-radius:10px;max-width:90%}
|
| 22 |
+
.me{background:#1b2553;align-self:flex-end}
|
| 23 |
+
.bot{background:#0b143f;align-self:flex-start}
|
| 24 |
+
#log{background:#060a1d;color:#b8d0ff;border:var(--border);border-radius:12px;height:140px;overflow:auto;padding:8px;font-size:12px}
|
| 25 |
+
.chip{display:inline-flex;align-items:center;gap:6px;border-radius:999px;padding:6px 10px;border:var(--border)}
|
| 26 |
+
.ok{background:rgba(46,204,113,.12);border-color:rgba(46,204,113,.35)}
|
| 27 |
+
.warn{background:rgba(243,156,18,.12);border-color:rgba(243,156,18,.35)}
|
| 28 |
+
.err{background:rgba(231,76,60,.12);border-color:rgba(231,76,60,.35)}
|
| 29 |
+
</style>
|
| 30 |
+
</head>
|
| 31 |
+
<body>
|
| 32 |
+
<header>
|
| 33 |
+
<div>
|
| 34 |
+
<h1>Qwen2.5‑Coder‑3B‑Instruct — WebGPU (ONNX)</h1>
|
| 35 |
+
<div class="muted">Pure front‑end · Transformers.js + ONNX Runtime Web · WebGPU forced</div>
|
| 36 |
+
</div>
|
| 37 |
+
<div class="row">
|
| 38 |
+
<button id="load">Load model</button>
|
| 39 |
+
<button id="cancel" class="secondary" style="display:none;">Cancel (UI reset only)</button>
|
| 40 |
+
<button id="clearlog" class="ghost">Clear log</button>
|
| 41 |
+
</div>
|
| 42 |
+
</header>
|
| 43 |
+
|
| 44 |
+
<div class="card">
|
| 45 |
+
<div class="row" style="margin-bottom:8px">
|
| 46 |
+
<span class="chip warn">First run will download ~2.1 GB (q4, smallest ONNX). Desktop Chrome/Edge recommended.</span>
|
| 47 |
+
</div>
|
| 48 |
+
|
| 49 |
+
<!-- Confirmation step -->
|
| 50 |
+
<div id="confirm" style="display:none">
|
| 51 |
+
<p>This will download model files (~2.1 GB, quantized q4) to your browser cache and run inference via WebGPU. Proceed?</p>
|
| 52 |
+
<div class="row">
|
| 53 |
+
<button id="confirm-yes">Yes, download now</button>
|
| 54 |
+
<button id="confirm-no" class="secondary">No, go back</button>
|
| 55 |
+
</div>
|
| 56 |
+
</div>
|
| 57 |
+
|
| 58 |
+
<div id="loading" style="display:none">
|
| 59 |
+
<div class="row">
|
| 60 |
+
<progress id="progress" value="0" max="100"></progress>
|
| 61 |
+
<span id="progress-label" class="muted">0%</span>
|
| 62 |
+
</div>
|
| 63 |
+
<div class="muted">WebGPU requires HTTPS or localhost. Chrome/Edge supported by default; Safari/Firefox may need experimental flags.</div>
|
| 64 |
+
</div>
|
| 65 |
+
|
| 66 |
+
<div id="ui" style="display:none">
|
| 67 |
+
<input id="sys" type="text" placeholder="System prompt (optional): You are a senior coding assistant. Keep answers concise." />
|
| 68 |
+
<div id="chat"></div>
|
| 69 |
+
<textarea id="prompt" placeholder="Ask something (you can paste code)…"></textarea>
|
| 70 |
+
<div class="row">
|
| 71 |
+
<button id="send">Send</button>
|
| 72 |
+
<button id="stop" class="ghost" disabled>Stop</button>
|
| 73 |
+
<span id="status" class="chip" style="display:none"></span>
|
| 74 |
+
</div>
|
| 75 |
+
</div>
|
| 76 |
+
</div>
|
| 77 |
+
|
| 78 |
+
<div class="card">
|
| 79 |
+
<h3 style="margin:6px 0">Debug log</h3>
|
| 80 |
+
<div id="log"></div>
|
| 81 |
+
</div>
|
| 82 |
+
|
| 83 |
+
<script type="module">
|
| 84 |
+
// ================= Configuration =================
|
| 85 |
+
// ONNX repo (3B Instruct), with multiple quantizations in onnx/ (model_q4.onnx, model_q4f16.onnx, model_int8.onnx, model_fp16.onnx, ...)
|
| 86 |
+
const MODEL_ID = 'onnx-community/Qwen2.5-Coder-3B-Instruct';
|
| 87 |
+
const CDN_PRIMARY = 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
|
| 88 |
+
const CDN_FALLBACK = 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2';
|
| 89 |
+
|
| 90 |
+
// Smallest first: q4 → q4f16 → int8 → fp16
|
| 91 |
+
const options = {
|
| 92 |
+
device: 'webgpu', // force WebGPU
|
| 93 |
+
dtype: 'q4', // prefer smallest ONNX weights (model_q4.onnx + .onnx_data)
|
| 94 |
+
progress_callback: setProgress,
|
| 95 |
+
};
|
| 96 |
+
|
| 97 |
+
// ================= DOM & State =================
|
| 98 |
+
const $ = s=>document.querySelector(s);
|
| 99 |
+
const logDiv=$('#log'), chatDiv=$('#chat');
|
| 100 |
+
const loadBtn=$('#load'), cancelBtn=$('#cancel');
|
| 101 |
+
const clearBtn=$('#clearlog');
|
| 102 |
+
const ui=$('#ui'), loading=$('#loading');
|
| 103 |
+
const pbar=$('#progress'), plabel=$('#progress-label');
|
| 104 |
+
const sysEl=$('#sys'), promptEl=$('#prompt');
|
| 105 |
+
const sendBtn=$('#send'), stopBtn=$('#stop');
|
| 106 |
+
const statusChip=$('#status');
|
| 107 |
+
const confirmBox=$('#confirm');
|
| 108 |
+
const confirmYes=$('#confirm-yes');
|
| 109 |
+
const confirmNo=$('#confirm-no');
|
| 110 |
+
|
| 111 |
+
let pipe=null, cancelled=false, abortGen=null;
|
| 112 |
+
|
| 113 |
+
// ================= Utils =================
|
| 114 |
+
function log(...a){ const s=a.map(x=>typeof x==='string'?x:JSON.stringify(x)).join(' ');
|
| 115 |
+
logDiv.textContent+=s+'
|
| 116 |
+
'; logDiv.scrollTop=logDiv.scrollHeight; console.log('[LOG]',...a); }
|
| 117 |
+
function chip(kind,text){ statusChip.className='chip '+(kind||''); statusChip.textContent=text; statusChip.style.display='inline-flex'; }
|
| 118 |
+
function clearChip(){ statusChip.style.display='none'; }
|
| 119 |
+
function setProgress(evt){ if(evt?.status==='progress'){ const pct=Math.round(Math.max(0,Math.min(100,evt.progress||0))); pbar.value=pct; plabel.textContent=`${pct}% ${evt?.name||evt?.file||''}`.trim(); } else if(evt?.status){ log(`status: ${evt.status} ${evt?.name||evt?.file||''}`); } }
|
| 120 |
+
|
| 121 |
+
window.addEventListener('error',e=>log('window.error:',e.message,e.filename,`${e.lineno}:${e.colno}`));
|
| 122 |
+
window.addEventListener('unhandledrejection',e=>log('unhandledrejection:',e.reason?.message||e.reason));
|
| 123 |
+
|
| 124 |
+
async function importTransformers(){
|
| 125 |
+
try{ log('Importing library:',CDN_PRIMARY); return await import(CDN_PRIMARY); }
|
| 126 |
+
catch(e){ log('Primary failed, fallback:',CDN_FALLBACK,e?.message); return await import(CDN_FALLBACK); }
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// ================= Events =================
|
| 130 |
+
loadBtn.addEventListener('click', ()=>{
|
| 131 |
+
// Show confirmation instead of starting download immediately
|
| 132 |
+
loadBtn.style.display='none';
|
| 133 |
+
confirmBox.style.display='block';
|
| 134 |
+
});
|
| 135 |
+
|
| 136 |
+
confirmNo.addEventListener('click', ()=>{
|
| 137 |
+
confirmBox.style.display='none';
|
| 138 |
+
loadBtn.style.display='inline-block';
|
| 139 |
+
});
|
| 140 |
+
|
| 141 |
+
confirmYes.addEventListener('click', async ()=>{
|
| 142 |
+
try{
|
| 143 |
+
cancelled=false; confirmBox.style.display='none';
|
| 144 |
+
cancelBtn.style.display='inline-block';
|
| 145 |
+
loading.style.display='block'; pbar.value=0; plabel.textContent='0%';
|
| 146 |
+
const { pipeline } = await importTransformers();
|
| 147 |
+
log('Transformers.js loaded. Creating text-generation pipeline…');
|
| 148 |
+
pipe = await pipeline('text-generation', MODEL_ID, options);
|
| 149 |
+
if(cancelled) log('Note: cancel only resets UI and cannot interrupt underlying downloads.');
|
| 150 |
+
loading.style.display='none'; cancelBtn.style.display='none'; ui.style.display='block';
|
| 151 |
+
chip('ok','Model ready'); setTimeout(clearChip,1200);
|
| 152 |
+
}catch(err){
|
| 153 |
+
loading.style.display='none'; cancelBtn.style.display='none'; loadBtn.style.display='inline-block';
|
| 154 |
+
log('❌ Load failed:', err?.message||err);
|
| 155 |
+
if((err?.message||'').includes('404')) log('Check repo ID or private permissions:', MODEL_ID);
|
| 156 |
+
log('Reminder: Transformers.js expects ONNX weights under an onnx/ directory with proper configs.');
|
| 157 |
+
}
|
| 158 |
+
});
|
| 159 |
+
|
| 160 |
+
cancelBtn.addEventListener('click',()=>{ cancelled=true; loadBtn.style.display='inline-block'; cancelBtn.style.display='none'; loading.style.display='none'; log('Canceled (UI only).'); });
|
| 161 |
+
clearBtn.addEventListener('click',()=>{ logDiv.textContent=''; });
|
| 162 |
+
|
| 163 |
+
function addMsg(text, me=false){ const d=document.createElement('div'); d.className='msg '+(me?'me':'bot'); d.textContent=text; chatDiv.appendChild(d); chatDiv.scrollTop=chatDiv.scrollHeight; return d; }
|
| 164 |
+
|
| 165 |
+
sendBtn.addEventListener('click', async ()=>{
|
| 166 |
+
try{
|
| 167 |
+
if(!pipe) return chip('warn','Model not loaded');
|
| 168 |
+
const user=promptEl.value.trim(); if(!user) return; promptEl.value='';
|
| 169 |
+
const sys=sysEl.value.trim();
|
| 170 |
+
addMsg(user,true);
|
| 171 |
+
const botEl=addMsg('���');
|
| 172 |
+
stopBtn.disabled=false; chip('', 'Generating…');
|
| 173 |
+
|
| 174 |
+
// Stream tokens with callback_function (API shape may vary slightly between versions)
|
| 175 |
+
let outText='';
|
| 176 |
+
abortGen = new AbortController();
|
| 177 |
+
const gen = await pipe(
|
| 178 |
+
`${sys?`[SYSTEM]
|
| 179 |
+
${sys}
|
| 180 |
+
|
| 181 |
+
`:''}[USER]
|
| 182 |
+
${user}
|
| 183 |
+
|
| 184 |
+
[ASSISTANT]
|
| 185 |
+
`,
|
| 186 |
+
{
|
| 187 |
+
max_new_tokens: 256,
|
| 188 |
+
temperature: 0.7,
|
| 189 |
+
top_p: 0.9,
|
| 190 |
+
repetition_penalty: 1.05,
|
| 191 |
+
callback_function: (x)=>{ outText += x; botEl.textContent = outText; },
|
| 192 |
+
signal: abortGen.signal,
|
| 193 |
+
}
|
| 194 |
+
);
|
| 195 |
+
if(typeof gen?.[0]?.generated_text === 'string' && !outText){ botEl.textContent = gen[0].generated_text; }
|
| 196 |
+
chip('ok','Done'); setTimeout(clearChip, 1200);
|
| 197 |
+
}catch(err){
|
| 198 |
+
if(err?.name==='AbortError'){ chip('warn','Stopped'); setTimeout(clearChip,1200); return; }
|
| 199 |
+
log('❌ Generation error:', err?.message||err);
|
| 200 |
+
chip('err','Generation failed');
|
| 201 |
+
}finally{ stopBtn.disabled=true; abortGen=null; }
|
| 202 |
+
});
|
| 203 |
+
|
| 204 |
+
stopBtn.addEventListener('click',()=>{ try{ abortGen?.abort(); }catch{} });
|
| 205 |
+
</script>
|
| 206 |
+
</body>
|
| 207 |
</html>
|