Spaces:
Running
Running
Use compact INT8 browser inference and Chinese UI
Browse files- README.md +3 -3
- index.html +9 -9
- model.onnx → model.int8.onnx +2 -2
- model.onnx.data +0 -3
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: ⚡
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: green
|
|
@@ -8,6 +8,6 @@ pinned: false
|
|
| 8 |
license: apache-2.0
|
| 9 |
---
|
| 10 |
|
| 11 |
-
# Chapter 1 ·
|
| 12 |
|
| 13 |
-
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Chapter 1 · 浏览器本地文本分类
|
| 3 |
emoji: ⚡
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: green
|
|
|
|
| 8 |
license: apache-2.0
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Chapter 1 · 浏览器本地文本分类
|
| 12 |
|
| 13 |
+
本 Space 使用 INT8 量化后的 ONNX 模型,在访问者浏览器中完成本地推理,不需要服务器 CPU/GPU。
|
index.html
CHANGED
|
@@ -8,21 +8,21 @@
|
|
| 8 |
</style>
|
| 9 |
</head>
|
| 10 |
<body><main>
|
| 11 |
-
<header class="hero"><div class="eyebrow">Chapter 1 · Transformers</div><h1>Disaster Tweet Classifier</h1><p>
|
| 12 |
<div class="grid">
|
| 13 |
-
<section class="card"><h2>
|
| 14 |
-
<section class="card"><h2>
|
| 15 |
-
<section class="card"><h2>
|
| 16 |
-
<section class="card"><h2>
|
| 17 |
</div></main>
|
| 18 |
<script src="https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2"></script>
|
| 19 |
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/ort.min.js"></script>
|
| 20 |
<script>
|
| 21 |
-
const MODEL='model.onnx', LABELS=['not disaster','disaster'];
|
| 22 |
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"]];
|
| 23 |
const $=id=>document.getElementById(id), text=$('text'), status=$('status'); let tokenizer,session;
|
| 24 |
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)});
|
| 25 |
-
async function load(){try{tokenizer=await window.transformers.AutoTokenizer.from_pretrained('./');session=await ort.InferenceSession.create(MODEL,{executionProviders:['wasm']});status.className='meta';status.textContent='
|
| 26 |
-
async function classify(){if(!session||!tokenizer){status.textContent='
|
| 27 |
-
$('run').onclick=classify;$('clear').onclick=()=>{text.value='';$('result').hidden=true;status.textContent='
|
| 28 |
</script></body></html>
|
|
|
|
| 8 |
</style>
|
| 9 |
</head>
|
| 10 |
<body><main>
|
| 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">The demo uses the same BERT tokenizer files and an ONNX export of the trained classifier. The exported logits matched PyTorch within 6e-7 on validation samples.</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>
|
| 18 |
<script src="https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2"></script>
|
| 19 |
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/ort.min.js"></script>
|
| 20 |
<script>
|
| 21 |
+
const MODEL='model.int8.onnx', LABELS=['not disaster','disaster'];
|
| 22 |
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"]];
|
| 23 |
const $=id=>document.getElementById(id), text=$('text'), status=$('status'); let tokenizer,session;
|
| 24 |
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)});
|
| 25 |
+
async function load(){try{tokenizer=await window.transformers.AutoTokenizer.from_pretrained('./');session=await ort.InferenceSession.create(MODEL,{executionProviders:['wasm']});status.className='meta';status.textContent='已就绪 · 模型在浏览器本地运行'}catch(e){status.className='meta error';status.textContent='Could not load browser model: '+e.message}}
|
| 26 |
+
async function classify(){if(!session||!tokenizer){status.textContent='模型仍在加载,请稍候…';return}if(!text.value.trim()){status.className='meta error';status.textContent='请先输入文本。';return}status.className='meta loading';status.textContent='正在本地分类…';try{let enc=await tokenizer(text.value,{padding:true,truncation:true,max_length:128,return_tensors:'np'});let feeds={input_ids:new ort.Tensor('int64',enc.input_ids.data,[1,enc.input_ids.dims[1]]),attention_mask:new ort.Tensor('int64',enc.attention_mask.data,[1,enc.attention_mask.dims[1]])};if(enc.token_type_ids)feeds.token_type_ids=new ort.Tensor('int64',enc.token_type_ids.data,[1,enc.token_type_ids.dims[1]]);let out=await session.run(feeds);let logits=Array.from(out.logits.data), ex=Math.max(...logits), probs=logits.map(x=>Math.exp(x-ex));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=`not disaster ${(probs[0]*100).toFixed(1)}% · disaster ${(probs[1]*100).toFixed(1)}%`;$('result').hidden=false;status.className='meta';status.textContent='完成 · 文本未发送到服务器'}catch(e){status.className='meta error';status.textContent='推理失败:'+e.message}}
|
| 27 |
+
$('run').onclick=classify;$('clear').onclick=()=>{text.value='';$('result').hidden=true;status.textContent='已就绪 · 模型在浏览器本地运行'};load();
|
| 28 |
</script></body></html>
|
model.onnx → model.int8.onnx
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:763c629a840da060c20aa39017e57b20151155a692c74860a8b36186181b754a
|
| 3 |
+
size 110733056
|
model.onnx.data
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:034e36ce9ea616300b2bd88deecad8ab6389b7f73cea8e583c5bf6aac49dc166
|
| 3 |
-
size 437939200
|
|
|
|
|
|
|
|
|
|
|
|