weixin_72661020/ai-text-detector
模型介绍文件和版本Pull Requests讨论分析

jarming/ai-text-detector 在昇腾 NPU 上的适配与验证

1. 简介

AI文本检测模型 是一个基于 BERT 的文本分类模型,能够精准识别文本内容是由人类书写还是 AI(ChatGPT)生成,预测准确率高达 90%。

  • 模型类型:BertForSequenceClassification
  • 标签:Human / ChatGPT
  • 支持语言:中文
  • 推理框架:PyTorch + Transformers
  • 权重下载地址(ModelScope):https://modelscope.cn/models/jarming/ai-text-detector

2. 验证环境

组件版本
NPUAscend910
CANN25.5.2
PyTorch2.9.0
torch-npu2.9.0
transformers4.57.6
Python3.10+

3. 推理方式

本模型为 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})")

4. Smoke 验证

python3 ~/jarming/ai-text-detector/inference.py

预期输出:

Using device: npu:0
Text: 今天天气真不错,适合出去走走。
Prediction: Human (confidence: 0.8977)
Inference test completed successfully!

5. 性能参考

测试条件:10 条测试文本,单条推理延迟(ms)。

指标CPUNPU (Ascend910)
平均延迟132.22 ms7.74 ms
最小延迟108.39 ms7.09 ms
最大延迟160.32 ms8.12 ms
加速比-17.08x

6. 精度评测

精度要求:与 CPU 误差 < 1%。

指标数值
最大概率差异0.0744%
是否通过是
所有用例预测一致是

10 条测试文本中,CPU 和 NPU 的预测结果完全一致,最大概率差异为 0.0744%,远低于 1% 的要求。

7. 交付物

  • 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:性能评测结果

8. 注意事项

  • 模型为 BertForSequenceClassification 分类模型,不支持 vLLM 部署
  • 直接使用 transformers 库和 AutoModelForSequenceClassification 加载
  • 使用 torch.float32 精度即可,无需量化
  • NPU 推理时需确保 torch_npu 已正确安装
下载使用量0