Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
from flask_cors import CORS
|
3 |
-
from transformers import Dinov2ForImageClassification, AutoProcessor
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
import fitz
|
@@ -11,8 +11,7 @@ CORS(app)
|
|
11 |
|
12 |
# Load model and processor
|
13 |
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
14 |
-
|
15 |
-
model = Dinov2ForImageClassification.from_pretrained(model_name) # USE THE CORRECT MODEL CLASS
|
16 |
processor = AutoProcessor.from_pretrained(model_name)
|
17 |
|
18 |
def pdf_to_images_pymupdf(pdf_data):
|
@@ -39,7 +38,6 @@ def classify_file():
|
|
39 |
|
40 |
try:
|
41 |
if file_type in ['jpg', 'jpeg', 'png', 'gif']:
|
42 |
-
# Handle image upload
|
43 |
img_data = uploaded_file.read()
|
44 |
image = Image.open(io.BytesIO(img_data)).convert("RGB")
|
45 |
inputs = processor(images=image, return_tensors="pt")
|
@@ -50,12 +48,10 @@ def classify_file():
|
|
50 |
return jsonify({'result': result})
|
51 |
|
52 |
elif file_type == 'pdf':
|
53 |
-
# Handle PDF upload
|
54 |
pdf_data = uploaded_file.read()
|
55 |
images = pdf_to_images_pymupdf(pdf_data)
|
56 |
|
57 |
if images:
|
58 |
-
# Process the first image in the pdf, you may need to loop through all images.
|
59 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
60 |
inputs = processor(images=image, return_tensors="pt")
|
61 |
outputs = model(**inputs)
|
@@ -73,4 +69,4 @@ def classify_file():
|
|
73 |
return jsonify({'error': f'An error occurred: {e}'}), 500
|
74 |
|
75 |
if __name__ == '__main__':
|
76 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
from flask_cors import CORS
|
3 |
+
from transformers import Dinov2ForImageClassification, AutoProcessor
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
import fitz
|
|
|
11 |
|
12 |
# Load model and processor
|
13 |
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
14 |
+
model = Dinov2ForImageClassification.from_pretrained(model_name)
|
|
|
15 |
processor = AutoProcessor.from_pretrained(model_name)
|
16 |
|
17 |
def pdf_to_images_pymupdf(pdf_data):
|
|
|
38 |
|
39 |
try:
|
40 |
if file_type in ['jpg', 'jpeg', 'png', 'gif']:
|
|
|
41 |
img_data = uploaded_file.read()
|
42 |
image = Image.open(io.BytesIO(img_data)).convert("RGB")
|
43 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
48 |
return jsonify({'result': result})
|
49 |
|
50 |
elif file_type == 'pdf':
|
|
|
51 |
pdf_data = uploaded_file.read()
|
52 |
images = pdf_to_images_pymupdf(pdf_data)
|
53 |
|
54 |
if images:
|
|
|
55 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
56 |
inputs = processor(images=image, return_tensors="pt")
|
57 |
outputs = model(**inputs)
|
|
|
69 |
return jsonify({'error': f'An error occurred: {e}'}), 500
|
70 |
|
71 |
if __name__ == '__main__':
|
72 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|