概述

本模型是基于 bert-base-chinese 预训练模型进行微调得到的,专注于中文糖尿病领域问答任务,旨在为用户提供准确且高效的糖尿病相关问题意图的识别。

数据集

我们使用了 DaCorp 数据集进行训练,该数据集来源于中文糖尿病问题分类体系及标注语料库。它涵盖了 6 个大类和 23 个细类,构建了一个包含 122732 个问答对的丰富中文糖尿病问答语料库。此外,其中 8000 个糖尿病问题经过了人工精细标注,形成了一个细粒度的糖尿病标注数据集,为模型的训练提供了高质量且具有针对性的数据支持,使得模型能够更好地理解和回答糖尿病相关的各类问题。

大类 细类 数量
A: Diagnosis (诊断) A0: other (其他) 16
A1: interpretation of clinical (临床解释) 344
A2: symptom/manifestations (症状/表现) 292
A3: test (检查) 65
B: Treatment (治疗) B0: other (其他) 388
B1: how to use drug (如何用药) 101
B2: drug choice (药物选择) 514
B3: adverse effects of drug (药物副作用) 69
B4: contraindications of drug (药物禁忌) 4
B5: Other Therapy (其他治疗方法) 162
B6: Treatment Seeking (寻求治疗) 788
C: Common Knowledge (常识) C0: other (其他) 527
C1: Definition (定义) 152
C2: Etiology (病因学) 860
C3: Fertility (生育力) 67
C4: Hereditary (遗传性) 49
D: Healthy lifestyle (健康生活方式) D0: other (其他) 43
D1: Diet (饮食) 2126
D2: Exercise (锻炼) 50
D3: weight-losing (减肥) 7
E: Epidemiology (流行病学) E1: Infect (传染性) 31
E2: Prevention (预防) 34
E3: Complication (并发症) 742
F: Other (其他) 569

模型

本模型以bert-base-chinese作为基础架构,在此基础上进行了微调。通过对 DaCorp 数据集中的问句与意图进行训练,模型学习到了糖尿病领域问答的特定模式和语义信息,从而能够准确地对输入的糖尿病相关问题进行理解,为用户提供专业的糖尿病问句意图识别服务。

用法

加载模型

def load_prediction_model(model_path, device):
  '''加载训练好的模型、分词器和标签编码器'''
  model = BertForSequenceClassification.from_pretrained(model_path)
  tokenizer = BertTokenizer.from_pretrained(model_path)
  label_encoder = torch.load(f"{model_path}/label_mapping.pth")
  model.to(device)
  return model, tokenizer, label_encoder

进行预测

  import torch
  from transformers import BertTokenizer, BertForSequenceClassification
  from sklearn.preprocessing import LabelEncoder
  
  
  def load_prediction_model(model_path, device):
      '''加载训练好的模型、分词器和标签编码器'''
      model = BertForSequenceClassification.from_pretrained(model_path)
      tokenizer = BertTokenizer.from_pretrained(model_path)
      label_encoder = torch.load(f"{model_path}/label_mapping.pth")
      model.to(device)
      return model, tokenizer, label_encoder
  
  
  def predict_intent(text, model, tokenizer, label_encoder, device, max_len=128):
      """执行预测"""
      encoding = tokenizer(
          text,
          truncation=True,
          padding="max_length",
          max_length=max_len,
          return_tensors="pt",
      )
      input_ids = encoding["input_ids"].to(device)
      attention_mask = encoding["attention_mask"].to(device)
  
      model.eval()
      with torch.no_grad():
          outputs = model(input_ids=input_ids, attention_mask=attention_mask)
          pred = torch.argmax(outputs.logits, dim=1).item()
  
      return label_encoder.inverse_transform([pred])[0]
  
  
  if __name__ == "__main__":
      # 配置参数
      MODEL_PATH = "bert-base-Diabetes-question-intent"  # 模型路径
      MAX_LEN = 128
      DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  
      # 加载模型
      model, tokenizer, label_encoder = load_prediction_model(MODEL_PATH, DEVICE)
      print("模型加载完成,输入end退出")
  
      # 预测
      while True:
          text = input("\n请输入问题文本:")
          if text.lower() in ["end"]:
              break
          if not text.strip():
              continue
  
          intent = predict_intent(text, model, tokenizer, label_encoder, DEVICE, MAX_LEN)
          print(f"预测意图: {intent}")
Downloads last month
9
Safetensors
Model size
0.1B params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support