Implementation of TensorFlowJs models in NestJs microservice architecture
This project consists of two microservices. The first microservice is called App and acts as a gateway. The App receives user's Text and sends it to another microservice called Ai through a gRPC connection. Ai analyzes the text and returns toxicity as a boolean. Also, the type of toxicity is described in six different categories: identity_attack
, insult
, obscene
, severe_toxicity
, sexual_explicit
, and threat
. The results are produced from a pre-trained network in Pre-trained TensorFlow.js models, specifically the Toxicity classifier. You can substitute your own model with the toxicity model and use this API for any custom application.
A straightforward illustration of the project is presented below:
If you liked the project and find it useful, support me by giving it a star 😉
Clone the project
git clone https://github.com/armin3731/nestjs-tensorflowjs-microservice
Go to the project directory
cd nestjs-tensorflowjs-microservice
Install dependencies
npm install
To run this project, you will need to add the following environment variables to your .env file (or simply rename .env.example file)
AI_HOST
AI_RPC_PORT
APP_HOST
APP_HTTP_PORT
APP_RPC_PORT
First, you need to run the App microservice:
# runs App microservice as default
$ npm run start
# or
$ npm run start app
Then open another terminal and run Ai microservice:
# Ai microservice
$ npm run start ai
Note! Starting the AI microservice will take some seconds because it loads the model at the start.
Note! To my dear friends from Iran. Use a tunneling system while running Ai microservice, because Google Cloud won't let you download the model.
If you use .env.example as your ENV variables, you will see a swagger API documentation on http://localhost:3000/api
POST /analyze_toxicity
Parameter | Type | Description |
---|---|---|
text |
string |
Required. The Text you want to find out if it's Toxic or Not |
- Caching the model to start faster
- Migration to Nest v10
ML: TensorflowJs
Server: Node, NestJs