Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,17 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
-
from transformers import
|
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 |
-
#
|
12 |
-
|
|
|
|
|
13 |
|
14 |
def pdf_to_images_pymupdf(pdf_data):
|
15 |
try:
|
@@ -38,8 +40,12 @@ def classify_file():
|
|
38 |
# Handle image upload
|
39 |
img_data = uploaded_file.read()
|
40 |
image = Image.open(io.BytesIO(img_data)).convert("RGB")
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
elif file_type == 'pdf':
|
45 |
# Handle PDF upload
|
@@ -47,10 +53,14 @@ def classify_file():
|
|
47 |
images = pdf_to_images_pymupdf(pdf_data)
|
48 |
|
49 |
if images:
|
50 |
-
# Process the first image in the pdf
|
51 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
else:
|
55 |
return jsonify({'error': 'PDF conversion failed.'}), 500
|
56 |
|
@@ -61,4 +71,4 @@ def classify_file():
|
|
61 |
return jsonify({'error': f'An error occurred: {e}'}), 500
|
62 |
|
63 |
if __name__ == '__main__':
|
64 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
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) # Allow cross-origin requests
|
10 |
|
11 |
+
# Load model and processor
|
12 |
+
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
13 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
14 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
15 |
|
16 |
def pdf_to_images_pymupdf(pdf_data):
|
17 |
try:
|
|
|
40 |
# Handle image upload
|
41 |
img_data = uploaded_file.read()
|
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 |
# Handle PDF upload
|
|
|
53 |
images = pdf_to_images_pymupdf(pdf_data)
|
54 |
|
55 |
if images:
|
56 |
+
# Process the first image in the pdf, you may need to loop through all images.
|
57 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
58 |
+
inputs = processor(images=image, return_tensors="pt")
|
59 |
+
outputs = model(**inputs)
|
60 |
+
logits = outputs.logits
|
61 |
+
predicted_class_idx = logits.argmax(-1).item()
|
62 |
+
result = model.config.id2label[predicted_class_idx]
|
63 |
+
return jsonify({'result': result})
|
64 |
else:
|
65 |
return jsonify({'error': 'PDF conversion failed.'}), 500
|
66 |
|
|
|
71 |
return jsonify({'error': f'An error occurred: {e}'}), 500
|
72 |
|
73 |
if __name__ == '__main__':
|
74 |
+
app.run(host="0.0.0.0", port=7860, debug=True) # Make it accessible from any network interface
|