Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,73 @@
|
|
1 |
-
|
2 |
-
from transformers import AutoModelForImageClassification, AutoProcessor
|
3 |
-
from PIL import Image
|
4 |
-
import io
|
5 |
-
import fitz # PyMuPDF
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
# For Hugging Face Spaces, we need to bind to 0.0.0.0 and use the default port 8080
|
75 |
-
app.run(debug=False, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
|
|
|
1 |
+
import gradio as gr
|
2 |
+
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")
|
14 |
+
images = []
|
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") # Or "png"
|
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 |
+
def classify_file(file):
|
26 |
+
# Get the file type
|
27 |
+
file_type = file.name.split('.')[-1].lower()
|
28 |
+
|
29 |
+
try:
|
30 |
+
if file_type in ['jpg', 'jpeg', 'png', 'gif']:
|
31 |
+
# Handle image upload
|
32 |
+
image = Image.open(file).convert("RGB")
|
33 |
+
inputs = processor(images=image, return_tensors="pt")
|
34 |
+
outputs = model(**inputs)
|
35 |
+
logits = outputs.logits
|
36 |
+
predicted_class_idx = logits.argmax(-1).item()
|
37 |
+
result = model.config.id2label[predicted_class_idx]
|
38 |
+
return result
|
39 |
+
|
40 |
+
elif file_type == 'pdf':
|
41 |
+
# Handle PDF upload
|
42 |
+
pdf_data = file.read()
|
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 |
+
logits = outputs.logits
|
51 |
+
predicted_class_idx = logits.argmax(-1).item()
|
52 |
+
result = model.config.id2label[predicted_class_idx]
|
53 |
+
return result
|
54 |
+
else:
|
55 |
+
return 'PDF conversion failed.'
|
56 |
+
|
57 |
+
else:
|
58 |
+
return 'Unsupported file type'
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
return f'An error occurred: {e}'
|
62 |
+
|
63 |
+
# Create the Gradio interface
|
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 an image or PDF for classification."
|
70 |
+
)
|
71 |
+
|
72 |
+
# Launch the Gradio app
|
73 |
+
demo.launch()
|
|
|
|