Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,14 @@ from transformers import AutoModelForImageClassification, AutoProcessor
|
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
import fitz # PyMuPDF
|
|
|
6 |
|
|
|
7 |
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
8 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
9 |
processor = AutoProcessor.from_pretrained(model_name)
|
10 |
|
|
|
11 |
def pdf_to_images_pymupdf(pdf_data):
|
12 |
try:
|
13 |
pdf_document = fitz.open(stream=pdf_data, filetype="pdf")
|
@@ -15,59 +18,57 @@ def pdf_to_images_pymupdf(pdf_data):
|
|
15 |
for page_num in range(pdf_document.page_count):
|
16 |
page = pdf_document.load_page(page_num)
|
17 |
pix = page.get_pixmap()
|
18 |
-
img_data = pix.tobytes("jpeg")
|
19 |
images.append(img_data)
|
20 |
return images
|
21 |
except Exception as e:
|
22 |
print(f"Error converting PDF: {e}")
|
23 |
return None
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
file_type = file.name.split('.')[-1].lower()
|
28 |
-
|
29 |
try:
|
30 |
-
|
|
|
|
|
31 |
# Handle image upload
|
32 |
-
image = Image.open(
|
33 |
inputs = processor(images=image, return_tensors="pt")
|
34 |
outputs = model(**inputs)
|
35 |
-
|
36 |
-
predicted_class_idx = logits.argmax(-1).item()
|
37 |
result = model.config.id2label[predicted_class_idx]
|
38 |
return result
|
39 |
|
40 |
-
elif
|
41 |
# Handle PDF upload
|
42 |
-
|
|
|
43 |
images = pdf_to_images_pymupdf(pdf_data)
|
44 |
|
45 |
if images:
|
46 |
-
# Process the first image in the pdf, you may need to loop through all images.
|
47 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
48 |
inputs = processor(images=image, return_tensors="pt")
|
49 |
outputs = model(**inputs)
|
50 |
-
|
51 |
-
predicted_class_idx = logits.argmax(-1).item()
|
52 |
result = model.config.id2label[predicted_class_idx]
|
53 |
return result
|
54 |
else:
|
55 |
-
return
|
56 |
|
57 |
else:
|
58 |
-
return
|
59 |
|
60 |
except Exception as e:
|
61 |
-
return f
|
62 |
|
63 |
-
#
|
64 |
demo = gr.Interface(
|
65 |
-
fn=classify_file,
|
66 |
-
inputs=gr.File(label="Upload PDF or Image"),
|
67 |
-
outputs="text",
|
68 |
-
title="Diabetic Retinopathy Detection",
|
69 |
-
description="Upload
|
70 |
)
|
71 |
|
72 |
-
# Launch
|
73 |
demo.launch()
|
|
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
import fitz # PyMuPDF
|
6 |
+
import os
|
7 |
|
8 |
+
# Load model and processor
|
9 |
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
10 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
11 |
processor = AutoProcessor.from_pretrained(model_name)
|
12 |
|
13 |
+
# PDF to image conversion
|
14 |
def pdf_to_images_pymupdf(pdf_data):
|
15 |
try:
|
16 |
pdf_document = fitz.open(stream=pdf_data, filetype="pdf")
|
|
|
18 |
for page_num in range(pdf_document.page_count):
|
19 |
page = pdf_document.load_page(page_num)
|
20 |
pix = page.get_pixmap()
|
21 |
+
img_data = pix.tobytes("jpeg")
|
22 |
images.append(img_data)
|
23 |
return images
|
24 |
except Exception as e:
|
25 |
print(f"Error converting PDF: {e}")
|
26 |
return None
|
27 |
|
28 |
+
# File classification
|
29 |
+
def classify_file(file_path):
|
|
|
|
|
30 |
try:
|
31 |
+
file_ext = os.path.splitext(file_path)[-1].lower()
|
32 |
+
|
33 |
+
if file_ext in ['.jpg', '.jpeg', '.png', '.gif']:
|
34 |
# Handle image upload
|
35 |
+
image = Image.open(file_path).convert("RGB")
|
36 |
inputs = processor(images=image, return_tensors="pt")
|
37 |
outputs = model(**inputs)
|
38 |
+
predicted_class_idx = outputs.logits.argmax(-1).item()
|
|
|
39 |
result = model.config.id2label[predicted_class_idx]
|
40 |
return result
|
41 |
|
42 |
+
elif file_ext == '.pdf':
|
43 |
# Handle PDF upload
|
44 |
+
with open(file_path, "rb") as f:
|
45 |
+
pdf_data = f.read()
|
46 |
images = pdf_to_images_pymupdf(pdf_data)
|
47 |
|
48 |
if images:
|
|
|
49 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
50 |
inputs = processor(images=image, return_tensors="pt")
|
51 |
outputs = model(**inputs)
|
52 |
+
predicted_class_idx = outputs.logits.argmax(-1).item()
|
|
|
53 |
result = model.config.id2label[predicted_class_idx]
|
54 |
return result
|
55 |
else:
|
56 |
+
return "PDF conversion failed."
|
57 |
|
58 |
else:
|
59 |
+
return "Unsupported file type."
|
60 |
|
61 |
except Exception as e:
|
62 |
+
return f"An error occurred: {e}"
|
63 |
|
64 |
+
# Gradio UI
|
65 |
demo = gr.Interface(
|
66 |
+
fn=classify_file,
|
67 |
+
inputs=gr.File(label="Upload PDF or Image"),
|
68 |
+
outputs="text",
|
69 |
+
title="Diabetic Retinopathy Detection",
|
70 |
+
description="Upload a fundus scan (image or PDF) to detect diabetic retinopathy."
|
71 |
)
|
72 |
|
73 |
+
# Launch app
|
74 |
demo.launch()
|