Spaces:
Sleeping
Sleeping
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 | |
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.") | |