Moncef Souri commited on
Commit
8cff4cb
Β·
1 Parent(s): 29154b3

fix(analysis): enforce 3-key JSON output and fix French placeholder responses

Browse files

- Rewrite prompt to instruction-style with filled example to prevent
model from echoing placeholder strings as values
- Strip extra keys (recommended_products, problèmes, etc.) at parse time
- Add alias mapping for alternate key names (advice, analysis, routine variants)
- Add placeholder guard to detect and reject echoed template strings
- Sync default fallback responses to match 3-key structure
- singleton Groq client + deduplicate detections before LLM call

.gitignore CHANGED
@@ -1,3 +1,46 @@
 
1
  .env
2
- venv
3
- .venv
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Environment
2
  .env
3
+ .env.*
4
+ !.env.example
5
+ *.env
6
+
7
+ # Python cache
8
+ __pycache__/
9
+ *.py[cod]
10
+ *.pyo
11
+ *.pyd
12
+ .Python
13
+
14
+ # Virtual environments
15
+ venv/
16
+ .venv/
17
+ env/
18
+ .env/
19
+
20
+ # Distribution / packaging
21
+ dist/
22
+ build/
23
+ *.egg-info/
24
+ *.egg
25
+
26
+ # Pytest / coverage
27
+ .pytest_cache/
28
+ .coverage
29
+ htmlcov/
30
+
31
+ # Jupyter
32
+ .ipynb_checkpoints/
33
+
34
+ # OS
35
+ .DS_Store
36
+ Thumbs.db
37
+
38
+ # Logs
39
+ *.log
40
+ logs/
41
+
42
+ # Model weights (large files β€” use Git LFS or HF Hub instead)
43
+ *.pt
44
+ *.pth
45
+ *.onnx
46
+ *.bin
app.py CHANGED
@@ -1,484 +1,28 @@
1
  import os
2
- from flask import Flask, request, jsonify
3
  from flask_cors import CORS
4
- from ultralytics import YOLO
5
- import cv2
6
- import numpy as np
7
- from groq import Groq
8
- import threading
9
- import torch
10
- from torchvision import transforms, models
11
- from PIL import Image
12
- import json
13
- import re
14
- import time
15
- import queue
16
 
17
- app = Flask(__name__)
 
 
18
 
19
- # Enable CORS for RapidAPI (allow all origins)
20
- CORS(app, resources={r"/*": {"origins": "*"}})
21
 
22
- # Configuration
23
- MODEL_PATH = 'model_moncef.pt'
24
- SKIN_TYPE_MODEL_PATH = 'skin_type_classification85.pth'
25
- MAX_IMAGE_SIZE = 1024 # Maximum dimension for input images
26
- GROQ_TIMEOUT = 8.0 # Timeout for Groq API calls in seconds
27
- REQUEST_TIMEOUT = 30.0 # Overall request timeout in seconds
28
- MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB max file size
29
 
30
- # Translation dictionaries
31
- class_translations_en = {
32
- 'acne': 'Acne', 'blackheads': 'Blackheads', 'dark circles': 'Dark Circles',
33
- 'dark spot': 'Dark Spot', 'eyebags': 'Eye Bags', 'redness': 'Redness',
34
- 'whiteheads': 'Whiteheads', 'wrinkles': 'Wrinkles', 'Acne': 'Acne',
35
- 'Acne Scar': 'Acne Scar', 'Blackhead': 'Blackhead', 'Dark Circles': 'Dark Circles',
36
- 'Dark Spots': 'Dark Spots', 'Enlarged Pores': 'Enlarged Pores', 'Eyebags': 'Eye Bags',
37
- 'Freckle': 'Freckle', 'Melasma': 'Melasma', 'Nodules': 'Nodules',
38
- 'Papules': 'Papules', 'Pustules': 'Pustules', 'Skin Redness': 'Skin Redness',
39
- 'Sun Spots': 'Sun Spots', 'Vascular': 'Vascular Lesions', 'Whitehead': 'Whitehead',
40
- 'Wrinkles': 'Wrinkles'
41
- }
42
 
43
- class_translations_fr = {
44
- 'acne': 'AcnΓ©', 'blackheads': 'Points noirs', 'dark circles': 'Cernes',
45
- 'dark spot': 'Tache brune', 'eyebags': 'Poches sous les yeux', 'redness': 'Rougeurs',
46
- 'whiteheads': 'Points blancs', 'wrinkles': 'Rides', 'Acne': 'AcnΓ©',
47
- 'Acne Scar': 'Cicatrice d\'acnΓ©', 'Blackhead': 'Point noir', 'Dark Circles': 'Cernes',
48
- 'Dark Spots': 'Taches brunes', 'Enlarged Pores': 'Pores dilatΓ©s', 'Eyebags': 'Poches sous les yeux',
49
- 'Freckle': 'Tache de rousseur', 'Melasma': 'MΓ©lasma', 'Nodules': 'Nodules',
50
- 'Papules': 'Papules', 'Pustules': 'Pustules', 'Skin Redness': 'Rougeurs',
51
- 'Sun Spots': 'Taches solaires', 'Vascular': 'LΓ©sions vasculaires', 'Whitehead': 'Point blanc',
52
- 'Wrinkles': 'Rides'
53
- }
54
 
55
- skin_type_classes_en = {0: 'Dry Skin', 1: 'Normal Skin', 2: 'Oily Skin'}
56
- skin_type_classes_fr = {0: 'Peau sèche', 1: 'Peau normale', 2: 'Peau grasse'}
57
 
58
- # Initialize models
59
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
60
- print(f"Using device: {device}")
61
 
62
- # Load YOLO model
63
- print("Loading YOLO model...")
64
- model = YOLO(MODEL_PATH)
65
-
66
- # Load skin type model
67
- print("Loading skin type model...")
68
- skin_type_model = models.resnet50(weights=None)
69
- num_ftrs = skin_type_model.fc.in_features
70
- skin_type_model.fc = torch.nn.Linear(num_ftrs, 3)
71
-
72
- try:
73
- checkpoint = torch.load(SKIN_TYPE_MODEL_PATH, map_location=device)
74
- model_dict = skin_type_model.state_dict()
75
- pretrained_dict = {}
76
-
77
- for k, v in checkpoint.items():
78
- if k in model_dict and v.shape == model_dict[k].shape:
79
- pretrained_dict[k] = v
80
- elif k == 'fc.weight' and model_dict['fc.weight'].shape[1] == v.shape[1]:
81
- pretrained_dict['fc.weight'] = v[:3, :]
82
- elif k == 'fc.bias' and len(v) >= 3:
83
- pretrained_dict['fc.bias'] = v[:3]
84
-
85
- skin_type_model.load_state_dict(pretrained_dict, strict=False)
86
- print("Skin type model loaded successfully")
87
- except Exception as e:
88
- print(f"Error loading skin type model: {str(e)}")
89
-
90
- skin_type_model = skin_type_model.to(device)
91
- skin_type_model.eval()
92
-
93
- # Image transform for skin type model
94
- transform = transforms.Compose([
95
- transforms.ToPILImage(),
96
- transforms.Resize((224, 224)),
97
- transforms.ToTensor(),
98
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
99
- ])
100
-
101
- def optimize_image(img, max_size=MAX_IMAGE_SIZE):
102
- """Resize large images to improve processing speed"""
103
- h, w = img.shape[:2]
104
- if max(h, w) > max_size:
105
- scale = max_size / max(h, w)
106
- new_w, new_h = int(w * scale), int(h * scale)
107
- img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)
108
- return img
109
-
110
- def detect_skin(image):
111
- """Detect skin regions in the image using HSV color space"""
112
- hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
113
- lower_skin = np.array([0, 20, 70], dtype=np.uint8)
114
- upper_skin = np.array([20, 255, 255], dtype=np.uint8)
115
- mask = cv2.inRange(hsv, lower_skin, upper_skin)
116
- skin_pixels = cv2.countNonZero(mask)
117
- total_pixels = image.shape[0] * image.shape[1]
118
- skin_ratio = skin_pixels / total_pixels
119
- return skin_ratio > 0.05
120
-
121
- def analyze_skin_issues_with_timeout(detections, language='en', timeout=GROQ_TIMEOUT):
122
- """Analyze skin issues with timeout protection"""
123
- result = {}
124
- exception = None
125
-
126
- def worker():
127
- nonlocal exception
128
- try:
129
- result['analysis'] = analyze_skin_issues(detections, language)
130
- except Exception as e:
131
- exception = e
132
-
133
- thread = threading.Thread(target=worker)
134
- thread.start()
135
- thread.join(timeout)
136
-
137
- if thread.is_alive():
138
- print("Groq API call timed out")
139
- return get_default_analysis(language, timeout=True)
140
-
141
- if exception:
142
- print(f"Groq API error: {str(exception)}")
143
- return get_default_analysis(language)
144
-
145
- return result.get('analysis', get_default_analysis(language))
146
-
147
- def analyze_skin_issues(detections, language='en'):
148
- """Analyze skin issues using Groq LLM"""
149
- groq_api_key = os.environ.get("GROQ_API_KEY")
150
- if not groq_api_key:
151
- return get_default_analysis(language, reason="API key missing")
152
-
153
- client = Groq(api_key=groq_api_key)
154
-
155
- problems = []
156
- for detection in detections:
157
- problems.append(f"{detection['class_name']} (confidence: {detection['confidence']:.2f})")
158
-
159
- skin_issues = ", ".join(problems)
160
-
161
- if language == 'fr':
162
- prompt = f"""En tant que spécialiste en dermatologie, analyse les problèmes de peau suivants détectés par IA: {skin_issues}.
163
-
164
- RΓ©ponds exactement dans ce format JSON:
165
- {{
166
- "conseils": "Liste de conseils précis et pratiques pour traiter ces problèmes spécifiques de peau",
167
- "interpretation": "Explication claire des causes possibles et de la gravité des problèmes détectés",
168
- "routine": "Proposition d'une routine quotidienne adaptΓ©e pour amΓ©liorer l'Γ©tat de la peau. Termine toujours par la phrase: 'Consultez la section des produits recommandΓ©s ci-dessous pour voir les solutions adaptΓ©es Γ  vos besoins spΓ©cifiques.'"
169
- }}
170
-
171
- Ne mets rien d'autre dans ta rΓ©ponse que ce JSON correctement formatΓ©."""
172
- else:
173
- prompt = f"""As a dermatology specialist, analyze the following skin problems detected by AI: {skin_issues}.
174
-
175
- Respond exactly in this JSON format:
176
- {{
177
- "conseils": "List of precise and practical advice for treating these specific skin problems",
178
- "interpretation": "Clear explanation of possible causes and severity of the detected problems",
179
- "routine": "Proposal for a daily routine adapted to improve skin condition. Always end with the phrase: 'Check the recommended products section below to see solutions tailored to your specific needs.'"
180
- }}
181
-
182
- Put nothing else in your response except this properly formatted JSON."""
183
-
184
- start_time = time.time()
185
- chat_completion = client.chat.completions.create(
186
- messages=[{"role": "user", "content": prompt}],
187
- model="llama-3.3-70b-versatile",
188
- stream=False,
189
- )
190
- response_time = time.time() - start_time
191
- print(f"Groq API response time: {response_time:.2f}s")
192
-
193
- response_content = chat_completion.choices[0].message.content
194
-
195
- # Try to parse JSON from the response
196
- json_match = re.search(r'\{.*\}', response_content, re.DOTALL)
197
- if json_match:
198
- try:
199
- return json.loads(json_match.group(0))
200
- except json.JSONDecodeError:
201
- pass
202
-
203
- # Fallback parsing if JSON extraction fails
204
- conseils = "Consult a dermatologist for personalized advice."
205
- interpretation = "Professional analysis is recommended for these skin problems."
206
- routine = "Follow a basic routine: cleansing, moisturizing, and daily sun protection."
207
-
208
- if language == 'fr':
209
- conseils = "Consultez un dermatologue pour des conseils personnalisΓ©s."
210
- interpretation = "Une analyse professionnelle est recommandée pour ces problèmes de peau."
211
- routine = "Suivez une routine de base: nettoyage, hydratation et protection solaire quotidienne."
212
-
213
- return {
214
- "conseils": conseils,
215
- "interpretation": interpretation,
216
- "routine": routine
217
- }
218
-
219
- def get_default_analysis(language='en', timeout=False, reason=None):
220
- """Return default analysis when Groq API fails or times out"""
221
- if language == 'fr':
222
- if timeout:
223
- return {
224
- "conseils": "L'analyse prend plus de temps que prΓ©vu. Veuillez rΓ©essayer plus tard.",
225
- "interpretation": "Le système d'analyse IA est temporairement occupé.",
226
- "routine": "Routine standard: nettoyer doucement, hydrater et protΓ©ger du soleil."
227
- }
228
- return {
229
- "conseils": "Consultez un dermatologue pour des conseils personnalisΓ©s.",
230
- "interpretation": "Une analyse professionnelle est recommandée pour ces problèmes de peau.",
231
- "routine": "Suivez une routine de base: nettoyage, hydratation et protection solaire quotidienne."
232
- }
233
- else:
234
- if timeout:
235
- return {
236
- "conseils": "Analysis is taking longer than expected. Please try again later.",
237
- "interpretation": "The AI analysis system is temporarily busy.",
238
- "routine": "Standard routine: gentle cleansing, moisturizing, and sun protection."
239
- }
240
- return {
241
- "conseils": "Consult a dermatologist for personalized advice.",
242
- "interpretation": "Professional analysis is recommended for these skin problems.",
243
- "routine": "Follow a basic routine: cleansing, moisturizing, and daily sun protection."
244
- }
245
-
246
- def warm_up_models():
247
- """Pre-load models and run dummy inference to reduce cold start time"""
248
- print("Warming up models...")
249
- dummy_img = np.zeros((224, 224, 3), dtype=np.uint8)
250
-
251
- # Warm up YOLO model
252
- start_time = time.time()
253
- model(dummy_img, conf=0.15, iou=0.2)
254
- print(f"YOLO warm-up time: {time.time() - start_time:.2f}s")
255
-
256
- # Warm up skin type model
257
- start_time = time.time()
258
- dummy_tensor = transform(dummy_img).unsqueeze(0).to(device)
259
- with torch.no_grad():
260
- skin_type_model(dummy_tensor)
261
- print(f"Skin type model warm-up time: {time.time() - start_time:.2f}s")
262
- print("Models warmed up and ready")
263
-
264
- # Call the warm-up function immediately after model loading
265
  with app.app_context():
266
- warm_up_models()
267
-
268
- # @app.before_request
269
- # def check_rapidapi_key():
270
- # if request.path.startswith('/predict'):
271
- # rapidapi_key = (
272
- # request.headers.get('X-RapidAPI-Key')
273
- # or request.headers.get('x-rapidapi-key')
274
- # or request.environ.get('HTTP_X_RAPIDAPI_KEY')
275
- # )
276
-
277
- # if not rapidapi_key:
278
- # return jsonify({
279
- # 'error': 'Missing X-RapidAPI-Key header',
280
- # 'received_headers': dict(request.headers)
281
- # }), 401
282
-
283
-
284
- @app.route('/predict', methods=['POST'])
285
- def predict():
286
- """Main prediction endpoint with timeout protection"""
287
- try:
288
- start_time = time.time()
289
- print("Processing new request")
290
-
291
- # Get language parameter, default to English
292
- language = request.args.get('lang', 'en').lower()
293
- if language not in ['en', 'fr']:
294
- language = 'en'
295
-
296
- class_translations = class_translations_fr if language == 'fr' else class_translations_en
297
- skin_type_classes = skin_type_classes_fr if language == 'fr' else skin_type_classes_en
298
-
299
- # Validate file upload
300
- if 'file' not in request.files:
301
- return jsonify({'error': 'No file found'}), 400
302
-
303
- file = request.files['file']
304
- if file.filename == '':
305
- return jsonify({'error': 'No file selected'}), 400
306
-
307
- # Check file size
308
- file_bytes = file.read()
309
- if len(file_bytes) > MAX_FILE_SIZE:
310
- return jsonify({'error': f'File too large. Maximum size is {MAX_FILE_SIZE//1024//1024}MB'}), 400
311
-
312
- # Decode image
313
- nparr = np.frombuffer(file_bytes, np.uint8)
314
- img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
315
- if img is None:
316
- return jsonify({'error': 'Invalid image format'}), 400
317
-
318
- # Optimize image size
319
- original_shape = img.shape
320
- img = optimize_image(img, MAX_IMAGE_SIZE)
321
- new_shape = img.shape
322
- if original_shape != new_shape:
323
- print(f"Resized image from {original_shape} to {new_shape}")
324
-
325
- # Check for skin in image
326
- if not detect_skin(img):
327
- return jsonify({'error': 'No skin detected in the image'}), 400
328
-
329
- # Use a queue to handle the result with timeout
330
- result_queue = queue.Queue()
331
-
332
- def process_request():
333
- try:
334
- # Run inference in parallel threads
335
- detections = []
336
- skin_type_result = {}
337
- errors = {}
338
-
339
- def run_yolo():
340
- nonlocal detections
341
- try:
342
- start = time.time()
343
- results = model(img, conf=0.15, iou=0.2)[0]
344
- for box in results.boxes:
345
- x1, y1, x2, y2 = box.xyxy[0].cpu().numpy().astype(int)
346
- confidence = float(box.conf[0])
347
- class_id = int(box.cls[0])
348
- class_name = results.names[class_id]
349
- translated_name = class_translations.get(class_name, class_name)
350
-
351
- detections.append({
352
- 'class_id': class_id,
353
- 'class_name': translated_name,
354
- 'confidence': confidence,
355
- 'bbox': [int(x1), int(y1), int(x2), int(y2)]
356
- })
357
- print(f"YOLO inference time: {time.time() - start:.2f}s, found {len(detections)} issues")
358
- except Exception as e:
359
- errors['yolo'] = str(e)
360
- print(f"YOLO error: {str(e)}")
361
-
362
- def run_skin_type():
363
- nonlocal skin_type_result
364
- try:
365
- start = time.time()
366
- pil_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
367
- img_tensor = transform(np.array(pil_image)).unsqueeze(0).to(device)
368
-
369
- with torch.no_grad():
370
- output = skin_type_model(img_tensor)
371
- probabilities = torch.nn.functional.softmax(output, dim=1)[0]
372
- predicted_class = output.argmax(1).item()
373
-
374
- skin_type_result = {
375
- 'skin_type': skin_type_classes[predicted_class],
376
- 'probabilities': {
377
- 'dry': float(probabilities[0] * 100),
378
- 'normal': float(probabilities[1] * 100),
379
- 'oily': float(probabilities[2] * 100)
380
- }
381
- }
382
- print(f"Skin type inference time: {time.time() - start:.2f}s")
383
- except Exception as e:
384
- errors['skin_type'] = str(e)
385
- print(f"Skin type model error: {str(e)}")
386
-
387
- # Start threads
388
- yolo_thread = threading.Thread(target=run_yolo)
389
- skin_thread = threading.Thread(target=run_skin_type)
390
-
391
- yolo_thread.start()
392
- skin_thread.start()
393
-
394
- # Wait for threads with timeout
395
- yolo_thread.join(timeout=15.0)
396
- skin_thread.join(timeout=15.0)
397
-
398
- # Check if threads completed successfully
399
- if 'yolo' in errors or len(detections) == 0:
400
- print("YOLO inference failed or timed out")
401
- detections = [] # Reset to empty list
402
-
403
- if 'skin_type' in errors or not skin_type_result:
404
- print("Skin type inference failed or timed out")
405
- skin_type_result = {
406
- 'skin_type': skin_type_classes[1], # Default to normal skin
407
- 'probabilities': {'dry': 0.0, 'normal': 100.0, 'oily': 0.0}
408
- }
409
-
410
- # Get AI analysis with timeout protection
411
- ai_analysis_start = time.time()
412
- ai_analysis = analyze_skin_issues_with_timeout(detections, language)
413
- print(f"AI analysis time: {time.time() - ai_analysis_start:.2f}s")
414
-
415
- # Prepare response
416
- response_data = {
417
- 'success': True,
418
- 'detections': detections,
419
- 'ai_analysis': ai_analysis,
420
- 'skin_type': skin_type_result['skin_type'],
421
- 'skin_type_probabilities': skin_type_result['probabilities'],
422
- 'processing_time': round(time.time() - start_time, 2),
423
- 'image_size': {'width': new_shape[1], 'height': new_shape[0]},
424
- 'disclaimer': "This analysis is not a medical diagnosis. For persistent issues, consult a dermatologist."
425
- }
426
-
427
- result_queue.put(('success', response_data))
428
- except Exception as e:
429
- result_queue.put(('error', str(e)))
430
-
431
- # Start processing in a separate thread
432
- processing_thread = threading.Thread(target=process_request)
433
- processing_thread.start()
434
-
435
- # Wait for the result with timeout
436
- try:
437
- status, result = result_queue.get(timeout=REQUEST_TIMEOUT)
438
- if status == 'success':
439
- total_time = time.time() - start_time
440
- print(f"Total processing time: {total_time:.2f}s")
441
- return jsonify(result)
442
- else:
443
- print(f"Processing error: {result}")
444
- return jsonify({'error': 'Internal processing error', 'message': result}), 500
445
- except queue.Empty:
446
- # Timeout occurred
447
- print(f"Request timed out after {REQUEST_TIMEOUT} seconds")
448
- return jsonify({
449
- 'error': 'Request timed out. Please try a smaller image or try again later.',
450
- 'suggestion': 'Try uploading a smaller image (under 1MB) for faster processing'
451
- }), 504
452
-
453
- except Exception as e:
454
- print(f"Unexpected error: {str(e)}")
455
- return jsonify({
456
- 'error': 'Internal server error',
457
- 'message': str(e)
458
- }), 500
459
-
460
- @app.route('/ping', methods=['GET'])
461
- def ping():
462
- """Health check endpoint that returns simple status"""
463
- return jsonify({
464
- 'status': 'healthy',
465
- 'message': 'Skin Analysis API is running',
466
- 'timestamp': time.time(),
467
- 'version': '1.0.0',
468
- 'uptime': 'N/A' # You can calculate uptime if needed
469
- }), 200
470
-
471
- @app.route('/health', methods=['GET'])
472
- def health():
473
- """Health check endpoint"""
474
- return jsonify({
475
- 'status': 'API online',
476
- 'timestamp': time.time(),
477
- 'device': str(device),
478
- 'model_status': 'loaded',
479
- 'disclaimer': "This is a non-medical analysis tool. Consult a dermatologist for medical advice."
480
- })
481
 
482
  if __name__ == '__main__':
483
- Port = os.environ.get("Port")
484
- app.run(debug=False, host='0.0.0.0', port=Port)
 
1
  import os
2
+ from flask import Flask
3
  from flask_cors import CORS
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ from models.loader import warm_up
6
+ from routes.predict import predict_bp
7
+ from routes.health import health_bp
8
 
 
 
9
 
10
+ def create_app() -> Flask:
11
+ app = Flask(__name__)
12
+ CORS(app, resources={r"/*": {"origins": "*"}})
 
 
 
 
13
 
14
+ app.register_blueprint(predict_bp)
15
+ app.register_blueprint(health_bp)
 
 
 
 
 
 
 
 
 
 
16
 
17
+ return app
 
 
 
 
 
 
 
 
 
 
18
 
 
 
19
 
20
+ app = create_app()
 
 
21
 
22
+ # Warm up models once at startup (eliminates cold-start latency on first request)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  with app.app_context():
24
+ warm_up()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  if __name__ == '__main__':
27
+ port = int(os.environ.get("PORT", 7860))
28
+ app.run(debug=False, host='0.0.0.0', port=port)
config/settings.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # config/settings.py
2
+
3
+ # ── Paths ──────────────────────────────────────────────────────────────────────
4
+ MODEL_PATH = 'model_moncef.pt'
5
+ SKIN_TYPE_MODEL_PATH = 'skin_type_classification85.pth'
6
+
7
+ # ── Limits ─────────────────────────────────────────────────────────────────────
8
+ MAX_IMAGE_SIZE = 640 # Reduced from 1024 β€” YOLO works great at 640
9
+ MAX_FILE_SIZE = 10 * 1024 * 1024 # 10 MB
10
+
11
+ # ── Timeouts (seconds) ─────────────────────────────────────────────────────────
12
+ GROQ_TIMEOUT = 4.0 # Fail fast, return default sooner
13
+ REQUEST_TIMEOUT = 30.0
14
+
15
+ # ── Groq ───────────────────────────────────────────────────────────────────────
16
+ GROQ_MODEL = "llama-3.1-8b-instant" # Much faster than 70b
17
+
18
+ # ── Analysis response cache ────────────────────────────────────────────────────
19
+ MAX_CACHE_SIZE = 100
20
+
21
+ # ── Translation: YOLO class β†’ display name ────────────────────────────────────
22
+ CLASS_TRANSLATIONS_EN = {
23
+ 'acne': 'Acne', 'blackheads': 'Blackheads', 'dark circles': 'Dark Circles',
24
+ 'dark spot': 'Dark Spot', 'eyebags': 'Eye Bags', 'redness': 'Redness',
25
+ 'whiteheads': 'Whiteheads', 'wrinkles': 'Wrinkles', 'Acne': 'Acne',
26
+ 'Acne Scar': 'Acne Scar', 'Blackhead': 'Blackhead', 'Dark Circles': 'Dark Circles',
27
+ 'Dark Spots': 'Dark Spots', 'Enlarged Pores': 'Enlarged Pores', 'Eyebags': 'Eye Bags',
28
+ 'Freckle': 'Freckle', 'Melasma': 'Melasma', 'Nodules': 'Nodules',
29
+ 'Papules': 'Papules', 'Pustules': 'Pustules', 'Skin Redness': 'Skin Redness',
30
+ 'Sun Spots': 'Sun Spots', 'Vascular': 'Vascular Lesions', 'Whitehead': 'Whitehead',
31
+ 'Wrinkles': 'Wrinkles',
32
+ }
33
+
34
+ CLASS_TRANSLATIONS_FR = {
35
+ 'acne': 'AcnΓ©', 'blackheads': 'Points noirs', 'dark circles': 'Cernes',
36
+ 'dark spot': 'Tache brune', 'eyebags': 'Poches sous les yeux', 'redness': 'Rougeurs',
37
+ 'whiteheads': 'Points blancs', 'wrinkles': 'Rides', 'Acne': 'AcnΓ©',
38
+ 'Acne Scar': "Cicatrice d'acnΓ©", 'Blackhead': 'Point noir', 'Dark Circles': 'Cernes',
39
+ 'Dark Spots': 'Taches brunes', 'Enlarged Pores': 'Pores dilatΓ©s',
40
+ 'Eyebags': 'Poches sous les yeux', 'Freckle': 'Tache de rousseur',
41
+ 'Melasma': 'MΓ©lasma', 'Nodules': 'Nodules', 'Papules': 'Papules',
42
+ 'Pustules': 'Pustules', 'Skin Redness': 'Rougeurs', 'Sun Spots': 'Taches solaires',
43
+ 'Vascular': 'LΓ©sions vasculaires', 'Whitehead': 'Point blanc', 'Wrinkles': 'Rides',
44
+ }
45
+
46
+ SKIN_TYPE_CLASSES_EN = {0: 'Dry Skin', 1: 'Normal Skin', 2: 'Oily Skin'}
47
+ SKIN_TYPE_CLASSES_FR = {0: 'Peau sèche', 1: 'Peau normale', 2: 'Peau grasse'}
models/loader.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # models/loader.py
2
+ """
3
+ Loads and warms up both ML models at startup.
4
+ Imported once by app.py β€” models are module-level singletons.
5
+ """
6
+
7
+ import time
8
+ import numpy as np
9
+ import torch
10
+ from torchvision import transforms, models as tv_models
11
+ from ultralytics import YOLO
12
+
13
+ from config.settings import MODEL_PATH, SKIN_TYPE_MODEL_PATH
14
+
15
+ # ── Device ─────────────────────────────────────────────────────────────────────
16
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
17
+ print(f"[loader] Using device: {device}")
18
+
19
+ # ── YOLO ───────────────────────────────────────────────────────────────────────
20
+ print("[loader] Loading YOLO model…")
21
+ yolo_model = YOLO(MODEL_PATH)
22
+
23
+ # ── Skin-type ResNet-50 ────────────────────────────────────────────────────────
24
+ print("[loader] Loading skin-type model…")
25
+ skin_type_model = tv_models.resnet50(weights=None)
26
+ num_ftrs = skin_type_model.fc.in_features
27
+ skin_type_model.fc = torch.nn.Linear(num_ftrs, 3)
28
+
29
+ try:
30
+ checkpoint = torch.load(SKIN_TYPE_MODEL_PATH, map_location=device)
31
+ model_dict = skin_type_model.state_dict()
32
+ pretrained = {}
33
+ for k, v in checkpoint.items():
34
+ if k in model_dict and v.shape == model_dict[k].shape:
35
+ pretrained[k] = v
36
+ elif k == 'fc.weight' and model_dict['fc.weight'].shape[1] == v.shape[1]:
37
+ pretrained['fc.weight'] = v[:3, :]
38
+ elif k == 'fc.bias' and len(v) >= 3:
39
+ pretrained['fc.bias'] = v[:3]
40
+ skin_type_model.load_state_dict(pretrained, strict=False)
41
+ print("[loader] Skin-type model loaded βœ“")
42
+ except Exception as exc:
43
+ print(f"[loader] WARNING β€” skin-type model load error: {exc}")
44
+
45
+ skin_type_model = skin_type_model.to(device)
46
+ skin_type_model.eval()
47
+
48
+ # ── Image transform ────────────────────────────────────────────────────────────
49
+ skin_transform = transforms.Compose([
50
+ transforms.ToPILImage(),
51
+ transforms.Resize((224, 224)),
52
+ transforms.ToTensor(),
53
+ transforms.Normalize(mean=[0.485, 0.456, 0.406],
54
+ std=[0.229, 0.224, 0.225]),
55
+ ])
56
+
57
+
58
+ def warm_up():
59
+ """Run a dummy forward pass on both models to eliminate first-request latency."""
60
+ print("[loader] Warming up models…")
61
+ dummy = np.zeros((224, 224, 3), dtype=np.uint8)
62
+
63
+ t = time.time()
64
+ yolo_model(dummy, conf=0.15, iou=0.2)
65
+ print(f"[loader] YOLO warm-up: {time.time()-t:.2f}s")
66
+
67
+ t = time.time()
68
+ tensor = skin_transform(dummy).unsqueeze(0).to(device)
69
+ with torch.no_grad():
70
+ skin_type_model(tensor)
71
+ print(f"[loader] Skin-type warm-up: {time.time()-t:.2f}s")
72
+
73
+ print("[loader] Models ready βœ“")
routes/health.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # routes/health.py
2
+ """
3
+ GET /ping β€” lightweight keep-alive (used by GitHub Actions cron)
4
+ GET /health β€” verbose status for monitoring dashboards
5
+ """
6
+
7
+ import time
8
+ from flask import Blueprint, jsonify
9
+ from models.loader import device
10
+
11
+ health_bp = Blueprint('health', __name__)
12
+
13
+
14
+ @health_bp.route('/ping', methods=['GET'])
15
+ def ping():
16
+ return jsonify({
17
+ 'status': 'healthy',
18
+ 'message': 'Skin Analysis API is running',
19
+ 'timestamp': time.time(),
20
+ 'version': '2.0.0',
21
+ }), 200
22
+
23
+
24
+ @health_bp.route('/health', methods=['GET'])
25
+ def health():
26
+ return jsonify({
27
+ 'status': 'API online',
28
+ 'timestamp': time.time(),
29
+ 'device': str(device),
30
+ 'model_status': 'loaded',
31
+ 'disclaimer': (
32
+ "This is a non-medical analysis tool. "
33
+ "Consult a dermatologist for medical advice."
34
+ ),
35
+ }), 200
routes/predict.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # routes/predict.py
2
+ """
3
+ POST /predict?lang=en|fr
4
+ Accepts multipart/form-data with a 'file' field (JPEG / PNG / WEBP).
5
+ """
6
+
7
+ import queue
8
+ import time
9
+ import threading
10
+
11
+ import cv2
12
+ import numpy as np
13
+ from flask import Blueprint, request, jsonify
14
+
15
+ from config.settings import (
16
+ MAX_FILE_SIZE, REQUEST_TIMEOUT,
17
+ CLASS_TRANSLATIONS_EN, CLASS_TRANSLATIONS_FR,
18
+ SKIN_TYPE_CLASSES_EN, SKIN_TYPE_CLASSES_FR,
19
+ )
20
+ from services.image_utils import optimize_image, detect_skin
21
+ from services.inference import run_inference
22
+ from services.analysis import analyze_skin_issues
23
+
24
+ predict_bp = Blueprint('predict', __name__)
25
+
26
+
27
+ @predict_bp.route('/predict', methods=['POST'])
28
+ def predict():
29
+ start_time = time.time()
30
+ print("[predict] New request received")
31
+
32
+ # ── Language ───────────────────────────────────────────────────────────────
33
+ language = request.args.get('lang', 'en').lower()
34
+ if language not in ('en', 'fr'):
35
+ language = 'en'
36
+
37
+ class_translations = CLASS_TRANSLATIONS_FR if language == 'fr' else CLASS_TRANSLATIONS_EN
38
+ skin_type_classes = SKIN_TYPE_CLASSES_FR if language == 'fr' else SKIN_TYPE_CLASSES_EN
39
+
40
+ # ── File validation ────────────────────────────────────────────────────────
41
+ if 'file' not in request.files:
42
+ return jsonify({'error': 'No file found'}), 400
43
+
44
+ file = request.files['file']
45
+ if file.filename == '':
46
+ return jsonify({'error': 'No file selected'}), 400
47
+
48
+ file_bytes = file.read()
49
+ if len(file_bytes) > MAX_FILE_SIZE:
50
+ return jsonify({'error': f'File too large. Max {MAX_FILE_SIZE // 1024 // 1024} MB'}), 400
51
+
52
+ # ── Decode & resize ────────────────────────────────────────────────────────
53
+ nparr = np.frombuffer(file_bytes, np.uint8)
54
+ img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
55
+ if img is None:
56
+ return jsonify({'error': 'Invalid image format'}), 400
57
+
58
+ original_shape = img.shape
59
+ img = optimize_image(img)
60
+ new_shape = img.shape
61
+ if original_shape != new_shape:
62
+ print(f"[predict] Resized {original_shape} β†’ {new_shape}")
63
+
64
+ # ── Skin check ─────────────────────────────────────────────────────────────
65
+ if not detect_skin(img):
66
+ return jsonify({'error': 'No skin detected in the image'}), 400
67
+
68
+ # ── Main processing (with overall timeout) ─────────────────────────────────
69
+ result_queue: queue.Queue = queue.Queue()
70
+
71
+ def process():
72
+ try:
73
+ detections, skin_type_result = run_inference(
74
+ img, class_translations, skin_type_classes
75
+ )
76
+ ai_t0 = time.time()
77
+ ai_analysis = analyze_skin_issues(detections, language)
78
+ print(f"[predict] AI analysis: {time.time()-ai_t0:.2f}s")
79
+
80
+ result_queue.put(('ok', {
81
+ 'success': True,
82
+ 'detections': detections,
83
+ 'ai_analysis': ai_analysis,
84
+ 'skin_type': skin_type_result['skin_type'],
85
+ 'skin_type_probabilities': skin_type_result['probabilities'],
86
+ 'processing_time': round(time.time() - start_time, 2),
87
+ 'image_size': {'width': new_shape[1], 'height': new_shape[0]},
88
+ 'disclaimer': (
89
+ "This analysis is not a medical diagnosis. "
90
+ "For persistent issues, consult a dermatologist."
91
+ ),
92
+ }))
93
+ except Exception as exc:
94
+ result_queue.put(('err', str(exc)))
95
+
96
+ threading.Thread(target=process, daemon=True).start()
97
+
98
+ try:
99
+ status, payload = result_queue.get(timeout=REQUEST_TIMEOUT)
100
+ if status == 'ok':
101
+ print(f"[predict] Total: {time.time()-start_time:.2f}s")
102
+ return jsonify(payload)
103
+ print(f"[predict] Processing error: {payload}")
104
+ return jsonify({'error': 'Internal processing error', 'message': payload}), 500
105
+
106
+ except queue.Empty:
107
+ print(f"[predict] Timed out after {REQUEST_TIMEOUT}s")
108
+ return jsonify({
109
+ 'error': 'Request timed out. Try a smaller image or retry later.',
110
+ 'suggestion': 'Upload an image under 1 MB for faster processing.',
111
+ }), 504
services/analysis.py ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # services/analysis.py
2
+ """
3
+ Groq LLM analysis with:
4
+ β€’ singleton Groq client (created once, reused across all requests)
5
+ β€’ deduplication of detections before sending to Groq (fewer tokens β†’ faster)
6
+ β€’ in-memory response cache (identical detections β†’ instant reply)
7
+ β€’ hard timeout so slow API calls never block the endpoint
8
+ β€’ strict 3-key output: conseils, interpretation, routine
9
+ """
10
+
11
+ import os
12
+ import re
13
+ import json
14
+ import time
15
+ import hashlib
16
+ import threading
17
+ from collections import defaultdict
18
+ from typing import Optional
19
+
20
+ from groq import Groq
21
+
22
+ from config.settings import GROQ_TIMEOUT, GROQ_MODEL, MAX_CACHE_SIZE
23
+
24
+ # ── Singleton Groq client ──────────────────────────────────────────────────────
25
+ # Created once on first use and reused across all requests.
26
+ # Avoids the overhead of building a new HTTP client per request.
27
+ _groq_client: Optional[Groq] = None
28
+ _groq_client_lock = threading.Lock()
29
+
30
+ def _get_client() -> Optional[Groq]:
31
+ global _groq_client
32
+ if _groq_client is not None:
33
+ return _groq_client
34
+ with _groq_client_lock:
35
+ # Double-check inside the lock (another thread may have initialized it)
36
+ if _groq_client is None:
37
+ api_key = os.environ.get("GROQ_API_KEY")
38
+ if not api_key:
39
+ return None
40
+ _groq_client = Groq(api_key=api_key)
41
+ print("[analysis] Groq client initialized βœ“")
42
+ return _groq_client
43
+
44
+
45
+ # ── Simple in-memory cache ─────────────────────────────────────────────────────
46
+ _cache: dict[str, dict] = {}
47
+ _cache_lock = threading.Lock()
48
+
49
+
50
+ def _cache_key(detections: list, language: str) -> str:
51
+ issues = sorted(d['class_name'] for d in detections)
52
+ return hashlib.md5(f"{','.join(issues)}:{language}".encode()).hexdigest()
53
+
54
+
55
+ def _store(key: str, value: dict) -> None:
56
+ with _cache_lock:
57
+ if len(_cache) >= MAX_CACHE_SIZE:
58
+ oldest = next(iter(_cache))
59
+ del _cache[oldest]
60
+ _cache[key] = value
61
+
62
+
63
+ # ── Deduplication ──────────────────────────────────────────────────────────────
64
+ def _deduplicate(detections: list) -> list:
65
+ """
66
+ Collapse multiple detections of the same class into one entry,
67
+ keeping only the highest confidence score per class.
68
+
69
+ Example: 8x 'Acne Scar' detections β†’ 1x 'Acne Scar' (highest confidence)
70
+ This significantly reduces Groq prompt size and speeds up the API call.
71
+ """
72
+ best: dict[str, float] = {}
73
+ for d in detections:
74
+ name = d['class_name']
75
+ if name not in best or d['confidence'] > best[name]:
76
+ best[name] = d['confidence']
77
+
78
+ # Return sorted by confidence descending so most prominent issues come first
79
+ return [
80
+ {'class_name': name, 'confidence': conf}
81
+ for name, conf in sorted(best.items(), key=lambda x: -x[1])
82
+ ]
83
+
84
+
85
+ # ── Default fallback ───────────────────────────────────────────────────────────
86
+ def get_default_analysis(language: str = 'en', timeout: bool = False) -> dict:
87
+ if language == 'fr':
88
+ if timeout:
89
+ return {
90
+ "conseils": "L'analyse prend plus de temps que prΓ©vu. Veuillez rΓ©essayer.",
91
+ "interpretation": "Le système d'analyse IA est temporairement occupé.",
92
+ "routine": "Routine standard: nettoyage doux, hydratation et protection solaire quotidienne. Consultez la section des produits recommandΓ©s ci-dessous pour voir les solutions adaptΓ©es Γ  vos besoins spΓ©cifiques.",
93
+ }
94
+ return {
95
+ "conseils": "Consultez un dermatologue pour des conseils personnalisΓ©s.",
96
+ "interpretation": "Une analyse professionnelle est recommandée pour ces problèmes de peau.",
97
+ "routine": "Nettoyage doux matin et soir, hydratation quotidienne et protection solaire. Consultez la section des produits recommandΓ©s ci-dessous pour voir les solutions adaptΓ©es Γ  vos besoins spΓ©cifiques.",
98
+ }
99
+ if timeout:
100
+ return {
101
+ "conseils": "Analysis is taking longer than expected. Please try again.",
102
+ "interpretation": "The AI analysis system is temporarily busy.",
103
+ "routine": "Standard routine: gentle cleansing, moisturising, and daily sun protection. Check the recommended products section below to see solutions tailored to your specific needs.",
104
+ }
105
+ return {
106
+ "conseils": "Consult a dermatologist for personalised advice.",
107
+ "interpretation": "Professional analysis is recommended for these skin problems.",
108
+ "routine": "Follow a basic routine: cleansing, moisturising, and daily sun protection. Check the recommended products section below to see solutions tailored to your specific needs.",
109
+ }
110
+
111
+
112
+ # ── Placeholder guard ──────────────────────────────────────────────────────────
113
+ _PLACEHOLDERS = {
114
+ "conseils courts et pratiques", "brief practical advice",
115
+ "causes et gravité brèves", "brief causes and severity",
116
+ "routine quotidienne courte", "brief daily routine",
117
+ "[tes conseils ici]", "[ton interpretation ici]", "[ta routine ici]",
118
+ "[your advice here]", "[your interpretation here]", "[your routine here]",
119
+ }
120
+
121
+ def _is_placeholder(value: str) -> bool:
122
+ return value.strip().lower() in _PLACEHOLDERS or len(value.strip()) < 20
123
+
124
+
125
+ # ── Strip any extra keys the model may add ────────────────────────────────────
126
+ def _extract_three_keys(parsed: dict) -> dict:
127
+ """Keep only the 3 required keys. Handle alternate key names the model might use."""
128
+ aliases = {
129
+ 'conseils': ['conseils', 'advice', 'tips', 'recommandations'],
130
+ 'interpretation': ['interpretation', 'analysis', 'analyse'],
131
+ 'routine': ['routine', 'daily_routine', 'skincare_routine'],
132
+ }
133
+ result = {}
134
+ for canonical, names in aliases.items():
135
+ for name in names:
136
+ if name in parsed:
137
+ result[canonical] = parsed[name]
138
+ break
139
+ return result
140
+
141
+
142
+ # ── Prompt builder ─────────────────────────────────────────────────────────────
143
+ def _build_prompt(skin_issues: str, language: str) -> str:
144
+ """
145
+ Instruction-style prompt with a filled-in example so the model understands
146
+ it must generate real content β€” not echo placeholder strings.
147
+ Enforces exactly 3 keys.
148
+ """
149
+ if language == 'fr':
150
+ return (
151
+ "Tu es un dermatologue expert. Problèmes de peau détectés par IA chez un patient:\n"
152
+ f"{skin_issues}\n\n"
153
+ "RΓ©dige une analyse dermatologique professionnelle.\n"
154
+ "Réponds UNIQUEMENT avec un objet JSON valide. Pas de texte avant ni après. "
155
+ "Exactement 3 clΓ©s, rien d'autre:\n\n"
156
+ '"conseils" : conseils pratiques et spécifiques pour traiter ces problèmes précis (2-4 phrases)\n'
157
+ '"interpretation" : causes probables, liens entre les problèmes et gravité globale (2-3 phrases)\n'
158
+ '"routine" : routine quotidienne complète adaptée à ces problèmes, '
159
+ 'terminer obligatoirement par la phrase exacte: '
160
+ '"Consultez la section des produits recommandΓ©s ci-dessous pour voir les solutions adaptΓ©es Γ  vos besoins spΓ©cifiques."\n\n'
161
+ "Exemple de format (remplace le contenu par ta vraie analyse):\n"
162
+ '{"conseils":"Utilisez un nettoyant doux contenant de l\'acide salicylique pour traiter les pores dilatés et les cicatrices d\'acné. Évitez de toucher votre visage pour prévenir l\'inflammation.","interpretation":"La présence de pores dilatés et de cicatrices indique une peau à tendance acnéique avec excès de sébum. La gravité est modérée et nécessite une routine adaptée.","routine":"Le matin, nettoyez avec un gel doux puis appliquez un sérum à la niacinamide et une crème solaire SPF 30. Le soir, utilisez un rétinol léger suivi d\'un hydratant. Exfoliez chimiquement 2 fois par semaine. Consultez la section des produits recommandés ci-dessous pour voir les solutions adaptées à vos besoins spécifiques."}'
163
+ )
164
+ else:
165
+ return (
166
+ "You are an expert dermatologist. AI-detected skin issues for a patient:\n"
167
+ f"{skin_issues}\n\n"
168
+ "Write a professional dermatological analysis.\n"
169
+ "Reply ONLY with a valid JSON object. No text before or after. "
170
+ "Exactly 3 keys, nothing else:\n\n"
171
+ '"conseils" : specific practical advice to treat these exact detected issues (2-4 sentences)\n'
172
+ '"interpretation" : probable causes, links between issues, and overall severity (2-3 sentences)\n'
173
+ '"routine" : complete daily skincare routine tailored to these issues, '
174
+ 'MUST end with the exact phrase: '
175
+ '"Check the recommended products section below to see solutions tailored to your specific needs."\n\n'
176
+ "Example format (replace content with your real analysis):\n"
177
+ '{"conseils":"Use a gentle salicylic acid cleanser twice daily to address enlarged pores and acne scars. Avoid picking at blemishes to prevent further scarring and inflammation.","interpretation":"The combination of enlarged pores and acne scars suggests an acne-prone, oily skin type with post-inflammatory changes. Overall severity is moderate and manageable with consistent skincare.","routine":"Morning: gentle cleanser, niacinamide serum, SPF 30 sunscreen. Evening: mild retinol followed by a lightweight moisturiser. Exfoliate chemically with AHA or BHA twice a week. Check the recommended products section below to see solutions tailored to your specific needs."}'
178
+ )
179
+
180
+
181
+ # ── Core Groq call ─────────────────────────────────────────────────────────────
182
+ def _call_groq(detections: list, language: str) -> dict:
183
+ client = _get_client()
184
+ if not client:
185
+ return get_default_analysis(language)
186
+
187
+ # Deduplicate before building the prompt β€” keeps token count minimal
188
+ deduped = _deduplicate(detections)
189
+ skin_issues = "\n".join(
190
+ f"- {d['class_name']} (confidence: {d['confidence']:.2f})"
191
+ for d in deduped
192
+ )
193
+ print(f"[analysis] Sending {len(deduped)} unique classes to Groq (was {len(detections)} detections)")
194
+
195
+ prompt = _build_prompt(skin_issues, language)
196
+
197
+ t0 = time.time()
198
+ completion = client.chat.completions.create(
199
+ messages=[{"role": "user", "content": prompt}],
200
+ model=GROQ_MODEL,
201
+ max_tokens=350,
202
+ stream=False,
203
+ )
204
+ print(f"[analysis] Groq response time: {time.time()-t0:.2f}s")
205
+
206
+ content = completion.choices[0].message.content
207
+ match = re.search(r'\{.*\}', content, re.DOTALL)
208
+ if match:
209
+ try:
210
+ parsed = json.loads(match.group(0))
211
+ cleaned = _extract_three_keys(parsed)
212
+
213
+ # Fallback: direct key grab if alias mapping missed them
214
+ if not cleaned:
215
+ cleaned = {k: parsed[k] for k in ('conseils', 'interpretation', 'routine') if k in parsed}
216
+
217
+ required = ('conseils', 'interpretation', 'routine')
218
+ if all(k in cleaned and not _is_placeholder(str(cleaned[k])) for k in required):
219
+ return {k: cleaned[k] for k in required}
220
+
221
+ print("[analysis] Groq returned incomplete or placeholder values β€” falling back")
222
+ except json.JSONDecodeError:
223
+ print("[analysis] JSON parse error β€” falling back")
224
+
225
+ return get_default_analysis(language)
226
+
227
+
228
+ # ── Public entry point (with cache + timeout) ──────────────────────────────────
229
+ def analyze_skin_issues(detections: list, language: str = 'en') -> dict:
230
+ """
231
+ Returns AI analysis dict with exactly 3 keys: conseils, interpretation, routine.
232
+ Hits cache first; falls back to default on timeout or error.
233
+ """
234
+ key = _cache_key(detections, language)
235
+
236
+ with _cache_lock:
237
+ if key in _cache:
238
+ print("[analysis] Cache hit βœ“")
239
+ return _cache[key]
240
+
241
+ result: dict = {}
242
+ exception: Optional[Exception] = None
243
+
244
+ def worker():
245
+ nonlocal exception
246
+ try:
247
+ result['data'] = _call_groq(detections, language)
248
+ except Exception as exc:
249
+ exception = exc
250
+
251
+ t = threading.Thread(target=worker, daemon=True)
252
+ t.start()
253
+ t.join(GROQ_TIMEOUT)
254
+
255
+ if t.is_alive():
256
+ print("[analysis] Groq timed out β€” returning default")
257
+ return get_default_analysis(language, timeout=True)
258
+
259
+ if exception:
260
+ print(f"[analysis] Groq error: {exception}")
261
+ return get_default_analysis(language)
262
+
263
+ analysis = result.get('data', get_default_analysis(language))
264
+ _store(key, analysis)
265
+ return analysis
services/image_utils.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # services/image_utils.py
2
+ """
3
+ Lightweight image helpers β€” no ML imports here.
4
+ """
5
+
6
+ import cv2
7
+ import numpy as np
8
+
9
+ from config.settings import MAX_IMAGE_SIZE
10
+
11
+
12
+ def optimize_image(img: np.ndarray, max_size: int = MAX_IMAGE_SIZE) -> np.ndarray:
13
+ """Downscale image so the longest side ≀ max_size."""
14
+ h, w = img.shape[:2]
15
+ if max(h, w) > max_size:
16
+ scale = max_size / max(h, w)
17
+ new_w = int(w * scale)
18
+ new_h = int(h * scale)
19
+ img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)
20
+ return img
21
+
22
+
23
+ def detect_skin(image: np.ndarray, threshold: float = 0.05) -> bool:
24
+ """Return True when β‰₯ threshold fraction of pixels look like skin (HSV heuristic)."""
25
+ hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
26
+ lower_skin = np.array([0, 20, 70], dtype=np.uint8)
27
+ upper_skin = np.array([20, 255, 255], dtype=np.uint8)
28
+ mask = cv2.inRange(hsv, lower_skin, upper_skin)
29
+ skin_ratio = cv2.countNonZero(mask) / (image.shape[0] * image.shape[1])
30
+ return skin_ratio > threshold
services/inference.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # services/inference.py
2
+ """
3
+ Runs YOLO detection and skin-type classification in parallel threads.
4
+ Returns (detections, skin_type_result).
5
+ """
6
+
7
+ import time
8
+ import threading
9
+ from typing import Optional
10
+
11
+ import cv2
12
+ import numpy as np
13
+ import torch
14
+ from PIL import Image
15
+
16
+ from models.loader import yolo_model, skin_type_model, skin_transform, device
17
+
18
+
19
+ def run_inference(
20
+ img: np.ndarray,
21
+ class_translations: dict,
22
+ skin_type_classes: dict,
23
+ yolo_timeout: float = 15.0,
24
+ ) -> tuple[list, dict]:
25
+ """
26
+ Parameters
27
+ ----------
28
+ img : BGR numpy image (already resized)
29
+ class_translations : lang-specific label map
30
+ skin_type_classes : lang-specific skin-type label map
31
+
32
+ Returns
33
+ -------
34
+ detections : list of detection dicts
35
+ skin_type_result : dict with skin_type + probabilities
36
+ """
37
+ detections: list = []
38
+ skin_type_result: dict = {}
39
+ errors: dict = {}
40
+
41
+ # ── YOLO thread ────────────────────────────────────────────────────────────
42
+ def run_yolo():
43
+ try:
44
+ t = time.time()
45
+ results = yolo_model(img, conf=0.15, iou=0.2)[0]
46
+ for box in results.boxes:
47
+ x1, y1, x2, y2 = box.xyxy[0].cpu().numpy().astype(int)
48
+ confidence = float(box.conf[0])
49
+ class_id = int(box.cls[0])
50
+ class_name = results.names[class_id]
51
+ translated = class_translations.get(class_name, class_name)
52
+ detections.append({
53
+ 'class_id': class_id,
54
+ 'class_name': translated,
55
+ 'confidence': confidence,
56
+ 'bbox': [int(x1), int(y1), int(x2), int(y2)],
57
+ })
58
+ print(f"[inference] YOLO: {time.time()-t:.2f}s β€” {len(detections)} detections")
59
+ except Exception as exc:
60
+ errors['yolo'] = str(exc)
61
+ print(f"[inference] YOLO error: {exc}")
62
+
63
+ # ── Skin-type thread ───────────────────────────────────────────────────────
64
+ def run_skin_type():
65
+ try:
66
+ t = time.time()
67
+ rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
68
+ tensor = skin_transform(rgb).unsqueeze(0).to(device)
69
+ with torch.no_grad():
70
+ output = skin_type_model(tensor)
71
+ probs = torch.nn.functional.softmax(output, dim=1)[0]
72
+ pred = output.argmax(1).item()
73
+ skin_type_result.update({
74
+ 'skin_type': skin_type_classes[pred],
75
+ 'probabilities': {
76
+ 'dry': float(probs[0] * 100),
77
+ 'normal': float(probs[1] * 100),
78
+ 'oily': float(probs[2] * 100),
79
+ },
80
+ })
81
+ print(f"[inference] Skin-type: {time.time()-t:.2f}s")
82
+ except Exception as exc:
83
+ errors['skin_type'] = str(exc)
84
+ print(f"[inference] Skin-type error: {exc}")
85
+
86
+ # ── Run in parallel ────────────────────────────────────────────────────────
87
+ yolo_thread = threading.Thread(target=run_yolo, daemon=True)
88
+ skin_thread = threading.Thread(target=run_skin_type, daemon=True)
89
+ yolo_thread.start()
90
+ skin_thread.start()
91
+ yolo_thread.join(timeout=yolo_timeout)
92
+ skin_thread.join(timeout=yolo_timeout)
93
+
94
+ # ── Fallbacks on failure ───────────────────────────────────────────────────
95
+ if 'yolo' in errors or not detections:
96
+ detections = []
97
+
98
+ if 'skin_type' in errors or not skin_type_result:
99
+ skin_type_result = {
100
+ 'skin_type': skin_type_classes[1], # default: Normal
101
+ 'probabilities': {'dry': 0.0, 'normal': 100.0, 'oily': 0.0},
102
+ }
103
+
104
+ return detections, skin_type_result