File size: 13,632 Bytes
8cff4cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# 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