Spaces:
Sleeping
Sleeping
Download app/api/analysis.py from MoodMate-AI/moodmate-api: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/MoodMate-AI/moodmate-api/resolve/main/app/api/analysis.py
- Command line
-
hf download hf://spaces/MoodMate-AI/moodmate-api/app/api/analysis.py
-
curl -L -o analysis.py https://huggingface.co/spaces/MoodMate-AI/moodmate-api/resolve/main/app/api/analysis.py
1.14 kB
| 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"]) | |
| 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.") | |
| async def health(): | |
| return {"status": "ok", "service": "MoodMate AI Engine"} | |