Spaces:
Configuration error
Configuration error
Delete app/app_routes.py
Browse files- app/app_routes.py +0 -143
app/app_routes.py
DELETED
@@ -1,143 +0,0 @@
|
|
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()
|
13 |
-
|
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):
|
34 |
-
"""التحقق من صحة امتداد الملف"""
|
35 |
-
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
36 |
-
|
37 |
-
def find_allergens(text, user_allergens):
|
38 |
-
"""البحث عن الحساسيات في النص"""
|
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():
|
62 |
-
return jsonify({
|
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())
|
70 |
-
})
|
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 = {
|
115 |
-
"success": True,
|
116 |
-
"extracted_text": extracted_text,
|
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)
|
135 |
-
}), 500
|
136 |
-
|
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 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|