Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +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 |
import os
|
7 |
-
import torch
|
8 |
-
import torch.nn.functional as F
|
9 |
from werkzeug.utils import secure_filename
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
13 |
-
# Load model and processor
|
14 |
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
15 |
-
|
16 |
-
|
17 |
|
18 |
# PDF to image conversion
|
19 |
def pdf_to_images_pymupdf(pdf_data):
|
@@ -30,6 +28,7 @@ def pdf_to_images_pymupdf(pdf_data):
|
|
30 |
print(f"Error converting PDF: {e}")
|
31 |
return None
|
32 |
|
|
|
33 |
# File classification function (modified for API)
|
34 |
def classify_file(file_path):
|
35 |
try:
|
@@ -37,15 +36,10 @@ def classify_file(file_path):
|
|
37 |
if file_ext in ['.jpg', '.jpeg', '.png', '.gif']:
|
38 |
# Handle image upload
|
39 |
image = Image.open(file_path).convert("RGB")
|
40 |
-
|
41 |
-
outputs = model(**inputs)
|
42 |
-
probabilities = F.softmax(outputs.logits, dim=-1)[0].tolist()
|
43 |
-
predicted_class_idx = outputs.logits.argmax(-1).item()
|
44 |
-
result_label = model.config.id2label[predicted_class_idx]
|
45 |
-
confidence = probabilities[predicted_class_idx] * 100
|
46 |
return {
|
47 |
-
"prediction":
|
48 |
-
"confidence":
|
49 |
}
|
50 |
elif file_ext == '.pdf':
|
51 |
# Handle PDF upload
|
@@ -54,15 +48,10 @@ def classify_file(file_path):
|
|
54 |
images = pdf_to_images_pymupdf(pdf_data)
|
55 |
if images:
|
56 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
57 |
-
|
58 |
-
outputs = model(**inputs)
|
59 |
-
probabilities = F.softmax(outputs.logits, dim=-1)[0].tolist()
|
60 |
-
predicted_class_idx = outputs.logits.argmax(-1).item()
|
61 |
-
result_label = model.config.id2label[predicted_class_idx]
|
62 |
-
confidence = probabilities[predicted_class_idx] * 100
|
63 |
return {
|
64 |
-
"prediction":
|
65 |
-
"confidence":
|
66 |
}
|
67 |
else:
|
68 |
return {"error": "PDF conversion failed."}
|
@@ -71,6 +60,7 @@ def classify_file(file_path):
|
|
71 |
except Exception as e:
|
72 |
return {"error": f"An error occurred: {e}"}
|
73 |
|
|
|
74 |
# API endpoint for file classification
|
75 |
@app.route('/classify', methods=['POST'])
|
76 |
def classify():
|
@@ -81,11 +71,12 @@ def classify():
|
|
81 |
return jsonify({"error": "No file selected"}), 400
|
82 |
|
83 |
filename = secure_filename(file.filename)
|
84 |
-
filepath = os.path.join('/tmp', filename)
|
85 |
file.save(filepath)
|
86 |
result = classify_file(filepath)
|
87 |
-
os.remove(filepath)
|
88 |
return jsonify(result), 200 # Return JSON response
|
89 |
|
|
|
90 |
if __name__ == '__main__':
|
91 |
app.run(host='0.0.0.0', port=5000)
|
|
|
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 |
import os
|
|
|
|
|
7 |
from werkzeug.utils import secure_filename
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
+
# Load model and processor using pipeline
|
12 |
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
13 |
+
classifier = pipeline("image-classification", model=model_name)
|
14 |
+
|
15 |
|
16 |
# PDF to image conversion
|
17 |
def pdf_to_images_pymupdf(pdf_data):
|
|
|
28 |
print(f"Error converting PDF: {e}")
|
29 |
return None
|
30 |
|
31 |
+
|
32 |
# File classification function (modified for API)
|
33 |
def classify_file(file_path):
|
34 |
try:
|
|
|
36 |
if file_ext in ['.jpg', '.jpeg', '.png', '.gif']:
|
37 |
# Handle image upload
|
38 |
image = Image.open(file_path).convert("RGB")
|
39 |
+
result = classifier(image)[0] # Get the top prediction
|
|
|
|
|
|
|
|
|
|
|
40 |
return {
|
41 |
+
"prediction": result["label"],
|
42 |
+
"confidence": result["score"] * 100,
|
43 |
}
|
44 |
elif file_ext == '.pdf':
|
45 |
# Handle PDF upload
|
|
|
48 |
images = pdf_to_images_pymupdf(pdf_data)
|
49 |
if images:
|
50 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
51 |
+
result = classifier(image)[0] # Get the top prediction
|
|
|
|
|
|
|
|
|
|
|
52 |
return {
|
53 |
+
"prediction": result["label"],
|
54 |
+
"confidence": result["score"] * 100,
|
55 |
}
|
56 |
else:
|
57 |
return {"error": "PDF conversion failed."}
|
|
|
60 |
except Exception as e:
|
61 |
return {"error": f"An error occurred: {e}"}
|
62 |
|
63 |
+
|
64 |
# API endpoint for file classification
|
65 |
@app.route('/classify', methods=['POST'])
|
66 |
def classify():
|
|
|
71 |
return jsonify({"error": "No file selected"}), 400
|
72 |
|
73 |
filename = secure_filename(file.filename)
|
74 |
+
filepath = os.path.join('/tmp', filename) # Save to a temporary location
|
75 |
file.save(filepath)
|
76 |
result = classify_file(filepath)
|
77 |
+
os.remove(filepath) # remove temp file
|
78 |
return jsonify(result), 200 # Return JSON response
|
79 |
|
80 |
+
|
81 |
if __name__ == '__main__':
|
82 |
app.run(host='0.0.0.0', port=5000)
|