File size: 9,609 Bytes
371da07 22b21fc 371da07 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
import json
from config import google_api
import os
import base64
from google import genai
from google.genai import types
def process_text(extracted_text):
"""Lab Test and metadata entity recognition using gemini flash"""
''' Return type: JSON '''
print("Performing Named Entity Recognition...")
client = genai.Client(
api_key=google_api,
)
model = "gemini-2.0-flash"
contents = [
types.Content(
role="user",
parts=[
types.Part.from_text(text="""The following text is extracted from a medical lab report using OCR.
There may be errors such as missing decimals, incorrect test names, and incorrect reference ranges.
Please correct the errors and extract both metadata and structured lab test data.
ALWAYS MAKE SURE THAT THE VALUE ALIGNS WITH THE REAL RANGE OF THE TEST
AND CLEARLY IDENTIFY REDS WITH LOW AND HIGH
Return the output in structured JSON format with all the information in lowercase to standardization.
And follow the JSON format provided and don't add any additional details in meta data or lab report other than that are specified
Extracted Text:
Dr. Onkar Test Sanjeevan Hospital\\n\\nMBBS, MD | Reg No: T123 12/4, Paud Road, Kothrud, Pune - 411023\\nPh: 0202526245, 8983390126, Timing: 09:15 AM -\\n02:30 PM, 05:30 PM - 09:30 PM, APPOINTMENTS\\nONLY | Closed: Monday,Friday\\n\\n \\n\\nPatient UID: 87 Report No: 00018\\n\\nName: AMAR SHAHA (Male} Rey, Date: 09-Jul-20\\n\\nAge 40 years Sample Collected At Hospital Lab\\n\\nAddress: MG Road, PUNE Sample Type/Quantity: Blood\\n\\nRef. By Doctor . Sample Collection D/T: 09-Jul-20, 9.50 AM\\nCr Test Result D/T: 09-Jul-20, 4:53 PM\\n\\n \\n \\n\\nDr. Amit Deshmukh\\n\\n \\n\\nHEMOGRAM\\n\\nINVESTIGATION RESULT UNIT REF, RANGE\\nHAEMOGLOBIN : 14 gms/dl 12.0 - 17.0\\nRBC COUNT E 44 millfeumm 4.1-5.1\\nHAEMOTOCRIT (PCV) E 30 % 32.0 - 47.0\\nMCV $ 78 fl 760 - 100.0\\nMCH H 3246 Py 260-320\\nMCHC | : 328 n% 315-3465 ,\\nROW ; 13.9 % 11.6-150\\nMPV ; 11.2 fn 68- 12.6\\nWBC COUNT : 4567 /eamm 4000 - 11000\\nDIFFERENTIAL COUNT\\nNEUTROPHILS |» : 56 %y 40-70\\nLYMPHOCYTES ; 20 % 20.0- 45.0\\nEOSINOPHILS . 4 « % 0-6\\nMONOCYTES : 5 %
Expected JSON format:
{
\"metadata\": {
\"patient_name\": \"Prasahsst Pawar\",
\"age\": \"20\",
\"gender\": \"Male\",
\"lab_name\": \"XYZ Diagnostics\",
\"report_date\": \"05-03-2025\"
},
\"lab_tests\": [
{
\"test_name\": \"hemoglobin\",
\"value\": \"14.2\",
\"unit\": \"g/dL\",
\"reference_range\": \"13.5 - 17.5 g/dL\"
},
{
\"test_name\": \"rbc count\",
\"value\": \"5.2\",
\"unit\": \"million/cu mm\",
\"reference_range\": \"4.1-5.1\"
},
{
\"test_name\": \"glucose\",
\"value\": \"65\",
\"unit\": \"mg/dL\",
\"reference_range\": \"70 - 110 mg/dL\"
}
],
\"reds\":{
\"low\":[\"glucose\"],
\"high\":[\"rbc count\"]
}
}"""),
],
),
types.Content(
role="model",
parts=[
types.Part.from_text(text="""{
\"lab_tests\": [
{
\"reference_range\": \"12.0 - 17.0\",
\"test_name\": \"haemoglobin\",
\"unit\": \"gms/dl\",
\"value\": \"14\"
},
{
\"reference_range\": \"4.1-5.1\",
\"test_name\": \"rbc count\",
\"unit\": \"mill/cu mm\",
\"value\": \"4.4\"
},
{
\"reference_range\": \"32.0 - 47.0\",
\"test_name\": \"haematocrit (pcv)\",
\"unit\": \"%\",
\"value\": \"30\"
},
{
\"reference_range\": \"76.0 - 100.0\",
\"test_name\": \"mcv\",
\"unit\": \"fl\",
\"value\": \"78\"
},
{
\"reference_range\": \"26.0-32.0\",
\"test_name\": \"mch\",
\"unit\": \"pg\",
\"value\": \"32.46\"
},
{
\"reference_range\": \"31.5-34.5\",
\"test_name\": \"mchc\",
\"unit\": \"%\",
\"value\": \"32.8\"
},
{
\"reference_range\": \"11.6-15.0\",
\"test_name\": \"rdw\",
\"unit\": \"%\",
\"value\": \"13.9\"
},
{
\"reference_range\": \"6.8- 12.6\",
\"test_name\": \"mpv\",
\"unit\": \"fn\",
\"value\": \"11.2\"
},
{
\"reference_range\": \"4000 - 11000\",
\"test_name\": \"wbc count\",
\"unit\": \"/cu mm\",
\"value\": \"4567\"
},
{
\"reference_range\": \"40-70\",
\"test_name\": \"neutrophils\",
\"unit\": \"%\",
\"value\": \"56\"
},
{
\"reference_range\": \"20.0- 45.0\",
\"test_name\": \"lymphocytes\",
\"unit\": \"%\",
\"value\": \"20\"
},
{
\"reference_range\": \"0-6\",
\"test_name\": \"eosinophils\",
\"unit\": \"%\",
\"value\": \"4\"
},
{
\"reference_range\": \"2-10\",
\"test_name\": \"monocytes\",
\"unit\": \"%\",
\"value\": \"5\"
}
],
\"metadata\": {
\"age\": \"40\",
\"gender\": \"male\",
\"lab_name\": \"sanjeevan hospital\",
\"patient_name\": \"amar shaha\",
\"report_date\": \"09-jul-20\"
},
\"reds\": {
\"high\": [
\"mch\"
],
\"low\": [
\"haematocrit (pcv)\"
]
}
}"""),
],
),
types.Content(
role="user",
parts=[
types.Part.from_text(text=extracted_text),
],
),
]
generate_content_config = types.GenerateContentConfig(
temperature=1,
top_p=0.95,
top_k=40,
max_output_tokens=8192,
response_mime_type="application/json",
response_schema=genai.types.Schema(
type = genai.types.Type.OBJECT,
enum = [],
required = ["metadata", "lab_tests", "reds"],
properties = {
"metadata": genai.types.Schema(
type = genai.types.Type.OBJECT,
enum = [],
required = ["patient_name", "age", "gender", "lab_name", "report_date"],
properties = {
"patient_name": genai.types.Schema(
type = genai.types.Type.STRING,
),
"age": genai.types.Schema(
type = genai.types.Type.STRING,
),
"gender": genai.types.Schema(
type = genai.types.Type.STRING,
),
"lab_name": genai.types.Schema(
type = genai.types.Type.STRING,
),
"report_date": genai.types.Schema(
type = genai.types.Type.STRING,
),
},
),
"lab_tests": genai.types.Schema(
type = genai.types.Type.ARRAY,
items = genai.types.Schema(
type = genai.types.Type.OBJECT,
enum = [],
required = ["test_name", "value", "unit", "reference_range"],
properties = {
"test_name": genai.types.Schema(
type = genai.types.Type.STRING,
),
"value": genai.types.Schema(
type = genai.types.Type.STRING,
),
"unit": genai.types.Schema(
type = genai.types.Type.STRING,
),
"reference_range": genai.types.Schema(
type = genai.types.Type.STRING,
),
},
),
),
"reds": genai.types.Schema(
type = genai.types.Type.OBJECT,
enum = [],
required = ["low", "high"],
properties = {
"low": genai.types.Schema(
type = genai.types.Type.ARRAY,
items = genai.types.Schema(
type = genai.types.Type.STRING,
),
),
"high": genai.types.Schema(
type = genai.types.Type.ARRAY,
items = genai.types.Schema(
type = genai.types.Type.STRING,
),
),
},
),
},
),
system_instruction=[
types.Part.from_text(text="""Always return the output as JSON only"""),
],
)
# for chunk in client.models.generate_content_stream(
# model=model,
# contents=contents,
# config=generate_content_config,
# ):
# print(chunk.text, end="")
try:
response = client.models.generate_content(
model=model, contents=contents, config=generate_content_config
)
json_response = response.text # Ensure response is JSON formatted
parsed_json = json.loads(json_response) # Convert JSON string to Python dictionary
return parsed_json
except json.JSONDecodeError:
print("Error: Invalid JSON response from the model.")
return None
|