Release frozen D512 ONNX weights under MIT
Browse filesIncludes the cleaned ONNX model, vocabulary, inference manifest, MIT license, and model card with CPU inference examples.
Files changed (5)
- LICENSEAdded+21
- README.mdAdded+128
- manifest.jsonAdded+28
- model.onnxAdded+3
- vocab.jsonAdded
123456789101112131415161718192021MIT License
Copyright (c) 2026 Funmula Corp Limited
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128---license: mitlanguage:- zhlibrary_name: onnxruntimepipeline_tag: token-classificationtags:- onnx- punctuation-restoration- input-method- traditional-chinese---
# 靈犀注音(0c)D512 補標點模型(雙向 encoder)
為沒有標點的繁體中文片段補上標點。只插入標點、不改動原字,段尾不補。標籤:, 。 ? ! 、 ;,另有一類「不插入」。
- 架構:8 層、hidden 512、8 heads、FFN 768 的雙向 Transformer encoder,從零預訓練的 D512 骨幹改為雙向注意力後微調- 窗口 128 字、步長 64- 格式:ONNX(opset 18),CPU 推論- 字表:27857 字(`vocab.json`)
## 檔案
| 檔案 | 說明 ||---|---|| `model.onnx` | 標點分類模型,SHA-256 `4db3ed40df777347d9c455da01285b01de192d4c210c5f7cd9f0646c727880d8` || `vocab.json` | 特殊 token 與字表,SHA-256 `556dffaffbf4c05211a846dcee55ef735cea68c5754f9bffcc6f9588124172b3` || `manifest.json` | 標籤、門檻、窗口與檔案雜湊 |
## 評測
匯出核對用的 1171 段開發資料(訓練期間看過,不是未曝光測試集),門檻 0.63:precision 0.897、recall 0.755、F1 0.820。ONNX 與 PyTorch 插入結果逐段相同。
## 安裝
需要 Python 3.9 以上,只用 CPU。
```shpip install onnxruntime numpy huggingface_hub```
第一次執行會從 Hugging Face 下載模型(約 130 MB),之後使用本機快取。
## 使用
下面是完整程式,存成 `punctuate.py` 就能執行。門檻 0.63 是 precision 約 0.9 的操作點;調低會補更多標點,但錯補也會變多。
```pythonimport jsonimport re
import numpy as npimport onnxruntime as ortfrom huggingface_hub import snapshot_download
path = snapshot_download("Funmula/0c-punct-d512")manifest = json.load(open(f"{path}/manifest.json", encoding="utf-8"))vocab = json.load(open(f"{path}/vocab.json", encoding="utf-8"))CHARS = {c: i + 8 for i, c in enumerate(vocab["characters"])} # 0–7 是特殊 token,4 = UNKLABELS = manifest["labels"] # 第 0 類 = 不插入THRESHOLD, WINDOW, STRIDE = manifest["threshold"], manifest["window"], manifest["stride"]session = ort.InferenceSession(f"{path}/model.onnx", providers=["CPUExecutionProvider"])
URL = re.compile(r"(?:https?://|www\.)[^\s,。!?、;「」『』()【】]+", re.I)ENUMERATORS = set("一二三四五六七八九十壹貳參肆伍陸柒捌玖拾甲乙丙丁戊己庚辛壬癸")
def is_han(c): return "㐀" <= c <= "鿿" or "\U00020000" <= c <= "\U000323af"
def allowed(text): # 第 i 個字之後可插標點:該字是漢字且不在網址內,緊接的字是漢字或空白,下一個可見字元是漢字或英數字。 protected = {i for m in URL.finditer(text) for i in range(m.start(), m.end())} out = [] for i, c in enumerate(text): nxt = text[i + 1:i + 2] visible = text[i + 1:].lstrip()[:1] out.append(is_han(c) and i not in protected and (not nxt or is_han(nxt) or nxt.isspace()) and (not visible or is_han(visible) or visible.isascii() and visible.isalnum())) return out
def window_probs(text): ids = np.array([[1, 7] + [CHARS.get(c, 4) for c in text]], dtype=np.int64) # [BOS][任務] 原文 return session.run(["probs"], {"ids": ids})[0][2:] # 第 i 列 = 第 i 個字之後各標籤的機率
def probabilities(text): # 超過一個窗口時滑動,每個位置取離窗口邊緣最遠的那一窗。 n = len(text) starts = [0] if n <= WINDOW else list(range(0, n - WINDOW, STRIDE)) + [n - WINDOW] out, best = [None] * n, [-1] * n for s in starts: probs = window_probs(text[s:s + WINDOW]) for j in range(len(probs)): margin = min(j, len(probs) - 1 - j) if n > WINDOW else 0 if margin > best[s + j]: best[s + j], out[s + j] = margin, probs[j] return np.stack(out)
def punctuate(text): # 只插入標點、不改原字;段尾不補。 if not text: return text ok, probs = allowed(text), probabilities(text) out = [] for i, c in enumerate(text): out.append(c) if i == len(text) - 1 or not ok[i]: continue k = int(probs[i, 1:].argmax()) + 1 if i == 0 and LABELS[k] == "、" and c in ENUMERATORS: # 開頭單個數字後不補頓號 continue if probs[i, k] >= THRESHOLD: out.append(LABELS[k]) return "".join(out)
print(punctuate("好的我知道了明天見")) # 好的我知道了,明天見```
輸入法內每次最多送 256 字;上面的程式本身沒有長度限制。12345678910111213141516171819202122232425262728{ "schema": "zuin-punct-runtime-v1", "labels": [ "", ",", "。", "?", "!", "、", ";" ], "threshold": 0.63, "window": 128, "stride": 64, "input": "bidirectional", "end_of_text": "never_inserted", "max_text_runes": 256, "files": { "model.onnx": "4db3ed40df777347d9c455da01285b01de192d4c210c5f7cd9f0646c727880d8", "vocab.json": "556dffaffbf4c05211a846dcee55ef735cea68c5754f9bffcc6f9588124172b3" }, "spec": { "hidden": 512, "layers": 8, "heads": 8, "intermediate": 768 }}123version https://git-lfs.github.com/spec/v1oid sha256:4db3ed40df777347d9c455da01285b01de192d4c210c5f7cd9f0646c727880d8size 130584075LICENSEAdded+21
README.mdAdded+128
manifest.jsonAdded+28
model.onnxAdded+3
vocab.jsonAdded