somoncef commited on
Commit
29154b3
·
verified ·
1 Parent(s): 6ce3ada

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +373 -362
app.py CHANGED
@@ -1,50 +1,33 @@
1
  import os
2
- import time
3
- import json
4
- import re
5
- import queue
6
- import hashlib
7
- import threading
8
- from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError as FuturesTimeoutError
9
- from functools import lru_cache
10
- from collections import OrderedDict
11
-
12
  import cv2
13
  import numpy as np
 
 
14
  import torch
15
  from torchvision import transforms, models
16
  from PIL import Image
17
- from flask import Flask, request, jsonify
18
- from flask_cors import CORS
19
- from ultralytics import YOLO
20
- from groq import Groq
21
 
22
  app = Flask(__name__)
 
 
23
  CORS(app, resources={r"/*": {"origins": "*"}})
24
 
25
- # ─────────────────────────────────────────────
26
  # Configuration
27
- # ─────────────────────────────────────────────
28
- MODEL_PATH = 'model_moncef.pt'
29
- SKIN_TYPE_MODEL_PATH = 'skin_type_classification85.pth'
30
- MAX_IMAGE_SIZE = 640 # reduced from 1024 – still fine for YOLO
31
- YOLO_INPUT_SIZE = 416 # YOLO inference size (imgsz param)
32
- GROQ_TIMEOUT = 8.0
33
- REQUEST_TIMEOUT = 25.0
34
- MAX_FILE_SIZE = 10 * 1024 * 1024 # 10 MB
35
-
36
- # LRU image-result cache (keyed by MD5 of raw bytes)
37
- CACHE_MAX_SIZE = 50 # keep last 50 unique image results in memory
38
- _cache_lock = threading.Lock()
39
- _result_cache: OrderedDict = OrderedDict() # { md5: (timestamp, result) }
40
- CACHE_TTL = 3600 # seconds – cached results expire after 1 hour
41
-
42
- # Shared thread pool – avoids per-request thread creation overhead
43
- _executor = ThreadPoolExecutor(max_workers=4)
44
 
45
- # ─────────────────────────────────────────────
46
  # Translation dictionaries
47
- # ─────────────────────────────────────────────
48
  class_translations_en = {
49
  'acne': 'Acne', 'blackheads': 'Blackheads', 'dark circles': 'Dark Circles',
50
  'dark spot': 'Dark Spot', 'eyebags': 'Eye Bags', 'redness': 'Redness',
@@ -56,418 +39,446 @@ class_translations_en = {
56
  'Sun Spots': 'Sun Spots', 'Vascular': 'Vascular Lesions', 'Whitehead': 'Whitehead',
57
  'Wrinkles': 'Wrinkles'
58
  }
 
59
  class_translations_fr = {
60
  'acne': 'Acné', 'blackheads': 'Points noirs', 'dark circles': 'Cernes',
61
  'dark spot': 'Tache brune', 'eyebags': 'Poches sous les yeux', 'redness': 'Rougeurs',
62
  'whiteheads': 'Points blancs', 'wrinkles': 'Rides', 'Acne': 'Acné',
63
- 'Acne Scar': "Cicatrice d'acné", 'Blackhead': 'Point noir', 'Dark Circles': 'Cernes',
64
  'Dark Spots': 'Taches brunes', 'Enlarged Pores': 'Pores dilatés', 'Eyebags': 'Poches sous les yeux',
65
  'Freckle': 'Tache de rousseur', 'Melasma': 'Mélasma', 'Nodules': 'Nodules',
66
  'Papules': 'Papules', 'Pustules': 'Pustules', 'Skin Redness': 'Rougeurs',
67
  'Sun Spots': 'Taches solaires', 'Vascular': 'Lésions vasculaires', 'Whitehead': 'Point blanc',
68
  'Wrinkles': 'Rides'
69
  }
70
- skin_type_classes_en = {0: 'Dry Skin', 1: 'Normal Skin', 2: 'Oily Skin'}
 
71
  skin_type_classes_fr = {0: 'Peau sèche', 1: 'Peau normale', 2: 'Peau grasse'}
72
 
73
- # ─────────────────────────────────────────────
74
- # Device & model setup
75
- # ─────────────────────────────────────────────
76
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
77
  print(f"Using device: {device}")
78
 
79
- # ── PyTorch CPU optimisations (ignored silently on GPU) ──────────────────────
80
- torch.set_num_threads(os.cpu_count() or 2) # use all available CPU threads
81
-
82
- # Try to set the best available quantisation engine – safely skipped if unsupported
83
- import platform
84
- _machine = platform.machine().lower()
85
- _engines = ['qnnpack'] if any(a in _machine for a in ('arm', 'aarch')) else ['fbgemm', 'qnnpack']
86
- for _eng in _engines:
87
- try:
88
- torch.backends.quantized.engine = _eng
89
- print(f"Quantisation engine set to: {_eng}")
90
- break
91
- except RuntimeError:
92
- continue
93
- else:
94
- print("No quantisation engine available – quantisation will be skipped")
95
-
96
- # ── YOLO ─────────────────────────────────────────────────────────────────────
97
  print("Loading YOLO model...")
98
- yolo_model = YOLO(MODEL_PATH)
99
- # Export/use ONNX runtime if available for faster CPU inference
100
- try:
101
- import onnxruntime # noqa – just checking availability
102
- onnx_path = MODEL_PATH.replace('.pt', '.onnx')
103
- if not os.path.exists(onnx_path):
104
- print("Exporting YOLO to ONNX for faster CPU inference...")
105
- yolo_model.export(format='onnx', imgsz=YOLO_INPUT_SIZE, simplify=True, opset=12)
106
- print(f"ONNX model saved to {onnx_path}")
107
- yolo_model = YOLO(onnx_path)
108
- print("Using ONNX YOLO model")
109
- except Exception as e:
110
- print(f"ONNX not available, using PyTorch YOLO: {e}")
111
 
112
- # ── Skin-type ResNet-50 ───────────────────────────────────────────────────────
113
  print("Loading skin type model...")
114
  skin_type_model = models.resnet50(weights=None)
115
  num_ftrs = skin_type_model.fc.in_features
116
  skin_type_model.fc = torch.nn.Linear(num_ftrs, 3)
117
 
118
  try:
119
- checkpoint = torch.load(SKIN_TYPE_MODEL_PATH, map_location=device)
120
- model_dict = skin_type_model.state_dict()
121
- pretrained = {}
 
122
  for k, v in checkpoint.items():
123
  if k in model_dict and v.shape == model_dict[k].shape:
124
- pretrained[k] = v
125
  elif k == 'fc.weight' and model_dict['fc.weight'].shape[1] == v.shape[1]:
126
- pretrained['fc.weight'] = v[:3, :]
127
  elif k == 'fc.bias' and len(v) >= 3:
128
- pretrained['fc.bias'] = v[:3]
129
- skin_type_model.load_state_dict(pretrained, strict=False)
130
- print("Skin type model loaded")
 
131
  except Exception as e:
132
- print(f"Error loading skin type model: {e}")
133
 
134
  skin_type_model = skin_type_model.to(device)
135
  skin_type_model.eval()
136
 
137
- # ── Static INT8 quantisation (CPU only) ──────────────────────────────────────
138
- if device.type == 'cpu':
139
- try:
140
- skin_type_model = torch.quantization.quantize_dynamic(
141
- skin_type_model, {torch.nn.Linear}, dtype=torch.qint8
142
- )
143
- print("Skin-type model quantised to INT8")
144
- except Exception as e:
145
- print(f"Quantisation skipped: {e}")
146
-
147
- # ── Image transform ───────────────────────────────────────────────────────────
148
- _transform = transforms.Compose([
149
  transforms.Resize((224, 224)),
150
  transforms.ToTensor(),
151
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
152
  ])
153
 
154
- # ─────────────────────────────────────────────
155
- # Helper utilities
156
- # ─────────────────────────────────────────────
157
-
158
- def _image_hash(raw_bytes: bytes) -> str:
159
- return hashlib.md5(raw_bytes).hexdigest()
160
-
161
-
162
- def _cache_get(key: str):
163
- with _cache_lock:
164
- entry = _result_cache.get(key)
165
- if entry is None:
166
- return None
167
- ts, value = entry
168
- if time.time() - ts > CACHE_TTL:
169
- del _result_cache[key]
170
- return None
171
- # Move to end (LRU)
172
- _result_cache.move_to_end(key)
173
- return value
174
-
175
-
176
- def _cache_set(key: str, value):
177
- with _cache_lock:
178
- if key in _result_cache:
179
- _result_cache.move_to_end(key)
180
- _result_cache[key] = (time.time(), value)
181
- while len(_result_cache) > CACHE_MAX_SIZE:
182
- _result_cache.popitem(last=False)
183
-
184
-
185
  def optimize_image(img, max_size=MAX_IMAGE_SIZE):
 
186
  h, w = img.shape[:2]
187
  if max(h, w) > max_size:
188
- scale = max_size / max(h, w)
189
- new_w = int(w * scale)
190
- new_h = int(h * scale)
191
- img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)
192
  return img
193
 
194
-
195
- def detect_skin(image) -> bool:
196
- """Fast skin detection using a small downscaled sample."""
197
- small = cv2.resize(image, (128, 128), interpolation=cv2.INTER_AREA)
198
- hsv = cv2.cvtColor(small, cv2.COLOR_BGR2HSV)
199
- mask = cv2.inRange(hsv,
200
- np.array([0, 20, 70], dtype=np.uint8),
201
- np.array([20, 255, 255], dtype=np.uint8))
202
- return cv2.countNonZero(mask) / (128 * 128) > 0.05
203
-
204
-
205
- # ─────────────────────────────────────────────
206
- # Groq AI analysis
207
- # ─────────────────────────────────────────────
208
-
209
- def get_default_analysis(language='en', timeout=False):
210
- if language == 'fr':
211
- if timeout:
212
- return {
213
- "conseils": "L'analyse prend plus de temps que prévu. Veuillez réessayer plus tard.",
214
- "interpretation": "Le système d'analyse IA est temporairement occupé.",
215
- "routine": "Routine standard: nettoyer doucement, hydrater et protéger du soleil."
216
- }
217
- return {
218
- "conseils": "Consultez un dermatologue pour des conseils personnalisés.",
219
- "interpretation": "Une analyse professionnelle est recommandée pour ces problèmes de peau.",
220
- "routine": "Suivez une routine de base: nettoyage, hydratation et protection solaire quotidienne."
221
- }
222
- if timeout:
223
- return {
224
- "conseils": "Analysis is taking longer than expected. Please try again later.",
225
- "interpretation": "The AI analysis system is temporarily busy.",
226
- "routine": "Standard routine: gentle cleansing, moisturizing, and sun protection."
227
- }
228
- return {
229
- "conseils": "Consult a dermatologist for personalized advice.",
230
- "interpretation": "Professional analysis is recommended for these skin problems.",
231
- "routine": "Follow a basic routine: cleansing, moisturizing, and daily sun protection."
232
- }
233
-
234
 
235
  def analyze_skin_issues(detections, language='en'):
236
- """Call Groq LLM. Runs in the shared thread pool."""
237
  groq_api_key = os.environ.get("GROQ_API_KEY")
238
  if not groq_api_key:
239
- return get_default_analysis(language)
240
-
241
  client = Groq(api_key=groq_api_key)
242
- problems = [f"{d['class_name']} (confidence: {d['confidence']:.2f})" for d in detections]
 
 
 
 
243
  skin_issues = ", ".join(problems)
244
-
245
  if language == 'fr':
246
- prompt = (
247
- f"En tant que spécialiste en dermatologie, analyse les problèmes de peau suivants détectés par IA: {skin_issues}.\n\n"
248
- "Réponds exactement dans ce format JSON:\n"
249
- '{\n'
250
- ' "conseils": "Liste de conseils précis et pratiques pour traiter ces problèmes spécifiques de peau",\n'
251
- ' "interpretation": "Explication claire des causes possibles et de la gravité des problèmes détectés",\n'
252
- ' "routine": "Proposition d\'une routine quotidienne adaptée pour améliorer l\'état de la peau. '
253
- 'Termine toujours par la phrase: \'Consultez la section des produits recommandés ci-dessous pour voir les solutions adaptées à vos besoins spécifiques.\'"\n'
254
- '}\n\nNe mets rien d\'autre dans ta réponse que ce JSON correctement formaté.'
255
- )
256
  else:
257
- prompt = (
258
- f"As a dermatology specialist, analyze the following skin problems detected by AI: {skin_issues}.\n\n"
259
- "Respond exactly in this JSON format:\n"
260
- '{\n'
261
- ' "conseils": "List of precise and practical advice for treating these specific skin problems",\n'
262
- ' "interpretation": "Clear explanation of possible causes and severity of the detected problems",\n'
263
- ' "routine": "Proposal for a daily routine adapted to improve skin condition. '
264
- 'Always end with the phrase: \'Check the recommended products section below to see solutions tailored to your specific needs.\'"\n'
265
- '}\n\nPut nothing else in your response except this properly formatted JSON.'
266
- )
267
-
268
- t0 = time.time()
269
- completion = client.chat.completions.create(
270
  messages=[{"role": "user", "content": prompt}],
271
  model="llama-3.3-70b-versatile",
272
  stream=False,
273
  )
274
- print(f"Groq response time: {time.time() - t0:.2f}s")
275
-
276
- content = completion.choices[0].message.content
277
- json_match = re.search(r'\{.*\}', content, re.DOTALL)
 
 
 
278
  if json_match:
279
  try:
280
  return json.loads(json_match.group(0))
281
  except json.JSONDecodeError:
282
  pass
283
- return get_default_analysis(language)
284
-
285
-
286
- # ─────────────────────────────────────────────
287
- # Inference helpers (run inside thread pool)
288
- # ─────────────────────────────────────────────
289
-
290
- def run_yolo(img, class_translations):
291
- t0 = time.time()
292
- results = yolo_model(img, conf=0.15, iou=0.2, imgsz=YOLO_INPUT_SIZE, verbose=False)[0]
293
- detections = []
294
- for box in results.boxes:
295
- x1, y1, x2, y2 = box.xyxy[0].cpu().numpy().astype(int)
296
- confidence = float(box.conf[0])
297
- class_id = int(box.cls[0])
298
- class_name = results.names[class_id]
299
- translated = class_translations.get(class_name, class_name)
300
- detections.append({
301
- 'class_id': class_id,
302
- 'class_name': translated,
303
- 'confidence': confidence,
304
- 'bbox': [int(x1), int(y1), int(x2), int(y2)]
305
- })
306
- print(f"YOLO: {time.time()-t0:.2f}s → {len(detections)} detections")
307
- return detections
308
-
309
-
310
- def run_skin_type(img, skin_type_classes):
311
- t0 = time.time()
312
- pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
313
- tensor = _transform(pil).unsqueeze(0).to(device)
314
- with torch.no_grad():
315
- output = skin_type_model(tensor)
316
- probs = torch.nn.functional.softmax(output, dim=1)[0]
317
- predicted = output.argmax(1).item()
318
- result = {
319
- 'skin_type': skin_type_classes[predicted],
320
- 'probabilities': {
321
- 'dry': float(probs[0] * 100),
322
- 'normal': float(probs[1] * 100),
323
- 'oily': float(probs[2] * 100)
324
- }
325
  }
326
- print(f"Skin-type: {time.time()-t0:.2f}s → {result['skin_type']}")
327
- return result
328
-
329
 
330
- # ─────────────────────────────────────────────
331
- # Warm-up
332
- # ─────────────────────────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
 
334
  def warm_up_models():
 
335
  print("Warming up models...")
336
- dummy = np.zeros((224, 224, 3), dtype=np.uint8)
337
-
338
- t0 = time.time()
339
- yolo_model(dummy, conf=0.15, iou=0.2, imgsz=YOLO_INPUT_SIZE, verbose=False)
340
- print(f"YOLO warm-up: {time.time()-t0:.2f}s")
341
-
342
- t0 = time.time()
343
- tensor = _transform(Image.fromarray(dummy)).unsqueeze(0).to(device)
 
 
344
  with torch.no_grad():
345
- skin_type_model(tensor)
346
- print(f"Skin-type warm-up: {time.time()-t0:.2f}s")
347
- print("Models ready.")
348
-
349
 
 
350
  with app.app_context():
351
  warm_up_models()
352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
 
354
- # ─────────────────────────────────────────────
355
- # Routes
356
- # ─────────────────────────────────────────────
357
 
358
  @app.route('/predict', methods=['POST'])
359
  def predict():
360
- global_start = time.time()
361
- print("New /predict request")
362
-
363
- # ── Language ──────────────────────────────────────────────────────────────
364
- language = request.args.get('lang', 'en').lower()
365
- if language not in ('en', 'fr'):
366
- language = 'en'
367
- class_translations = class_translations_fr if language == 'fr' else class_translations_en
368
- skin_type_classes = skin_type_classes_fr if language == 'fr' else skin_type_classes_en
369
-
370
- # ── File validation ───────────────────────────────────────────────────────
371
- if 'file' not in request.files:
372
- return jsonify({'error': 'No file found'}), 400
373
- file = request.files['file']
374
- if file.filename == '':
375
- return jsonify({'error': 'No file selected'}), 400
376
-
377
- file_bytes = file.read()
378
- if len(file_bytes) > MAX_FILE_SIZE:
379
- return jsonify({'error': f'File too large. Max {MAX_FILE_SIZE//1024//1024}MB'}), 400
380
-
381
- # ── Cache lookup ──────────────────────────────────────────────────────────
382
- cache_key = f"{_image_hash(file_bytes)}_{language}"
383
- cached = _cache_get(cache_key)
384
- if cached is not None:
385
- cached['cache_hit'] = True
386
- cached['processing_time'] = round(time.time() - global_start, 3)
387
- print(f"Cache hit – returning in {cached['processing_time']}s")
388
- return jsonify(cached)
389
-
390
- # ── Decode image ──────────────────────────────────────────────────────────
391
- nparr = np.frombuffer(file_bytes, np.uint8)
392
- img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
393
- if img is None:
394
- return jsonify({'error': 'Invalid image format'}), 400
395
-
396
- img = optimize_image(img, MAX_IMAGE_SIZE)
397
-
398
- if not detect_skin(img):
399
- return jsonify({'error': 'No skin detected in the image'}), 400
400
-
401
- # ── Submit inference tasks in parallel ────────────────────────────────────
402
- future_yolo = _executor.submit(run_yolo, img, class_translations)
403
- future_skin = _executor.submit(run_skin_type, img, skin_type_classes)
404
-
405
- # ── Collect YOLO & skin-type results (max 15s each) ───────────────────────
406
  try:
407
- detections = future_yolo.result(timeout=15.0)
408
- except Exception as e:
409
- print(f"YOLO failed: {e}")
410
- detections = []
411
-
412
- try:
413
- skin_result = future_skin.result(timeout=15.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
414
  except Exception as e:
415
- print(f"Skin-type failed: {e}")
416
- skin_result = {
417
- 'skin_type': skin_type_classes[1],
418
- 'probabilities': {'dry': 0.0, 'normal': 100.0, 'oily': 0.0}
419
- }
420
-
421
- # ── Groq AI analysis (parallel with above, capped at GROQ_TIMEOUT) ────────
422
- # We submit Groq *immediately after* getting detections (not waiting for skin-type),
423
- # so it overlaps with skin-type inference time.
424
- future_groq = _executor.submit(analyze_skin_issues, detections, language)
425
- try:
426
- ai_analysis = future_groq.result(timeout=GROQ_TIMEOUT)
427
- except Exception:
428
- ai_analysis = get_default_analysis(language, timeout=True)
429
-
430
- # ── Build response ────────────────────────────────────────────────────────
431
- h, w = img.shape[:2]
432
- response_data = {
433
- 'success': True,
434
- 'cache_hit': False,
435
- 'detections': detections,
436
- 'ai_analysis': ai_analysis,
437
- 'skin_type': skin_result['skin_type'],
438
- 'skin_type_probabilities': skin_result['probabilities'],
439
- 'processing_time': round(time.time() - global_start, 2),
440
- 'image_size': {'width': w, 'height': h},
441
- 'disclaimer': "This analysis is not a medical diagnosis. For persistent issues, consult a dermatologist."
442
- }
443
-
444
- _cache_set(cache_key, response_data)
445
- print(f"Total time: {response_data['processing_time']}s")
446
- return jsonify(response_data)
447
-
448
-
449
  @app.route('/ping', methods=['GET'])
450
  def ping():
 
451
  return jsonify({
452
- 'status': 'healthy',
453
- 'message': 'Skin Analysis API is running',
454
  'timestamp': time.time(),
455
- 'version': '2.0.0',
 
456
  }), 200
457
 
458
-
459
  @app.route('/health', methods=['GET'])
460
- def health():
 
461
  return jsonify({
462
- 'status': 'API online',
463
- 'timestamp': time.time(),
464
- 'device': str(device),
465
  'model_status': 'loaded',
466
- 'cache_size': len(_result_cache),
467
- 'disclaimer': "This is a non-medical analysis tool. Consult a dermatologist for medical advice."
468
  })
469
 
470
-
471
  if __name__ == '__main__':
472
- port = int(os.environ.get("Port", 7860))
473
- app.run(debug=False, host='0.0.0.0', port=port)
 
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',
 
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)