Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,45 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
from transformers import
|
4 |
-
from pdf2image import convert_from_path
|
5 |
-
import pytesseract
|
6 |
from PIL import Image
|
7 |
-
import os
|
8 |
-
import io
|
9 |
-
from typing import List, Tuple
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract' # Path to Tesseract executable
|
18 |
-
|
19 |
-
# Streamlit UI
|
20 |
-
st.title("Medical Lab Report Analyzer")
|
21 |
-
st.write(
|
22 |
-
"Upload an image or PDF file of a medical lab report to get an interpretation, actionable recommendations, and additional insights."
|
23 |
-
)
|
24 |
-
|
25 |
-
# Upload the image or PDF file
|
26 |
-
uploaded_file = st.file_uploader(
|
27 |
-
"Upload Image or PDF", type=["jpg", "jpeg", "png", "pdf"]
|
28 |
-
)
|
29 |
-
|
30 |
-
def extract_text_from_image(image: Image.Image) -> str:
|
31 |
-
return pytesseract.image_to_string(image)
|
32 |
-
|
33 |
-
def extract_text_from_pdf(pdf_path: str) -> str:
|
34 |
-
images = convert_from_path(pdf_path)
|
35 |
text = ""
|
36 |
-
for
|
37 |
-
text +=
|
38 |
return text
|
39 |
|
40 |
-
def
|
41 |
-
|
42 |
-
|
43 |
-
output_text = text_model(text, max_length=1000)[0]["generated_text"]
|
44 |
-
|
45 |
-
return [
|
46 |
-
("Report Interpretation", output_text),
|
47 |
-
("Actionable Recommendations", "Consult your physician for further tests if the values are abnormal."),
|
48 |
-
("Additional Insights", "Regular check-ups can help monitor and maintain healthy levels.")
|
49 |
-
]
|
50 |
-
|
51 |
-
# Process the uploaded file
|
52 |
-
if uploaded_file:
|
53 |
-
file_type = uploaded_file.type
|
54 |
-
file_name = uploaded_file.name
|
55 |
-
st.write(f"Uploaded File: {file_name}")
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
extracted_text = extract_text_from_pdf("temp.pdf")
|
61 |
-
os.remove("temp.pdf")
|
62 |
-
else: # For image files
|
63 |
-
image = Image.open(io.BytesIO(uploaded_file.getvalue()))
|
64 |
-
extracted_text = extract_text_from_image(image)
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
st.text_area("Lab Report Text", extracted_text, height=200)
|
69 |
-
|
70 |
-
# Get lab report interpretation and recommendations
|
71 |
-
st.subheader("Analysis & Insights")
|
72 |
-
insights = generate_insights(extracted_text)
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from transformers import pipeline
|
|
|
|
|
4 |
from PIL import Image
|
|
|
|
|
|
|
5 |
|
6 |
+
# Load Hugging Face model
|
7 |
+
model = pipeline("text-classification", model="medicalai/ClinicalBERT")
|
8 |
|
9 |
+
def extract_text_from_pdf(pdf_file):
|
10 |
+
reader = PdfReader(pdf_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
text = ""
|
12 |
+
for page in reader.pages:
|
13 |
+
text += page.extract_text()
|
14 |
return text
|
15 |
|
16 |
+
def get_lab_report_interpretation(text):
|
17 |
+
result = model(text)
|
18 |
+
return result[0]['label']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def process_image(image_file):
|
21 |
+
img = Image.open(image_file)
|
22 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Streamlit UI
|
25 |
+
st.title("Medical Lab Report Analyzer")
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Upload image and PDF
|
28 |
+
uploaded_pdf = st.file_uploader("Upload your PDF Medical Lab Report", type="pdf")
|
29 |
+
uploaded_image = st.file_uploader("Upload Image of Medical Report", type="jpg png jpeg")
|
30 |
+
|
31 |
+
if uploaded_pdf is not None:
|
32 |
+
st.subheader("Extracted Text from PDF:")
|
33 |
+
pdf_text = extract_text_from_pdf(uploaded_pdf)
|
34 |
+
st.write(pdf_text)
|
35 |
+
|
36 |
+
st.subheader("Lab Report Interpretation:")
|
37 |
+
interpretation = get_lab_report_interpretation(pdf_text)
|
38 |
+
st.write(interpretation)
|
39 |
+
|
40 |
+
elif uploaded_image is not None:
|
41 |
+
st.image(process_image(uploaded_image), caption="Uploaded Image", use_column_width=True)
|
42 |
+
st.warning("For text extraction from image, please upload a PDF.")
|
43 |
+
|
44 |
+
else:
|
45 |
+
st.info("Please upload a medical PDF or image for analysis.")
|