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 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 |
-
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,12 +38,8 @@ def classify_file():
|
|
40 |
# Handle image upload
|
41 |
img_data = uploaded_file.read()
|
42 |
image = Image.open(io.BytesIO(img_data)).convert("RGB")
|
43 |
-
|
44 |
-
|
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,14 +47,10 @@ def classify_file():
|
|
53 |
images = pdf_to_images_pymupdf(pdf_data)
|
54 |
|
55 |
if images:
|
56 |
-
# Process the first image in the pdf
|
57 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
58 |
-
|
59 |
-
|
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,4 +61,4 @@ def classify_file():
|
|
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)
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
from transformers import pipeline
|
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 |
+
# Using the pipeline to automatically load the model and processor
|
12 |
+
pipe = pipeline("image-classification", model="AsmaaElnagger/Diabetic_RetinoPathy_detection")
|
|
|
|
|
13 |
|
14 |
def pdf_to_images_pymupdf(pdf_data):
|
15 |
try:
|
|
|
38 |
# Handle image upload
|
39 |
img_data = uploaded_file.read()
|
40 |
image = Image.open(io.BytesIO(img_data)).convert("RGB")
|
41 |
+
result = pipe(image) # Use pipeline for classification
|
42 |
+
return jsonify({'result': result[0]['label']})
|
|
|
|
|
|
|
|
|
43 |
|
44 |
elif file_type == 'pdf':
|
45 |
# Handle PDF upload
|
|
|
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 |
+
result = pipe(image) # Use pipeline for classification
|
53 |
+
return jsonify({'result': result[0]['label']})
|
|
|
|
|
|
|
|
|
54 |
else:
|
55 |
return jsonify({'error': 'PDF conversion failed.'}), 500
|
56 |
|
|
|
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)
|