MohammadArif commited on
Commit
aea3610
·
verified ·
1 Parent(s): 14cb1fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -49
app.py CHANGED
@@ -1,24 +1,53 @@
 
1
  import easyocr
2
- import requests
3
- from transformers import AutoModelForTokenClassification, AutoTokenizer
4
- from transformers import pipeline
5
 
6
- # Initialize EasyOCR for text extraction from medical report image
7
  reader = easyocr.Reader(['en'])
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  # Function to extract text from image
10
- def extract_text_from_image(image_path):
11
- result = reader.readtext(image_path)
12
  text = ' '.join([item[1] for item in result])
13
  return text
14
 
15
- # Load Med7 model for medical NER (Named Entity Recognition)
16
- model_name = "jeff1evesque/med7"
17
- tokenizer = AutoTokenizer.from_pretrained(model_name)
18
- model = AutoModelForTokenClassification.from_pretrained(model_name)
19
-
20
- # Initialize the NER pipeline with Med7 model
21
- nlp = pipeline("ner", model=model, tokenizer=tokenizer)
 
 
 
 
22
 
23
  # Function to extract medical entities (test results) from text
24
  def extract_medical_values(text):
@@ -26,53 +55,52 @@ def extract_medical_values(text):
26
  medical_data = {}
27
 
28
  for entity in entities:
29
- if entity['entity_group'] == 'LAB_RESULT': # Change based on Med7's NER labels
30
  medical_data[entity['word']] = entity['score']
31
 
32
  return medical_data
33
 
34
- # Function to check the values with LabTestAPI or similar API
35
- def validate_medical_value(test_name, test_value):
36
- # Example of API call (replace with actual API)
37
- api_url = "https://api.labtestapi.com/get_reference_range"
38
- params = {
39
- "test_name": test_name,
40
- "value": test_value
41
- }
42
- response = requests.get(api_url, params=params)
43
- if response.status_code == 200:
44
- result = response.json()
45
- return result['normal_range']
46
- else:
47
- return None
48
-
49
  # Function to analyze medical report
50
- def analyze_report(image_path):
51
- # Step 1: Extract text from the medical report image
52
- text = extract_text_from_image(image_path)
 
 
 
 
53
 
54
- # Step 2: Extract medical values using Med7
55
  medical_values = extract_medical_values(text)
56
 
57
  analysis_results = []
58
 
59
  for test_name, test_value in medical_values.items():
60
- # Step 3: Validate test value against normal range
61
- normal_range = validate_medical_value(test_name, test_value)
62
- if normal_range:
63
- if test_value < normal_range[0]:
64
- analysis_results.append(f"{test_name} is low. Consider consulting a doctor.")
65
- elif test_value > normal_range[1]:
66
- analysis_results.append(f"{test_name} is high. Consult a doctor.")
67
- else:
68
- analysis_results.append(f"{test_name} is within the normal range.")
69
- else:
70
- analysis_results.append(f"Could not validate {test_name}.")
71
 
72
  return analysis_results
73
 
74
- # Example Usage
75
- image_path = "path_to_medical_report_image.png"
76
- analysis = analyze_report(image_path)
77
- for result in analysis:
78
- print(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
  import easyocr
3
+ from transformers import AutoModelForTokenClassification, AutoTokenizer, pipeline
4
+ from PIL import Image
5
+ import numpy as np
6
 
7
+ # Initialize EasyOCR for text extraction
8
  reader = easyocr.Reader(['en'])
9
 
10
+ # Load ClinicalBERT for NER (Named Entity Recognition)
11
+ model_name = "emilyalsentzer/Bio_ClinicalBERT"
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ model = AutoModelForTokenClassification.from_pretrained(model_name)
14
+ nlp = pipeline("ner", model=model, tokenizer=tokenizer)
15
+
16
+ # Mock function for validating test results
17
+ def validate_medical_value(test_name, test_value):
18
+ reference_ranges = {
19
+ "Blood Pressure": (90, 120), # Example range: systolic
20
+ "Glucose": (70, 100), # Normal range for glucose in mg/dL
21
+ }
22
+
23
+ if test_name in reference_ranges:
24
+ min_range, max_range = reference_ranges[test_name]
25
+ if test_value < min_range:
26
+ return f"{test_name} is low."
27
+ elif test_value > max_range:
28
+ return f"{test_name} is high."
29
+ else:
30
+ return f"{test_name} is within the normal range."
31
+ else:
32
+ return f"{test_name} range is unknown."
33
+
34
  # Function to extract text from image
35
+ def extract_text_from_image(image):
36
+ result = reader.readtext(image)
37
  text = ' '.join([item[1] for item in result])
38
  return text
39
 
40
+ # Function to check image clarity (basic approach using edge detection)
41
+ def check_image_clarity(image):
42
+ # Convert image to grayscale
43
+ gray_image = image.convert("L")
44
+ image_np = np.array(gray_image)
45
+
46
+ # Simple edge detection (Laplacian method)
47
+ laplacian_var = cv2.Laplacian(image_np, cv2.CV_64F).var()
48
+
49
+ # Threshold for determining clarity
50
+ return laplacian_var > 100 # This threshold can be adjusted
51
 
52
  # Function to extract medical entities (test results) from text
53
  def extract_medical_values(text):
 
55
  medical_data = {}
56
 
57
  for entity in entities:
58
+ if entity['entity_group'] in ['LAB_RESULT', 'DISEASE', 'MEDICATION']: # Modify based on actual labels
59
  medical_data[entity['word']] = entity['score']
60
 
61
  return medical_data
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  # Function to analyze medical report
64
+ def analyze_report(image):
65
+ # Step 1: Check image clarity
66
+ if not check_image_clarity(image):
67
+ return "Image is unclear, please upload a clearer image."
68
+
69
+ # Step 2: Extract text from the medical report image
70
+ text = extract_text_from_image(image)
71
 
72
+ # Step 3: Extract medical values using ClinicalBERT
73
  medical_values = extract_medical_values(text)
74
 
75
  analysis_results = []
76
 
77
  for test_name, test_value in medical_values.items():
78
+ # Step 4: Validate test value against normal range
79
+ validation_result = validate_medical_value(test_name, test_value)
80
+ analysis_results.append(validation_result)
 
 
 
 
 
 
 
 
81
 
82
  return analysis_results
83
 
84
+ # Streamlit UI
85
+ st.title("Medical Report Analyzer")
86
+ st.write("Upload your medical report image to analyze the test results.")
87
+
88
+ # Upload the image
89
+ uploaded_image = st.file_uploader("Upload Image", type=["jpg", "png", "jpeg"])
90
+
91
+ if uploaded_image is not None:
92
+ # Open the uploaded image
93
+ image = Image.open(uploaded_image)
94
+
95
+ # Show image for verification
96
+ st.image(image, caption="Uploaded Medical Report", use_column_width=True)
97
+
98
+ # Analyze the report
99
+ analysis_results = analyze_report(image)
100
+
101
+ # Display results
102
+ if isinstance(analysis_results, list):
103
+ for result in analysis_results:
104
+ st.write(result)
105
+ else:
106
+ st.write(analysis_results)