Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import easyocr
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
from transformers import pipeline
|
8 |
+
|
9 |
+
# Initialize OCR reader
|
10 |
+
reader = easyocr.Reader(['en'])
|
11 |
+
|
12 |
+
# Initialize Hugging Face text classifier (use a pre-trained or custom model)
|
13 |
+
classifier = pipeline("text-classification", model="distilbert-base-uncased")
|
14 |
+
|
15 |
+
# Function to check if the image is clear
|
16 |
+
def is_image_clear(image):
|
17 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
18 |
+
laplacian_var = cv2.Laplacian(gray, cv2.CV_64F).var()
|
19 |
+
return laplacian_var > 100 # Threshold to determine clarity
|
20 |
+
|
21 |
+
# Function to extract text using OCR
|
22 |
+
def extract_text_from_image(image):
|
23 |
+
result = reader.readtext(image)
|
24 |
+
text = " ".join([res[1] for res in result])
|
25 |
+
return text
|
26 |
+
|
27 |
+
# Function to analyze the text for abnormalities
|
28 |
+
def analyze_report(text):
|
29 |
+
result = classifier(text)
|
30 |
+
severity = "Normal"
|
31 |
+
explanation = "No abnormalities detected"
|
32 |
+
|
33 |
+
# Example logic to determine severity (you can expand this)
|
34 |
+
if 'elevated' in text or 'high' in text:
|
35 |
+
severity = "Moderate"
|
36 |
+
explanation = "Elevated levels detected (e.g., glucose, blood pressure)"
|
37 |
+
elif 'critical' in text:
|
38 |
+
severity = "Severe"
|
39 |
+
explanation = "Critical levels detected"
|
40 |
+
return severity, explanation
|
41 |
+
|
42 |
+
# Streamlit UI
|
43 |
+
st.title("Medical Report Analysis Chatbot")
|
44 |
+
|
45 |
+
uploaded_file = st.file_uploader("Upload your medical report image", type=["jpg", "jpeg", "png"])
|
46 |
+
|
47 |
+
if uploaded_file:
|
48 |
+
image = Image.open(uploaded_file)
|
49 |
+
st.image(image, caption="Uploaded Medical Report", use_column_width=True)
|
50 |
+
|
51 |
+
# Convert to OpenCV format for clarity check
|
52 |
+
open_cv_image = np.array(image)
|
53 |
+
open_cv_image = open_cv_image[:, :, ::-1].copy() # Convert RGB to BGR
|
54 |
+
|
55 |
+
if is_image_clear(open_cv_image):
|
56 |
+
st.success("Image clarity: Clear")
|
57 |
+
|
58 |
+
# Extract text
|
59 |
+
text = extract_text_from_image(open_cv_image)
|
60 |
+
st.write("Extracted Text: ")
|
61 |
+
st.write(text)
|
62 |
+
|
63 |
+
# Analyze report
|
64 |
+
severity, explanation = analyze_report(text)
|
65 |
+
st.write(f"Report Type: Medical Report")
|
66 |
+
st.write(f"Severity Level: {severity}")
|
67 |
+
st.write(f"Explanation: {explanation}")
|
68 |
+
|
69 |
+
# Ask for doctor consultation
|
70 |
+
consultation = st.radio("Would you like to consult a doctor?", ("Yes", "No"))
|
71 |
+
if consultation == "Yes":
|
72 |
+
st.write("Consultation fee: $50")
|
73 |
+
st.write("Your report will be sent to the doctor for further diagnosis.")
|
74 |
+
else:
|
75 |
+
st.warning("Image clarity: Blurry. Please upload a clearer image.")
|