bravewiki commited on
Commit
c953483
1 Parent(s): 14867d2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from PIL import Image
4
+ import pytesseract
5
+ import PyPDF2
6
+ import pdfplumber
7
+ import torch
8
+
9
+ # Load the BART model for summarization and NLI
10
+ @st.cache_resource
11
+ def load_model():
12
+ return pipeline("zero-shot-classification", model="facebook/bart-large-mnli", device=0 if torch.cuda.is_available() else -1)
13
+
14
+ classifier = load_model()
15
+
16
+ # OCR for Image using Tesseract
17
+ def extract_text_from_image(image):
18
+ return pytesseract.image_to_string(image)
19
+
20
+ # Extract text from PDF using pdfplumber
21
+ def extract_text_from_pdf(pdf_file):
22
+ text = ""
23
+ with pdfplumber.open(pdf_file) as pdf:
24
+ for page in pdf.pages:
25
+ text += page.extract_text()
26
+ return text
27
+
28
+ # Summarize, interpret and give actionable insights
29
+ def analyze_report(text):
30
+ # Provide a summary
31
+ summary = classifier(text, candidate_labels=["summary"], multi_label=False)['labels'][0]
32
+
33
+ # Interpretation of results
34
+ interpretation = classifier(text, candidate_labels=["interpretation", "normal", "abnormal"], multi_label=True)
35
+
36
+ # Recommendations
37
+ recommendations = classifier(text, candidate_labels=["follow-up", "Holistic/OTC treatment", "dietary change", "medication"], multi_label=True)
38
+
39
+ return {
40
+ "summary": summary,
41
+ "interpretation": interpretation['labels'],
42
+ "recommendations": recommendations['labels']
43
+ }
44
+
45
+ # Streamlit UI
46
+ st.title("Medical Lab Report Analyzer")
47
+ st.write("Upload your medical lab report (PDF/Image) for insights.")
48
+
49
+ uploaded_file = st.file_uploader("Choose a PDF/Image file", type=["pdf", "png", "jpg", "jpeg"])
50
+
51
+ if uploaded_file:
52
+ file_type = uploaded_file.type
53
+
54
+ # Extract text based on file type
55
+ if file_type == "application/pdf":
56
+ with st.spinner("Extracting text from PDF..."):
57
+ extracted_text = extract_text_from_pdf(uploaded_file)
58
+ else:
59
+ with st.spinner("Extracting text from Image..."):
60
+ image = Image.open(uploaded_file)
61
+ extracted_text = extract_text_from_image(image)
62
+
63
+ # Analyze the extracted text
64
+ if extracted_text:
65
+ with st.spinner("Analyzing report..."):
66
+ result = analyze_report(extracted_text)
67
+
68
+ # Display the results
69
+ st.subheader("Summary")
70
+ st.write(result['summary'])
71
+
72
+ st.subheader("Interpretation of Results")
73
+ for label in result['interpretation']:
74
+ st.write(f"- {label.capitalize()}")
75
+
76
+ st.subheader("Actionable Recommendations")
77
+ for rec in result['recommendations']:
78
+ st.write(f"- {rec.capitalize()}")
79
+ else:
80
+ st.error("No text could be extracted. Please try with a different file.")