Spaces:
Runtime error
Download README.md from Oheha/cybully-api: direct link, hf CLI and curl.
- Browser
- Download file 7.49 kB
-
https://huggingface.co/spaces/Oheha/cybully-api/resolve/main/README.md
- Command line
-
hf download hf://spaces/Oheha/cybully-api/README.md
-
curl -L -o README.md https://huggingface.co/spaces/Oheha/cybully-api/resolve/main/README.md
title: Cybully API
emoji: 🛡️
colorFrom: indigo
colorTo: blue
sdk: docker
app_port: 7860
pinned: false
Cybully Safety MVP
This repository implements the 1-week MVP plan from the system design document:
- Next.js app shell with Supabase email/password auth and SSR session handling.
- User-facing text submission flow.
- Moderator incident queue, detail view, review actions, and polling refresh.
- FastAPI backend with Supabase Postgres persistence, direct mini-project processing, optional RabbitMQ workers, and stubbed alert storage.
The MVP is text-only. Image/video moderation, MinIO, vector search, Kubernetes, and production SendGrid delivery are intentionally out of scope.
Project Layout
apps/web Next.js App Router frontend and backend-for-frontend routes
services/api FastAPI app, SQLAlchemy models, Alembic migration, workers, tests
scripts Manual demo payloads and localization benchmark samples
Supabase Mini-Project Setup
- Copy the environment file.
Copy-Item .env.example .env
Create a Supabase project and run
supabase/schema.sqlin the Supabase SQL editor.Put your Supabase Postgres Session Pooler connection string into
.env.
DATABASE_URL=postgresql://postgres.YOUR_PROJECT_REF:YOUR_PERCENT_ENCODED_PASSWORD@aws-1-YOUR-REGION.pooler.supabase.com:5432/postgres?sslmode=require
PIPELINE_MODE=direct
SCORER_PROVIDER=auto
PIPELINE_MODE=direct removes the need for Docker, RabbitMQ, and local PostgreSQL. The API processes the text and persists the incident during the request.
If your database password contains reserved characters such as $ or %, percent-encode them in DATABASE_URL.
- Configure backend and moderator values in
.env.
MODERATOR_EMAILS=your-moderator-email@example.com
If you change .env while the API is already running, restart uvicorn. The SQLAlchemy engine is created at startup, so the running process will not pick up a new DATABASE_URL until restart.
5. Create apps/web/.env.local for the public Supabase web keys.
NEXT_PUBLIC_SUPABASE_URL=https://YOUR_PROJECT_REF.supabase.co
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=replace-with-supabase-publishable-key
- Install and run the backend.
cd services/api
python -m pip install -e ".[dev]"
uvicorn app.main:app --reload --port 8000
- Install and run the frontend.
npm install
npm run dev:web
- Open the apps.
- Web app: http://localhost:3000
- FastAPI docs: http://localhost:8000/docs
For this mini-project setup, SCORER_PROVIDER=auto uses Detoxify when the ML dependency is available, and automatically falls back to the local heuristic scorer if Detoxify is unavailable or fails to initialize. To force Detoxify only, set SCORER_PROVIDER=detoxify and install python -m pip install -e ".[dev,ml]".
Backend Deployment (Railway)
The backend is ready for Railway Docker deployment from this monorepo.
- Create a new Railway project and choose Deploy from GitHub repo.
- Open the backend service settings and set:
- Root Directory:
services/api - Builder:
Dockerfile(auto-detected fromservices/api/Dockerfile)
- Root Directory:
- Add service variables in Railway:
ENVIRONMENT=productionPIPELINE_MODE=directSCORER_PROVIDER=autoMODEL_INFERENCE_DEVICE=cpuRUN_MIGRATIONS=1DATABASE_URL(Supabase session pooler URL withsslmode=require)SUPABASE_URLSUPABASE_PUBLISHABLE_KEYSUPABASE_SECRET_KEYBACKEND_INTERNAL_TOKENALLOWED_CORS_ORIGINS(your deployed frontend origin)MODERATOR_EMAILS(comma-separated moderator email allowlist)ADMIN_NOTIFICATION_EMAIL
- In service Networking, generate a public domain.
- Trigger a deploy. Container startup runs
alembic upgrade headbefore booting Uvicorn.
Optional: if you deploy from repo root instead of setting Root Directory, set RAILWAY_DOCKERFILE_PATH=/services/api/Dockerfile.
Health check endpoint: GET /health
Manual Demo
- Open http://localhost:3000.
- Sign in with a Supabase email/password account, or create one at
/sign-up. - New sign-ups must confirm their email before sign-in when Supabase email confirmation is enabled.
- To preview moderator screens immediately, sign in with an email listed in
MODERATOR_EMAILS. - Submit text at
/app. - In direct mode, the API writes the incident to Supabase during the request and returns a tracking id.
- Review incidents at
/moderation. - Open an incident detail page and mark it reviewed, dismissed, or escalated.
- Check high-severity alert stubs in the lower panel of
/moderation.
You can also use scripts/demo_payloads.http against the FastAPI service directly. Prefer a Supabase bearer token for user-scoped calls. X-Internal-Token remains available as a fallback for local service-to-service or manual script use.
Backend Development
Install dependencies in a Python 3.11 environment:
cd services/api
python -m pip install -e ".[dev]"
pytest
If you prefer Alembic over the Supabase SQL editor, run migrations manually:
cd services/api
alembic upgrade head
Optional RabbitMQ worker entrypoints for future queue mode:
python -m app.workers.inference
python -m app.workers.persistence
python -m app.workers.alerts
Frontend Development
Install and run the Next.js app:
npm install
npm run dev:web
Useful checks:
npm run lint:web
npm run build:web
npm run test:web
Localization Benchmark
scripts/localization_samples.json contains a small Nigerian Pidgin/code-mixed sample set for the MVP benchmark. For the sprint acceptance pass, run these texts through /app or scripts/demo_payloads.http, compare the expected risk labels with the persisted model output, and record false negatives as known Detoxify baseline gaps.
API Summary
POST /api/v1/analyze/text: analyze text directly in mini mode, persist it, and return a tracking id.GET /api/v1/incidents: list incidents withseverity,status,limit, andoffset.GET /api/v1/incidents/{id}: fetch incident detail.PATCH /api/v1/incidents/{id}: update moderation status and note, with reviewer identity and moderation timestamp persistence.GET /api/v1/alerts: list stubbed high-severity alerts.
All /api/v1/* backend routes require either a Supabase bearer token or X-Internal-Token. The Next.js server routes now forward the signed-in user's Supabase access token to FastAPI.
Current Status
- Landing page, sign-in, sign-up, submit flow, moderation list, incident detail, and settings screens are implemented.
- Supabase email/password auth is live.
- FastAPI bearer-token validation against Supabase Auth is live.
- Direct-mode incident persistence to Supabase Postgres is working with the Session Pooler configuration.
- Stub alert persistence is wired, but production email delivery is still intentionally out of scope.
Optional Queue Mode
The original asynchronous RabbitMQ pipeline is still available. Set:
PIPELINE_MODE=queue
SCORER_PROVIDER=detoxify
RABBITMQ_URL=...
Then run the API plus app.workers.inference, app.workers.persistence, and app.workers.alerts. The included docker-compose.yml remains available for a fuller local infrastructure setup, but it is no longer required for the Supabase mini-project path.