Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py.py +80 -0
- requirements.txt +7 -0
app.py.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
from flask_cors import CORS
|
| 4 |
+
from transformers import AutoModelForImageClassification, AutoProcessor
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
import fitz
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
CORS(app)
|
| 12 |
+
|
| 13 |
+
model_name = "AsmaaElnagger/Diabetic_RetinoPathy_detection"
|
| 14 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 15 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
| 16 |
+
|
| 17 |
+
def pdf_to_images_pymupdf(pdf_data):
|
| 18 |
+
try:
|
| 19 |
+
pdf_document = fitz.open(stream=pdf_data, filetype="pdf")
|
| 20 |
+
images = []
|
| 21 |
+
for page_num in range(pdf_document.page_count):
|
| 22 |
+
page = pdf_document.load_page(page_num)
|
| 23 |
+
pix = page.get_pixmap()
|
| 24 |
+
img_data = pix.tobytes("jpeg")
|
| 25 |
+
images.append(img_data)
|
| 26 |
+
return images
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Error converting PDF: {e}")
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
@app.route('/classify', methods=['POST', 'OPTIONS'])
|
| 32 |
+
def classify_file():
|
| 33 |
+
if 'file' not in request.files:
|
| 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', 'gif']:
|
| 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 |
+
with torch.no_grad():
|
| 45 |
+
outputs = model(**inputs)
|
| 46 |
+
logits = outputs.logits
|
| 47 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 48 |
+
result = model.config.id2label[predicted_class_idx]
|
| 49 |
+
return jsonify({'result': result})
|
| 50 |
+
|
| 51 |
+
elif file_type == 'pdf':
|
| 52 |
+
pdf_data = uploaded_file.read()
|
| 53 |
+
images = pdf_to_images_pymupdf(pdf_data)
|
| 54 |
+
|
| 55 |
+
if images:
|
| 56 |
+
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
| 57 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 58 |
+
with torch.no_grad():
|
| 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 |
+
|
| 67 |
+
else:
|
| 68 |
+
return jsonify({'error': 'Unsupported file type'}), 400
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return jsonify({'error': f'An error occurred: {e}'}), 500
|
| 72 |
+
|
| 73 |
+
def main():
|
| 74 |
+
st.title("RetinApp Backend Status")
|
| 75 |
+
st.write("Flask backend is running and ready for classification requests.")
|
| 76 |
+
|
| 77 |
+
if __name__ == '__main__':
|
| 78 |
+
main() # Run the Streamlit app (which will also run the Flask app)
|
| 79 |
+
# The Flask app will be accessible on the same server where Streamlit is running.
|
| 80 |
+
# When deployed on Hugging Face Spaces, Streamlit will handle the serving.
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
flask
|
| 3 |
+
flask-cors
|
| 4 |
+
transformers
|
| 5 |
+
torch
|
| 6 |
+
Pillow
|
| 7 |
+
PyMuPDF
|