Foery commited on
Commit
8b668ac
·
verified ·
1 Parent(s): cc7570d

Use compact INT8 browser inference and Chinese UI

Browse files
Files changed (4) hide show
  1. README.md +3 -3
  2. index.html +9 -9
  3. model.onnx → model.int8.onnx +2 -2
  4. model.onnx.data +0 -3
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Text Classification · Browser Inference
3
  emoji: ⚡
4
  colorFrom: purple
5
  colorTo: green
@@ -8,6 +8,6 @@ pinned: false
8
  license: apache-2.0
9
  ---
10
 
11
- # Chapter 1 · Browser-side BERT inference
12
 
13
- This Static Space runs the exported ONNX classifier in the visitor's browser with ONNX Runtime Web. No server-side CPU/GPU is used and input text is not sent to a backend.
 
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>Try the fine-tuned BERT classifier directly in your browser. The ONNX model runs locally with ONNX Runtime Web, so your text stays on your device.</p><div class="badge">⚡ Browser-side inference · no server CPU/GPU</div></header>
12
  <div class="grid">
13
- <section class="card"><h2>Classify a message</h2><label class="label" for="text">Text input</label><textarea id="text" placeholder="Write a tweet or sentence..."></textarea><div class="actions"><button class="primary" id="run">Classify locally</button><button class="secondary" id="clear">Clear</button></div><div id="status" class="meta loading">Loading tokenizer and ONNX model…</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>Try real test examples</h2><p class="meta">These IDs and texts come from the original test set. Click one to load it, then classify locally.</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>Experiment snapshot</h2><div class="stat"><div><strong>83.85%</strong><small>validation accuracy</small></div><div><strong>2</strong><small>training epochs</small></div><div><strong>4 GPU</strong><small>CloudML training</small></div><div><strong>6e-7</strong><small>max ONNX/PyTorch diff</small></div></div></section>
16
- <section class="card"><h2>What runs where?</h2><p class="meta">The Static Space serves HTML, JavaScript, tokenizer files and ONNX weights. Your browser downloads them once and performs tokenization and inference locally using WebAssembly. No Gradio container or Hugging Face CPU quota is required.</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.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='Ready · inference runs locally in this browser'}catch(e){status.className='meta error';status.textContent='Could not load browser model: '+e.message}}
26
- async function classify(){if(!session||!tokenizer){status.textContent='Model is still loading…';return}if(!text.value.trim()){status.className='meta error';status.textContent='Enter some text first.';return}status.className='meta loading';status.textContent='Classifying locally…';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='Done · no text was sent to a server'}catch(e){status.className='meta error';status.textContent='Inference error: '+e.message}}
27
- $('run').onclick=classify;$('clear').onclick=()=>{text.value='';$('result').hidden=true;status.textContent='Ready · inference runs locally in this browser'};load();
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:3c61ef86508a0716f14bd7f86dbd442aa3e2c069e515a0c37174f537979a9fb7
3
- size 106894
 
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