Spaces:
Sleeping
Sleeping
Update app/utils.py
Browse files- app/utils.py +21 -16
app/utils.py
CHANGED
@@ -154,27 +154,29 @@ class AllergyAnalyzer:
|
|
154 |
logger.error(f"Error tokenizing text: {str(e)}")
|
155 |
return []
|
156 |
|
157 |
-
def check_allergen_in_excel(self, token):
|
158 |
-
"""التحقق من وجود التوكن في ملف الإكسل"""
|
159 |
try:
|
160 |
if not self.allergy_data:
|
161 |
return None
|
162 |
|
163 |
for allergy_name, ingredients in self.allergy_data.items():
|
164 |
-
|
|
|
165 |
return allergy_name
|
166 |
return None
|
167 |
except Exception as e:
|
168 |
logger.error(f"Error checking allergen in Excel: {str(e)}")
|
169 |
return None
|
170 |
|
171 |
-
def check_allergy_risk(self, ingredient, api_key):
|
172 |
-
"""الاستعلام من Claude API عن الحساسيات"""
|
173 |
try:
|
|
|
174 |
prompt = f"""
|
175 |
-
You are a professional food safety expert. Analyze the ingredient '{ingredient}' and determine
|
176 |
-
|
177 |
-
Respond only with the category name or 'None' if not found.
|
178 |
"""
|
179 |
url = "https://api.anthropic.com/v1/messages"
|
180 |
headers = {
|
@@ -196,8 +198,8 @@ Respond only with the category name or 'None' if not found.
|
|
196 |
|
197 |
if "content" in response_json and isinstance(response_json["content"], list):
|
198 |
result = response_json["content"][0]["text"].strip().lower()
|
199 |
-
#
|
200 |
-
if result in
|
201 |
return result
|
202 |
return None
|
203 |
|
@@ -206,12 +208,15 @@ Respond only with the category name or 'None' if not found.
|
|
206 |
|
207 |
return None
|
208 |
|
209 |
-
def analyze_image(self, image, claude_api_key=None):
|
210 |
-
"""تحليل الصورة للكشف عن الحساسيات"""
|
211 |
try:
|
212 |
if not self.allergy_data:
|
213 |
raise ValueError("Allergy data not loaded")
|
214 |
|
|
|
|
|
|
|
215 |
# استخراج النص من الصورة
|
216 |
extracted_text = self.ocr_model.process_image(image)
|
217 |
if extracted_text.startswith("Error processing image"):
|
@@ -228,15 +233,15 @@ Respond only with the category name or 'None' if not found.
|
|
228 |
claude_matches = {}
|
229 |
|
230 |
for token in tokens:
|
231 |
-
# البحث أولاً في قاعدة البيانات
|
232 |
-
allergy = self.check_allergen_in_excel(token)
|
233 |
if allergy:
|
234 |
if allergy not in database_matches:
|
235 |
database_matches[allergy] = []
|
236 |
database_matches[allergy].append(token)
|
237 |
elif claude_api_key:
|
238 |
-
# إذا لم يُوجد في ملف الإكسل، استدعِ Claude API
|
239 |
-
allergy = self.check_allergy_risk(token, claude_api_key)
|
240 |
if allergy:
|
241 |
if allergy not in claude_matches:
|
242 |
claude_matches[allergy] = []
|
|
|
154 |
logger.error(f"Error tokenizing text: {str(e)}")
|
155 |
return []
|
156 |
|
157 |
+
def check_allergen_in_excel(self, token, user_allergies):
|
158 |
+
"""التحقق من وجود التوكن في ملف الإكسل مع مراعاة حساسيات المستخدم"""
|
159 |
try:
|
160 |
if not self.allergy_data:
|
161 |
return None
|
162 |
|
163 |
for allergy_name, ingredients in self.allergy_data.items():
|
164 |
+
# نتحقق فقط من الحساسيات التي يهتم بها المستخدم
|
165 |
+
if allergy_name.lower() in user_allergies and token in ingredients:
|
166 |
return allergy_name
|
167 |
return None
|
168 |
except Exception as e:
|
169 |
logger.error(f"Error checking allergen in Excel: {str(e)}")
|
170 |
return None
|
171 |
|
172 |
+
def check_allergy_risk(self, ingredient, api_key, user_allergies):
|
173 |
+
"""الاستعلام من Claude API عن الحساسيات مع مراعاة حساسيات المستخدم"""
|
174 |
try:
|
175 |
+
# نطلب من Claude التحقق فقط للحساسيات المحددة من المستخدم
|
176 |
prompt = f"""
|
177 |
+
You are a professional food safety expert. Analyze the ingredient '{ingredient}' and determine if it belongs to any of these allergen categories:
|
178 |
+
{', '.join(user_allergies)}.
|
179 |
+
Respond only with the category name if found or 'None' if not found.
|
180 |
"""
|
181 |
url = "https://api.anthropic.com/v1/messages"
|
182 |
headers = {
|
|
|
198 |
|
199 |
if "content" in response_json and isinstance(response_json["content"], list):
|
200 |
result = response_json["content"][0]["text"].strip().lower()
|
201 |
+
# نتحقق فقط من الحساسيات التي يهتم بها المستخدم
|
202 |
+
if result in user_allergies:
|
203 |
return result
|
204 |
return None
|
205 |
|
|
|
208 |
|
209 |
return None
|
210 |
|
211 |
+
def analyze_image(self, image, claude_api_key=None, user_allergies=None):
|
212 |
+
"""تحليل الصورة للكشف عن الحساسيات مع مراعاة حساسيات المستخدم"""
|
213 |
try:
|
214 |
if not self.allergy_data:
|
215 |
raise ValueError("Allergy data not loaded")
|
216 |
|
217 |
+
if not user_allergies:
|
218 |
+
raise ValueError("User allergies not provided")
|
219 |
+
|
220 |
# استخراج النص من الصورة
|
221 |
extracted_text = self.ocr_model.process_image(image)
|
222 |
if extracted_text.startswith("Error processing image"):
|
|
|
233 |
claude_matches = {}
|
234 |
|
235 |
for token in tokens:
|
236 |
+
# البحث أولاً في قاعدة البيانات للحساسيات المحددة فقط
|
237 |
+
allergy = self.check_allergen_in_excel(token, user_allergies)
|
238 |
if allergy:
|
239 |
if allergy not in database_matches:
|
240 |
database_matches[allergy] = []
|
241 |
database_matches[allergy].append(token)
|
242 |
elif claude_api_key:
|
243 |
+
# إذا لم يُوجد في ملف الإكسل، استدعِ Claude API للحساسيات المحددة فقط
|
244 |
+
allergy = self.check_allergy_risk(token, claude_api_key, user_allergies)
|
245 |
if allergy:
|
246 |
if allergy not in claude_matches:
|
247 |
claude_matches[allergy] = []
|