Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,25 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import AutoTokenizer,
|
|
|
3 |
from PIL import Image
|
4 |
import pytesseract
|
5 |
-
import PyPDF2
|
6 |
import pdfplumber
|
7 |
import torch
|
8 |
|
9 |
-
# Load
|
10 |
@st.cache_resource
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# OCR for Image using Tesseract
|
20 |
def extract_text_from_image(image):
|
@@ -28,34 +33,33 @@ def extract_text_from_pdf(pdf_file):
|
|
28 |
text += page.extract_text() or ""
|
29 |
return text
|
30 |
|
31 |
-
# Analyze and interpret the medical report
|
32 |
def analyze_medical_text(text):
|
33 |
-
# Summarize the extracted text
|
34 |
-
|
35 |
|
36 |
-
#
|
37 |
-
interpretation =
|
38 |
-
|
39 |
candidate_labels=["normal", "abnormal", "urgent", "needs follow-up", "critical condition"],
|
40 |
multi_label=True
|
41 |
)
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
text,
|
46 |
candidate_labels=["medication", "dietary change", "exercise", "follow-up with a doctor", "lifestyle change"],
|
47 |
multi_label=True
|
48 |
)
|
49 |
|
50 |
return {
|
51 |
-
"summary":
|
52 |
"interpretation": interpretation['labels'],
|
53 |
"recommendations": recommendations['labels']
|
54 |
}
|
55 |
|
56 |
# Streamlit UI
|
57 |
-
st.title("Medical Lab Report Analyzer with ClinicalBERT")
|
58 |
-
st.write("Upload your medical lab report (PDF/Image) to get a summary and actionable insights
|
59 |
|
60 |
uploaded_file = st.file_uploader("Choose a PDF/Image file", type=["pdf", "png", "jpg", "jpeg"])
|
61 |
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
3 |
+
from transformers import AutoModelForSequenceClassification
|
4 |
from PIL import Image
|
5 |
import pytesseract
|
|
|
6 |
import pdfplumber
|
7 |
import torch
|
8 |
|
9 |
+
# Load BART for zero-shot classification and Bio_ClinicalBERT for text summarization
|
10 |
@st.cache_resource
|
11 |
+
def load_models():
|
12 |
+
# Bio_ClinicalBERT for text summarization
|
13 |
+
tokenizer_bert = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
|
14 |
+
model_bert = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
|
15 |
+
summarizer = pipeline("summarization", model=model_bert, tokenizer=tokenizer_bert, device=0 if torch.cuda.is_available() else -1)
|
16 |
|
17 |
+
# BART model for zero-shot classification
|
18 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli", device=0 if torch.cuda.is_available() else -1)
|
19 |
+
|
20 |
+
return summarizer, classifier
|
21 |
+
|
22 |
+
summarizer, classifier = load_models()
|
23 |
|
24 |
# OCR for Image using Tesseract
|
25 |
def extract_text_from_image(image):
|
|
|
33 |
text += page.extract_text() or ""
|
34 |
return text
|
35 |
|
36 |
+
# Analyze and interpret the medical report
|
37 |
def analyze_medical_text(text):
|
38 |
+
# Summarize the extracted text using ClinicalBERT
|
39 |
+
summarized_text = summarizer(text, max_length=100, min_length=30, do_sample=False)[0]['summary_text']
|
40 |
|
41 |
+
# Use BART for classification insights
|
42 |
+
interpretation = classifier(
|
43 |
+
summarized_text,
|
44 |
candidate_labels=["normal", "abnormal", "urgent", "needs follow-up", "critical condition"],
|
45 |
multi_label=True
|
46 |
)
|
47 |
|
48 |
+
recommendations = classifier(
|
49 |
+
summarized_text,
|
|
|
50 |
candidate_labels=["medication", "dietary change", "exercise", "follow-up with a doctor", "lifestyle change"],
|
51 |
multi_label=True
|
52 |
)
|
53 |
|
54 |
return {
|
55 |
+
"summary": summarized_text,
|
56 |
"interpretation": interpretation['labels'],
|
57 |
"recommendations": recommendations['labels']
|
58 |
}
|
59 |
|
60 |
# Streamlit UI
|
61 |
+
st.title("Medical Lab Report Analyzer with ClinicalBERT and BART")
|
62 |
+
st.write("Upload your medical lab report (PDF/Image) to get a summary and actionable insights.")
|
63 |
|
64 |
uploaded_file = st.file_uploader("Choose a PDF/Image file", type=["pdf", "png", "jpg", "jpeg"])
|
65 |
|