Medical Conversation and Diagnosis Chatbot: Diagnosis (B)

Medical Chatbot

Posted by Cheng Kang on 2022-03-29

This blog aims to show you how to get your own medical chatbot. There are two parts:
A. Medical Conversation with Patients
B. Symptomes Extraction and Diseases Diagnozing

You can find the codes from this:


  1. Proper Medical Data
  2. Chanllenge and Solution
  3. Train Your Medical Diagnosis NLP Model
  4. Test Your Model
  5. Deploy a Medical Chatbot

Proper Medical Data

This lower half targets on building task-oriented dialogue system for automatic medical diagnosis 1,2,3 , which converses with patients to collect additional symptoms beyond their self-reports and makes a disease diagnosis in the end.

MedDialog4 provides 3.4 million annotation-free conversations between patients and doctors, which could be used for domain-adaptive pretraining. In this project, we can use medical chatbot to extract symptomes from dialogues between patients and doctors. We downloaded this medical dialogue data from ICLR 2021 Workshop: Machine Learning for Preventing and Combating Pandemics which aims to generate context consistent and medically meaningful responses conditioned on the dialogue history. MedDG is a large-scale entity-centric medical dialogue dataset related to 12 types of common gastrointestinal diseases, with more than 17K conversations and 385K utterances collected from the online health consultation community. Each conversation is annotated with five different categories of entities, including diseases, symptoms, attributes, tests, and medicines. For more details about this dataset, please refer to this preprint.


Chanllenge and Solution

We are now preparing our articel which discussed about two solutions to fix symptoms extraction problems. During chatting with the users, Chatbot, on the one hand, should generate the conversation, on the other hand, it also should extract positive and negative symtoms. In our study, one available way is to use two models. Let one model judge the positive or negative, and let another one extract symptoms.


Train Your Medical Diagnosis NLP Model

Below is the data strcture. The disadvantage of this medical dialogue data is that it did not provide the labels which contain patients symptoms, and it only labeled whether there is a symptom word in the phrase.

In our following research, we relabeled this medical data again to avoid chaos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{'id': 'Patients', 'Sentence': '你好,肚脐周围隐隐作痛,不知道怎么回事(女,29岁)', 'Symptom': ['腹痛'], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []}, 
{'id': 'Doctor', 'Sentence': '你好,这种情况多长时间了?', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': ['时长'], 'Disease': []},
{'id': 'Patients', 'Sentence': '两三天了。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '隐隐作痛,疼一会就不疼了。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '有没有用过什么药?做过什么检查吗。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '没有用药,也没有检查。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '大便正常吗。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '正常的。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '还有其他症状吗?恶心想吐吗。', 'Symptom': ['恶心', '呕吐'], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '没有。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '是隐隐约约的疼吗。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '食欲也好的,稍微有点腹胀。', 'Symptom': ['腹胀'], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '可能是胃肠功能紊乱。', 'Symptom': ['胃肠功能紊乱'], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '是的,隐隐约约的疼。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '吃点曲美步汀试试看。亲。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '感觉被针扎了一下,几秒钟就好了。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '感觉问题不是特别大。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '您吃我说的药物试试看。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '家里有个复方仙鹤草肠炎片能吃嘛吗。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': ['肠炎']},
{'id': 'Doctor', 'Sentence': '这个主要治腹泻的。没拉肚子不用吃。', 'Symptom': ['腹泻', '腹泻'], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '哦哦,好的。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '嗯嗯。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '哪方面会引起胃肠功能絮乱。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '可能是饮食方面可能是精神因素也可能是自身免疫系统。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '也可能是自身免疫系统消化系统。亲。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '好的。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '嗯嗯。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '好像有点像拉肚子,中午一吃完饭1,没过一会就想拉,稍微有点拉。', 'Symptom': ['腹泻'], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '那可以吃点您说的那个药物。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Patients', 'Sentence': '哦,好的。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []},
{'id': 'Doctor', 'Sentence': '嗯嗯。', 'Symptom': [], 'Medicine': [], 'Test': [], 'Attribute': [], 'Disease': []}]

Before training, we extract the conversation between Doctors and Patients, as well as binary labels and multi-symtomes labels by data_preprocessing_track1.py. After this, we separate the dataset into train, train, validation.

Config of T5 in t5_symtoms_extract_binary.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
args_dict = dict(
data_dir="", # path for data files
output_dir="", # path to save the checkpoints
model_name_or_path=t5_model,
tokenizer_name_or_path=t5_model,
max_seq_length=512,
learning_rate=3e-4,
weight_decay=0.0,
adam_epsilon=1e-8,
warmup_steps=0,
train_batch_size=16,
eval_batch_size=8,
num_train_epochs=20,
gradient_accumulation_steps=16,
n_gpu=gpu_number,
# early_stop_callback=False,
fp_16=False, # if you want to enable 16-bit training then install apex and set this to true
opt_level='apex', # you can find out more on optimisation levels here https://nvidia.github.io/apex/amp.html#opt-levels-and-properties
max_grad_norm=1.0, # if you enable 16-bit training then set this to a sensible value, 0.5 is a good default
seed=42,
)

This is the batch file with the developing environment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/sh
#SBATCH --partition=gpufast
#SBATCH --time=4:00:00
#SBATCH --gres=gpu:1
#SBATCH --mem-per-gpu=32000
#SBATCH --job-name=T5-C
#SBATCH --err=T5-classifier.err
#SBATCH --out=T5-classifier.out
#SBATCH --mail-user=kangchen@cvut.cz # where send info about job
#SBATCH --mail-type=ALL # what to send, valid type values are NONE, BEGIN, END, FAIL, REQUEUE, ALL
/bin/hostname
srun -l /bin/hostname
srun -l /bin/pwd
ml load Python/3.7.2-fosscuda-2019a
cd python_script/ICLR-2021/
source TestEnv/bin/activate
python t5_symtoms_extract_multi.py

Test Your Model

Chatbot in you computer by excuting chatbot_gui.py.
There is an unsuccessful example which can present our onging research. We trained our incompletely labeled medical dialogue dataset with t5 (text-to-text transfer transformer) model, but the performance is not acceptabel. The next step is to relabel our dataset again and fine-tune t5 model.

Doctor: Do you have a stomach ache every time after eating, is your stomach bloated? Has the constipation started recently, or have you been constipated all the time?
Patients: It will be very bloated after eating…it will also be bloated and painful at other times! It also started this time… I was not constipated before. Detected Symptoms: [‘asymptomatic’]
Doctor: Hi! How long have you been like this?
Patients: One week ~ I have been taking Shugan and Wei Wan for two days…it’s a little better…but it still hurts…still constipated. Detected Symptoms: [‘stomach ache’, ‘bloating’]


Deploy a Medical Chatbot

How to deploy your model in a HTML web is pending…


Reference:

[1] Guangtao Zeng, Wenmian Yang, Zeqian Ju, Yue Yang, Sicheng Wang, Ruisi Zhang, Meng Zhou, Jiaqi Zeng, Xiangyu Dong, Ruoyu Zhang, Hongchao Fang, Penghui Zhu, Shu Chen, Pengtao Xie. MedDialog: Large-scale Medical Dialogue Datasets. In EMNLP, 2020.

[2] Zhongyu Wei, Qianlong Liu, Baolin Peng, Huaixiao Tou, Ting Chen, Xuanjing Huang, Kam-fai Wong, Xiangying Dai. Task-oriented Dialogue System for Automatic Diagnosis. In ACL, 2018.

[3] Lin Xu, Qixian Zhou, Ke Gong, Xiaodan Liang, Jianheng Tang, Liang Lin. End-to-End Knowledge-Routed Relational Dialogue System for Automatic Diagnosis. In AAAI, 2019.

[4] Yuan Xia, Jingbo Zhou, Zhenhui Shi, Chao Lu, Haifeng Huang. Generative Adversarial Regularized Mutual Information Policy Gradient Framework for Automatic Diagnosis. In AAAI 2020.