Update app/routes.py
Browse files- app/routes.py +62 -36
app/routes.py
CHANGED
@@ -1,6 +1,12 @@
|
|
|
|
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()
|
@@ -8,15 +14,20 @@ ocr_model = OCRModel()
|
|
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': ['
|
|
|
|
|
|
|
|
|
|
|
20 |
}
|
21 |
|
22 |
def allowed_file(filename):
|
@@ -28,21 +39,23 @@ def find_allergens(text, user_allergens):
|
|
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():
|
@@ -50,6 +63,7 @@ def index():
|
|
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())
|
@@ -57,38 +71,44 @@ def index():
|
|
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 = {
|
@@ -97,14 +117,18 @@ def process_image():
|
|
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)
|
@@ -113,5 +137,7 @@ def process_image():
|
|
113 |
@main.route('/api/allergens', methods=['GET'])
|
114 |
def get_known_allergens():
|
115 |
return jsonify({
|
116 |
-
"allergens": KNOWN_ALLERGENS
|
|
|
|
|
117 |
})
|
|
|
1 |
+
# routes.py
|
2 |
from flask import Blueprint, jsonify, request
|
3 |
import io
|
4 |
from app.utils import OCRModel
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# إعداد التسجيل
|
8 |
+
logging.basicConfig(level=logging.INFO)
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
|
11 |
main = Blueprint('main', __name__)
|
12 |
ocr_model = OCRModel()
|
|
|
14 |
# تحديد امتدادات الملفات المسموح بها
|
15 |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
|
16 |
|
17 |
+
# قائمة الحساسيات المعروفة
|
18 |
KNOWN_ALLERGENS = {
|
19 |
+
'gluten': ['wheat', 'barley', 'rye', 'oats', 'gluten', 'flour', 'bread', 'pasta'],
|
20 |
+
'dairy': ['milk', 'yogurt', 'cheese', 'lactose', 'cream', 'butter', 'whey'],
|
21 |
+
'nuts': ['nuts', 'peanuts', 'almonds', 'walnuts', 'cashews', 'pistachios'],
|
22 |
+
'eggs': ['eggs', 'egg', 'albumin', 'mayonnaise'],
|
23 |
+
'soy': ['soy', 'soybeans', 'tofu', 'edamame'],
|
24 |
+
'fish': ['fish', 'salmon', 'tuna', 'cod', 'tilapia'],
|
25 |
+
'shellfish': ['shellfish', 'shrimp', 'crab', 'lobster', 'oyster', 'mussels'],
|
26 |
+
'sesame': ['sesame', 'tahini'],
|
27 |
+
'mustard': ['mustard'],
|
28 |
+
'celery': ['celery'],
|
29 |
+
'lupin': ['lupin'],
|
30 |
+
'sulfites': ['sulfites', 'sulphites']
|
31 |
}
|
32 |
|
33 |
def allowed_file(filename):
|
|
|
39 |
text = text.lower()
|
40 |
found_allergens = set()
|
41 |
allergen_details = {}
|
42 |
+
allergen_locations = {}
|
43 |
|
44 |
for allergen in user_allergens:
|
45 |
allergen = allergen.strip().lower()
|
|
|
46 |
if allergen in KNOWN_ALLERGENS:
|
47 |
for variant in KNOWN_ALLERGENS[allergen]:
|
48 |
if variant.lower() in text:
|
49 |
found_allergens.add(allergen)
|
50 |
allergen_details[allergen] = variant
|
51 |
+
# تخزين موقع الكلمة في النص
|
52 |
+
allergen_locations[allergen] = text.index(variant.lower())
|
53 |
elif allergen in text:
|
54 |
found_allergens.add(allergen)
|
55 |
allergen_details[allergen] = allergen
|
56 |
+
allergen_locations[allergen] = text.index(allergen)
|
57 |
|
58 |
+
return found_allergens, allergen_details, allergen_locations
|
59 |
|
60 |
@main.route('/')
|
61 |
def index():
|
|
|
63 |
"message": "Welcome to the Text Recognition and Sensitivity Checking Service",
|
64 |
"endpoints": {
|
65 |
"/api/ocr": "POST - Image analysis and sensitivity testing",
|
66 |
+
"/api/allergens": "GET - List of known allergens"
|
67 |
},
|
68 |
"supported_formats": list(ALLOWED_EXTENSIONS),
|
69 |
"known_allergens": list(KNOWN_ALLERGENS.keys())
|
|
|
71 |
|
72 |
@main.route('/api/ocr', methods=['POST'])
|
73 |
def process_image():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
try:
|
75 |
+
# التحقق من وجود الملف
|
76 |
+
if 'file' not in request.files:
|
77 |
+
logger.warning("No file uploaded")
|
78 |
+
return jsonify({"error": "No file uploaded"}), 400
|
79 |
+
|
80 |
+
# التحقق من وجود قائمة الحساسيات
|
81 |
+
if 'allergens' not in request.form:
|
82 |
+
logger.warning("Allergens not specified")
|
83 |
+
return jsonify({"error": "Allergens not specified"}), 400
|
84 |
+
|
85 |
+
file = request.files['file']
|
86 |
+
if file.filename == '':
|
87 |
+
logger.warning("No file selected")
|
88 |
+
return jsonify({"error": "No file selected"}), 400
|
89 |
+
|
90 |
+
# التحقق من نوع الملف
|
91 |
+
if not allowed_file(file.filename):
|
92 |
+
logger.warning(f"Invalid file type: {file.filename}")
|
93 |
+
return jsonify({
|
94 |
+
"error": "File type not supported",
|
95 |
+
"supported_formats": list(ALLOWED_EXTENSIONS)
|
96 |
+
}), 400
|
97 |
+
|
98 |
+
# تحضير قائمة الحساسيات
|
99 |
+
user_allergens = request.form['allergens'].split(',')
|
100 |
+
logger.info(f"Processing image for allergens: {user_allergens}")
|
101 |
+
|
102 |
# قراءة الصورة
|
103 |
file_bytes = file.read()
|
104 |
file_stream = io.BytesIO(file_bytes)
|
105 |
|
106 |
# معالجة الصورة
|
107 |
extracted_text = ocr_model.process_image(file_stream)
|
108 |
+
logger.info(f"Extracted text: {extracted_text}")
|
109 |
|
110 |
# البحث عن الحساسيات
|
111 |
+
found_allergens, allergen_details, allergen_locations = find_allergens(extracted_text, user_allergens)
|
112 |
|
113 |
# تحضير الرد
|
114 |
response = {
|
|
|
117 |
"analysis": {
|
118 |
"found_allergens": list(found_allergens),
|
119 |
"allergen_details": allergen_details,
|
120 |
+
"allergen_locations": allergen_locations,
|
121 |
"has_allergens": len(found_allergens) > 0,
|
122 |
+
"warning": "⚠️ Warning: Allergens found!" if found_allergens else "✅ No allergens found",
|
123 |
+
"severity": "high" if len(found_allergens) > 0 else "none"
|
124 |
}
|
125 |
}
|
126 |
|
127 |
+
logger.info(f"Analysis completed successfully: {found_allergens}")
|
128 |
return jsonify(response)
|
129 |
|
130 |
except Exception as e:
|
131 |
+
logger.error(f"Error processing request: {str(e)}", exc_info=True)
|
132 |
return jsonify({
|
133 |
"error": "An error occurred while processing the image.",
|
134 |
"details": str(e)
|
|
|
137 |
@main.route('/api/allergens', methods=['GET'])
|
138 |
def get_known_allergens():
|
139 |
return jsonify({
|
140 |
+
"allergens": KNOWN_ALLERGENS,
|
141 |
+
"total_count": len(KNOWN_ALLERGENS),
|
142 |
+
"last_updated": "2024-03-24" # تحديث هذا التاريخ عند تحديث القائمة
|
143 |
})
|