yomna-ashraf commited on
Commit
fc32d0a
·
verified ·
1 Parent(s): 76c478c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -20
app.py CHANGED
@@ -1,17 +1,15 @@
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,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
- 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,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, 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,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) # Make it accessible from any network interface
 
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)