AI文本检测模型 是一个基于 BERT 的文本分类模型,能够精准识别文本内容是由人类书写还是 AI(ChatGPT)生成,预测准确率高达 90%。
| 组件 | 版本 |
|---|---|
| NPU | Ascend910 |
| CANN | 25.5.2 |
| PyTorch | 2.9.0 |
| torch-npu | 2.9.0 |
| transformers | 4.57.6 |
| Python | 3.10+ |
本模型为 BertForSequenceClassification,不支持 vLLM 部署。直接使用 transformers 库进行推理。
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_path = "~/jarming/ai-text-detector/model/jarming/ai-text-detector"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path, torch_dtype=torch.float32)
model.eval()
device = "npu:0" # 或 "cpu"、"cuda:0"
model.to(device)
text = "今天天气真不错,适合出去走走。"
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
pred = torch.argmax(probs, dim=-1).item()
labels = {0: "Human", 1: "ChatGPT"}
print(f"Prediction: {labels[pred]} (confidence: {probs[0][pred].item():.4f})")python3 ~/jarming/ai-text-detector/inference.py预期输出:
Using device: npu:0
Text: 今天天气真不错,适合出去走走。
Prediction: Human (confidence: 0.8977)
Inference test completed successfully!测试条件:10 条测试文本,单条推理延迟(ms)。
| 指标 | CPU | NPU (Ascend910) |
|---|---|---|
| 平均延迟 | 132.22 ms | 7.74 ms |
| 最小延迟 | 108.39 ms | 7.09 ms |
| 最大延迟 | 160.32 ms | 8.12 ms |
| 加速比 | - | 17.08x |
精度要求:与 CPU 误差 < 1%。
| 指标 | 数值 |
|---|---|
| 最大概率差异 | 0.0744% |
| 是否通过 | 是 |
| 所有用例预测一致 | 是 |
10 条测试文本中,CPU 和 NPU 的预测结果完全一致,最大概率差异为 0.0744%,远低于 1% 的要求。
inference.py:NPU 推理脚本eval/accuracy_run.py:精度评测脚本eval/accuracy_run_perf.py:性能评测脚本eval/accuracy_run.log:精度评测日志eval/accuracy_run_perf.log:性能评测日志eval/accuracy.json:精度评测结果eval/performance.json:性能评测结果