epiph1 commited on
Commit
9c65229
1 Parent(s): 9ed01a3

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +366 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import langchain
4
+
5
+ langchain.debug = False
6
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
7
+ os.environ["OPENAI_API_KEY"]
8
+
9
+
10
+ def save_docs(docs):
11
+ import shutil
12
+ import os
13
+
14
+ output_dir = "/home/user/app/docs/"
15
+
16
+ if os.path.exists(output_dir):
17
+ shutil.rmtree(output_dir)
18
+
19
+ if not os.path.exists(output_dir):
20
+ os.makedirs(output_dir)
21
+
22
+ for doc in docs:
23
+ shutil.copy(doc.name, output_dir)
24
+
25
+ return "Successful!"
26
+
27
+
28
+ global mdm_value
29
+
30
+
31
+ def process_docs():
32
+ from langchain.chat_models import ChatOpenAI
33
+ from langchain.chains import ConversationChain
34
+ import os
35
+ from docx import Document
36
+
37
+ global mdm_value
38
+
39
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo-16k")
40
+ agent = ConversationChain(llm=llm, verbose=True)
41
+
42
+ folder_path = "/home/user/app/docs/"
43
+ for root, dirs, files in os.walk(folder_path):
44
+ for file in files:
45
+ if file.endswith(".docx") or file.endswith(".docx"):
46
+ word_file_path = os.path.join(root, file)
47
+
48
+ doc = Document(word_file_path)
49
+ text = []
50
+ for paragraph in doc.paragraphs:
51
+ text.append(paragraph.text)
52
+ doc_content = "\n".join(text)
53
+
54
+ one_word_propmt = """Classify into "MINIMAL", "LOW", "MODERATE", "HIGH". You should ALWAYS give only ONE option and NOTHING ELSE.
55
+ ONLY return ONE option. AWAYS answer in ONLY one word. DO NOT give full sentences.
56
+ ALWAYS give the answer in ALL UPPER CASE. DO NOT EVER give answer in any other case."""
57
+
58
+ #####################################################
59
+
60
+ instruction1 = """You are an expert at calculating the Risk of Complications and/or Morbidity or Mortality of the Patient Management Decisions Made at Visit (choose highest) You are provided with a detailed medical report. Classify it into "MINIMAL", "LOW", "MODERATE", and "HIGH" on the basis of the Given rules.
61
+
62
+ MINIMAL
63
+ Minimal risk of morbidity from additional diagnostic testing or treatment
64
+ Examples only
65
+ • Rest
66
+ • Gargles
67
+ • Elastic bandages
68
+ Superficial dressings
69
+
70
+ LOW
71
+ Low risk of morbidity from additional diagnostic testing or treatment
72
+ Examples only
73
+ OTC drugs
74
+ • Minor surgery w/no identified risk factors
75
+ • Physical/Occtherapy
76
+
77
+ MODERATE
78
+ MODERATE risk of morbidity from additional diagnostic testing or treatment
79
+ Examples only
80
+ Prescription drug management
81
+ Decision regarding minor surgery with identified patient or procedure risk factors
82
+ Decision regarding elective major surgery without identified patient or procedure risk factors
83
+ Diagnosis or treatment significantly limited by social determinants of health
84
+
85
+ HIGH
86
+ HIGH risk of morbidity from additional diagnostic testing or treatment
87
+ Examples only
88
+ Parenteral controlled substances (DEA controlled substance given by route other than digestive tract)
89
+ Drug therapy requiring intensive monitoring for toxicity
90
+ Decision regarding elective major surgery with identified patient or procedure risk factors
91
+ Decision regarding emergency major surgery
92
+ Decision regarding hospitalization or escalation of hospital level care (i.e. transfer to ICU)
93
+ Decision not to resuscitate or to deescalate care because of poor prognosis
94
+
95
+
96
+ Here is the Report """
97
+
98
+ instruction1 += "\n\n"
99
+ instruction1 += doc_content
100
+ instruction1 += "\n\n"
101
+ instruction1 += "Study it and just provide your answer"
102
+ response1 = agent.predict(input=instruction1)
103
+
104
+ instruction2 = one_word_propmt
105
+ instruction2 += "\n\n"
106
+ instruction2 += response1
107
+ response2 = agent.predict(input=instruction2) # main
108
+
109
+ #####################################################
110
+
111
+ instruction3 = """Calculate Amount and/or Complexity of Data to be Reviewed & Analyzed (choose highest criteria met). You are provided with a detailed medical report. Classify it into "MINIMAL", "LOW", "MODERATE", and "HIGH" on the basis of the Given rules.
112
+
113
+
114
+ CATEGORY 1
115
+ 1. Review of prior external note(s) from each unique source (each unique source counted once, regardless of # of notes reviewed)
116
+ 2. Review of the result(s) of each unique test
117
+ 3. Ordering of each unique test (includes review of result, do not count in #2)
118
+ 4. Assessment requiring an Independent historian
119
+
120
+ CATEGORY 2
121
+ Independent interpretation of tests performed by another physician/other qualified healthcare professional (not separately reported)
122
+ Do not count independent interpretation for a test billed or ordered by a colleague in the same specialty
123
+
124
+ CATEGORY 3
125
+ Discussion of management or test interpretation- with external physician/other qualified health care professional/ appropriate source (not separately reported)
126
+ Requires direct interactive exchange (not via intermediaries or notes)
127
+
128
+
129
+
130
+ MINIMAL
131
+ If Minimal or No Data Reviewed
132
+
133
+
134
+ LOW
135
+ if it meets any combination of 2 from items 1-3 Or Meet item 4 (independent historian)
136
+ 1. Review of prior external note(s) from each unique source (each unique source counted once, regardless of # of notes reviewed)
137
+ 2. Review of the result(s) of each unique test
138
+ 3. Ordering of each unique test (includes review of result, do not count in #2)
139
+ 4. Assessment requiring an Independent historian
140
+
141
+
142
+ MODERATE
143
+ Meet 1 of 3 categories below
144
+ Category 1: Meet any combination of 3 from items 1-4
145
+ Category 2: Independent interpretation of test
146
+ Category 3: Discussion management, or test interpretation (external)
147
+
148
+
149
+ HIGH
150
+ Meet 2 of 3 categories below
151
+ Category 1: Meet any combination of 3 from items 1-4
152
+ Category 2: Independent interpretation of test
153
+ Category 3: Discussion management, or test interpretation (external)
154
+
155
+
156
+ Here is the Report """
157
+
158
+ instruction3 += "\n\n"
159
+ instruction3 += doc_content
160
+ instruction3 += "\n\n"
161
+ instruction3 += "Study it and just provide your answer"
162
+ response3 = agent.predict(input=instruction3)
163
+
164
+ instruction4 = one_word_propmt
165
+ instruction4 += "\n\n"
166
+ instruction4 += response3
167
+ response4 = agent.predict(input=instruction4) # main
168
+
169
+ #####################################################
170
+
171
+ instruction5 = """You are an expert at calculating the complexity of medical problems based on Medical reports. You are provided with a detailed medical report. Classify it into "MINIMAL", "LOW", "MODERATE", and "HIGH" on the basis of the Given rules.
172
+
173
+ MINIMAL
174
+ 1 self- limited or minor problem
175
+ (runs a definite or prescribed course, is transient in nature, and is not likely to permanently alter health status)
176
+
177
+ LOW
178
+ 2 or more self-limited or minor problems; or
179
+ 1 stable chronic illness (chronic illness which is at treatment goal for the specific patient); or
180
+ 1 acute, uncomplicated illness or injury (full recovery w/out functional impairment is expected); or
181
+ Stable, acute illness (treatment newly or recently initiated, resolution may not be complete, but condition stable); or
182
+ Acute, uncomplicated illness or injury requiring hospital inpatient or observation level care (little to no risk of mortality with treatment, but treatment required is delivered in inpt or obs setting)
183
+
184
+ MODERATE
185
+ 1 or more chronic illnesses with ex- acerbation, progression, or side effects of treatment (requires supportive care or attention to treatment for side effects); or
186
+ 2 or more stable chronic illnesses; or
187
+ 1 undiagnosed new problem with uncertain prognosis (likely to result in high risk of morbidity w/out tx); or
188
+ 1 acute illness with systemic symptoms (illness that causes systemic symptoms and has high risk of morbidity without treatment); or
189
+ 1 acute complicated injury (eval of body systems not part of injured organ, extensive injury, or multiple tx options are multiple and/ or associated with risk of morbidity
190
+
191
+
192
+ HIGH
193
+ • 1 or more chronic illness- es with severe exacerbation, progression, or side effects of treatment (significant risk of morbidity: may require escalation in level of care); or
194
+ 1 acute or chronic illness or injury that poses a threat to life or bodily
195
+ function (in the near term without treatment e.g. AMI, pulmonary embolus, severe respiratory distress psychiatric illness with potential threat to self or others, peritonitis, acute renal failure)
196
+
197
+ Here is the Report """
198
+
199
+ instruction5 += "\n\n"
200
+ instruction5 += doc_content
201
+ instruction5 += "\n\n"
202
+ instruction5 += "Study it and just provide your answer"
203
+ response5 = agent.predict(input=instruction5)
204
+
205
+ instruction6 = one_word_propmt
206
+ instruction6 += "\n\n"
207
+ instruction6 += response5
208
+ response6 = agent.predict(input=instruction6) # main
209
+
210
+ ######################################################
211
+
212
+ inputs = [response2, response4, response6]
213
+ sorted_inputs = sorted(
214
+ inputs, key=lambda x: ["MINIMAL", "LOW", "MODERATE", "HIGH"].index(x)
215
+ )
216
+ mdm_value = sorted_inputs[1]
217
+ print(mdm_value)
218
+
219
+ return (
220
+ "Successful!",
221
+ response1,
222
+ response2,
223
+ response3,
224
+ response4,
225
+ response5,
226
+ response6,
227
+ )
228
+
229
+
230
+ def get_code1(input):
231
+ global mdm_value
232
+ mdm_value = mdm_value
233
+
234
+ if input == "NEW" and mdm_value == "MINIMAL":
235
+ hcpcs_code = "99202"
236
+ elif input == "NEW" and mdm_value == "LOW":
237
+ hcpcs_code = "99203"
238
+ elif input == "NEW" and mdm_value == "MODERATE":
239
+ hcpcs_code = "99204"
240
+ elif input == "NEW" and mdm_value == "HIGH":
241
+ hcpcs_code = "99205"
242
+
243
+ elif input == "ESTABLISHED" and mdm_value == "MINIMAL":
244
+ hcpcs_code = "99212"
245
+ elif input == "ESTABLISHED" and mdm_value == "LOW":
246
+ hcpcs_code = "99213"
247
+ elif input == "ESTABLISHED" and mdm_value == "MODERATE":
248
+ hcpcs_code = "99214"
249
+ elif input == "ESTABLISHED" and mdm_value == "HIGH":
250
+ hcpcs_code = "99215"
251
+
252
+ return hcpcs_code
253
+
254
+
255
+ def get_code2(input):
256
+ if input == "20 min":
257
+ hcpcs_code = "99242"
258
+ elif input == "30 min":
259
+ hcpcs_code = "99243"
260
+ elif input == "40 min":
261
+ hcpcs_code = "99244"
262
+ elif input == "55 min":
263
+ hcpcs_code = "99245"
264
+
265
+ return hcpcs_code
266
+
267
+
268
+ import gradio as gr
269
+
270
+ css = """
271
+ .col{
272
+ max-width: 70%;
273
+ margin: 0 auto;
274
+ display: flex;
275
+ flex-direction: column;
276
+ justify-content: center;
277
+ align-items: center;
278
+ }
279
+ """
280
+
281
+ with gr.Blocks(css=css) as demo:
282
+ gr.Markdown("## <center>Medical assistant bot</center>")
283
+
284
+ with gr.Tab("Get help with medical documents"):
285
+ with gr.Column(elem_classes="col"):
286
+ with gr.Tab("Upload and Process Document"):
287
+ with gr.Row():
288
+ with gr.Column():
289
+ docs_upload_input = gr.Files(label="Upload File")
290
+ docs_upload_button = gr.Button("Upload")
291
+ docs_upload_output = gr.Textbox(label="Output")
292
+
293
+ docs_process_button = gr.Button("Process")
294
+ docs_process_output = gr.Textbox(label="Output")
295
+
296
+ gr.ClearButton(
297
+ [docs_upload_input, docs_upload_output, docs_process_output]
298
+ )
299
+
300
+ with gr.Column():
301
+ ai_output1 = gr.Textbox(label="Output 1")
302
+ ai_output2 = gr.Textbox(label="Output 2")
303
+ ai_output3 = gr.Textbox(label="Output 3")
304
+ ai_output4 = gr.Textbox(label="Output 4")
305
+ ai_output5 = gr.Textbox(label="Output 5")
306
+ ai_output6 = gr.Textbox(label="Output 6")
307
+
308
+ gr.ClearButton(
309
+ [
310
+ ai_output1,
311
+ ai_output2,
312
+ ai_output3,
313
+ ai_output4,
314
+ ai_output5,
315
+ ai_output6,
316
+ ]
317
+ )
318
+
319
+ with gr.Tab("Get HCPCS Code (Primary Care Exception)"):
320
+ with gr.Column():
321
+ code_type_input1 = gr.Dropdown(choices=["NEW", "ESTABLISHED"])
322
+ code_type_button1 = gr.Button("Submit")
323
+ code_type_output1 = gr.Textbox(label="Output")
324
+
325
+ gr.ClearButton([code_type_input1, code_type_output1])
326
+
327
+ with gr.Tab("Get HCPCS Code (CONSULTATION)"):
328
+ with gr.Column():
329
+ code_type_input2 = gr.Dropdown(
330
+ choices=["20 min", "30 min", "40 min", "55 min"]
331
+ )
332
+ code_type_button2 = gr.Button("Submit")
333
+ code_type_output2 = gr.Textbox(label="Output")
334
+
335
+ gr.ClearButton([code_type_input2, code_type_output2])
336
+
337
+ #########################################################################################################
338
+
339
+ docs_upload_button.click(
340
+ save_docs, inputs=docs_upload_input, outputs=docs_upload_output
341
+ )
342
+ docs_process_button.click(
343
+ process_docs,
344
+ inputs=None,
345
+ outputs=[
346
+ docs_process_output,
347
+ ai_output1,
348
+ ai_output2,
349
+ ai_output3,
350
+ ai_output4,
351
+ ai_output5,
352
+ ai_output6,
353
+ ],
354
+ )
355
+
356
+ code_type_button1.click(
357
+ get_code1, inputs=code_type_input1, outputs=code_type_output1
358
+ )
359
+ code_type_button2.click(
360
+ get_code2, inputs=code_type_input2, outputs=code_type_output2
361
+ )
362
+
363
+ #########################################################################################################
364
+
365
+ demo.queue()
366
+ demo.launch(debug=True, share=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ langchain
2
+ python-docx
3
+ gradio
4
+ openai