File size: 11,785 Bytes
898d076 228c968 42ba117 52bb493 42ba117 52bb493 42ba117 898d076 52bb493 42ba117 52bb493 42ba117 52bb493 42ba117 52bb493 898d076 52bb493 898d076 52bb493 898d076 6d2b657 898d076 c937396 4c39be6 c937396 898d076 c937396 6d2b657 4c39be6 c937396 6d2b657 c937396 52bb493 c937396 898d076 c937396 4c39be6 898d076 6d2b657 898d076 6d2b657 c937396 898d076 e402274 4a9cb85 |
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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
import gradio as gr
import pandas as pd
import json
import fitz # PyMuPDF
import pytesseract
from pdf2image import convert_from_path
import os
import base64
from google import genai
from google.genai import types
google_api=os.getenv("google_api")
def read_pdf(pdf_path):
text = ""
doc = fitz.open(pdf_path)
for page_num in range(len(doc)):
page = doc.load_page(page_num)
page_text = page.get_text("text").strip() # Extract text from page
# Extract Images for OCR
images = page.get_images(full=True) # Check if the page has images
ocr_text = ""
if images: # If images exist, process them
print(f"Page {page_num + 1} contains images, performing OCR...")
img_pages = convert_from_path(pdf_path, first_page=page_num + 1, last_page=page_num + 1)
for img in img_pages:
ocr_text += pytesseract.image_to_string(img).strip() + "\n"
# Combine both text extraction methods
combined_text = f"{page_text}\n{ocr_text}".strip()
if combined_text:
text += combined_text + "\n\n"
return text.strip()
def generate(extracted_text):
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="")
response = client.models.generate_content(
model=model,
contents=contents,
config=generate_content_config,
)
json_response = response.text # The API should return JSON text
parsed_json = json.loads(json_response) # Convert JSON string to Python dictionary
return parsed_json
# Gradio interface function
def process_pdf(pdf):
text = read_pdf(pdf) # Extract text from PDF
# # print(text)
output = generate(text) # Generate structured JSON
return output
def show_to_UI(pdf):
output = process_pdf(pdf) # Call process_pdf to get JSON
# Extract metadata
metadata = output["metadata"]
labtests = pd.DataFrame(output["lab_tests"])
reds = pd.DataFrame(output["reds"])
metadata_str = f"**Patient Name:** {metadata['patient_name']}\n\n" \
f"**Age:** {metadata['age']}\n\n" \
f"**Gender:** {metadata['gender']}\n\n" \
f"**Lab Name:** {metadata['lab_name']}\n\n" \
f"**Report Date:** {metadata['report_date']}"
return metadata_str, labtests, reds, output
# Define Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Medical Lab Report Processor")
with gr.Row():
pdf_input = gr.File(label="Upload PDF Report")
submit_btn = gr.Button("Process")
metadata_output = gr.Markdown("**Patient Name: Prashasst...**")
lab_test_output = gr.Dataframe(label="Lab Test Results")
reds_output = gr.Dataframe(label="Reds")
output_JSON = gr.JSON(label="Extracted Report") # Show JSON output
submit_btn.click(show_to_UI, inputs=[pdf_input], outputs=[metadata_output, lab_test_output, reds_output,output_JSON])
demo.launch() |