# Patient Record Summarization ## I. Introduction 目標希望能夠建置一個機器模型,使其能夠輸入病人的護理紀錄資料,整理後輸出一段自然語言做為總結。 ## II. Data 目前,建立模型訓練的流程已存放於[Datarch](https://gitlab.smart-aging.tech/ds/infrastructure/datarch)平台的[ai_textsummarization_finetune](https://gitlab.smart-aging.tech/ds/infrastructure/datarch/-/tree/master/pipelines/ai_textsummarization_finetune)專案中。此流程涵蓋兩個步驟以處理訓練資料: ### 1. [Process Shiftime](https://gitlab.smart-aging.tech/ds/infrastructure/datarch/-/blob/master/pipelines/ai_textsummarization_finetune/process_shiftime.py) 首先,此步驟從organizations table中取得各機構的交班時間。接著,利用該交班時間將shifts table中同一班別的交班紀錄合併。最後,將合併後的資料上傳至GCS平台,以作為模型的label。 此步驟所需使用的資料集包括: - jubo-ai.raw_prod_datahub_mongo.organizations - jubo-ai.raw_prod_datahub_mongo.shifts :::info 在處理資料前,需要將內部測試用的entries(其中internalOrg==False)進行清除,同時合併同班別的交班紀錄。 ::: ### 2. [Make Training Data](https://gitlab.smart-aging.tech/ds/infrastructure/datarch/-/blob/master/pipelines/ai_textsummarization_finetune/make_train_data.py) 下一階段,我們將以機構名稱、患者ID以及班別時間三個條件進行護理紀錄的篩選。對於上述三個條件都相同的紀錄,將其合併成一筆交班紀錄的訓練文字。 在此步驟中所用到的資料集包括: - jubo-ai.raw_prod_datahub_mongo.nursingnotedetails 曾進行過實驗將血糖紀錄、生命跡象等等資訊以rule-base方式轉為文字並加入護理紀錄中(處理方式如下),這對於模型理解該筆訓練資料有正向影響,有關實驗數據的詳細資訊將在後續的模型部分提供。 :::info **String concat:** TP : 35 → 生命徵象 - 體溫異常: 過低(35) ACsugarvalue : 145 → 血糖紀錄 - 飯前AC異常: 過高(145) ::: ### 使用方式 到GCS Composer上的[Airflow Server](https://l664dc221fa6ca907p-tp.appspot.com/home)打開"ai_textsummarization_finetune" Project  附註:目前pipeline在jubo-datarch-aids的測試環境中運行。出於測試效率的考量,pipeline中的[scripts](https://gitlab.smart-aging.tech/ds/infrastructure/datarch/-/tree/master/pipelines/ai_textsummarization_finetune)僅會生成50筆訓練資料,若該pipeline建立在prod環境將會正常生成訓練資料。 ## III. Metrics 此專案主要使用三種Metrics來評估模型成效: - [ROUGE Score](https://en.wikipedia.org/wiki/ROUGE_(metric)) - [BLEURT](https://ai.googleblog.com/2020/05/evaluating-natural-language-generation.html) - [BERT Score](https://huggingface.co/spaces/harshhpareek/bertscore) ## IV. Model 根據生成目的,模型可以分為兩類: ### 1. 一般交班紀錄 在這類型的交班紀錄中,主要從輸入的護理紀錄中提取重要訊息,並搭配類別標籤作為輸入。考慮到護理紀錄通常包含中英文,所以選擇了多語言的[multilingual T5](https://huggingface.co/csebuetnlp/mT5_m2m_crossSum_enhanced)模型作為Base Model。相關的訓練流程已整合至MLP內部,主要使用transformers.trainer進行訓練,同時搭配LoRA以加速訓練過程。有關LoRA模型訓練的細節,可參考[此處](https://www.philschmid.de/fine-tune-flan-t5-peft)。 模型label部分主要都是一般交班紀錄的格式,範例如下: :::info [查房-D] 復健科羅醫師查房,因個案經常性主訴頭暈,羅醫師評估個案每日水量約600ml,明顯不足,故建議喝水量增加。 ::: 目前已針對mt5 model finetune在護理紀錄上做了幾種實驗,實驗數據如下:  (hpo: 是否使用優化後超參數 / concat: 是否加入rule-base concat string / 60k: 使用60000筆訓練數據) #### Finding: 通過實驗數據可以得知,在目前的階段,進行超參數的調整效益並不顯著。若要進行完整的超參數調整,將需要耗費大量的時間,同時對資源需求也相當高。另一方面,rule-base concat string以及增加數據量,可以在一定程度上提升模型的成效。 #### Best model storage and recurrence - Training config - [Base model](https://huggingface.co/csebuetnlp/mT5_m2m_crossSum_enhanced) - [Datasets](https://console.cloud.google.com/bigquery?referrer=search&authuser=3&project=jubo-ai&ws=!1m5!1m4!4m3!1sjubo-ai!2smeta_dev_aiProduct!3snursingnoteSummary_trainset_concat) -> Random select 250k entries - [LoRA model Storage](https://console.cloud.google.com/storage/browser/jubo-ai-models/test-app-vincent?authuser=3&project=jubo-ai&supportedpurview=project&pageState=(%22StorageObjectListTable%22:(%22f%22:%22%255B%255D%22))&prefix=&forceOnObjectsSortingFiltering=false) ### 2. ISBAR交班紀錄 ISBAR是一種新型的交班紀錄撰寫框架,主要將交班紀錄分為以下5個類別來記錄: * Introduction(病人介紹):基本資訊 * Situation(狀況):病人發生了什麼事 * Background(背景):病人的臨床背景基礎資料 * Assessment(評估):目前的資料所顯示的問題為何 * Recommendation(建議):還需要做些什麼,有哪些改善此問題的建議 相對於從輸入中提取重要資訊以生成輸出,生成ISBAR交班紀錄的任務較為困難。因此針對這類任務改為採用[Taiwan-LLaMa](https://github.com/MiuLab/Taiwan-LLaMa)來完成,該模型是以LLaMa 2為基礎,並在這之上針對繁體中文進行參數調整,後續經過[Instruction Tunning](https://www.philschmid.de/instruction-tune-llama-2)或[Few-Shot Prompting](https://promptingguide.azurewebsites.net/techniques/fewshot)即可完成ISBAR交班紀錄的生成。 目前階段僅使用Zero/Few Shot的方式來進行生成,具體的使用範例可參見[此處](https://colab.research.google.com/drive/13jDxaKOBu9HsUWhVpTIOHl7JlBFbGV5F?usp=sharing)。 #### Model Inference: 由於目前MLP中用來建立NLP App的[scripts](https://gitlab.smart-aging.tech/ds/infrastructure/mlp/-/blob/develop/scripts/package_docker.py)只支援建立一個CPU Support的NLP Inference Image,以將模型打包。然而,LLAMA Model需要使用GPU來進行Training&Inference,因此目前只將MT5 Model打包,並放上GCR。Image存放在[此處](https://console.cloud.google.com/gcr/images/jubo-ai/global/ai-nursingnote-textsummary?authuser=2&project=jubo-ai),並使用與之相容的MLP Version Commit SHA作為標籤(tag)。 #### 建立一個Model Inference API: 1. 從GCR上下載Image `docker pull gcr.io/jubo-ai/ai-nursingnote-textsummary:7bbaf4b7db56e8c8382eaa1fe0bf1d2077b3619f` 2. Run docker application `docker run -p 5000:5000 -it --rm --name textsummary gcr.io/jubo-ai/ai-nursingnote-textsummary:7bbaf4b7db56e8c8382eaa1fe0bf1d2077b3619f` 3. Inference with Python ``` import requests api_url = "http://127.0.0.1:5000/predict/" data = {"text": "女訴與家屬討論後決定爾後海總醫院回診即可及住院期間監測血糖有偏高,故開立血糖藥。 個案精神可,生命徵象 - 體溫:36.5,脈搏:86,呼吸:18,收縮壓:133,舒張壓:79,血氧:95,供氧:4,鼻胃管存,消化可,抽痰,痰量中色白稀,尿管存,引流暢色黃,無不適,續觀。"} response = requests.post(api_url, json=data) result = response.json() ``` ## V. Machine Learning Playground [MLP](https://gitlab.smart-aging.tech/ds/infrastructure/mlp)基於PyTorch開發,旨在消除模型設計中的重複性工作。目前,Text Summarization任務已成功整合到該框架中。內部實作主要依賴於[huggingface trainer](https://huggingface.co/docs/transformers/main_classes/trainer)以及[PEFT LoRA](https://huggingface.co/docs/peft/conceptual_guides/lora)進行。提MLP提供了完整的服務,包括從訓練到推論等一系列的服務,並且對每個Building Block編寫了相應的單元測試、程式碼風格檢查以及環境測試。 詳細的使用方法可以在[這裡](https://gitlab.smart-aging.tech/ds/infrastructure/mlp/-/blob/develop/doc/how-to-use-text-summary.md)找到。 ### 在MLP上利用訓練資料做Text Summarization的模型訓練: 1. 將訓練資料上傳到GCS,且上傳格式須為.csv,[詳細檔名定義](https://gitlab.smart-aging.tech/ds/infrastructure/mlp/-/blob/develop/doc/dataset_format.md) 2. 修改config/text_summary_config.yaml,設定所需的超參數,同時指定訓練資料在GCS中的存放位置。 3. 使用以下指令開始訓練 `mlp train --config path/to/config` ## VI. Future Work 在建立Inference API方面,受限於僅能使用CPU Memory進行存取和運算,而LLaMa模型僅使用CPU做predict時每筆輸入都將耗時1分鐘以上。因此目前尚未將LLaMa模型進行打包並上傳。未來有幾個可探索的方向,包含: 1. 利用[Instruction Tunning](https://www.philschmid.de/instruction-tune-llama-2)的方法來優化LLaMa模型,並解決GPU部署的問題,從而使模型能夠支援GPU進行Inference。 2. 利用[Few-Shot Prompting](https://promptingguide.azurewebsites.net/techniques/fewshot)結合LLaMa生成訓練資料,並使用這些資料分別訓練ISBAR五個類別的MT5模型。利用模型建立五個endpoints後,最後再將其結果合併並輸出。 ## VII. Reference 1. [期中進度簡報](https://gamma.app/docs/Patient-Record-Summarization-spopco4p7fr63kx) 2. [期末報告 - AI Team](https://gamma.app/docs/Jubo-Intern-Final-Presentation-k6rxipj8l1wqchi) 3. [期末報告 - 全體](https://gamma.app/docs/ML-Backend-y0cwomxsnx97u9c) 4. [專案說明](https://jubohealthtw.sharepoint.com/:w:/s/aids-team/EaaISPrjmctDn9JjByqRVJEBerY0-pFLU77J4UgrF009Ww) 5. [Midtern Review Document](https://jubohealthtw-my.sharepoint.com/:w:/g/personal/vincentwang_jubo_health/EaixUK7NpPFDiYlEiAIs3RkB4yK4UcR5uF00KG-OP9PTcg) 6. [Tutorial document of using text summary in MLP](https://gitlab.smart-aging.tech/ds/infrastructure/mlp/-/blob/develop/doc/how-to-use-text-summary.md) 7. [Data building pipeline](https://gitlab.smart-aging.tech/ds/infrastructure/datarch/-/tree/master/pipelines/ai_textsummarization_finetune) 8. [參數調整實驗數據](https://drive.google.com/file/d/1E79Z-UxLqjksvwm2IvJm5a6wiG1XWdHY/view?usp=sharing) 9. [Demo Website](https://www.jubo-ai.com/) 10. [Data Storage - BQ](https://console.cloud.google.com/bigquery?referrer=search&authuser=3&project=jubo-ai&ws=!1m4!1m3!3m2!1sjubo-ai!2smeta_dev_aiProduct) 11. [Data Storage - GCS](https://console.cloud.google.com/storage/browser/jubo-ai-mlp/dataset/training_data/nlp/text-summarization;tab=objects?pageState=(%22StorageObjectListTable%22:(%22f%22:%22%255B%255D%22))&authuser=3&project=jubo-ai&supportedpurview=project&prefix=&forceOnObjectsSortingFiltering=false) 12. [Model Storage](https://console.cloud.google.com/storage/browser/jubo-ai-models/test-app-vincent?authuser=3&project=jubo-ai&supportedpurview=project&pageState=(%22StorageObjectListTable%22:(%22f%22:%22%255B%255D%22))&prefix=&forceOnObjectsSortingFiltering=false)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up