Update app/routes.py
Browse files- app/routes.py +88 -36
app/routes.py
CHANGED
|
@@ -1,65 +1,117 @@
|
|
| 1 |
-
from flask import Blueprint, jsonify, request
|
| 2 |
-
import os
|
| 3 |
-
from werkzeug.utils import secure_filename
|
| 4 |
-
from app.utils import OCRModel
|
| 5 |
-
import torch
|
| 6 |
import io
|
|
|
|
| 7 |
|
| 8 |
main = Blueprint('main', __name__)
|
| 9 |
ocr_model = OCRModel()
|
| 10 |
|
|
|
|
| 11 |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def allowed_file(filename):
|
|
|
|
| 14 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@main.route('/')
|
| 17 |
def index():
|
| 18 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
@main.route('/api/ocr', methods=['POST'])
|
| 21 |
def process_image():
|
|
|
|
| 22 |
if 'file' not in request.files:
|
| 23 |
-
return jsonify({"error": "No file
|
| 24 |
|
|
|
|
| 25 |
if 'allergens' not in request.form:
|
| 26 |
-
return jsonify({"error": "
|
| 27 |
|
| 28 |
file = request.files['file']
|
| 29 |
if file.filename == '':
|
| 30 |
-
return jsonify({"error": "No selected
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
# قراءة الصورة
|
| 38 |
file_bytes = file.read()
|
| 39 |
file_stream = io.BytesIO(file_bytes)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
response = {
|
| 54 |
-
"text": result,
|
| 55 |
"found_allergens": list(found_allergens),
|
|
|
|
| 56 |
"has_allergens": len(found_allergens) > 0,
|
| 57 |
-
"warning": "
|
| 58 |
}
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Blueprint, jsonify, request
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import io
|
| 3 |
+
from app.utils import OCRModel
|
| 4 |
|
| 5 |
main = Blueprint('main', __name__)
|
| 6 |
ocr_model = OCRModel()
|
| 7 |
|
| 8 |
+
# تحديد امتدادات الملفات المسموح بها
|
| 9 |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
|
| 10 |
|
| 11 |
+
# قائمة الحساسيات المعروفة (يمكن تخصيصها حسب الحاجة)
|
| 12 |
+
KNOWN_ALLERGENS = {
|
| 13 |
+
'gluten': ['wheat', 'barley', 'gluten'],
|
| 14 |
+
'dairy': ['milk', 'yogurt', 'cheese', 'lactose'],
|
| 15 |
+
'nuts': ['nuts', 'peanuts', 'almonds', 'walnuts'],
|
| 16 |
+
'eggs': ['eggs'],
|
| 17 |
+
'soy': ['soy'],
|
| 18 |
+
'fish': ['fish'],
|
| 19 |
+
'shellfish': ['oyster', 'shrimp', 'shrimp'],
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
def allowed_file(filename):
|
| 23 |
+
"""التحقق من صحة امتداد الملف"""
|
| 24 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 25 |
|
| 26 |
+
def find_allergens(text, user_allergens):
|
| 27 |
+
"""البحث عن الحساسيات في النص"""
|
| 28 |
+
text = text.lower()
|
| 29 |
+
found_allergens = set()
|
| 30 |
+
allergen_details = {}
|
| 31 |
+
|
| 32 |
+
for allergen in user_allergens:
|
| 33 |
+
allergen = allergen.strip().lower()
|
| 34 |
+
# البحث في القائمة الرئيسية للحساسيات
|
| 35 |
+
if allergen in KNOWN_ALLERGENS:
|
| 36 |
+
for variant in KNOWN_ALLERGENS[allergen]:
|
| 37 |
+
if variant.lower() in text:
|
| 38 |
+
found_allergens.add(allergen)
|
| 39 |
+
allergen_details[allergen] = variant
|
| 40 |
+
# البحث المباشر عن النص المدخل
|
| 41 |
+
elif allergen in text:
|
| 42 |
+
found_allergens.add(allergen)
|
| 43 |
+
allergen_details[allergen] = allergen
|
| 44 |
+
|
| 45 |
+
return found_allergens, allergen_details
|
| 46 |
+
|
| 47 |
@main.route('/')
|
| 48 |
def index():
|
| 49 |
+
return jsonify({
|
| 50 |
+
"message": "Welcome to the Text Recognition and Sensitivity Checking Service",
|
| 51 |
+
"endpoints": {
|
| 52 |
+
"/api/ocr": "POST - Image analysis and sensitivity testing",
|
| 53 |
+
},
|
| 54 |
+
"supported_formats": list(ALLOWED_EXTENSIONS),
|
| 55 |
+
"known_allergens": list(KNOWN_ALLERGENS.keys())
|
| 56 |
+
})
|
| 57 |
|
| 58 |
@main.route('/api/ocr', methods=['POST'])
|
| 59 |
def process_image():
|
| 60 |
+
# التحقق من وجود الملف
|
| 61 |
if 'file' not in request.files:
|
| 62 |
+
return jsonify({"error": "No file uploaded"}), 400
|
| 63 |
|
| 64 |
+
# التحقق من وجود قائمة الحساسيات
|
| 65 |
if 'allergens' not in request.form:
|
| 66 |
+
return jsonify({"error": "Sensitivities not specified"}), 400
|
| 67 |
|
| 68 |
file = request.files['file']
|
| 69 |
if file.filename == '':
|
| 70 |
+
return jsonify({"error": "No file selected"}), 400
|
| 71 |
|
| 72 |
+
# التحقق من نوع الملف
|
| 73 |
+
if not allowed_file(file.filename):
|
| 74 |
+
return jsonify({
|
| 75 |
+
"error": "File type not supported",
|
| 76 |
+
"supported_formats": list(ALLOWED_EXTENSIONS)
|
| 77 |
+
}), 400
|
| 78 |
+
|
| 79 |
+
# تحضير قائمة الحساسيات
|
| 80 |
+
user_allergens = request.form['allergens'].split(',')
|
| 81 |
|
| 82 |
+
try:
|
| 83 |
+
# قراءة الصورة
|
| 84 |
file_bytes = file.read()
|
| 85 |
file_stream = io.BytesIO(file_bytes)
|
| 86 |
|
| 87 |
+
# معالجة الصورة
|
| 88 |
+
extracted_text = ocr_model.process_image(file_stream)
|
| 89 |
+
|
| 90 |
+
# البحث عن الحساسيات
|
| 91 |
+
found_allergens, allergen_details = find_allergens(extracted_text, user_allergens)
|
| 92 |
+
|
| 93 |
+
# تحضير الرد
|
| 94 |
+
response = {
|
| 95 |
+
"success": True,
|
| 96 |
+
"extracted_text": extracted_text,
|
| 97 |
+
"analysis": {
|
|
|
|
|
|
|
|
|
|
| 98 |
"found_allergens": list(found_allergens),
|
| 99 |
+
"allergen_details": allergen_details,
|
| 100 |
"has_allergens": len(found_allergens) > 0,
|
| 101 |
+
"warning": "Warning: Allergens found!" if found_allergens else "No allergens found"
|
| 102 |
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
return jsonify(response)
|
| 106 |
+
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return jsonify({
|
| 109 |
+
"error": "An error occurred while processing the image.",
|
| 110 |
+
"details": str(e)
|
| 111 |
+
}), 500
|
| 112 |
+
|
| 113 |
+
@main.route('/api/allergens', methods=['GET'])
|
| 114 |
+
def get_known_allergens():
|
| 115 |
+
return jsonify({
|
| 116 |
+
"allergens": KNOWN_ALLERGENS
|
| 117 |
+
})
|