Spaces:
Sleeping
Sleeping
Update app/routes.py
Browse files- app/routes.py +14 -32
app/routes.py
CHANGED
@@ -30,22 +30,6 @@ def allowed_file(filename):
|
|
30 |
"""Validate file extension"""
|
31 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
32 |
|
33 |
-
@main.route('/')
|
34 |
-
def index():
|
35 |
-
return jsonify({
|
36 |
-
"message": "Welcome to the Advanced OCR and Allergy Detection Service",
|
37 |
-
"endpoints": {
|
38 |
-
"/api/ocr": "POST - Image analysis and allergy detection",
|
39 |
-
"/api/allergens": "GET - List of known allergens from dataset"
|
40 |
-
},
|
41 |
-
"supported_formats": list(ALLOWED_EXTENSIONS),
|
42 |
-
"features": [
|
43 |
-
"OCR text extraction",
|
44 |
-
"Allergy detection from database",
|
45 |
-
"AI-powered allergy risk assessment"
|
46 |
-
]
|
47 |
-
})
|
48 |
-
|
49 |
@main.route('/api/ocr', methods=['POST'])
|
50 |
def process_image():
|
51 |
global allergy_analyzer
|
@@ -66,6 +50,14 @@ def process_image():
|
|
66 |
"supported_formats": list(ALLOWED_EXTENSIONS)
|
67 |
}), 400
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# تأكد من تهيئة محلل الحساسيات
|
70 |
if allergy_analyzer is None:
|
71 |
init_allergy_analyzer(current_app._get_current_object())
|
@@ -75,15 +67,17 @@ def process_image():
|
|
75 |
file_stream = io.BytesIO(file_bytes)
|
76 |
image = Image.open(file_stream)
|
77 |
|
78 |
-
# تحليل الصورة
|
79 |
analysis_results = allergy_analyzer.analyze_image(
|
80 |
image,
|
81 |
-
current_app.config['CLAUDE_API_KEY']
|
|
|
82 |
)
|
83 |
|
84 |
# بناء الاستجابة
|
85 |
response = {
|
86 |
"success": True,
|
|
|
87 |
"extracted_text": analysis_results.get("extracted_text", ""),
|
88 |
"analysis": {
|
89 |
"detected_allergens": analysis_results.get("detected_allergens", []),
|
@@ -93,7 +87,7 @@ def process_image():
|
|
93 |
},
|
94 |
"warnings": {
|
95 |
"has_allergens": len(analysis_results.get("detected_allergens", [])) > 0,
|
96 |
-
"message": "⚠️ Warning: Allergens found!" if analysis_results.get("detected_allergens") else "✅ No allergens found",
|
97 |
"severity": "high" if analysis_results.get("detected_allergens") else "none"
|
98 |
}
|
99 |
}
|
@@ -106,16 +100,4 @@ def process_image():
|
|
106 |
return jsonify({
|
107 |
"error": "An error occurred while processing the image.",
|
108 |
"details": str(e)
|
109 |
-
}), 500
|
110 |
-
|
111 |
-
@main.route('/api/allergens', methods=['GET'])
|
112 |
-
def get_known_allergens():
|
113 |
-
global allergy_analyzer
|
114 |
-
if allergy_analyzer is None:
|
115 |
-
init_allergy_analyzer(current_app._get_current_object())
|
116 |
-
allergens = allergy_analyzer.get_allergen_list()
|
117 |
-
return jsonify({
|
118 |
-
"allergens": allergens,
|
119 |
-
"total_count": len(allergens),
|
120 |
-
"last_updated": "2024-03-24"
|
121 |
-
})
|
|
|
30 |
"""Validate file extension"""
|
31 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
@main.route('/api/ocr', methods=['POST'])
|
34 |
def process_image():
|
35 |
global allergy_analyzer
|
|
|
50 |
"supported_formats": list(ALLOWED_EXTENSIONS)
|
51 |
}), 400
|
52 |
|
53 |
+
# الحصول على حساسيات المستخدم من الطلب
|
54 |
+
user_allergies = request.form.get('user_allergies', '').split(',')
|
55 |
+
user_allergies = [a.strip().lower() for a in user_allergies if a.strip()]
|
56 |
+
|
57 |
+
if not user_allergies:
|
58 |
+
logger.warning("No user allergies provided")
|
59 |
+
return jsonify({"error": "User allergies not provided"}), 400
|
60 |
+
|
61 |
# تأكد من تهيئة محلل الحساسيات
|
62 |
if allergy_analyzer is None:
|
63 |
init_allergy_analyzer(current_app._get_current_object())
|
|
|
67 |
file_stream = io.BytesIO(file_bytes)
|
68 |
image = Image.open(file_stream)
|
69 |
|
70 |
+
# تحليل الصورة مع مراعاة حساسيات المستخدم
|
71 |
analysis_results = allergy_analyzer.analyze_image(
|
72 |
image,
|
73 |
+
current_app.config['CLAUDE_API_KEY'],
|
74 |
+
user_allergies=user_allergies
|
75 |
)
|
76 |
|
77 |
# بناء الاستجابة
|
78 |
response = {
|
79 |
"success": True,
|
80 |
+
"user_allergies": user_allergies,
|
81 |
"extracted_text": analysis_results.get("extracted_text", ""),
|
82 |
"analysis": {
|
83 |
"detected_allergens": analysis_results.get("detected_allergens", []),
|
|
|
87 |
},
|
88 |
"warnings": {
|
89 |
"has_allergens": len(analysis_results.get("detected_allergens", [])) > 0,
|
90 |
+
"message": "⚠️ Warning: Allergens found that match your allergies!" if analysis_results.get("detected_allergens") else "✅ No allergens found that match your allergies",
|
91 |
"severity": "high" if analysis_results.get("detected_allergens") else "none"
|
92 |
}
|
93 |
}
|
|
|
100 |
return jsonify({
|
101 |
"error": "An error occurred while processing the image.",
|
102 |
"details": str(e)
|
103 |
+
}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|