Server-side Inference in Node.js
Server-side Inference in Node.js
Although Transformers.js was originally designed to be used in the browser, itβs also able to run inference on the server. In this tutorial, we will design a simple Node.js API that uses Transformers.js for sentiment analysis.
Weβll also show you how to use the library in both CommonJS and ECMAScript modules, so you can choose the module system that works best for your project:
ECMAScript modules (ESM) - The official standard format to package JavaScript code for reuse. Itβs the default module system in modern browsers, with modules imported using
importand exported usingexport. Fortunately, starting with version 13.2.0, Node.js has stable support of ES modules.CommonJS - The default module system in Node.js. In this system, modules are imported using
require()and exported usingmodule.exports.
Although you can always use the Python library for server-side inference, using Transformers.js means that you can write all of your code in JavaScript (instead of having to set up and communicate with a separate Python process).
Useful links:
Prerequisites
Getting started
Letβs start by creating a new Node.js project and installing Transformers.js via NPM:
Copied
Next, create a new file called app.js, which will be the entry point for our application. Depending on whether youβre using ECMAScript modules or CommonJS, you will need to do some things differently (see below).
Weβll also create a helper class called MyClassificationPipeline control the loading of the pipeline. It uses the singleton pattern to lazily create a single instance of the pipeline when getInstance is first called, and uses this pipeline for all subsequent calls:
ECMAScript modules (ESM)
To indicate that your project uses ECMAScript modules, you need to add "type": "module" to your package.json:
Copied
Next, you will need to add the following imports to the top of app.js:
Copied
Following that, letβs import Transformers.js and define the MyClassificationPipeline class.
Copied
CommonJS
Start by adding the following imports to the top of app.js:
Copied
Following that, letβs import Transformers.js and define the MyClassificationPipeline class. Since Transformers.js is an ESM module, we will need to dynamically import the library using the import() function:
Copied
Creating a basic HTTP server
Next, letβs create a basic server with the built-in HTTP module. We will listen for requests made to the server (using the /classify endpoint), extract the text query parameter, and run this through the pipeline.
Copied
Since we use lazy loading, the first request made to the server will also be responsible for loading the pipeline. If you would like to begin loading the pipeline as soon as the server starts running, you can add the following line of code after defining MyClassificationPipeline:
Copied
To start the server, run the following command:
Copied
The server should be live at http://127.0.0.1:3000/, which you can visit in your web browser. You should see the following message:
Copied
This is because we arenβt targeting the /classify endpoint with a valid text query parameter. Letβs try again, this time with a valid request. For example, you can visit http://127.0.0.1:3000/classify?text=I%20love%20Transformers.js and you should see:
Copied
Great! Weβve successfully created a basic HTTP server that uses Transformers.js to classify text.
(Optional) Customization
Model caching
By default, the first time you run the application, it will download the model files and cache them on your file system (in ./node_modules/@xenova/transformers/.cache/). All subsequent requests will then use this model. You can change the location of the cache by setting env.cacheDir. For example, to cache the model in the .cache directory in the current working directory, you can add:
Copied
Use local models
If you want to use local model files, you can set env.localModelPath as follows:
Copied
You can also disable loading of remote models by setting env.allowRemoteModels to false:
Copied
Last updated