Baidu LAC (Chinese lexical analysis) running on ONNX Runtime — no PaddlePaddle required
Project description
LAC-ONNX
百度 LAC(Lexical Analysis of Chinese)模型的 ONNX 版本,支持中文分词、词性标注和命名实体识别(NER)。
原始模型来自 PaddleNLP 的 LAC 任务(Taskflow('ner', mode='fast')),使用 Paddle 3.x 静态图导出后转换为 ONNX 格式。CRF 解码层使用 numpy 实现,无需依赖 PaddlePaddle。
特性
- 零 Paddle 依赖 — 仅需
onnxruntime+numpy - 轻量 — 模型文件约 30 MB
- 完整功能 — 分词 + 词性标注 + NER,与原始 LAC 结果完全一致
- 即开即用 — 模型随包安装,无需下载,无需联网
安装
pip install lac-onnx
使用
from lac_onnx import LAC
lac = LAC()
# 单句分析
result = lac.run('张三在北京市工作')
# [('张三', 'PER'), ('在', 'p'), ('北京市', 'LOC'), ('工作', 'n')]
# 批量分析
results = lac.run(['张三在北京', '李四去上海'])
# [[('张三', 'PER'), ...], [('李四', 'PER'), ...]]
# NER 过滤
for word, tag in lac.run('张三在中国银行办理业务'):
if tag in ('PER', 'LOC', 'ORG'):
print(word, tag)
自定义模型目录
lac = LAC(model_dir='/path/to/your/model')
目录下需包含:lac_encoder.onnx、lac_crf_transitions.npy、word.dic、tag.dic、q2b.dic。
标签说明
| 标签 | 含义 | 标签 | 含义 |
|---|---|---|---|
| PER | 人名 | n | 名词 |
| LOC | 地名 | v | 动词 |
| ORG | 机构名 | a | 形容词 |
| TIME | 时间 | m | 数词 |
| nz | 专有名词 | w | 标点符号 |
| p | 介词 | c | 连词 |
| u | 助词 | d | 副词 |
完整标签集参见包内 tag.dic。
模型架构
输入字符 → 字符 ID 编码 → Embedding(128d)
→ 2层双向 GRU(hidden=128) → FC(256→59) ← ONNX 模型
→ CRF Viterbi 解码 ← numpy 实现
→ BIO 标签序列 → 分词 + 标注结果
文件说明
| 文件 | 大小 | 说明 |
|---|---|---|
lac_onnx/lac_encoder.onnx |
30 MB | ONNX 模型(Embedding + BiGRU + FC) |
lac_onnx/lac_crf_transitions.npy |
14 KB | CRF 转移矩阵 |
lac_onnx/word.dic |
745 KB | 字符词表(58224 字符) |
lac_onnx/tag.dic |
425 B | 标签表(57 个 BIO 标签) |
lac_onnx/q2b.dic |
44 KB | 全角→半角字符映射 |
paddle_static/ |
30 MB | 原始 Paddle 静态图模型(仅供参考,不含在 pip 包中) |
转换过程
- 使用 PaddleNLP
Taskflow('ner', mode='fast')下载 LAC 静态图模型 - 从
inference.pdiparams提取权重,重建 Paddle 动态图模型(nn.Embedding+nn.GRU+nn.Linear) - 通过
paddle.jit.save导出新的静态图(不含viterbi_decode算子) - 使用
paddle2onnx转换为 ONNX 格式 - CRF 转移矩阵单独保存为 numpy 文件,运行时用 numpy 实现 Viterbi 解码
许可证
原始 LAC 模型由百度发布,采用 Apache License 2.0 许可。
本仓库的转换代码和示例同样采用 Apache License 2.0。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lac_onnx-0.1.0.tar.gz.
File metadata
- Download URL: lac_onnx-0.1.0.tar.gz
- Upload date:
- Size: 29.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b274900464134b1cddcc4b9acef7135f145240eec367705c4508620ccc7dbecf
|
|
| MD5 |
3180746e0cacc88a4ab8e29726c03ca6
|
|
| BLAKE2b-256 |
95a97b21aab44529559587de072818c46b75c2c80756bce2b731df40cb2252ec
|
Provenance
The following attestation bundles were made for lac_onnx-0.1.0.tar.gz:
Publisher:
workflow.yml on kniost/LAC-ONNX
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lac_onnx-0.1.0.tar.gz -
Subject digest:
b274900464134b1cddcc4b9acef7135f145240eec367705c4508620ccc7dbecf - Sigstore transparency entry: 1316985228
- Sigstore integration time:
-
Permalink:
kniost/LAC-ONNX@d306c5a27f6d6d0bbb2d8c681d468b74f9f01819 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/kniost
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@d306c5a27f6d6d0bbb2d8c681d468b74f9f01819 -
Trigger Event:
release
-
Statement type:
File details
Details for the file lac_onnx-0.1.0-py3-none-any.whl.
File metadata
- Download URL: lac_onnx-0.1.0-py3-none-any.whl
- Upload date:
- Size: 29.9 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9db1b6df4e45eb6d1677ba376f9af298df28d53a2bd4831ef58f330dcc085fc
|
|
| MD5 |
ad135ae782e99d128adf7bff33e1902f
|
|
| BLAKE2b-256 |
37d4efdca780f84a6f181f2defb15590d7943a705185c34bb71bf07f3b558c2c
|
Provenance
The following attestation bundles were made for lac_onnx-0.1.0-py3-none-any.whl:
Publisher:
workflow.yml on kniost/LAC-ONNX
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lac_onnx-0.1.0-py3-none-any.whl -
Subject digest:
b9db1b6df4e45eb6d1677ba376f9af298df28d53a2bd4831ef58f330dcc085fc - Sigstore transparency entry: 1316985238
- Sigstore integration time:
-
Permalink:
kniost/LAC-ONNX@d306c5a27f6d6d0bbb2d8c681d468b74f9f01819 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/kniost
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@d306c5a27f6d6d0bbb2d8c681d468b74f9f01819 -
Trigger Event:
release
-
Statement type: