File size: 3,073 Bytes
c953483
4fdc44f
c953483
4fdc44f
 
 
c953483
4fdc44f
 
 
 
c953483
4fdc44f
 
 
 
 
c953483
4fdc44f
c953483
4fdc44f
c953483
4fdc44f
 
 
 
c953483
4fdc44f
c953483
 
4fdc44f
 
 
c953483
 
4fdc44f
c953483
4fdc44f
 
 
 
c953483
4fdc44f
 
 
 
c953483
 
 
4fdc44f
 
c953483
 
 
4fdc44f
 
c953483
 
 
 
 
 
 
 
 
 
 
 
4fdc44f
c953483
 
 
4fdc44f
c953483
 
 
 
 
 
 
4fdc44f
c953483
 
4fdc44f
c953483
 
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
import streamlit as st
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
import torch
import easyocr
from PIL import Image
import fitz  # PyMuPDF

# Load EasyOCR Reader
reader = easyocr.Reader(['en'])

# Load ClinicalBERT Model
@st.cache_resource
def load_clinicalbert():
    model_name = "emilyalsentzer/Bio_ClinicalBERT"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSequenceClassification.from_pretrained(model_name)
    return pipeline("text-classification", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)

clinicalbert_analyzer = load_clinicalbert()

# OCR function using EasyOCR
def extract_text_from_image(image):
    image = Image.open(image).convert('RGB')
    result = reader.readtext(image, detail=0)
    extracted_text = " ".join(result)
    return extracted_text

# PDF text extraction using PyMuPDF
def extract_text_from_pdf(pdf_file):
    text = ""
    with fitz.open(pdf_file) as pdf:
        for page in pdf:
            text += page.get_text()
    return text

# Analysis function using ClinicalBERT
def analyze_report(text):
    # Define prompts for analysis
    summary_prompt = f"Summarize the following medical report:\n{text}"
    interpretation_prompt = f"Interpret the following lab results:\n{text}"
    recommendation_prompt = f"Provide actionable recommendations based on this medical report:\n{text}"

    # Use ClinicalBERT for text analysis
    summary = clinicalbert_analyzer(summary_prompt)[0]['label']
    interpretation = clinicalbert_analyzer(interpretation_prompt)[0]['label']
    recommendations = clinicalbert_analyzer(recommendation_prompt)[0]['label']

    return {
        "summary": summary,
        "interpretation": interpretation,
        "recommendations": recommendations
    }

# Streamlit UI
st.title("Clinical Lab Report Analyzer")
st.write("Upload your medical lab report (PDF/Image) to get a summary, interpretation, and actionable recommendations.")

uploaded_file = st.file_uploader("Choose a PDF/Image file", type=["pdf", "png", "jpg", "jpeg"])

if uploaded_file:
    file_type = uploaded_file.type

    # Extract text based on file type
    if file_type == "application/pdf":
        with st.spinner("Extracting text from PDF..."):
            extracted_text = extract_text_from_pdf(uploaded_file)
    else:
        with st.spinner("Extracting text from Image..."):
            extracted_text = extract_text_from_image(uploaded_file)

    # Analyze the extracted text
    if extracted_text:
        with st.spinner("Analyzing the medical report using ClinicalBERT..."):
            result = analyze_report(extracted_text)
        
        # Display the results
        st.subheader("Summary")
        st.write(result['summary'])

        st.subheader("Interpretation of Results")
        st.write(result['interpretation'])

        st.subheader("Actionable Recommendations")
        st.write(result['recommendations'])
    else:
        st.error("No text could be extracted. Please try with a different file.")