Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,12 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pdfplumber
|
3 |
import pytesseract
|
4 |
-
|
5 |
-
|
6 |
-
import re
|
7 |
-
|
8 |
-
# Manually specify the path for Tesseract in Linux
|
9 |
-
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
|
10 |
-
|
11 |
-
|
12 |
-
# Load pre-trained Hugging Face models
|
13 |
-
summarizer = pipeline("summarization", model="t5-small")
|
14 |
-
medical_qa = pipeline("question-answering", model="deepset/bert-base-cased-squad2")
|
15 |
-
|
16 |
-
# Function to extract text from PDF
|
17 |
-
def extract_text_from_pdf(pdf_file):
|
18 |
-
with pdfplumber.open(pdf_file) as pdf:
|
19 |
-
text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
|
20 |
-
return text if text else "No text found in PDF."
|
21 |
-
|
22 |
-
# Function to extract text from images (JPG, PNG)
|
23 |
-
def extract_text_from_image(image_file):
|
24 |
-
image = Image.open(image_file)
|
25 |
-
text = pytesseract.image_to_string(image)
|
26 |
-
return text.strip() if text else "No text found in Image."
|
27 |
-
|
28 |
-
# Function to summarize medical report
|
29 |
-
def summarize_report(text):
|
30 |
-
if len(text) > 500: # Handle long text
|
31 |
-
text = text[:500]
|
32 |
-
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
|
33 |
-
return summary[0]['summary_text']
|
34 |
-
|
35 |
-
# Function to find medical terms dynamically using regex
|
36 |
-
def extract_medical_terms(text):
|
37 |
-
words = re.findall(r'\b[A-Z][a-z]+(?:[ -][A-Z][a-z]+)*\b', text)
|
38 |
-
return list(set(words))
|
39 |
-
|
40 |
-
# Function to explain medical terms
|
41 |
-
def explain_term(term):
|
42 |
-
context = "Hypercholesterolemia is a condition with high cholesterol in the blood. Atherosclerosis refers to artery narrowing due to fat buildup."
|
43 |
-
response = medical_qa(question=f"What is {term}?", context=context)
|
44 |
-
return response["answer"]
|
45 |
-
|
46 |
-
# Streamlit UI
|
47 |
-
st.title("🩺 AI Medical Report Analyzer")
|
48 |
-
st.write("Upload a medical **PDF or Image (JPG, PNG)** to get a summarized report with term explanations.")
|
49 |
-
|
50 |
-
uploaded_file = st.file_uploader("Upload a PDF or Image", type=["pdf", "jpg", "png"])
|
51 |
-
|
52 |
-
if uploaded_file:
|
53 |
-
file_type = uploaded_file.type
|
54 |
-
|
55 |
-
if file_type == "application/pdf":
|
56 |
-
text = extract_text_from_pdf(uploaded_file)
|
57 |
-
st.subheader("📜 Extracted Text from PDF:")
|
58 |
-
elif file_type in ["image/png", "image/jpeg"]:
|
59 |
-
text = extract_text_from_image(uploaded_file)
|
60 |
-
st.subheader("🖼️ Extracted Text from Image:")
|
61 |
-
|
62 |
-
st.text_area("Report Content:", text, height=200)
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
st.subheader("📑 AI-Generated Summary:")
|
67 |
-
st.markdown(f"**{summary}**")
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
explanation = explain_term(term)
|
75 |
-
st.markdown(f"**{term}:** {explanation}")
|
76 |
-
else:
|
77 |
-
st.write("No medical terms detected.")
|
|
|
|
|
|
|
1 |
import pytesseract
|
2 |
+
import subprocess
|
3 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Check if Tesseract is installed
|
6 |
+
tesseract_path = subprocess.run(["which", "tesseract"], capture_output=True, text=True).stdout.strip()
|
|
|
|
|
7 |
|
8 |
+
if not tesseract_path:
|
9 |
+
st.error("Tesseract is not installed. Make sure 'apt.txt' contains 'tesseract-ocr'.")
|
10 |
+
else:
|
11 |
+
st.success(f"Tesseract is installed at: {tesseract_path}")
|
12 |
+
pytesseract.pytesseract.tesseract_cmd = tesseract_path
|
|
|
|
|
|
|
|