Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,15 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
-
from
|
3 |
-
from transformers import Dinov2ForImageClassification, AutoProcessor
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
-
import fitz
|
7 |
-
import
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
CORS(app)
|
11 |
|
12 |
-
# Load model and processor
|
13 |
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
14 |
-
model =
|
15 |
processor = AutoProcessor.from_pretrained(model_name)
|
16 |
|
17 |
def pdf_to_images_pymupdf(pdf_data):
|
@@ -34,39 +32,30 @@ def classify_file():
|
|
34 |
return jsonify({'error': 'No file provided'}), 400
|
35 |
|
36 |
uploaded_file = request.files['file']
|
37 |
-
file_type = uploaded_file.filename.rsplit('.', 1)[1].lower()
|
38 |
|
39 |
try:
|
40 |
-
if file_type in ['jpg', 'jpeg', 'png'
|
41 |
-
|
42 |
-
image = Image.open(io.BytesIO(img_data)).convert("RGB")
|
43 |
-
inputs = processor(images=image, return_tensors="pt")
|
44 |
-
outputs = model(**inputs)
|
45 |
-
logits = outputs.logits
|
46 |
-
predicted_class_idx = logits.argmax(-1).item()
|
47 |
-
result = model.config.id2label[predicted_class_idx]
|
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 |
-
|
55 |
-
|
56 |
-
inputs = processor(images=image, return_tensors="pt")
|
57 |
-
outputs = model(**inputs)
|
58 |
-
logits = outputs.logits
|
59 |
-
predicted_class_idx = logits.argmax(-1).item()
|
60 |
-
result = model.config.id2label[predicted_class_idx]
|
61 |
-
return jsonify({'result': result})
|
62 |
-
else:
|
63 |
-
return jsonify({'error': 'PDF conversion failed.'}), 500
|
64 |
-
|
65 |
else:
|
66 |
return jsonify({'error': 'Unsupported file type'}), 400
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
except Exception as e:
|
69 |
-
return jsonify({'error':
|
70 |
|
71 |
if __name__ == '__main__':
|
72 |
-
app.run(host="0.0.0.0", port=7860
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
from transformers import AutoModelForImageClassification, AutoProcessor
|
|
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
+
import fitz # PyMuPDF
|
6 |
+
from flask_cors import CORS
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
CORS(app)
|
10 |
|
|
|
11 |
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
12 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
13 |
processor = AutoProcessor.from_pretrained(model_name)
|
14 |
|
15 |
def pdf_to_images_pymupdf(pdf_data):
|
|
|
32 |
return jsonify({'error': 'No file provided'}), 400
|
33 |
|
34 |
uploaded_file = request.files['file']
|
35 |
+
file_type = uploaded_file.filename.rsplit('.', 1)[-1].lower()
|
36 |
|
37 |
try:
|
38 |
+
if file_type in ['jpg', 'jpeg', 'png']:
|
39 |
+
image = Image.open(uploaded_file).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
elif file_type == 'pdf':
|
41 |
pdf_data = uploaded_file.read()
|
42 |
images = pdf_to_images_pymupdf(pdf_data)
|
43 |
+
if not images:
|
44 |
+
return jsonify({'error': 'Failed to convert PDF.'}), 500
|
45 |
+
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
else:
|
47 |
return jsonify({'error': 'Unsupported file type'}), 400
|
48 |
|
49 |
+
inputs = processor(images=image, return_tensors="pt")
|
50 |
+
outputs = model(**inputs)
|
51 |
+
logits = outputs.logits
|
52 |
+
predicted_class_idx = logits.argmax(-1).item()
|
53 |
+
result = model.config.id2label[predicted_class_idx]
|
54 |
+
|
55 |
+
return jsonify({'result': result})
|
56 |
+
|
57 |
except Exception as e:
|
58 |
+
return jsonify({'error': str(e)}), 500
|
59 |
|
60 |
if __name__ == '__main__':
|
61 |
+
app.run(host="0.0.0.0", port=7860)
|