Spaces:
Zienab
/
Runtime error

Zienab commited on
Commit
71c8ab5
·
verified ·
1 Parent(s): d35a7b6

Update app/routes.py

Browse files
Files changed (1) hide show
  1. app/routes.py +31 -7
app/routes.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  from werkzeug.utils import secure_filename
4
  from app.utils import OCRModel
5
  import torch
 
6
 
7
  main = Blueprint('main', __name__)
8
  ocr_model = OCRModel()
@@ -21,20 +22,43 @@ def process_image():
21
  if 'file' not in request.files:
22
  return jsonify({"error": "No file part"}), 400
23
 
 
 
 
24
  file = request.files['file']
25
  if file.filename == '':
26
  return jsonify({"error": "No selected file"}), 400
27
 
 
 
 
 
28
  if file and allowed_file(file.filename):
29
- filename = secure_filename(file.filename)
30
- filepath = os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
31
- file.save(filepath)
32
 
33
  try:
34
- result = ocr_model.process_image(filepath)
35
- # حذف الملف بعد المعالجة
36
- os.remove(filepath)
37
- return jsonify({"result": result})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  except Exception as e:
39
  return jsonify({"error": str(e)}), 500
40
 
 
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()
 
22
  if 'file' not in request.files:
23
  return jsonify({"error": "No file part"}), 400
24
 
25
+ if 'allergens' not in request.form:
26
+ return jsonify({"error": "No allergens specified"}), 400
27
+
28
  file = request.files['file']
29
  if file.filename == '':
30
  return jsonify({"error": "No selected file"}), 400
31
 
32
+ # تحويل الحساسيات إلى قائمة
33
+ user_allergens = request.form['allergens'].lower().split(',')
34
+ user_allergens = {allergen.strip() for allergen in user_allergens}
35
+
36
  if file and allowed_file(file.filename):
37
+ # قراءة الصورة في الذاكرة بدلاً من حفظها
38
+ file_bytes = file.read()
39
+ file_stream = io.BytesIO(file_bytes)
40
 
41
  try:
42
+ # معالجة الصورة واستخراج النص
43
+ result = ocr_model.process_image(file_stream)
44
+
45
+ # تحليل النص وفحص الحساسيات
46
+ found_allergens = set()
47
+ text_lower = result.lower()
48
+
49
+ for allergen in user_allergens:
50
+ if allergen in text_lower:
51
+ found_allergens.add(allergen)
52
+
53
+ response = {
54
+ "text": result,
55
+ "found_allergens": list(found_allergens),
56
+ "has_allergens": len(found_allergens) > 0,
57
+ "warning": "تحذير: هذا المنتج يحتوي على مواد قد تسبب لك حساسية!" if found_allergens else "لم يتم العثور على مواد مسببة للحساسية"
58
+ }
59
+
60
+ return jsonify(response)
61
+
62
  except Exception as e:
63
  return jsonify({"error": str(e)}), 500
64