mashaelalbu commited on
Commit
0ef66b6
·
verified ·
1 Parent(s): 1d79ca2

Update app/routes.py

Browse files
Files changed (1) hide show
  1. app/routes.py +25 -14
app/routes.py CHANGED
@@ -1,4 +1,4 @@
1
- # routes.py
2
  from flask import Blueprint, jsonify, request, current_app
3
  import io
4
  import pandas as pd
@@ -66,14 +66,14 @@ def process_image():
66
  "supported_formats": list(ALLOWED_EXTENSIONS)
67
  }), 400
68
 
69
- # القائمة الثابتة للحساسيات (بدلاً من إدخال المستخدم)
70
- FIXED_ALLERGENS = [
71
- "dairy", "eggs", "peanuts", "soy",
72
- "tree nuts", "wheat", "fish",
73
- "shellfish", "sesame"
74
- ]
75
 
76
- logger.info(f"Processing image for fixed allergens: {FIXED_ALLERGENS}")
77
 
78
  # تأكد من تهيئة محلل الحساسيات
79
  if allergy_analyzer is None:
@@ -84,16 +84,27 @@ def process_image():
84
  file_stream = io.BytesIO(file_bytes)
85
  image = Image.open(file_stream)
86
 
87
- # تحليل الصورة باستخدام القائمة الثابتة
88
- analysis_results = allergy_analyzer.analyze_image(image, FIXED_ALLERGENS)
 
 
 
 
89
 
 
90
  response = {
91
  "success": True,
92
- "analysis": analysis_results,
 
 
 
 
 
 
93
  "warnings": {
94
- "has_allergens": len(analysis_results['detected_allergens']) > 0,
95
- "message": "⚠️ Warning: Allergens found!" if analysis_results['detected_allergens'] else "✅ No allergens found",
96
- "severity": "high" if analysis_results['detected_allergens'] else "none"
97
  }
98
  }
99
 
 
1
+ # routes.py (معدل)
2
  from flask import Blueprint, jsonify, request, current_app
3
  import io
4
  import pandas as pd
 
66
  "supported_formats": list(ALLOWED_EXTENSIONS)
67
  }), 400
68
 
69
+ # الحصول على قائمة الحساسيات من المستخدم أو استخدام القائمة الثابتة
70
+ user_allergens = request.form.get('allergens')
71
+ if user_allergens:
72
+ user_allergens = [a.strip().lower() for a in user_allergens.split(',')]
73
+ else:
74
+ user_allergens = list(allergy_analyzer.allergy_dict.keys())
75
 
76
+ logger.info(f"Processing image for allergens: {user_allergens}")
77
 
78
  # تأكد من تهيئة محلل الحساسيات
79
  if allergy_analyzer is None:
 
84
  file_stream = io.BytesIO(file_bytes)
85
  image = Image.open(file_stream)
86
 
87
+ # تحليل الصورة
88
+ analysis_results = allergy_analyzer.analyze_image(
89
+ image,
90
+ user_allergens,
91
+ current_app.config['CLAUDE_API_KEY']
92
+ )
93
 
94
+ # بناء الاستجابة
95
  response = {
96
  "success": True,
97
+ "extracted_text": analysis_results.get("extracted_text", ""),
98
+ "analysis": {
99
+ "detected_allergens": analysis_results.get("detected_allergens", []),
100
+ "database_matches": analysis_results.get("database_matches", {}),
101
+ "claude_matches": analysis_results.get("claude_matches", {}),
102
+ "analyzed_tokens": analysis_results.get("analyzed_tokens", [])
103
+ },
104
  "warnings": {
105
+ "has_allergens": len(analysis_results.get("detected_allergens", [])) > 0,
106
+ "message": "⚠️ Warning: Allergens found!" if analysis_results.get("detected_allergens") else "✅ No allergens found",
107
+ "severity": "high" if analysis_results.get("detected_allergens") else "none"
108
  }
109
  }
110