Spaces:
Running
Running
Download index.html from setariaa/nlp-sms-classifier: direct link, hf CLI and curl.
- Browser
- Download file 5.81 kB
-
https://huggingface.co/spaces/setariaa/nlp-sms-classifier/resolve/main/index.html
- Command line
-
hf download hf://spaces/setariaa/nlp-sms-classifier/index.html
-
curl -L -o index.html https://huggingface.co/spaces/setariaa/nlp-sms-classifier/resolve/main/index.html
5.81 kB
| <html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>NLP 实验 · 英文短信分类</title> | |
| <style>*{box-sizing:border-box}body{margin:0;background:#f4f7fb;color:#233349;font:16px/1.7 system-ui,"Microsoft YaHei",sans-serif}main{max-width:1000px;margin:48px auto;padding:0 24px}.eyebrow{color:#476a96;font-size:13px;letter-spacing:2px}h1{font-size:34px;line-height:1.3;margin:12px 0}p{color:#64748b}.stats{display:flex;gap:12px;flex-wrap:wrap;margin:24px 0}.stat{background:#e8eef7;padding:10px 18px;border-radius:12px}.stat b{color:#234a7b}.card{background:white;border:1px solid #dce4ef;border-radius:18px;padding:28px;box-shadow:0 8px 28px #dce4ef66}label{display:block;font-weight:600;margin-bottom:10px}textarea{width:100%;height:135px;padding:14px;border:1px solid #bdcce0;border-radius:10px;font:15px/1.5 inherit;resize:vertical}button{border:0;border-radius:9px;padding:12px 22px;background:#235a94;color:white;font-size:15px;cursor:pointer}button:disabled{opacity:.5;cursor:wait}.examples{display:flex;flex-wrap:wrap;gap:8px;margin:14px 0 20px}.examples button{background:#eff4fa;color:#345574;font-size:13px;padding:8px 12px}#status{font-size:14px;color:#60748c}#result{margin-top:24px;padding-top:22px;border-top:1px solid #e4ebf3}#result h2{margin:0 0 12px;font-size:23px}.prob{display:flex;justify-content:space-between;margin-top:10px}progress{width:100%;height:12px;accent-color:#3575b6}.links{display:flex;gap:20px;flex-wrap:wrap;margin-top:24px}a{color:#235a94}.note{font-size:13px;margin-top:18px}.error{color:#b3261e!important}footer{font-size:13px;color:#708096;margin:28px 0}@media(max-width:600px){main{margin:24px auto;padding:0 16px}h1{font-size:27px}.card{padding:20px}}</style> | |
| <link rel="icon" href="data:,"><script src="vendor/ort.wasm.min.js"></script></head><body><main><div class="eyebrow">自然语言处理 / CHAPTER 1 · 2 · 5</div><h1>英文短信,正常还是垃圾?</h1><p>BERT 预训练模型微调实验。模型加载后,分类直接在你的浏览器中完成。</p> | |
| <div class="stats"><div class="stat">测试准确率 <b>98.84%</b></div><div class="stat">Spam F1 <b>95.34%</b></div><div class="stat">测试样本 <b>774 条</b></div></div> | |
| <section class="card"><label for="text">输入英文短信</label><textarea id="text" placeholder="输入一条英文短信,或选择下面的示例…"></textarea><div class="examples"><button id="ham">正常短信示例</button><button id="spam">垃圾短信示例</button></div><button id="classify" disabled>模型加载中…</button><p id="status" role="status">正在加载模型(约 18 MB),首次打开需要稍等。</p><div id="result" hidden><h2 id="label"></h2><div class="prob"><span>正常短信 · ham</span><b id="hamValue"></b></div><progress id="hamBar" max="1"></progress><div class="prob"><span>垃圾短信 · spam</span><b id="spamValue"></b></div><progress id="spamBar" max="1"></progress></div><p class="note">适用于英文短信,最多使用前 96 个 token。概率未经校准;测试集表现不保证对所有新短信有效。</p></section> | |
| <nav class="links"><a href="实验报告.html" target="_blank">阅读实验报告</a><a href="作业提交.zip" download>下载完整作业</a><a href="https://huggingface.co/spaces/setariaa/nlp-sms-classifier/tree/main" target="_blank" rel="noreferrer">查看代码与模型</a></nav><footer>模型:prajjwal1/bert-tiny · 数据:UCI SMS Spam Collection · 训练、Prompting 与自定义 Jailbreak 的代码及原始结果见作业包。</footer></main> | |
| <script type="module"> | |
| import {tokenize} from './tokenizer.js'; | |
| const $=id=>document.getElementById(id);let session,vocab; | |
| $('ham').onclick=()=>{$('text').value='Can we meet at the library at six?'}; | |
| $('spam').onclick=()=>{$('text').value='(Bank of Granite issues Strong-Buy) EXPLOSIVE PICK FOR OUR MEMBERS *****UP OVER 300% *********** Nasdaq Symbol CDGT That is a $5.00 per..'}; | |
| async function classify(text){ | |
| const ids=tokenize(text,vocab);const tensor=a=>new ort.Tensor('int64',BigInt64Array.from(a,BigInt),[1,ids.length]); | |
| const output=await session.run({input_ids:tensor(ids),attention_mask:tensor(ids.map(()=>1)),token_type_ids:tensor(ids.map(()=>0))}); | |
| const logits=output.logits.data,m=Math.max(...logits),e=Array.from(logits,x=>Math.exp(x-m)),sum=e.reduce((a,b)=>a+b,0);return e.map(x=>x/sum); | |
| } | |
| $('classify').onclick=async()=>{const text=$('text').value.trim();if(!text){$('status').textContent='请先输入一条英文短信。';return}try{$('classify').disabled=true;$('status').className='';$('status').textContent='正在分类…';const p=await classify(text);$('label').textContent=p[1]>p[0]?'垃圾短信 (spam)':'正常短信 (ham)';for(const [i,name] of ['ham','spam'].entries()){$(name+'Value').textContent=(p[i]*100).toFixed(2)+'%';$(name+'Bar').value=p[i]}$('result').hidden=false;$('status').textContent='分类完成 · 浏览器本地推理'}catch(error){$('status').className='error';$('status').textContent='分类失败:'+error.message}finally{$('classify').disabled=false}}; | |
| try{ort.env.wasm.wasmPaths=new URL('./vendor/',location.href).href;ort.env.wasm.numThreads=1;ort.env.wasm.proxy=false;vocab=await fetch('model/vocab.json').then(r=>{if(!r.ok)throw Error('词表加载失败');return r.json()});session=await ort.InferenceSession.create('model/model.onnx',{executionProviders:['wasm']});$('classify').disabled=false;$('classify').textContent='开始分类';$('status').textContent='模型已就绪 · 输入的短信不会上传到推理服务器';window.classifierReady=true;window.classifyText=classify}catch(error){$('status').className='error';$('status').textContent='模型加载失败,请刷新重试:'+error.message} | |
| </script></body></html> | |