yomna-ashraf commited on
Commit
6e597cb
·
verified ·
1 Parent(s): 44b22eb

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. app.py +61 -0
  3. requirements.txt +0 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /code
4
+
5
+ COPY requirements.txt .
6
+
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ COPY . .
10
+
11
+ EXPOSE 7860
12
+
13
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
16
+ try:
17
+ pdf_document = fitz.open(stream=pdf_data, filetype="pdf")
18
+ images = []
19
+ for page_num in range(pdf_document.page_count):
20
+ page = pdf_document.load_page(page_num)
21
+ pix = page.get_pixmap()
22
+ img_data = pix.tobytes("jpeg")
23
+ images.append(img_data)
24
+ return images
25
+ except Exception as e:
26
+ print(f"Error converting PDF: {e}")
27
+ return None
28
+
29
+ @app.route('/classify', methods=['POST'])
30
+ def classify_file():
31
+ if 'file' not in request.files:
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)
requirements.txt ADDED
Binary file (112 Bytes). View file