jinee commited on
Commit
283ff69
β€’
1 Parent(s): b7ce850
Files changed (5) hide show
  1. README.md +4 -4
  2. app.py +170 -0
  3. fake_sample_mimic.xlsx +0 -0
  4. requirements.txt +4 -0
  5. sequential.csv +22 -0
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
  title: Note Demo
3
  emoji: πŸ“‰
4
- colorFrom: pink
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 4.18.0
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Note Demo
3
  emoji: πŸ“‰
4
+ colorFrom: indigo
5
+ colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 4.17.0
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+
3
+ excel_file = './fake_sample_mimic.xlsx'
4
+
5
+ sheet_names = ['df', 'diagnoses', 'procedure', 'chart', 'medication','note']
6
+ data_frames = {}
7
+ for sheet_name in sheet_names:
8
+ data_frames[sheet_name] = pd.read_excel(excel_file, sheet_name=sheet_name)
9
+
10
+
11
+
12
+ df = data_frames["df"]
13
+ diagnoses = data_frames["diagnoses"]
14
+ procedure = data_frames["procedure"]
15
+ chart = data_frames["chart"]
16
+ medication = data_frames["medication"]
17
+ note = data_frames["note"]
18
+
19
+
20
+ sequential_file = pd.read_csv("./sequential.csv")
21
+
22
+ input_sequential_1 = sequential_file.iloc[0,1]
23
+ input_sequential_2 = sequential_file.iloc[1,1]
24
+
25
+
26
+ """
27
+ #For your information: Due to memory limitations of the huggingface-space, the NOTE model does not actually work, and the results (generated_1, generated_2) of inference with the same model are attached. However, the code below is actually code that can be inferred after downloading the model, and the model can be found at "https://huggingface/jinee/note".
28
+ """
29
+
30
+ # from transformers import AutoModelForCausalLM, AutoTokenizer
31
+ # model = AutoModelForCausalLM.from_pretrained("jinee/note", load_in_4bit=True, device_map="auto")
32
+ # tokenizer = AutoTokenizer.from_pretrained("jinee/note")
33
+ # tokenizer.padding_side = 'right'
34
+ # tokenizer.add_eos_token = True
35
+ # tokenizer.pad_token = tokenizer.eos_token
36
+ # tokenizer.add_eos_token, tokenizer.add_bos_token
37
+
38
+ # from transformers import pipeline
39
+ # instruction = '''
40
+ # As a doctor, you need to create a discharge summary based on input data.
41
+ # Never change the dates or numbers in the input data and use them as is. And please follow the format below for your report.
42
+ # Also, never make up information that is not in the input data, and write a report only with information that can be identified from the input data.
43
+
44
+ # 1. Patient information (SUBJECT_ID, HADM_ID, hospitalization and discharge date, hospitalization period, gender, date of birth, age, allergy)
45
+ # 2. Diagnostic information and past history (if applicable)
46
+ # 3. Surgery or procedure information
47
+ # 4. Significant medication administration during hospitalization and discharge medication history
48
+ # 5. Meaningful lab tests during hospitalization
49
+ # 6. Summary of significant text records/notes
50
+ # 7. Discharge outcomes and treatment plan
51
+ # 8. Overall summary of at least 500 characters in lines including the above contents
52
+ # '''
53
+
54
+ # import torch
55
+ # def generation(model, tokenizer, input_data):
56
+ # torch.cuda.empty_cache()
57
+ # pipe = pipeline('text-generation',
58
+ # model = model,
59
+ # tokenizer = tokenizer,
60
+ # torch_dtype=torch.bfloat16,
61
+ # device_map = 'auto')
62
+ # global instruction
63
+
64
+ # sequences = pipe(
65
+ # f"[INST]{instruction}: {input_data} [/INST]",
66
+ # do_sample=True,
67
+ # max_new_tokens=1024,
68
+ # temperature=0.7,
69
+ # top_k=50,
70
+ # top_p=0.95,
71
+ # early_stopping =True,
72
+ # num_return_sequences=1,)
73
+
74
+ # text = sequences[0]['generated_text']
75
+ # start_index = text.find('[/INST]')
76
+ # if start_index != -1:
77
+ # summary_ = text[start_index + len('[/INST]'):]
78
+ # return(summary_)
79
+ # else:
80
+ # return("'[summary_] 'is not founded.")
81
+
82
+
83
+ # result1 = generation(model, tokenizer, input_sequential_1)
84
+ # result2= generation(model, tokenizer, input_sequential_2)
85
+
86
+ # generated_1 = result1
87
+ # generated_2 = result2
88
+
89
+
90
+ import gradio as gr
91
+ import pandas as pd
92
+
93
+ text_set1 = input_sequential_1
94
+ text_set2 = input_sequential_2
95
+ generated_1 = "Discharge Summary:\n\n1. Patient information: SUBJECT_ID: 1111, HADM_ID: 2222, ADMITTIME: 2139-06-06 00:00:00, DISCHTIME: 2139-06-09 00:00:00, GENDER: M, DOB: 2100-05-31 00:00:00, AGE: 40. Diagnoses: 40301 Hypertensive chronic kidney disease, 486 Pneumonia, 48154 Peritoneal dialysis, 40293 Chronic glomerulonephritis in diseases classified elsewhere / P0.5498 Peritoneal dialysis.\n2. During the hospitalization, the patient was diagnosed with severe cardiomegaly likely related to chronic hypertension and heart failure (2139-06-06). Initial medications included Vancomycin, Lisinopril, Metformin, and Norepinephrine. The patient's vital signs showed an unstable condition with a heart rate of 180 beats per minute, blood pressure of 180/110 mmHg, and respiratory rate of 30 breaths per minute. A critical condition with an enlarged cardiac silhouette was observed on imaging, and urgent cardiac evaluation was recommended due to potential pre-operative risk.\n3. On 2139-06-07, the patient's condition was stabilized, and no medications were administered.\n4. The following day (2139-06-08), the patient underwent valve replacement surgery, and medications included Dobutamine and Meropenem post-surgery. The patient's vital signs improved, with a heart rate of 120 beats per minute, blood pressure of 120/80 mmHg, and respiratory rate of 36.6 breaths per minute. Imaging showed a decreased cardiac silhouette size, indicating an improvement in heart size and function post-surgery.\n5. On 2139-06-09, the patient was discharged, and medications included Acetaminophen, Warfarin, and Amiodarone. Vital signs showed a stable condition with a heart rate of 130 beats per minute, blood pressure of 130/80 mmHg, and respiratory rate of 18 breaths per minute.\n\nOverall, the patient underwent successful valve replacement surgery on 2139-06-08, and no post-operative complications were observed. The patient was discharged with routine post-surgical follow-up, Acetaminophen, Warfarin, and Amiodarone for further management. The hospitalization period was 3 days (LOS: 3 / D0)."
96
+
97
+ generated_2 = " Discharge Summary:\n\n1. Patient Information:\n - SUBJECT_ID: 7777\n - HADM_ID: 8888\n - Admission and Discharge Dates: March 10, 2021 - March 15, 2021\n - Hospitalization Period: 5 days\n - Gender: Female\n - Date of Birth: September 21, 1939\n - Age: 82\n - Allergy: N/A\n\n2. Diagnostic Information and Past History:\n - Dx1: Other postoperative infection\n - Dx2: Acute edema of lung, unspecified\n - Dx3: Acute diastolic heart failure\n\n3. Surgery or Procedure Information:\n - Px1: Open and other replacement of mitral valve with tissue graft\n - Px2: Excision or destruction of other lesion or tissue of heart, open approach\n - Px3: Open heart surgery with extracorporeal circulation\n - Px4: Coronary arteriography using two catheters\n\n4. Significant Medication Administration during Hospitalization and Discharge Medication History:\n - March 10, 2021: Morphine 10.0 mg, Metoprolol 50.0 mg, Furosemide 40.0 mg\n - March 11, 2021: Heparin 5000.0 IU, Vancomycin 1000.0 mg\n - March 12, 2021: Lisinopril 20.0 mg\n - March 13, 2021: N/A\n - March 14, 2021: N/A\n - March 15, 2021: N/A\n - Discharge Medications: N/A\n\n5. Meaningful Lab Tests during Hospitalization:\n - March 10, 2021: N/A\n - March 11, 2021: Heart rate: 215 beats per minute, Hemoglobin: 95.09 g/dL, Sodium: 180.11 cm2\n - March 12, 2021: Creatinine: 13.85 mg/dL, Glucose: 2151-03-12 note: N/A\n - March 13, 2021: Potassium: 142.94 mEq/L, Sodium: 4.62 mEq/L, 2151-03-13 note: radiology: Findings:\n\n6. Summary of Significant Text Records/Notes:\n - Post-operative chest X-ray shows stable mediastinal contours. Normal heart size with no pericardial effusion. Surgical changes noted in the chest, consistent with recent cardiac surgery. No pneumothorax or significant pleural effusions.\n - Abdominal ultrasound shows post-operative changes without any acute complication.\n\n7. Discharge Outcomes and Treatment Plan:\n - Impression: Stable post-operative chest findings. No evidence of acute complications related to recent cardiac surgery. Abdominal findings are unremarkable for any acute pathology.\n - Conclusion: Post-operative imaging findings are satisfactory and within expected limits. Continued monitoring and routine post-operative care recommended.\n\n8. Overall Summary:\n The patient is a 82-year-old female with a history of other postoperative infection, acute edema of lung, and acute diastolic heart failure who underwent open and other replacement of mitral valve with tissue graft, excision or destruction of other lesion or tissue of heart, open approach, open heart surgery with extracorporeal circulation, and coronary arteriography using two catheters. During her hospitalization, she received Morphine, Metoprolol, Furosemide, Heparin, Vancomycin, and Lisinopril. Her lab tests showed stable vital signs and no significant abnormalities. The post-operative imaging findings were satisfactory, and the patient was discharged with continued monitoring"
98
+
99
+
100
+ def on_click_button1_tab1():
101
+ return text_set1
102
+
103
+ def on_click_button1_tab2():
104
+ return text_set2
105
+
106
+ def on_click_button2_tab1():
107
+ return generated_1
108
+
109
+ def on_click_button2_tab2():
110
+ return generated_2
111
+ # Gradio μΈν„°νŽ˜μ΄μŠ€ ꡬ성
112
+ with gr.Blocks() as demo:
113
+
114
+ gr.Markdown("# NOTE: Notable generation Of patient Text summaries through Efficient approach based on direct preference optimization")
115
+ gr.Markdown("## We propose NOTE, which generates comprehensive discharge summaries of patients using MIMIC-III. ")
116
+ gr.Markdown(" ### This page serves as a demo application indicating that our NOTE can be applied in practice.\n ### To enable actual functionality, you will need to download the model and fake data. \n You can find the model and fake data at the provided link, where you can also review the code used during model training. LINK: https://huggingface.co/jinee/note")
117
+
118
+ with gr.Tab("Sample 1"):
119
+ gr.Markdown("### Sample Fake data")
120
+ gr.Markdown("#### Demographic")
121
+ gr.DataFrame(df1_df)
122
+ gr.Markdown("#### Diagnoses")
123
+ gr.DataFrame(df1_diagnoses)
124
+ gr.Markdown("#### Procedures")
125
+ gr.DataFrame(df1_procedure)
126
+ gr.Markdown("#### Chart events")
127
+ gr.DataFrame(df1_chart)
128
+ gr.Markdown("#### medications")
129
+ gr.DataFrame(df1_medication)
130
+ gr.Markdown("#### Notes")
131
+ gr.DataFrame(df1_note)
132
+
133
+ button1_tab1 = gr.Button("Button 1: Generate a sequential dataset")
134
+ text1_tab1 = gr.Textbox()
135
+ button1_tab1.click(on_click_button1_tab1, outputs=text1_tab1)
136
+
137
+ button2_tab1 = gr.Button("Button 2: Generate a summary")
138
+ text2_tab1 = gr.Textbox()
139
+ button2_tab1.click(on_click_button2_tab1, outputs=text2_tab1)
140
+
141
+ with gr.Tab("Sample 2"):
142
+ gr.Markdown("### Sample Fake data")
143
+ gr.Markdown("#### Demographic")
144
+ gr.DataFrame(df2_df)
145
+ gr.Markdown("#### Diagnoses")
146
+ gr.DataFrame(df2_diagnoses)
147
+ gr.Markdown("#### Procedures")
148
+ gr.DataFrame(df2_procedure)
149
+ gr.Markdown("#### Chart events")
150
+ gr.DataFrame(df2_chart)
151
+ gr.Markdown("#### medications")
152
+ gr.DataFrame(df2_medication)
153
+ gr.Markdown("#### Notes")
154
+ gr.DataFrame(df2_note)
155
+
156
+ button1_tab2 = gr.Button("Button 1: Generate a sequential dataset")
157
+ text1_tab2 = gr.Textbox()
158
+ button1_tab2.click(on_click_button1_tab2, outputs=text1_tab2)
159
+
160
+ button2_tab2 = gr.Button("Button 2: Generate a summary")
161
+ text2_tab2 = gr.Textbox()
162
+ button2_tab2.click(on_click_button2_tab2, outputs=text2_tab2)
163
+
164
+ demo.launch(share=True)
165
+
166
+
167
+
168
+
169
+
170
+
fake_sample_mimic.xlsx ADDED
Binary file (16.8 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ accelerate==0.24.1
2
+ openpyxl==3.1.0
3
+ transformers==4.35.2
4
+ bitsandbytes==0.41.2.post2
sequential.csv ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ index,text
2
+ 0,"Demographic SUBJECT_ID: 1111, HADM_ID: 2222, ADMITTIME: 2139-06-06 00:00:00, DISCHTIME: 2139-06-09 00:00:00, GENDER: M, DOB: 2100-05-31 00:00:00, AGE: 40, LOS: 3 / D0. 40301 Hypertensive chronic kidney disease, malignant, with chronic kidney disease stage V or end stage renal disease D1. 486 Pneumonia, organism unspecified D2. 58281 Chronic glomerulonephritis in diseases classified elsewhere / P0. 5498 Peritoneal dialysis
3
+ 2139-06-06Medication:medi: Vancomycin 1.0*3 Lisinopril 10.0*3 Metformin 500.0*3 Norepinephrine 0.1*0LAB TEST: chart:110:bpm2139-06-06 180/110:mmHg2139-06-06 30:breaths/min2139-06-06 38.5:C2139-06-06note: note: Nursing: Patient's condition is critical with unstable vitals. Immediate surgery is planned. radiology: Findings: Enlargement of the cardiac silhouette suggesting cardiomegaly. Pulmonary edema present.
4
+ Impression: Severe cardiomegaly likely related to chronic hypertension and heart failure.
5
+ Conclusion: Urgent cardiac evaluation recommended. Potential pre-operative risk.
6
+ 2139-06-07Medication:medi: Dobutamine 5.0*0 Meropenem 1.0*0LAB TEST: chart:78:bpm2139-06-07 120/80:mmHg2139-06-07 36.6:C2139-06-07note: note:NAN
7
+ 2139-06-08Medication:medi: NAN LAB TEST: chart: NAN note: note: radiology: Findings: Decreased cardiac silhouette size post valve replacement. No signs of acute congestive heart failure.
8
+ Impression: Improvement in heart size and function post-surgery.
9
+ Conclusion: No post-operative complications. Continue with routine post-surgical follow-up.
10
+ 2139-06-09Medication:medi: Acetaminophen 1000.0*1 Warfarin 5.0*1 Amiodarone 200.0*1LAB TEST: chart:85:bpm2139-06-09 130/80:mmHg2139-06-09 18:breaths/min2139-06-09 37:C2139-06-09note: note:NAN"
11
+ 1,"Demographic SUBJECT_ID: 7777, HADM_ID: 8888, ADMITTIME: 2151-03-10 00:00:00, DISCHTIME: 2151-03-15 00:00:00, GENDER: F, DOB: 2067-09-21 00:00:00, AGE: 82, LOS: 5 / D0. 99859 Other postoperative infection D1. 5184 Acute edema of lung, unspecified D2. 42831 Acute diastolic heart failure / P0. 3523 Open and other replacement of mitral valve with tissue graft P1. 3733 Excision or destruction of other lesion or tissue of heart, open approach P2. 3961 Extracorporeal circulation auxiliary to open heart surgery P3. 8856 Coronary arteriography using two catheters
12
+ 2151-03-10Medication:medi: Morphine 10.0*1 Metoprolol 50.0*4 Furosemide 40.0*5LAB TEST: chart: NAN note: note:NAN
13
+ 2151-03-11Medication:medi: Heparin 5000.0*2 Vancomycin 1000.0*1LAB TEST: chart:95.09:bpm2151-03-11note: note:NAN
14
+ 2151-03-12Medication:medi: Lisinopril 20.0*3LAB TEST: chart:13.85:g/dL2151-03-12 180.11:cm2151-03-12note: note:NAN
15
+ 2151-03-13Medication:medi: NAN LAB TEST: chart:142.94:mEq/L2151-03-13 4.62:mEq/L2151-03-13note: note: radiology: Findings:
16
+ Post-operative chest X-ray shows stable mediastinal contours. Normal heart size with no pericardial effusion. Surgical changes noted in the chest, consistent with recent cardiac surgery. No pneumothorax or significant pleural effusions. Abdominal ultrasound shows post-operative changes without any acute complication.
17
+ Impression:
18
+ Stable post-operative chest findings. No evidence of acute complications related to recent cardiac surgery. Abdominal findings are unremarkable for any acute pathology.
19
+ Conclusion:
20
+ Post-operative imaging findings are satisfactory and within expected limits. Continued monitoring and routine post-operative care recommended.
21
+ 2151-03-14Medication:medi: NAN LAB TEST: chart:92.08:mmHg2151-03-14 69.45:kg2151-03-14 0.51:mg/dL2151-03-14note: note:NAN
22
+ 2151-03-15Medication:medi: NAN LAB TEST: chart:10.1:x10^9/L2151-03-15note: note:NAN"