mashaelalbu commited on
Commit
36ac855
·
verified ·
1 Parent(s): 8b08fe3

Update app/utils.py

Browse files
Files changed (1) hide show
  1. app/utils.py +9 -9
app/utils.py CHANGED
@@ -10,6 +10,7 @@ import nltk
10
  import requests
11
  import io
12
  from flask import current_app
 
13
 
14
  logger = logging.getLogger(__name__)
15
 
@@ -144,7 +145,7 @@ class AllergyAnalyzer:
144
  results.append(allergy)
145
  return results
146
 
147
- def analyze_image(self, image, user_allergens):
148
  """تحليل الصورة مباشرة للكشف عن الحساسيات"""
149
  try:
150
  # استخراج النص من الصورة
@@ -167,10 +168,9 @@ class AllergyAnalyzer:
167
  database_matches[allergy] = database_matches.get(allergy, []) + [token]
168
  else:
169
  # إذا لم توجد في قاعدة البيانات، نستخدم Claude API
170
- claude_api_key = current_app.config.get('CLAUDE_API_KEY')
171
  if claude_api_key:
172
  for allergy in user_allergens:
173
- if self.check_claude_allergens(token, allergy, claude_api_key):
174
  detected_allergens.add(allergy)
175
  claude_matches[allergy] = claude_matches.get(allergy, []) + [token]
176
 
@@ -191,7 +191,7 @@ class AllergyAnalyzer:
191
  "error": str(e)
192
  }
193
 
194
- def check_claude_allergens(self, token, allergy, api_key):
195
  """الاستعلام من Claude API عن الحساسيات"""
196
  try:
197
  # تحضير الصورة للطلب
@@ -200,10 +200,10 @@ class AllergyAnalyzer:
200
  img_byte_arr = img_byte_arr.getvalue()
201
 
202
  prompt = f"""
203
- Analyze if this product contains or is derived from {allergy}.
204
- Focus on the ingredient: {token}.
205
- Respond ONLY with 'Yes' or 'No'. No explanations.
206
- """
207
 
208
  url = "https://api.anthropic.com/v1/messages"
209
  headers = {
@@ -245,4 +245,4 @@ Respond ONLY with 'Yes' or 'No'. No explanations.
245
 
246
  except Exception as e:
247
  logger.error(f"Error querying Claude API: {str(e)}")
248
- return
 
10
  import requests
11
  import io
12
  from flask import current_app
13
+ import base64
14
 
15
  logger = logging.getLogger(__name__)
16
 
 
145
  results.append(allergy)
146
  return results
147
 
148
+ def analyze_image(self, image, user_allergens, claude_api_key=None):
149
  """تحليل الصورة مباشرة للكشف عن الحساسيات"""
150
  try:
151
  # استخراج النص من الصورة
 
168
  database_matches[allergy] = database_matches.get(allergy, []) + [token]
169
  else:
170
  # إذا لم توجد في قاعدة البيانات، نستخدم Claude API
 
171
  if claude_api_key:
172
  for allergy in user_allergens:
173
+ if self.check_claude_allergens(token, allergy, claude_api_key, image):
174
  detected_allergens.add(allergy)
175
  claude_matches[allergy] = claude_matches.get(allergy, []) + [token]
176
 
 
191
  "error": str(e)
192
  }
193
 
194
+ def check_claude_allergens(self, token, allergy, api_key, image):
195
  """الاستعلام من Claude API عن الحساسيات"""
196
  try:
197
  # تحضير الصورة للطلب
 
200
  img_byte_arr = img_byte_arr.getvalue()
201
 
202
  prompt = f"""
203
+ Analyze if this product contains or is derived from {allergy}.
204
+ Focus on the ingredient: {token}.
205
+ Respond ONLY with 'Yes' or 'No'. No explanations.
206
+ """
207
 
208
  url = "https://api.anthropic.com/v1/messages"
209
  headers = {
 
245
 
246
  except Exception as e:
247
  logger.error(f"Error querying Claude API: {str(e)}")
248
+ return False