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:
Web Crawler
This upper half aims to build a meidcal conversational dialogue system for automaticly medically responses, and to generate medical context consistent and medically meaningful responses conditioned on the dialogue history1,2.
According to Gururangan, Suchin et al3, when tailoring a pretrained model to the domain of a target task, a second phase of pretraining in-domain leads to performance gains. MedDialog4 provides 3.4 million annotation-free conversations between patients and doctors, which could be used for domain-adaptive pretraining.
There are two availabel Medical Dialogues you can directly use to generate your own NLP model: MedDialog dataset (Chinese) and MedDialog dataset (English). The authors developed their own web crawler but you should update these below codes, because these websites are updating frequentely.
If want to develop your own web crawler, you can try our framework. There is an internet medical diagnosis and treatment suggestion system in China, which can instantly chat with patients. We developed a web crawlerr to exract the medical dialogues between patients and doctors from this website. As we know, there in no such a proper corpus which can train a chatbot with an instant response. Finally, save them as json files.
Step 1:
There are 34 provinces in China, and the list provs in Line 34 is the province list.
Extract the information from this website with:
医生姓名(Doctors’ name),
医院科室(Facalties),
信息中心页(Information Center Links),
地区(Regions). And save the file as ‘step-1-result.csv’.
In step-1-doctorReferLinks.py from Line 147 to Line 150, you can choose which category you want, for example, mental diseases or psychological illness (精神, 心理 diseases) :
1 | includedDepartmentKeywords = ['精神', '心理'] |
Step 2:
You can further extract more information by step-2-doctorMoreLinks.py, which includes:
医生姓名(Doctors’ name),
医生ID(Doctors’ ID),
个人网站(Personal Webpage),
评价分享链接(Evaluation Links),
医院科室(Faculties),
信息中心页(Information Center Links),
地区(Regions). Input the file ‘step-1-result.csv’ saved from the Step 1 and output the file as ‘step-2-result.csv’
Step 3:
We can then develop a web crawler now. Firstly, you should get an account:
config = {
"url": {
"login": "https://passport.haodf.com/user/showlogin",
"target": "https://www.haodf.com/bingcheng/8822504854.html" # https://www.haodf.com/kanbing/6478777497.html
},
"account": {
"username": "userID",
"password": "passward"
}
}
And then, according to the structure of this web’s HTML, you can extract the dialogues.
Step 4:
Translate the Chinese language to English. The efficient way is to use pygtrans, because it has a wider stream than other packages. In line 19, you can make the threshold bigger according to the speed (<100000).
1 | from pygtrans import Translate |
NLP SOTA Models: Conversation
You can find more Text Generation models from Hugging Face. In this blog, we used GPT2 and T5 to respectively trian a medical conversational nlp model and a diagnosis model.
Transformers:
bertGPT:
GPT-2:
T5:
Train Your Medical Conversatinal Model
After you get the medical dialogues which looks like below:
Patient: clubfoot inversion.
Doctor: Use plaster to correct and treat as soon as possible, the sooner the better the effect.Patient: Skin beauty, there is a mole on the lip. In 2007, I had a laser in the old home town hospital, and then it became bigger. I had an electric cautery in a beauty salon 5 months ago, and it became bigger again. I want to consult Dr. Shen and I Is it still possible to remove moles in this case? Can it be cured in your hospital? At first it was the size of a needle tip, but now it is the size of a mung bean!
Doctor: It can be surgery or laser.Patient: Seborrheic dermatitis, there are many small spots in the T-shaped area, which are not easy to see in sunlight, but are particularly conspicuous in a brighter environment. Especially under the eyes and the bridge of the nose, it feels like a lot of pockmarks, and it is not smooth to the touch.
Doctor: It is recommended to go to the hospital for a diagnosis and then to treat it.
You can find the training source codes from my github repository - Medical-Dialogue-Chatbot.
This is the batch file with the environment:
1 | #!/bin/sh |
Test Your Model
After crawling medical dialogues from two websites onlone, and merged them with MedDialog and MedDG. Althrough the Chatbot trained by GPT2 can generate reasonabel responses, the trustworthiness of medical Chatbot and the operability of the generated answers still under tests.
Example 1:
USER: Seborrheic dermatitis, there are many small spots in the T-shaped area, which are not easy to see in sunlight, but are particularly conspicuous in a brighter environment. Especially under the eyes and the bridge of the nose, it feels like a lot of pockmarks, and it is not smooth to the touch.
CHATBOT: Sometimes the bumps are a little more painful, but it is also quite normal as well. It’s normal with acne because you’re not expecting. It’s normal at that point. If you are like me and were like me, it never occurred to us that all this scarring was inevitable. In all likelihood, we are pretty normal when we’re acne prone. I don’t think it was easy to cut all the bumps. In fact, I did it for a few years before having
BUNCHMARK: It is recommended to go to the hospital for a diagnosis and then to treat it.
Example 2:
USER: I have undergone surgery for congenital heart disease. The child has suffered from syncope seven times. There is no cause for the onset. He just said that he felt uncomfortable before the attack, and then slowly fell down and lost consciousness.
CHATBOT: This was my youngest child. It is one of the more serious types of congenital heart disease, caused by a bacterial infection. This is extremely painful for him. It can lead to sudden death; he is unable to see and cannot keep up with the movements of his limbs. Tamil Nadu in 1992. Tamil Nadu, 2004 Indian Institute of Medical Sciences, Chennai Tamil Nadu Hospital, Chennai Tamil Nadu: Tamil Nadu. Tamil Nadu: The child who was born to me has also been seriously
BUNCHMARK: It is recommended to come to our hospital for further examination such as upright tilt test and flat panel test.
Reference: