Structured JSON logging (core/logging_config.py):
- Replace ad-hoc print() statements with logger.info/warning/error
- Each log line is one JSON object: timestamp (ISO8601 UTC), level, logger,
message, module, exception traceback, plus user-supplied 'extra' fields
- Quiets uvicorn.access and huggingface_hub noise to WARNING
- Configurable via LOG_LEVEL env var
/health endpoint (public, no auth):
- 200 + {"status":"healthy"} → DB ok, predictor loaded
- 200 + {"status":"degraded", ...} → DB ok, predictor still warming
- 503 + {"status":"unhealthy","reason"} → DB unreachable, restart needed
- Compatible with HF Spaces / Railway / uptime monitors
Fail-fast model loading:
- After download attempt, verify distilbert_stage1 exists on disk
- If missing, raise RuntimeError to prevent app from starting in a broken
state where every /api/predict returns 500 at runtime
- Operators can override with ALLOW_DEGRADED_STARTUP=1 (offline dev, etc.)
- Critical-level log includ…
Rate limiting (slowapi):
- /login: 10 attempts/minute per IP
- /setup-password: 3 attempts/10min per IP
- /admin/export/users: 5 exports/hour
- /browse/export.csv: 10 exports/hour
- Uses X-Forwarded-For for HF Spaces proxy compatibility
- Logs failed login attempts with IP for forensics
Admin-only restrictions (close manager privilege escalation):
- /admin/users/update: managers can no longer reset passwords or modify users
(per requirement: managers can only ADD users, not edit them)
- /admin/export/users: only admins can exfiltrate user emails/login times
- Edit button hidden in admin UI for managers
Audit trail:
- Failed logins logged with IP
- All CSV exports logged (users + browse responses)
- Password change actions explicitly flagged in activity log
Input validation (Pydantic Field constraints):
- PredictRequest: quote 5000c, group 50c, service 200c
- CorrectionRequest: all fields capped (200c types, 5000c quote)
- BrowseEditRequest: response/solution 10000c, follow-up 2000…