|
import streamlit as st |
|
from transformers import pipeline |
|
from PIL import Image |
|
import pytesseract |
|
import PyPDF2 |
|
import pdfplumber |
|
import torch |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
return pipeline("zero-shot-classification", model="facebook/bart-large-mnli", device=0 if torch.cuda.is_available() else -1) |
|
|
|
classifier = load_model() |
|
|
|
|
|
def extract_text_from_image(image): |
|
return pytesseract.image_to_string(image) |
|
|
|
|
|
def extract_text_from_pdf(pdf_file): |
|
text = "" |
|
with pdfplumber.open(pdf_file) as pdf: |
|
for page in pdf.pages: |
|
text += page.extract_text() |
|
return text |
|
|
|
|
|
def analyze_report(text): |
|
|
|
summary = classifier(text, candidate_labels=["summary"], multi_label=False)['labels'][0] |
|
|
|
|
|
interpretation = classifier(text, candidate_labels=["interpretation", "normal", "abnormal"], multi_label=True) |
|
|
|
|
|
recommendations = classifier(text, candidate_labels=["follow-up", "Holistic/OTC treatment", "dietary change", "medication"], multi_label=True) |
|
|
|
return { |
|
"summary": summary, |
|
"interpretation": interpretation['labels'], |
|
"recommendations": recommendations['labels'] |
|
} |
|
|
|
|
|
st.title("Medical Lab Report Analyzer") |
|
st.write("Upload your medical lab report (PDF/Image) for insights.") |
|
|
|
uploaded_file = st.file_uploader("Choose a PDF/Image file", type=["pdf", "png", "jpg", "jpeg"]) |
|
|
|
if uploaded_file: |
|
file_type = uploaded_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..."): |
|
image = Image.open(uploaded_file) |
|
extracted_text = extract_text_from_image(image) |
|
|
|
|
|
if extracted_text: |
|
with st.spinner("Analyzing report..."): |
|
result = analyze_report(extracted_text) |
|
|
|
|
|
st.subheader("Summary") |
|
st.write(result['summary']) |
|
|
|
st.subheader("Interpretation of Results") |
|
for label in result['interpretation']: |
|
st.write(f"- {label.capitalize()}") |
|
|
|
st.subheader("Actionable Recommendations") |
|
for rec in result['recommendations']: |
|
st.write(f"- {rec.capitalize()}") |
|
else: |
|
st.error("No text could be extracted. Please try with a different file.") |
|
|