rijdev commited on
Commit
65a8134
Β·
verified Β·
1 Parent(s): 1c00366

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -35
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 Hugging Face OCR model
7
  processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
8
  model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
9
 
10
- # Directory where patient records are stored
11
- PATIENT_RECORDS_DIR = "records/"
 
12
 
13
- # Function to extract patient name from filename
14
  def extract_patient_name(file_name):
15
- match = re.match(r"([A-Za-z]+[A-Za-z]*)_.*\.(jpg|png|jpeg|pdf)$", file_name)
16
- if match:
17
- return match.group(1)
18
- return None
19
-
20
- # OCR function
21
- def extract_text_from_image(image_path):
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
- generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
26
- return generated_text.strip()
27
-
28
- # Save text to patient record
29
- def save_to_patient_record(patient_name, text):
30
- os.makedirs(PATIENT_RECORDS_DIR, exist_ok=True)
31
- filepath = os.path.join(PATIENT_RECORDS_DIR, f"{patient_name}_records.txt")
32
- with open(filepath, "a") as file:
33
- file.write("\n\n===== New Upload =====\n")
34
- file.write(text)
35
-
36
- # Main process
37
- def process_uploaded_lab_result(file_path):
38
- print(f"Processing: {file_path}")
39
- patient_name = extract_patient_name(os.path.basename(file_path))
40
  if not patient_name:
41
- return "❌ Could not determine patient name from filename."
 
 
 
 
 
42
 
43
- ocr_text = extract_text_from_image(file_path)
44
- save_to_patient_record(patient_name, ocr_text)
45
- return f"βœ… OCR completed and saved under {patient_name}'s record."
 
 
 
 
 
46
 
47
- # Example usage
48
  if __name__ == "__main__":
49
- file_to_upload = "JuanDelaCruz_2025-06-13.jpg" # Example uploaded file
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()