File size: 1,140 Bytes
d143e1a
 
 
 
 
b2a5955
 
d143e1a
 
 
 
b2a5955
030f99c
d143e1a
 
 
 
 
 
 
030f99c
d143e1a
 
 
 
 
 
 
 
 
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
import anyio
from fastapi import APIRouter, HTTPException, Depends
from ..schemas.analysis import AnalyzeRequest
from ..ai.service import analyze_mood
from ..core.security import get_current_user
from ..core.ratelimit import limiter
from fastapi import Request

router = APIRouter(tags=["Analysis"])

@router.post("/analyze")
@limiter.limit("5/minute")
async def analyze_text(payload: AnalyzeRequest, request: Request, user: dict = Depends(get_current_user)):
    """
    Authenticated sentiment analysis endpoint.
    Accepts text, returns mood/intensity/confidence/suggestions.
    Now protected and optimized with threadpool.
    """
    try:
        # Run CPU-bound sync inference in a separate thread
        return await anyio.to_thread.run_sync(analyze_mood, payload.content, payload.language)
    except ValueError as e:
        raise HTTPException(status_code=422, detail=str(e))
    except Exception as e:
        # Log error here in a real app
        raise HTTPException(status_code=500, detail="Internal analysis error.")

@router.get("/health")
async def health():
    return {"status": "ok", "service": "MoodMate AI Engine"}