An experimental API for detecting hate speech and profanity, incorporating rate limiting and request validation functionality, and deploying it on Cloudflare Workers.
- Check Profanity
- Request Validation
- Rate Limiting Api
- TypeScript
- Hono
- Zod
- Cloudflare Workers AI (
bge-small-en-v1.5embeddings) - Cloudflare Vectorize
- Cloudflare Workers Rate Limiting
Make sure you have Node.js installed on your machine.
Clone the repository:
git clone https://github.com/nyintosh/profanity-apiNavigate to the project directory:
cd profanity-apiInstall dependencies: Run one of the following commands based on your preferred package manager:
- Using npm:
npm install- Using yarn:
yarn install- Using pnpm:
pnpm installEverything runs on Cloudflare's free Workers plan and needs no API keys. The bindings are declared in wrangler.toml.
Single words are matched against the term list in dataset.csv. Short phrases are embedded with Workers AI and compared against the same terms in a Vectorize index. To set it up in your own account:
npx wrangler login
npx wrangler vectorize create profanity-api --dimensions=384 --metric=cosine
cf auth login # the Cloudflare CLI: npm install --global cf
npm run db:seedTo start the development server: Run one of the following commands based on your preferred package manager:
- Using npm:
npm run dev- Using yarn:
yarn dev- Using pnpm:
pnpm devIf you want to add additional features and improvements, we welcome contributions! Please follow these guidelines:
- Fork the repository
- Create a new branch
- Make your changes
- Submit a pull request
You can test the API of the project at https://profanity-api.nyintosh.workers.dev. Below are the documentation details for the API endpoint:
- Endpoint: /
- Method: POST
- Content Type: application/json
- Request Body:
{ "text": "string (2 to 500 characters)" } - Response:
{ "isVulgar": true, "flags": ["matched term"], "avgScore": 0.93 }
Each IP can make 10 requests per minute. GET / returns a short usage summary.