Spaces:
Sleeping
Sleeping
srinidhidevaraj
commited on
Commit
•
cdca3a4
1
Parent(s):
bd1c620
Update helpers.py
Browse files- helpers.py +215 -214
helpers.py
CHANGED
@@ -1,214 +1,215 @@
|
|
1 |
-
import re
|
2 |
-
import os
|
3 |
-
import simple_icd_10_cm as cm
|
4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
-
# from openai import OpenAI
|
6 |
-
from prompt_template import *
|
7 |
-
from langchain_groq import ChatGroq
|
8 |
-
from groq import Groq
|
9 |
-
from dotenv import load_dotenv
|
10 |
-
import csv
|
11 |
-
import time
|
12 |
-
load_dotenv()
|
13 |
-
|
14 |
-
os.environ["LANGCHAIN_TRACING_V2"]="true"
|
15 |
-
groq_api_key=os.environ.get('GROQ_API_KEY')
|
16 |
-
os.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
Spanish
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
cleaned_text =
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
cleaned_text =
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
for
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
|
|
|
1 |
+
import re
|
2 |
+
import os
|
3 |
+
import simple_icd_10_cm as cm
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
# from openai import OpenAI
|
6 |
+
from prompt_template import *
|
7 |
+
from langchain_groq import ChatGroq
|
8 |
+
from groq import Groq
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
import csv
|
11 |
+
import time
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
os.environ["LANGCHAIN_TRACING_V2"]="true"
|
15 |
+
# groq_api_key=os.environ.get('GROQ_API_KEY')
|
16 |
+
groq_api_key=os.getenv('GROQ_API_KEY')
|
17 |
+
os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"
|
18 |
+
LANGCHAIN_API_KEY=os.environ.get("LANGCHAIN_API_KEY")
|
19 |
+
|
20 |
+
client = Groq()
|
21 |
+
|
22 |
+
CHAPTER_LIST = cm.chapter_list
|
23 |
+
|
24 |
+
def construct_translation_prompt(medical_note):
|
25 |
+
"""
|
26 |
+
Construct a prompt template for translating spanish medical notes to english.
|
27 |
+
|
28 |
+
Args:
|
29 |
+
medical_note (str): The medical case note.
|
30 |
+
|
31 |
+
Returns:
|
32 |
+
str: A structured template ready to be used as input for a language model.
|
33 |
+
"""
|
34 |
+
translation_prompt = """You are an expert Spanish-to-English translator. You are provided with a clinical note written in Spanish.
|
35 |
+
You must translate the note into English. You must ensure that you properly translate the medical and technical terms from Spanish to English without any mistakes.
|
36 |
+
Spanish Medical Note:
|
37 |
+
{medical_note}"""
|
38 |
+
|
39 |
+
return translation_prompt.format(medical_note = medical_note)
|
40 |
+
|
41 |
+
def build_translation_prompt(input_note, system_prompt=""):
|
42 |
+
"""
|
43 |
+
Build a zero-shot prompt for translating spanish medical notes to english.
|
44 |
+
|
45 |
+
Args:
|
46 |
+
input_note (str): The input note or query.
|
47 |
+
system_prompt (str): Optional initial system prompt or instruction.
|
48 |
+
|
49 |
+
Returns:
|
50 |
+
list of dict: A structured list of dictionaries defining the role and content of each message.
|
51 |
+
"""
|
52 |
+
input_prompt = construct_translation_prompt(input_note)
|
53 |
+
|
54 |
+
|
55 |
+
return [{"role": "system", "content": system_prompt}, {"role": "user", "content": input_prompt}]
|
56 |
+
|
57 |
+
|
58 |
+
def remove_extra_spaces(text):
|
59 |
+
"""
|
60 |
+
Remove extra spaces from a given text.
|
61 |
+
|
62 |
+
Args:
|
63 |
+
text (str): The original text string.
|
64 |
+
|
65 |
+
Returns:
|
66 |
+
str: The cleaned text with extra spaces removed.
|
67 |
+
"""
|
68 |
+
return re.sub(r'\s+', ' ', text).strip()
|
69 |
+
|
70 |
+
def remove_last_parenthesis(text):
|
71 |
+
"""
|
72 |
+
Removes the last occurrence of content within parentheses from the provided text.
|
73 |
+
|
74 |
+
Args:
|
75 |
+
text (str): The input string from which to remove the last parentheses and its content.
|
76 |
+
|
77 |
+
Returns:
|
78 |
+
str: The modified string with the last parentheses content removed.
|
79 |
+
"""
|
80 |
+
pattern = r'\([^()]*\)(?!.*\([^()]*\))'
|
81 |
+
cleaned_text = re.sub(pattern, '', text)
|
82 |
+
return cleaned_text
|
83 |
+
|
84 |
+
def format_code_descriptions(text, model_name):
|
85 |
+
"""
|
86 |
+
Format the ICD-10 code descriptions by removing content inside brackets and extra spaces.
|
87 |
+
|
88 |
+
Args:
|
89 |
+
text (str): The original text containing ICD-10 code descriptions.
|
90 |
+
|
91 |
+
Returns:
|
92 |
+
str: The cleaned text with content in brackets removed and extra spaces cleaned up.
|
93 |
+
"""
|
94 |
+
pattern = r'\([^()]*\)(?!.*\([^()]*\))'
|
95 |
+
cleaned_text = remove_last_parenthesis(text)
|
96 |
+
cleaned_text = remove_extra_spaces(cleaned_text)
|
97 |
+
|
98 |
+
return cleaned_text
|
99 |
+
|
100 |
+
def construct_prompt_template(case_note, code_descriptions, model_name):
|
101 |
+
"""
|
102 |
+
Construct a prompt template for evaluating ICD-10 code descriptions against a given case note.
|
103 |
+
|
104 |
+
Args:
|
105 |
+
case_note (str): The medical case note.
|
106 |
+
code_descriptions (str): The ICD-10 code descriptions formatted as a single string.
|
107 |
+
|
108 |
+
Returns:
|
109 |
+
str: A structured template ready to be used as input for a language model.
|
110 |
+
"""
|
111 |
+
template = prompt_template_dict[model_name]
|
112 |
+
|
113 |
+
return template.format(note=case_note, code_descriptions=code_descriptions)
|
114 |
+
|
115 |
+
def build_zero_shot_prompt(input_note, descriptions, model_name, system_prompt=""):
|
116 |
+
"""
|
117 |
+
Build a zero-shot classification prompt with system and user roles for a language model.
|
118 |
+
|
119 |
+
Args:
|
120 |
+
input_note (str): The input note or query.
|
121 |
+
descriptions (list of str): List of ICD-10 code descriptions.
|
122 |
+
system_prompt (str): Optional initial system prompt or instruction.
|
123 |
+
|
124 |
+
Returns:
|
125 |
+
list of dict: A structured list of dictionaries defining the role and content of each message.
|
126 |
+
"""
|
127 |
+
if model_name == "llama3-70b-8192":
|
128 |
+
code_descriptions = "\n".join(["* " + x for x in descriptions])
|
129 |
+
else:
|
130 |
+
|
131 |
+
code_descriptions = "\n".join(["* " + x for x in descriptions])
|
132 |
+
|
133 |
+
|
134 |
+
input_prompt = construct_prompt_template(input_note, code_descriptions, model_name)
|
135 |
+
return [{"role": "system", "content": system_prompt}, {"role": "user", "content": input_prompt}]
|
136 |
+
|
137 |
+
def get_response(messages, model_name, temperature=0.0, max_tokens=500):
|
138 |
+
"""
|
139 |
+
Obtain responses from a specified model via the chat-completions API.
|
140 |
+
|
141 |
+
Args:
|
142 |
+
messages (list of dict): List of messages structured for API input.
|
143 |
+
model_name (str): Identifier for the model to query.
|
144 |
+
temperature (float): Controls randomness of response, where 0 is deterministic.
|
145 |
+
max_tokens (int): Limit on the number of tokens in the response.
|
146 |
+
|
147 |
+
Returns:
|
148 |
+
str: The content of the response message from the model.
|
149 |
+
"""
|
150 |
+
response = client.chat.completions.create(
|
151 |
+
model=model_name,
|
152 |
+
messages=messages,
|
153 |
+
temperature=temperature,
|
154 |
+
max_tokens=max_tokens
|
155 |
+
)
|
156 |
+
return response.choices[0].message.content
|
157 |
+
|
158 |
+
def remove_noisy_prefix(text):
|
159 |
+
# Removing numbers or letters followed by a dot and optional space at the beginning of the string
|
160 |
+
cleaned_text = text.replace("* ", "").strip()
|
161 |
+
cleaned_text = re.sub(r"^\s*\w+\.\s*", "", cleaned_text)
|
162 |
+
return cleaned_text.strip()
|
163 |
+
def parse_outputs(output, code_description_map, model_name):
|
164 |
+
"""
|
165 |
+
Parse model outputs to confirm ICD-10 codes based on a given description map.
|
166 |
+
|
167 |
+
Args:
|
168 |
+
output (str): The model output containing confirmations.
|
169 |
+
code_description_map (dict): Mapping of descriptions to ICD-10 codes.
|
170 |
+
|
171 |
+
Returns:
|
172 |
+
list of dict: A list of confirmed codes and their descriptions.
|
173 |
+
"""
|
174 |
+
confirmed_codes = []
|
175 |
+
split_outputs = [x for x in output.split("\n") if x]
|
176 |
+
for item in split_outputs:
|
177 |
+
try:
|
178 |
+
code_description, confirmation = item.split(":", 1)
|
179 |
+
# print(confirmation)
|
180 |
+
cnf,fact = confirmation.split(",", 1)
|
181 |
+
|
182 |
+
|
183 |
+
if model_name == "llama3-70b-8192":
|
184 |
+
code_description = remove_noisy_prefix(code_description)
|
185 |
+
else:
|
186 |
+
code_description = remove_noisy_prefix(code_description)
|
187 |
+
|
188 |
+
if confirmation.lower().strip().startswith("yes"):
|
189 |
+
try:
|
190 |
+
|
191 |
+
code = code_description_map[code_description]
|
192 |
+
|
193 |
+
|
194 |
+
confirmed_codes.append({"ICD Code": code, "Code Description": code_description,"Evidence From Notes":fact})
|
195 |
+
|
196 |
+
except Exception as e:
|
197 |
+
# print(str(e) + " Here")
|
198 |
+
continue
|
199 |
+
except:
|
200 |
+
continue
|
201 |
+
return confirmed_codes
|
202 |
+
|
203 |
+
def get_name_and_description(code, model_name):
|
204 |
+
"""
|
205 |
+
Retrieve the name and description of an ICD-10 code.
|
206 |
+
|
207 |
+
Args:
|
208 |
+
code (str): The ICD-10 code.
|
209 |
+
|
210 |
+
Returns:
|
211 |
+
tuple: A tuple containing the formatted description and the name of the code.
|
212 |
+
"""
|
213 |
+
full_data = cm.get_full_data(code).split("\n")
|
214 |
+
return format_code_descriptions(full_data[3], model_name), full_data[1]
|
215 |
+
|