Views
No views yet
| Identification | General Chinese | Traditional Chinese | Simplified Chinese | Classical Chinese | Cantonese |
|---|---|---|---|---|---|
| ZHLID (ours) | ✅ | ✅ | ✅ | ✅ | ✅ |
| langdetect | ✅ | ✅ | ✅ | ❌ | ❌ |
| GlotLID | ✅ | ❌ | ❌ | ❌ | ✅ |
| langid.py | ✅ | ❌ | ❌ | ❌ | ❌ |
| CLD3 | ✅ | ❌ | ❌ | ❌ | ❌ |
| Lingua | ✅ | ❌ | ❌ | ❌ | ❌ |
transformers with version higher than v4.48.0:pip install -U transformers>=4.48.0pip install flash-attn --no-build-isolationpipeline function in transformers:1from transformers import pipeline
2
3pipe = pipeline("text-classification", model="MusubiAI/ZHLID")
4text = "孔子\n大成至圣先师孔丘,字仲尼,子姓,孔氏,敬称孔子、孔夫子,生于鲁昌平乡陬邑。"
5
6res = pipe(text)
7print(res)
8# [{'label': 'zhcn_classical', 'score': 0.9998414516448975}]AutoModelForSequenceClassification:1import torch
2import torch.nn as nn
3from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
5model_id = "MusubiAI/ZHLID"
6model = AutoModelForSequenceClassification.from_pretrained(model_id)
7tokenizer = AutoTokenizer.from_pretrained(model_id)
8id2label = model.config.id2label
9
10text = "孔子\n大成至圣先师孔丘,字仲尼,子姓,孔氏,敬称孔子、孔夫子,生于鲁昌平乡陬邑。"
11inputs = tokenizer(text, return_tensors="pt")
12outputs = model(**inputs)
13
14with torch.no_grad():
15 logits = model(**inputs)["logits"]
16
17scores = nn.functional.softmax(logits, dim=-1)
18pred_score, pred_index = torch.max(scores, dim=-1)
19pred_score = pred_score.item()
20pred_index = pred_index.item()
21label = id2label[pred_index]
22prediction = {"label": label, "confidence_score": pred_score}
23print(prediction)
24# {'label': 'zhcn_classical', 'confidence_score': 0.99983811378479}vllm is also available:1from vllm import LLM
2import torch
3import torch.nn.functional as F
4
5
6llm = LLM(model="MusubiAI/ZHLID", task="classify")
7
8
9text = "孔子\n大成至圣先师孔丘,字仲尼,子姓,孔氏,敬称孔子、孔夫子,生于鲁昌平乡陬邑。"
10
11output = llm.classify(text)[0]
12probs = output.outputs.probs
13probabilities = torch.tensor(output.outputs.probs)
14
15# Get the top predicted class
16top_idx = torch.argmax(probabilities).item()
17top_prob = probabilities[top_idx].item()
18
19print(f"Confidence: {top_prob:.4f}")
20
21id2label = {
22 "0": "yue",
23 "1": "zhcn_classical",
24 "2": "zhtw_classical",
25 "3": "zhcn",
26 "4": "zhtw"
27}
28
29label = id2label[str(top_idx)]
30print(label)| Top-1 accuracy | Traditional Chinese | Simplified Chinese | Classical Chinese (Traditional) | Classical Chinese (Simplified) | Cantonese |
|---|---|---|---|---|---|
| ZHLID (ours) | 1.0 | 1.0 | 0.9 | 1.0 | 0.96 |
| GlotLID | 0.98 | 0.98 | - | - | 0.9 |
| langdetect | 0.3 | 0.9 | - | - | - |
1@misc{zhlid2025 ,
2 title = {ZHLID: Fine-grained Chinese Language Identification Package},
3 author = {Lung-Chuan Chen},
4 year = {2025},
5 howpublished = {\url{https://github.com/Musubi-ai/ZHLID}}
6}