Spaces:
Running
Running
Fix BERT tokenizer parsing and inference outputs
Browse files- index.html +6 -6
index.html
CHANGED
|
@@ -11,7 +11,7 @@
|
|
| 11 |
<header class="hero"><div class="eyebrow">Chapter 1 · Transformers</div><h1>Disaster Tweet Classifier</h1><p>在浏览器本地运行 INT8 量化模型。输入文本不会上传到服务器,页面无需 Hugging Face CPU/GPU 实例。</p><div class="badge">⚡ Browser-side inference · no server CPU/GPU</div></header>
|
| 12 |
<div class="grid">
|
| 13 |
<section class="card"><h2>输入文本并分类</h2><label class="label" for="text">输入文本</label><textarea id="text" placeholder="输入一条推文或句子…"></textarea><div class="actions"><button class="primary" id="run">开始本地分类</button><button class="secondary" id="clear">清空</button></div><div id="status" class="meta loading">正在加载量化模型…</div><div class="result" id="result" hidden><div class="result-title"><span id="prediction"></span><span id="confidence"></span></div><div class="bar"><div class="fill" id="confidence-bar"></div></div><div class="meta" id="details"></div></div></section>
|
| 14 |
-
<section class="card"><h2>尝试真实测试集样例</h2><p class="meta">以下 ID 和文本来自原始测试集。点击样例加载文本,再进行本地分类。</p><div class="chips" id="examples"></div><div class="notice">
|
| 15 |
<section class="card"><h2>实验概览</h2><div class="stat"><div><strong>83.85%</strong><small>验证集准确率</small></div><div><strong>2</strong><small>训练轮数</small></div><div><strong>4 GPU</strong><small>CloudML 训练</small></div><div><strong>6e-7</strong><small>ONNX/PyTorch 最大差异</small></div></div></section>
|
| 16 |
<section class="card"><h2>推理在哪里运行?</h2><p class="meta">Static Space 只提供页面、分词文件和量化模型。浏览器下载后使用 WebAssembly 在本地完成分词与推理,不需要 Gradio 容器,也不占用 Hugging Face CPU 配额。</p></section>
|
| 17 |
</div></main>
|
|
@@ -21,10 +21,10 @@ const MODEL='model.int8.onnx', LABELS=['非灾害','灾害'];
|
|
| 21 |
const examples=[['0','Just happened a terrible car crash'],['2','Heard about #earthquake is different cities, stay safe everyone.'],['3','there is a forest fire at spot pond, geese are fleeing across the street'],['9','Apocalypse lighting. #Spokane #wildfires'],['11','Typhoon Soudelor kills 28 in China and Taiwan'],['12',"We're shaking...It's an earthquake"],['21',"They'd probably still show more life than Arsenal did yesterday, eh? EH?"],['22','Hey! How are you?'],['27','What a nice hat?'],['29','Fuck off!'],['30',"No I don't like cold!"],['35',"NOOOOOOOOO! Don't do that!"],['42',"No don't tell me that!"],['43','What if?!'],['45','Awesome!'],['46',"Birmingham Wholesale Market is ablaze BBC News - Fire breaks out at Birmingham's Wholesale Market"],['47','@sunkxssedharry will you wear shorts for race ablaze ?'],['51',"#PreviouslyOnDoyinTv: Toke Makinwa's marriage crisis sets Nigerian Twitter ablaze..."],['58','Check these out: http://t.co/rOI2NSmEJJ'],['60',"They'd probably still show more life than Arsenal did yesterday"]];
|
| 22 |
const $=id=>document.getElementById(id), text=$('text'), status=$('status'); let session,vocab;
|
| 23 |
examples.forEach(([id,t])=>{let b=document.createElement('button');b.className='chip';b.textContent=`ID ${id} · ${t.slice(0, thirty=32)}${t.length>32?'…':''}`;b.onclick=()=>{text.value=t;text.focus()};$('examples').appendChild(b)});
|
| 24 |
-
function basicTokenize(input){
|
| 25 |
-
function wordPiece(word){if(vocab
|
| 26 |
-
function encode(input){const unk=
|
| 27 |
-
async function load(){try{status.textContent='正在加载分词表和量化模型…';let
|
| 28 |
-
async function classify(){if(!session||!vocab){status.textContent='模型仍在加载,请稍候…';return}if(!text.value.trim()){status.className='meta error';status.textContent='请先输入文本。';return}status.className='meta loading';status.textContent='正在本地分类…';try{let enc=encode(text.value)
|
| 29 |
$('run').onclick=classify;$('clear').onclick=()=>{text.value='';$('result').hidden=true;status.textContent='已就绪 · 模型在浏览器本地运行'};load();
|
| 30 |
</script></body></html>
|
|
|
|
| 11 |
<header class="hero"><div class="eyebrow">Chapter 1 · Transformers</div><h1>Disaster Tweet Classifier</h1><p>在浏览器本地运行 INT8 量化模型。输入文本不会上传到服务器,页面无需 Hugging Face CPU/GPU 实例。</p><div class="badge">⚡ Browser-side inference · no server CPU/GPU</div></header>
|
| 12 |
<div class="grid">
|
| 13 |
<section class="card"><h2>输入文本并分类</h2><label class="label" for="text">输入文本</label><textarea id="text" placeholder="输入一条推文或句子…"></textarea><div class="actions"><button class="primary" id="run">开始本地分类</button><button class="secondary" id="clear">清空</button></div><div id="status" class="meta loading">正在加载量化模型…</div><div class="result" id="result" hidden><div class="result-title"><span id="prediction"></span><span id="confidence"></span></div><div class="bar"><div class="fill" id="confidence-bar"></div></div><div class="meta" id="details"></div></div></section>
|
| 14 |
+
<section class="card"><h2>尝试真实测试集样例</h2><p class="meta">以下 ID 和文本来自原始测试集。点击样例加载文本,再进行本地分类。</p><div class="chips" id="examples"></div><div class="notice">页面使用与训练一致的 BERT 词表和量化 ONNX 模型。模型输出已与 PyTorch 对照,验证样例最大差异约为 6e-7。</div></section>
|
| 15 |
<section class="card"><h2>实验概览</h2><div class="stat"><div><strong>83.85%</strong><small>验证集准确率</small></div><div><strong>2</strong><small>训练轮数</small></div><div><strong>4 GPU</strong><small>CloudML 训练</small></div><div><strong>6e-7</strong><small>ONNX/PyTorch 最大差异</small></div></div></section>
|
| 16 |
<section class="card"><h2>推理在哪里运行?</h2><p class="meta">Static Space 只提供页面、分词文件和量化模型。浏览器下载后使用 WebAssembly 在本地完成分词与推理,不需要 Gradio 容器,也不占用 Hugging Face CPU 配额。</p></section>
|
| 17 |
</div></main>
|
|
|
|
| 21 |
const examples=[['0','Just happened a terrible car crash'],['2','Heard about #earthquake is different cities, stay safe everyone.'],['3','there is a forest fire at spot pond, geese are fleeing across the street'],['9','Apocalypse lighting. #Spokane #wildfires'],['11','Typhoon Soudelor kills 28 in China and Taiwan'],['12',"We're shaking...It's an earthquake"],['21',"They'd probably still show more life than Arsenal did yesterday, eh? EH?"],['22','Hey! How are you?'],['27','What a nice hat?'],['29','Fuck off!'],['30',"No I don't like cold!"],['35',"NOOOOOOOOO! Don't do that!"],['42',"No don't tell me that!"],['43','What if?!'],['45','Awesome!'],['46',"Birmingham Wholesale Market is ablaze BBC News - Fire breaks out at Birmingham's Wholesale Market"],['47','@sunkxssedharry will you wear shorts for race ablaze ?'],['51',"#PreviouslyOnDoyinTv: Toke Makinwa's marriage crisis sets Nigerian Twitter ablaze..."],['58','Check these out: http://t.co/rOI2NSmEJJ'],['60',"They'd probably still show more life than Arsenal did yesterday"]];
|
| 22 |
const $=id=>document.getElementById(id), text=$('text'), status=$('status'); let session,vocab;
|
| 23 |
examples.forEach(([id,t])=>{let b=document.createElement('button');b.className='chip';b.textContent=`ID ${id} · ${t.slice(0, thirty=32)}${t.length>32?'…':''}`;b.onclick=()=>{text.value=t;text.focus()};$('examples').appendChild(b)});
|
| 24 |
+
function basicTokenize(input){const text=String(input).toLowerCase();const spaced=text.replace(/[!"#$%&'()*+,-./:;<=>?@[\\\]^_`{|}~]/g,' $& ');return spaced.trim().split(/\s+/).filter(Boolean)}
|
| 25 |
+
function wordPiece(word){if(Object.prototype.hasOwnProperty.call(vocab,word))return [word];let pieces=[];let start=0;while(start<word.length){let end=word.length,found=null;while(start<end){let part=word.slice(start,end);if(start>0)part='##'+part;if(Object.prototype.hasOwnProperty.call(vocab,part)){found=part;break}end--}if(found===null)return ['[UNK]'];pieces.push(found);start=end}return pieces}
|
| 26 |
+
function encode(input){const unk=vocab['[UNK]'],pad=vocab['[PAD]'],cls=vocab['[CLS]'],sep=vocab['[SEP]'];if(![unk,pad,cls,sep].every(Number.isInteger))throw Error('词表缺少 BERT 特殊 token');let tokens=['[CLS]'];for(const word of basicTokenize(input))tokens.push(...wordPiece(word));tokens=tokens.slice(0,126);tokens.push('[SEP]');let ids=tokens.map(t=>Number.isInteger(vocab[t])?vocab[t]:unk);while(ids.length<128)ids.push(pad);ids[0]=cls;ids[tokens.length-1]=sep;let mask=ids.map((_,i)=>i<tokens.length?1:0);return {ids,mask,tokens}}
|
| 27 |
+
async function load(){try{status.textContent='正在加载分词表和量化模型…';let vocabText=await fetch('./vocab.txt',{cache:'no-store'}).then(r=>{if(!r.ok)throw Error('无法加载词表');return r.text()});vocab={};vocabText.split(/\r?\n/).forEach((t,i)=>{const token=t.replace(/\r$/,'');if(token)vocab[token]=i});if(!Number.isInteger(vocab['[UNK]'])||!Number.isInteger(vocab['[CLS]']))throw Error('词表格式不正确');session=await ort.InferenceSession.create(MODEL,{executionProviders:['wasm']});status.className='meta';status.textContent='已就绪 · 模型在浏览器本地运行'}catch(e){status.className='meta error';status.textContent='模型加载失败:'+e.message}}
|
| 28 |
+
async function classify(){if(!session||!vocab){status.textContent='模型仍在加载,请稍候…';return}if(!text.value.trim()){status.className='meta error';status.textContent='请先输入文本。';return}status.className='meta loading';status.textContent='正在本地分类…';try{let enc=encode(text.value);let feeds={input_ids:new ort.Tensor('int64',BigInt64Array.from(enc.ids.map(v=>BigInt(v))),[1,128]),attention_mask:new ort.Tensor('int64',BigInt64Array.from(enc.mask.map(v=>BigInt(v))),[1,128]),token_type_ids:new ort.Tensor('int64',BigInt64Array.from(Array(128).fill(0).map(()=>0n)),[1,128])};let out=await session.run(feeds);let logits=Array.from(out.logits.data);if(logits.length!==2||logits.some(v=>!Number.isFinite(v)))throw Error('模型输出无效');let maxLogit=Math.max(...logits), probs=logits.map(x=>Math.exp(x-maxLogit));let sum=probs.reduce((a,b)=>a+b,0);probs=probs.map(x=>x/sum);let i=probs[1]>probs[0]?1:0, conf=probs[i];$('prediction').textContent=LABELS[i];$('confidence').textContent=(conf*100).toFixed(1)+'%';$('confidence-bar').style.width=(conf*100)+'%';$('details').textContent=`非灾害 ${(probs[0]*100).toFixed(1)}% · 灾害 ${(probs[1]*100).toFixed(1)}%`;$('result').hidden=false;status.className='meta';status.textContent='完成 · 文本未发送到服务器'}catch(e){status.className='meta error';status.textContent='推理失败:'+e.message}}
|
| 29 |
$('run').onclick=classify;$('clear').onclick=()=>{text.value='';$('result').hidden=true;status.textContent='已就绪 · 模型在浏览器本地运行'};load();
|
| 30 |
</script></body></html>
|