Views
No views yet
1input: "病人:六岁宝宝拉大便都是一个礼拜或者10天才一次正常吗,要去医院检查什么项目\n医生:您好\n病人:六岁宝宝拉大便都是一个礼拜或者10天才一次正常吗,要去医院检查什么项目\n医生:宝宝之前大便什么样呢?多久一次呢\n病人:一般都是一个礼拜,最近这几个月都是10多天\n医生:大便干吗?\n病人:每次10多天拉的很多\n医生:"
2target: "成形还是不成形呢?孩子吃饭怎么样呢?"1import os
2from transformers import T5Tokenizer, T5ForConditionalGeneration, AutoTokenizer
3import torch
4device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5
6
7tokenizer = T5Tokenizer.from_pretrained("scutcyr/BianQue-1.0")
8model = T5ForConditionalGeneration.from_pretrained("scutcyr/BianQue-1.0")
9model = model.to(device)
10
11def preprocess(text):
12 text = text.replace("\n", "\\n").replace("\t", "\\t")
13 return text
14
15def postprocess(text):
16 return text.replace("\\n", "\n").replace("\\t", "\t")
17
18def answer(user_history, bot_history, sample=True, top_p=1, temperature=0.7):
19 '''sample:是否抽样。生成任务,可以设置为True;
20 top_p:0-1之间,生成的内容越多样
21 max_new_tokens=512 lost...'''
22
23 if len(bot_history)>0:
24 context = "\n".join([f"病人:{user_history[i]}\n医生:{bot_history[i]}" for i in range(len(bot_history))])
25 input_text = context + "\n病人:" + user_history[-1] + "\n医生:"
26 else:
27 input_text = "病人:" + user_history[-1] + "\n医生:"
28 return "我是利用人工智能技术,结合大数据训练得到的智能医疗问答模型扁鹊,你可以向我提问。"
29
30
31 input_text = preprocess(input_text)
32 print(input_text)
33 encoding = tokenizer(text=input_text, truncation=True, padding=True, max_length=768, return_tensors="pt").to(device)
34 if not sample:
35 out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=512, num_beams=1, length_penalty=0.6)
36 else:
37 out = model.generate(**encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=512, do_sample=True, top_p=top_p, temperature=temperature, no_repeat_ngram_size=3)
38 out_text = tokenizer.batch_decode(out["sequences"], skip_special_tokens=True)
39 print('医生: '+postprocess(out_text[0]))
40 return postprocess(out_text[0])
41
42answer_text = answer(user_history=["你好!",
43 "我最近经常失眠",
44 "两周了",
45 "上床几小时才睡得着"],
46 bot_history=["我是利用人工智能技术,结合大数据训练得到的智能医疗问答模型扁鹊,你可以向我提问。",
47 "失眠多久了?",
48 "睡眠怎么样?"])1@article{chen2023bianque1,
2 title={BianQue-1.0: Improving the "Question" Ability of Medical Chat Model through finetuning with Hybrid Instructions and Multi-turn Doctor QA Datasets},
3 author={Yirong Chen and Zhenyu Wang and Xiaofen Xing and Zhipei Xu and Kai Fang and Sihang Li and Junhong Wang and Xiangmin Xu},
4 year={2023},
5 url={https://github.com/scutcyr/BianQue}
6}