Spaces:
Running
Running
Download services/analysis.py from RabiHmedtak/API_RapidAPI_test: direct link, hf CLI and curl.
- Browser
- Download file 13.6 kB
-
https://huggingface.co/spaces/RabiHmedtak/API_RapidAPI_test/resolve/main/services/analysis.py
- Command line
-
hf download hf://spaces/RabiHmedtak/API_RapidAPI_test/services/analysis.py
-
curl -L -o analysis.py https://huggingface.co/spaces/RabiHmedtak/API_RapidAPI_test/resolve/main/services/analysis.py
13.6 kB
| # 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 |