# services/analysis.py """ Groq LLM analysis with: • singleton Groq client (created once, reused across all requests) • deduplication of detections before sending to Groq (fewer tokens → faster) • in-memory response cache (identical detections → instant reply) • hard timeout so slow API calls never block the endpoint • strict 3-key output: conseils, interpretation, routine """ import os import re import json import time import hashlib import threading from collections import defaultdict from typing import Optional from groq import Groq from config.settings import GROQ_TIMEOUT, GROQ_MODEL, MAX_CACHE_SIZE # ── Singleton Groq client ────────────────────────────────────────────────────── # Created once on first use and reused across all requests. # Avoids the overhead of building a new HTTP client per request. _groq_client: Optional[Groq] = None _groq_client_lock = threading.Lock() def _get_client() -> Optional[Groq]: global _groq_client if _groq_client is not None: return _groq_client with _groq_client_lock: # Double-check inside the lock (another thread may have initialized it) if _groq_client is None: api_key = os.environ.get("GROQ_API_KEY") if not api_key: return None _groq_client = Groq(api_key=api_key) print("[analysis] Groq client initialized ✓") return _groq_client # ── Simple in-memory cache ───────────────────────────────────────────────────── _cache: dict[str, dict] = {} _cache_lock = threading.Lock() def _cache_key(detections: list, language: str) -> str: issues = sorted(d['class_name'] for d in detections) return hashlib.md5(f"{','.join(issues)}:{language}".encode()).hexdigest() def _store(key: str, value: dict) -> None: with _cache_lock: if len(_cache) >= MAX_CACHE_SIZE: oldest = next(iter(_cache)) del _cache[oldest] _cache[key] = value # ── Deduplication ────────────────────────────────────────────────────────────── def _deduplicate(detections: list) -> list: """ Collapse multiple detections of the same class into one entry, keeping only the highest confidence score per class. Example: 8x 'Acne Scar' detections → 1x 'Acne Scar' (highest confidence) This significantly reduces Groq prompt size and speeds up the API call. """ best: dict[str, float] = {} for d in detections: name = d['class_name'] if name not in best or d['confidence'] > best[name]: best[name] = d['confidence'] # Return sorted by confidence descending so most prominent issues come first return [ {'class_name': name, 'confidence': conf} for name, conf in sorted(best.items(), key=lambda x: -x[1]) ] # ── Default fallback ─────────────────────────────────────────────────────────── def get_default_analysis(language: str = 'en', timeout: bool = False) -> dict: if language == 'fr': if timeout: return { "conseils": "L'analyse prend plus de temps que prévu. Veuillez réessayer.", "interpretation": "Le système d'analyse IA est temporairement occupé.", "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.", } return { "conseils": "Consultez un dermatologue pour des conseils personnalisés.", "interpretation": "Une analyse professionnelle est recommandée pour ces problèmes de peau.", "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.", } if timeout: return { "conseils": "Analysis is taking longer than expected. Please try again.", "interpretation": "The AI analysis system is temporarily busy.", "routine": "Standard routine: gentle cleansing, moisturising, and daily sun protection. Check the recommended products section below to see solutions tailored to your specific needs.", } return { "conseils": "Consult a dermatologist for personalised advice.", "interpretation": "Professional analysis is recommended for these skin problems.", "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.", } # ── Placeholder guard ────────────────────────────────────────────────────────── _PLACEHOLDERS = { "conseils courts et pratiques", "brief practical advice", "causes et gravité brèves", "brief causes and severity", "routine quotidienne courte", "brief daily routine", "[tes conseils ici]", "[ton interpretation ici]", "[ta routine ici]", "[your advice here]", "[your interpretation here]", "[your routine here]", } def _is_placeholder(value: str) -> bool: return value.strip().lower() in _PLACEHOLDERS or len(value.strip()) < 20 # ── Strip any extra keys the model may add ──────────────────────────────────── def _extract_three_keys(parsed: dict) -> dict: """Keep only the 3 required keys. Handle alternate key names the model might use.""" aliases = { 'conseils': ['conseils', 'advice', 'tips', 'recommandations'], 'interpretation': ['interpretation', 'analysis', 'analyse'], 'routine': ['routine', 'daily_routine', 'skincare_routine'], } result = {} for canonical, names in aliases.items(): for name in names: if name in parsed: result[canonical] = parsed[name] break return result # ── Prompt builder ───────────────────────────────────────────────────────────── def _build_prompt(skin_issues: str, language: str) -> str: """ Instruction-style prompt with a filled-in example so the model understands it must generate real content — not echo placeholder strings. Enforces exactly 3 keys. """ if language == 'fr': return ( "Tu es un dermatologue expert. Problèmes de peau détectés par IA chez un patient:\n" f"{skin_issues}\n\n" "Rédige une analyse dermatologique professionnelle.\n" "Réponds UNIQUEMENT avec un objet JSON valide. Pas de texte avant ni après. " "Exactement 3 clés, rien d'autre:\n\n" '"conseils" : conseils pratiques et spécifiques pour traiter ces problèmes précis (2-4 phrases)\n' '"interpretation" : causes probables, liens entre les problèmes et gravité globale (2-3 phrases)\n' '"routine" : routine quotidienne complète adaptée à ces problèmes, ' 'terminer obligatoirement par la phrase exacte: ' '"Consultez la section des produits recommandés ci-dessous pour voir les solutions adaptées à vos besoins spécifiques."\n\n' "Exemple de format (remplace le contenu par ta vraie analyse):\n" '{"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."}' ) else: return ( "You are an expert dermatologist. AI-detected skin issues for a patient:\n" f"{skin_issues}\n\n" "Write a professional dermatological analysis.\n" "Reply ONLY with a valid JSON object. No text before or after. " "Exactly 3 keys, nothing else:\n\n" '"conseils" : specific practical advice to treat these exact detected issues (2-4 sentences)\n' '"interpretation" : probable causes, links between issues, and overall severity (2-3 sentences)\n' '"routine" : complete daily skincare routine tailored to these issues, ' 'MUST end with the exact phrase: ' '"Check the recommended products section below to see solutions tailored to your specific needs."\n\n' "Example format (replace content with your real analysis):\n" '{"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."}' ) # ── Core Groq call ───────────────────────────────────────────────────────────── def _call_groq(detections: list, language: str) -> dict: client = _get_client() if not client: return get_default_analysis(language) # Deduplicate before building the prompt — keeps token count minimal deduped = _deduplicate(detections) skin_issues = "\n".join( f"- {d['class_name']} (confidence: {d['confidence']:.2f})" for d in deduped ) print(f"[analysis] Sending {len(deduped)} unique classes to Groq (was {len(detections)} detections)") prompt = _build_prompt(skin_issues, language) t0 = time.time() completion = client.chat.completions.create( messages=[{"role": "user", "content": prompt}], model=GROQ_MODEL, max_tokens=350, stream=False, ) print(f"[analysis] Groq response time: {time.time()-t0:.2f}s") content = completion.choices[0].message.content match = re.search(r'\{.*\}', content, re.DOTALL) if match: try: parsed = json.loads(match.group(0)) cleaned = _extract_three_keys(parsed) # Fallback: direct key grab if alias mapping missed them if not cleaned: cleaned = {k: parsed[k] for k in ('conseils', 'interpretation', 'routine') if k in parsed} required = ('conseils', 'interpretation', 'routine') if all(k in cleaned and not _is_placeholder(str(cleaned[k])) for k in required): return {k: cleaned[k] for k in required} print("[analysis] Groq returned incomplete or placeholder values — falling back") except json.JSONDecodeError: print("[analysis] JSON parse error — falling back") return get_default_analysis(language) # ── Public entry point (with cache + timeout) ────────────────────────────────── def analyze_skin_issues(detections: list, language: str = 'en') -> dict: """ Returns AI analysis dict with exactly 3 keys: conseils, interpretation, routine. Hits cache first; falls back to default on timeout or error. """ key = _cache_key(detections, language) with _cache_lock: if key in _cache: print("[analysis] Cache hit ✓") return _cache[key] result: dict = {} exception: Optional[Exception] = None def worker(): nonlocal exception try: result['data'] = _call_groq(detections, language) except Exception as exc: exception = exc t = threading.Thread(target=worker, daemon=True) t.start() t.join(GROQ_TIMEOUT) if t.is_alive(): print("[analysis] Groq timed out — returning default") return get_default_analysis(language, timeout=True) if exception: print(f"[analysis] Groq error: {exception}") return get_default_analysis(language) analysis = result.get('data', get_default_analysis(language)) _store(key, analysis) return analysis