Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,59 @@
|
|
|
|
|
| 1 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
| 8 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
PATIENT_RECORDS_DIR = "records
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
def extract_patient_name(file_name):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
image = Image.open(image_path).convert("RGB")
|
| 23 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 24 |
generated_ids = model.generate(pixel_values)
|
| 25 |
-
|
| 26 |
-
return
|
| 27 |
-
|
| 28 |
-
# Save
|
| 29 |
-
def
|
| 30 |
-
os.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
if not patient_name:
|
| 41 |
-
return "❌
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
# Example usage
|
| 48 |
if __name__ == "__main__":
|
| 49 |
-
|
| 50 |
-
result = process_uploaded_lab_result(file_to_upload)
|
| 51 |
-
print(result)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
|
| 7 |
+
# Load OCR model
|
| 8 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
| 9 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
| 10 |
|
| 11 |
+
# Folder to store extracted records
|
| 12 |
+
PATIENT_RECORDS_DIR = "records"
|
| 13 |
+
os.makedirs(PATIENT_RECORDS_DIR, exist_ok=True)
|
| 14 |
|
| 15 |
+
# Extract patient name from filename
|
| 16 |
def extract_patient_name(file_name):
|
| 17 |
+
# Example: JuanDelaCruz_2025-06-13.png → JuanDelaCruz
|
| 18 |
+
match = re.match(r"([A-Za-z]+[A-Za-z]*)_.*\.(jpg|jpeg|png)$", file_name)
|
| 19 |
+
return match.group(1) if match else None
|
| 20 |
+
|
| 21 |
+
# OCR logic
|
| 22 |
+
def perform_ocr(image_file):
|
| 23 |
+
image = Image.open(image_file).convert("RGB")
|
|
|
|
| 24 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 25 |
generated_ids = model.generate(pixel_values)
|
| 26 |
+
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
| 27 |
+
return text
|
| 28 |
+
|
| 29 |
+
# Save to patient record file
|
| 30 |
+
def save_record(patient_name, ocr_text):
|
| 31 |
+
file_path = os.path.join(PATIENT_RECORDS_DIR, f"{patient_name}_records.txt")
|
| 32 |
+
with open(file_path, "a") as f:
|
| 33 |
+
f.write("\n\n===== New Lab Result =====\n")
|
| 34 |
+
f.write(ocr_text)
|
| 35 |
+
|
| 36 |
+
# Gradio function
|
| 37 |
+
def process_lab_result(image):
|
| 38 |
+
file_name = os.path.basename(image.name)
|
| 39 |
+
patient_name = extract_patient_name(file_name)
|
| 40 |
+
|
| 41 |
if not patient_name:
|
| 42 |
+
return "❌ Cannot extract patient name from filename. Please name the file like JuanDelaCruz_2025-06-13.png"
|
| 43 |
+
|
| 44 |
+
ocr_text = perform_ocr(image)
|
| 45 |
+
save_record(patient_name, ocr_text)
|
| 46 |
+
|
| 47 |
+
return f"✅ OCR completed. Lab result saved to `{patient_name}_records.txt`.\n\n---\n📄 Extracted Text:\n{ocr_text}"
|
| 48 |
|
| 49 |
+
# Gradio interface
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=process_lab_result,
|
| 52 |
+
inputs=gr.File(label="Upload Lab Result Image (.png, .jpg)", type="file"),
|
| 53 |
+
outputs="text",
|
| 54 |
+
title="🩺 Lab Result OCR with Patient Linking",
|
| 55 |
+
description="Upload a lab result image named like `JuanDelaCruz_2025-06-13.png`. The system will extract the text and save it to the patient's record."
|
| 56 |
+
)
|
| 57 |
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
+
iface.launch()
|
|
|
|
|
|