Spaces:
Running
Running
File size: 9,610 Bytes
49832e3 cc7570d cf06767 cc7570d cf06767 cc7570d 8b668ac cc7570d 8b668ac f2b1c25 8b668ac cc7570d 6c49eb2 c5c87cd cc7570d c5c87cd cc7570d f2b1c25 8b668ac cc7570d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Disaster Tweet Classifier</title>
<style>
:root{font-family:Inter,ui-sans-serif,system-ui,-apple-system,sans-serif;color:#172033;background:#f4f7fb}*{box-sizing:border-box}body{margin:0}main{max-width:1080px;margin:auto;padding:28px 18px 56px}.hero{padding:34px;border-radius:24px;color:white;background:linear-gradient(125deg,#4c1d95,#0f766e);box-shadow:0 18px 45px #17203328}.eyebrow{text-transform:uppercase;letter-spacing:.12em;font-size:12px;font-weight:800;opacity:.8}.hero h1{font-size:clamp(30px,6vw,54px);line-height:1.05;margin:12px 0}.hero p{max-width:720px;line-height:1.65;margin:8px 0}.badge{display:inline-flex;margin-top:18px;padding:9px 13px;border-radius:999px;background:#ffffff26;font-size:13px;font-weight:700}.grid{display:grid;grid-template-columns:1.15fr .85fr;gap:20px;margin-top:20px}.card{background:#fff;border:1px solid #e5eaf2;border-radius:18px;padding:22px;box-shadow:0 7px 26px #17203310}.card h2{font-size:20px;margin:0 0 14px}.label{display:block;font-size:13px;font-weight:800;color:#536174;margin-bottom:8px}textarea,select{font:inherit;width:100%;border:1px solid #cfd8e5;border-radius:12px;padding:13px;background:#fbfcfe;color:#172033}textarea{min-height:150px;resize:vertical;line-height:1.5}.actions{display:flex;gap:10px;flex-wrap:wrap;margin-top:12px}button{border:0;border-radius:10px;padding:11px 16px;font:inherit;font-weight:800;cursor:pointer}.primary{background:#5b21b6;color:white}.secondary{background:#eef2f7;color:#334155}.result{margin-top:18px;padding:18px;border-radius:14px;background:#f6f8fc}.result-title{display:flex;justify-content:space-between;gap:10px;align-items:center;font-weight:900}.result-title span{color:#047857}.bar{height:9px;border-radius:999px;background:#e7ecf3;overflow:hidden;margin:9px 0 13px}.fill{height:100%;border-radius:inherit;background:linear-gradient(90deg,#5b21b6,#0f766e)}.meta{font-size:13px;color:#64748b;line-height:1.6}.chips{display:flex;flex-wrap:wrap;gap:8px}.chip{padding:8px 10px;border-radius:9px;background:#f1f5f9;color:#334155;font-size:13px;cursor:pointer}.chip:hover{background:#e5e7eb}.notice{margin-top:20px;color:#526071;font-size:13px;line-height:1.7}.loading{color:#a16207}.error{color:#b91c1c}.stat{display:grid;grid-template-columns:1fr 1fr;gap:10px}.stat div{padding:13px;border-radius:12px;background:#f8fafc}.stat strong{display:block;font-size:21px;color:#5b21b6}.stat small{color:#64748b}@media(max-width:760px){.grid{grid-template-columns:1fr}main{padding:16px 12px 38px}}
</style>
</head>
<body><main>
<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>
<div class="grid">
<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>
<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>
<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>
<section class="card"><h2>推理在哪里运行?</h2><p class="meta">Static Space 只提供页面、分词文件和量化模型。浏览器下载后使用 WebAssembly 在本地完成分词与推理,不需要 Gradio 容器,也不占用 Hugging Face CPU 配额。</p></section>
</div></main>
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/ort.min.js"></script>
<script>
const MODEL='model.int8.onnx', LABELS=['非灾害','灾害'];
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"]];
const $=id=>document.getElementById(id), text=$('text'), status=$('status'); let session,vocab;
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)});
function basicTokenize(input){const text=String(input).toLowerCase();const spaced=text.replace(/[!"#$%&'()*+,-./:;<=>?@[\\\]^_`{|}~]/g,' $& ');return spaced.trim().split(/\s+/).filter(Boolean)}
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}
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}}
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}}
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}}
$('run').onclick=classify;$('clear').onclick=()=>{text.value='';$('result').hidden=true;status.textContent='已就绪 · 模型在浏览器本地运行'};load();
</script></body></html>
|