Skip to content

dsdanielpark/gpt2-bert-medical-qa-chat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Development Status :: 3 - Alpha
Copyright (c) 2023 MinWoo Park

GPT-BERT Medical QA Chatbot

Contributor Covenant Python Version Code convention Black Fomatter

Be careful when cloning this repository: It contains large NLP model weight. (>0.45GB, git-lfs)
If you want to clone without git-lfs, use this command before git clone. The bandwidth provided by git-lfs for free is only 1GB per month, so there is almost no chance that a 0.45GB git-lfs download will work. So please download it manually.

git lfs install --skip-smudge &
export GIT_LFS_SKIP_SMUDGE=1

Since the advent of Chat GPT-4, there have been significant changes in the field. Nevertheless, Chat GPT-2 and Chat GPT-3 continue to be effective in specific domains as large-scale auto-regressive natural language processing models. This repository aims to qualitatively compare the performance of Chat GPT-2 and Chat GPT-4 in the medical domain, and estimate the resources and costs needed for Chat GPT-2 fine-tuning to reach the performance level of Chat GPT-4. Additionally, it seeks to assess how well up-to-date information can be incorporated and applied.

Although a few years behind GPT-4, the ultimate goal of this repository is to minimize costs and resources required for updating and obtaining usable weights after acquiring them. We plan to design experiments for few-shot learning in large-scale natural language processing models and test existing research. Please note that this repository is intended for research and practice purposes only, and we do not assume responsibility for any usage.

Additionally, this repository ultimately aims to achieve similar qualitative and quantitative performance as GPT-4 in certain domain areas through model lightweighting and optimization. For more details, please refer to my technical blog.

Keywords: GPT-2, Streamlit, Vector DB, Medical







Contents








Quick Start

Command-Line Interface

You can chat with the chatbot through the command-line interface using the following command.

git clone https://github.com/DSDanielPark/medical-qa-bert-chatgpt.git
cd medical-qa-bert-chatgpt
pip install -e .
python main.py


Streamlit application

A simple application can be implemented with streamlit as follows:

git clone https://github.com/DSDanielPark/medical-qa-bert-chatgpt.git
cd medical-qa-bert-chatgpt
pip install -e .
streamlit run chatbot.py

Docker

Check Docker Hub: https://hub.docker.com/r/parkminwoo91/medical-chatgpt-streamlit-v1
Docker version 20.10.24, build 297e128

Build from Docker Image

git clone https://github.com/DSDanielPark/medical-qa-bert-chatgpt.git
cd medical-qa-bert-chatgpt
docker build -t chatgpt .
docker run -p 8501:8501 -v ${PWD}/:/usr/src/app/data chatgpt     # There is no cost to pay for git-lfs, just download and mount it.
Since git clone downloads what needs to be downloaded from git-lfs, the volume must be mounted as follows. Or modify chatbot/config.py to mount to a different folder.

Build from Docker Compose

You can also implement it in a docker container like this:

git clone https://github.com/DSDanielPark/medical-qa-bert-chatgpt.git
cd medical-qa-bert-chatgpt

docker compose up

Build from Docker Hub

docker pull parkminwoo91/medical-chatgpt-streamlit-v1:latest
docker compose up

http://localhost:8501/

Streamlit is very convenient and quick to view landing pages, but lacks design flexibility and lacks control over the application layout. Also, if your application or data set is large, the entire source code will be re-run on every new change or interaction, so application flow can cause speed issues. That landing page will be replaced by flask with further optimizations. Streamlit chatbot has been recently developed, so it seems difficult to have the meaning of a simple demo now.

Pre-trained model infomation

Pre-trained model weight needed Downloading datasets and model weights through the Hugging Face Hub is executed, but for some TensorFlow models, you need to manually download and place them at the top of the project folder. The information for the downloadable model is as follows, and you can visit my Hugging Face repository to check it.

modules/chatbot/config.py

class Config:
    chat_params = {"gpt_tok":"danielpark/medical-QA-chatGPT2-tok-v1",
                   "tf_gpt_model":"danielpark/medical-QA-chatGPT2-v1",
                   "bert_tok":"danielpark/medical-QA-BioRedditBERT-uncased-v1",
                   "tf_q_extractor": "question_extractor_model",
                   "data":"danielpark/MQuAD-v1",
                   "max_answer_len": 20,
                   "isEval": False,
                   "runDocker":True, # Exceeds the bandwidth of git-lfs, mounts to local storage to find folder location for free use. I use the python utifunction package.
                   "container_mounted_folder_path": "/usr/src/app/data"} 

Dataset

The Medical Question and Answering dataset(MQuAD) has been refined, including the following datasets. You can download it through the Hugging Face dataset. Use the DATASETS method as follows. You can find more infomation at here.

from datasets import load_dataset
dataset = load_dataset("danielpark/MQuAD-v1")

Medical Q/A datasets gathered from the following websites.

  • eHealth Forum
  • iCliniq
  • Question Doctors
  • WebMD Data was gathered at the 5th of May 2017.

Pretrained Models

Hugging face pretrained models

TensorFlow models for extracting context from QA. I temporarily share TensorFlow model weights through my personal Google Drive.


Cites

@misc {hf_canonical_model_maintainers_2022,
        author       = { {HF Canonical Model Maintainers} },
        title        = { gpt2 (Revision 909a290) },
        year         = 2022,
        url          = { https://huggingface.co/gpt2 },
        doi          = { 10.57967/hf/0039 },
        publisher    = { Hugging Face }
}

@misc{vaswani2017attention,
      title = {Attention Is All You Need}, 
      author = {Ashish Vaswani and Noam Shazeer and Niki Parmar and Jakob Uszkoreit and Llion Jones and Aidan N. Gomez and Lukasz Kaiser and Illia Polosukhin},
      year = {2017},
      eprint = {1706.03762},
      archivePrefix = {arXiv},
      primaryClass = {cs.CL}
}

How to cite this project

@misc{medical_qa_bert_chatgpt,
      title  = {Medical QA Bert Chat GPT}, 
      author = {Minwoo Park},
      year   = {2023},
      url    = {https://github.com/dsdanielpark/medical-qa-bert-chatgpt},
}

Tips

About data handling

The MQuAD provides embedded question and answer arrays in string format, so it is recommended to convert the string-formatted arrays into float format as follows. This measure has been applied to save resources and time used for embedding.

from datasets import load_dataset
from utilfunction import col_convert
import pandas as pd

qa = load_dataset("danielpark/MQuAD-v1", "csv")
df_qa = pd.DataFrame(qa['train'])
df_qa = col_convert(df_qa, ['Q_FFNN_embeds', 'A_FFNN_embeds'])

About Tensorflow-GPU handling

Since the nvidia GPU driver fully supports wsl2, the method of supporting TensorFlow's gpu has changed. Please refer to the following pages to install it.


Remark

I have trained the model for 2 epochs using the mentioned dataset, utilizing 40 computing units from Google Colab Pro. The training was conducted for about 12 hours using an A100 multi-GPU with 56 GB of RAM or more. In the case of relatively simple question extractor or answer extractor models that perform summarization and indexing, the time required for training is minimal, and they are included in the inference module to evaluate whether the learning has been carried out appropriately. If the model is only responding to simple questions, the inference module should be changed; however, it is currently included in the evaluation unnecessarily to check performance and calculate the time and resources consumed. I plan to update this information once sufficient training is completed (by incorporating additional datasets), or when funding for experiments and resources to derive adequate learning.

  • Training 2 Epoch with MQuAD dataset, Comsuming 40 Google Colab Pro Computing unit, Take 12 hours using an A100 multi-GPU with 56 GB of RAM or more.

References

  1. Paper: Attention is All You Need
  2. Paper: BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
  3. Paper: GPT-2: Language Models are Unsupervised Multitask Learners
  4. Paper: Language Models are Unsupervised Multitask Learners
  5. GitHub Repository: DocProduct
  6. Applied AI Course
  7. Medium Article: Medical Chatbot using BERT and GPT-2
  8. GitHub Repository: Medical Question Answer Data
  9. Hugging Face Model Hub: GPT-2
  10. GitHub Repository: Streamlit Chat
  11. Streamlit Documentation
  12. Streamlit Tutorial: Deploying Streamlit Apps with Docker
  13. ChatterBot Documentation
  14. Blog Post: 3 Steps to Fix App Memory Leaks
  15. Blog Post: Common App Problems & Resource Limits
  16. GitHub Gist: Streamlit Chatbot Example (Lightweight and optimized)
  17. Databricks Blog: Democratizing Magic: ChatGPT and Open Models
  18. GitHub Repository: Pyllama

About

Medical domain-focused GPT-2 fine-tuning, optimization, and lightweighting research repository (compared to GPT-4).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published