Spaces:
Running
Running
import easyocr | |
import requests | |
from transformers import AutoModelForTokenClassification, AutoTokenizer | |
from transformers import pipeline | |
# Initialize EasyOCR for text extraction from medical report image | |
reader = easyocr.Reader(['en']) | |
# Function to extract text from image | |
def extract_text_from_image(image_path): | |
result = reader.readtext(image_path) | |
text = ' '.join([item[1] for item in result]) | |
return text | |
# Load Med7 model for medical NER (Named Entity Recognition) | |
model_name = "jeff1evesque/med7" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForTokenClassification.from_pretrained(model_name) | |
# Initialize the NER pipeline with Med7 model | |
nlp = pipeline("ner", model=model, tokenizer=tokenizer) | |
# Function to extract medical entities (test results) from text | |
def extract_medical_values(text): | |
entities = nlp(text) | |
medical_data = {} | |
for entity in entities: | |
if entity['entity_group'] == 'LAB_RESULT': # Change based on Med7's NER labels | |
medical_data[entity['word']] = entity['score'] | |
return medical_data | |
# Function to check the values with LabTestAPI or similar API | |
def validate_medical_value(test_name, test_value): | |
# Example of API call (replace with actual API) | |
api_url = "https://api.labtestapi.com/get_reference_range" | |
params = { | |
"test_name": test_name, | |
"value": test_value | |
} | |
response = requests.get(api_url, params=params) | |
if response.status_code == 200: | |
result = response.json() | |
return result['normal_range'] | |
else: | |
return None | |
# Function to analyze medical report | |
def analyze_report(image_path): | |
# Step 1: Extract text from the medical report image | |
text = extract_text_from_image(image_path) | |
# Step 2: Extract medical values using Med7 | |
medical_values = extract_medical_values(text) | |
analysis_results = [] | |
for test_name, test_value in medical_values.items(): | |
# Step 3: Validate test value against normal range | |
normal_range = validate_medical_value(test_name, test_value) | |
if normal_range: | |
if test_value < normal_range[0]: | |
analysis_results.append(f"{test_name} is low. Consider consulting a doctor.") | |
elif test_value > normal_range[1]: | |
analysis_results.append(f"{test_name} is high. Consult a doctor.") | |
else: | |
analysis_results.append(f"{test_name} is within the normal range.") | |
else: | |
analysis_results.append(f"Could not validate {test_name}.") | |
return analysis_results | |
# Example Usage | |
image_path = "path_to_medical_report_image.png" | |
analysis = analyze_report(image_path) | |
for result in analysis: | |
print(result) | |