kkhushisaid commited on
Commit
56deae5
1 Parent(s): e36836d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -68
app.py CHANGED
@@ -44,12 +44,7 @@ class App(object):
44
  default_chunk_size=default_chunk_size,
45
  ignore_label=ignore_label
46
  )
47
- parser = HfArgumentParser((
48
- ModelArguments,
49
- DataTrainingArguments,
50
- EvaluationArguments,
51
- TrainingArguments
52
- ))
53
  model_config = App._get_model_config()
54
  model_config['model_name_or_path'] = App._get_model_map()[model]
55
  if threshold == 'No threshold':
@@ -57,8 +52,7 @@ class App(object):
57
  model_config['threshold'] = None
58
  else:
59
  model_config['post_process'] = 'threshold_max'
60
- model_config['threshold'] = \
61
- App._get_threshold_map()[model_config['model_name_or_path']][threshold]
62
  print(model_config)
63
  #sys.exit(0)
64
  with tempfile.NamedTemporaryFile("w+", delete=False) as tmp:
@@ -67,7 +61,7 @@ class App(object):
67
  # If we pass only one argument to the script and it's the path to a json file,
68
  # let's parse it to get our arguments.
69
  self._model_args, self._data_args, self._evaluation_args, self._training_args = \
70
- parser.parse_json_file(json_file=tmp.name)
71
  # Initialize the text deid object
72
  self._text_deid = TextDeid(notation=self._data_args.notation, span_constraint=span_constraint)
73
  # Initialize the sequence tagger
@@ -90,7 +84,6 @@ class App(object):
90
  )
91
  # Load the required functions of the sequence tagger
92
  self._sequence_tagger.load()
93
-
94
 
95
  def get_ner_dataset(self, notes_file):
96
  ner_notes = self._dataset_creator.create(
@@ -106,17 +99,13 @@ class App(object):
106
  return ner_notes
107
 
108
  def get_predictions(self, ner_notes_file):
109
- # Set the required data and predictions of the sequence tagger
110
- # Can also use self._data_args.test_file instead of ner_dataset_file (make sure it matches ner_dataset_file)
111
  self._sequence_tagger.set_predict(
112
  test_file=ner_notes_file,
113
  max_test_samples=self._data_args.max_predict_samples,
114
  preprocessing_num_workers=self._data_args.preprocessing_num_workers,
115
  overwrite_cache=self._data_args.overwrite_cache
116
  )
117
- # Initialize the huggingface trainer
118
  self._sequence_tagger.setup_trainer(training_args=self._training_args)
119
- # Store predictions in the specified file
120
  predictions = self._sequence_tagger.predict()
121
  return predictions
122
 
@@ -148,7 +137,6 @@ class App(object):
148
  )
149
  return deid_notes
150
 
151
-
152
  @staticmethod
153
  def _get_highlights(deid_text):
154
  pattern = re.compile('<<(PATIENT|STAFF|AGE|DATE|LOCATION|PHONE|ID|EMAIL|PATORG|HOSPITAL|OTHERPHI):(.)*?>>')
@@ -220,56 +208,4 @@ def deid(text, model, threshold):
220
  # Create temp ner_notes file
221
  with tempfile.NamedTemporaryFile("w+", delete=False) as tmp:
222
  for ner_sentence in ner_notes:
223
- tmp.write(json.dumps(ner_sentence) + '\n')
224
- tmp.seek(0)
225
- predictions = app.get_predictions(tmp.name)
226
- # Get deid text
227
- with tempfile.NamedTemporaryFile("w+", delete=False) as tmp,\
228
- tempfile.NamedTemporaryFile("w+", delete=False) as tmp_1:
229
- for note in notes:
230
- tmp.write(json.dumps(note) + '\n')
231
- for note_prediction in predictions:
232
- tmp_1.write(json.dumps(note_prediction) + '\n')
233
- tmp.seek(0)
234
- tmp_1.seek(0)
235
- deid_text = list(app.get_deid_text_replaced(tmp.name, tmp_1.name))[0]['deid_text']
236
- deid_text_remove = list(app.get_deid_text_removed(tmp.name, tmp_1.name))[0]['deid_text']
237
- return [highlight_text for highlight_text in App._get_highlights(deid_text)], deid_text_remove
238
-
239
- recall_choices = ["No threshold", "99.5", "99.7"]
240
- recall_radio_input = gr.inputs.Radio(recall_choices, type="value", default='No threshold', label='RECALL THRESHOLD')
241
-
242
- model_choices = list(App._get_model_map().keys())
243
- model_radio_input = gr.inputs.Radio(model_choices, type="value", default='OBI-RoBERTa De-ID', label='DE-ID MODEL')
244
-
245
- title = 'DE-IDENTIFICATION OF ELECTRONIC HEALTH RECORDS'
246
- description = 'Models to remove private information (PHI/PII) from raw medical notes. The recall threshold (bias) can be used to remove PHI more aggressively.'
247
-
248
- gradio_input = gr.inputs.Textbox(
249
- lines=10,
250
- placeholder='Enter text with PHI',
251
- label='RAW MEDICAL NOTE'
252
- )
253
-
254
- gradio_highlight_output = gr.outputs.HighlightedText(
255
- label='LABELED DE-IDENTIFIED MEDICAL NOTE',
256
- )
257
-
258
- gradio_text_output = gr.outputs.Textbox(
259
- label='DE-IDENTIFIED MEDICAL NOTE'
260
- )
261
-
262
- examples = [["Physician Discharge Summary Admit date: 10/12/1982 Discharge date: 10/22/1982 Patient Information Jack Reacher, 54 y.o. male (DOB = 1/21/1928). Home Address: 123 Park Drive, San Diego, CA, 03245. Home Phone: 202-555-0199 (home). Hospital Care Team Service: Orthopedics Inpatient Attending: Roger C Kelly, MD Attending phys phone: (634)743-5135 Discharge Unit: HCS843 Primary Care Physician: Hassan V Kim, MD 512-832-5025.", "OBI-RoBERTa De-ID", "No threshold"], ["Consult NotePt: Ulysses Ogrady MC #0937884Date: 07/01/19 Williams Ct M OSCAR, JOHNNY Hyderabad, WI 62297\n\nHISTORY OF PRESENT ILLNESS: The patient is a 77-year-old-woman with long standing hypertension who presented as a Walk-in to me at the Brigham Health Center on Friday. Recently had been started q.o.d. on Clonidine since 01/15/19 to taper off of the drug. Was told to start Zestril 20 mg. q.d. again. The patient was sent to the Unit for direct admission for cardioversion and anticoagulation, with the Cardiologist, Dr. Wilson to follow.\nSOCIAL HISTORY: Lives alone, has one daughter living in Nantucket. Is a non-smoker, and does not drink alcohol.\nHOSPITAL COURSE AND TREATMENT: During admission, the patient was seen by Cardiology, Dr. Wilson, was started on IV Heparin, Sotalol 40 mg PO b.i.d. increased to 80 mg b.i.d., and had an echocardiogram. By 07-22-19 the patient had better rate control and blood pressure control but remained in atrial fibrillation. On 08.03.19, the patient was felt to be medically stable.", "OBI-RoBERTa De-ID", "99.5"], ["Consult NotePt: Ulysses Ogrady MC #0937884Date: 07/01/19 Williams Ct M OSCAR, JOHNNY Hyderabad, WI 62297\n\nHISTORY OF PRESENT ILLNESS: The patient is a 77-year-old-woman with long standing hypertension who presented as a Walk-in to me at the Brigham Health Center on Friday. Recently had been started q.o.d. on Clonidine since 01/15/19 to taper off of the drug. Was told to start Zestril 20 mg. q.d. again. The patient was sent to the Unit for direct admission for cardioversion and anticoagulation, with the Cardiologist, Dr. Wilson to follow.\nSOCIAL HISTORY: Lives alone, has one daughter living in Nantucket. Is a non-smoker, and does not drink alcohol.\nHOSPITAL COURSE AND TREATMENT: During admission, the patient was seen by Cardiology, Dr. Wilson, was started on IV Heparin, Sotalol 40 mg PO b.i.d. increased to 80 mg b.i.d., and had an echocardiogram. By 07-22-19 the patient had better rate control and blood pressure control but remained in atrial fibrillation. On 08.03.19, the patient was felt to be medically stable.", "OBI-ClinicalBERT De-ID", "99.5"], ['HPI: Pt is a 59 yo Khazakhstani male, with who was admitted to San Rafael Mount Hospital following a syncopal nauseas and was brought to Rafael Mount ED. Five weeks ago prior Anemia: On admission to Rafael Hospital, Hb/Hct: 11.6/35.5. Tobacco: Quit at 38 y/o; ETOH: 1-2 beers/week; Caffeine:\nDD:05/05/2022 DT:05/05/2022 WK:65255 :4653\nNO GROWTH TO DATE Specimen: 38:Z8912708G Collected\n\n2nd set biomarkers (WPH): Creatine Kinase Isoenzymes Hospitalized 2115 TCH for ROMI 2120 TCH new onset\n\nLab Tests Amador: the lab results show good levels of 10MG PO qd : 04/10/2021 - 05/15/2021 ACT : rosenberg 128\n placed 3/22 for bradycardia. P/G model #5435, serial # 4712198. \n\nSocial history: Married, glazier, 3 grown adult children. Has VNA. Former civil engineer, supervisor, consultant. She is looking forward to a good Christmas. She is here today',
263
- "OBI-ClinicalBERT De-ID", 'No threshold']]
264
-
265
- iface = gr.Interface(
266
- title=title,
267
- description=description,
268
- theme='huggingface',
269
- layout='horizontal',
270
- examples=examples,
271
- fn=deid,
272
- inputs=[gradio_input, model_radio_input, recall_radio_input],
273
- outputs=[gradio_highlight_output, gradio_text_output],
274
- )
275
- iface.launch()
 
44
  default_chunk_size=default_chunk_size,
45
  ignore_label=ignore_label
46
  )
47
+ parser = HfArgumentParser((ModelArguments, DataTrainingArguments, EvaluationArguments, TrainingArguments))
 
 
 
 
 
48
  model_config = App._get_model_config()
49
  model_config['model_name_or_path'] = App._get_model_map()[model]
50
  if threshold == 'No threshold':
 
52
  model_config['threshold'] = None
53
  else:
54
  model_config['post_process'] = 'threshold_max'
55
+ model_config['threshold'] = App._get_threshold_map()[model_config['model_name_or_path']][threshold]
 
56
  print(model_config)
57
  #sys.exit(0)
58
  with tempfile.NamedTemporaryFile("w+", delete=False) as tmp:
 
61
  # If we pass only one argument to the script and it's the path to a json file,
62
  # let's parse it to get our arguments.
63
  self._model_args, self._data_args, self._evaluation_args, self._training_args = \
64
+ parser.parse_json_file(json_file=tmp.name)
65
  # Initialize the text deid object
66
  self._text_deid = TextDeid(notation=self._data_args.notation, span_constraint=span_constraint)
67
  # Initialize the sequence tagger
 
84
  )
85
  # Load the required functions of the sequence tagger
86
  self._sequence_tagger.load()
 
87
 
88
  def get_ner_dataset(self, notes_file):
89
  ner_notes = self._dataset_creator.create(
 
99
  return ner_notes
100
 
101
  def get_predictions(self, ner_notes_file):
 
 
102
  self._sequence_tagger.set_predict(
103
  test_file=ner_notes_file,
104
  max_test_samples=self._data_args.max_predict_samples,
105
  preprocessing_num_workers=self._data_args.preprocessing_num_workers,
106
  overwrite_cache=self._data_args.overwrite_cache
107
  )
 
108
  self._sequence_tagger.setup_trainer(training_args=self._training_args)
 
109
  predictions = self._sequence_tagger.predict()
110
  return predictions
111
 
 
137
  )
138
  return deid_notes
139
 
 
140
  @staticmethod
141
  def _get_highlights(deid_text):
142
  pattern = re.compile('<<(PATIENT|STAFF|AGE|DATE|LOCATION|PHONE|ID|EMAIL|PATORG|HOSPITAL|OTHERPHI):(.)*?>>')
 
208
  # Create temp ner_notes file
209
  with tempfile.NamedTemporaryFile("w+", delete=False) as tmp:
210
  for ner_sentence in ner_notes:
211
+ tmp.write(json.dumps(ner_sentence) + '\n')