Building a Next.js Application

Building a Next.js application

In this tutorial, we’ll build a simple Next.js application that performs sentiment analysis using Transformers.js! Since Transformers.js can run in the browser or in Node.js, you can choose whether you want to perform inference client-side or server-side (we’ll show you how to do both). In either case, we will be developing with the new App Router paradigm. The final product will look something like this:

Demo

Useful links:

Prerequisites

Client-side inference

Step 1: Initialise the project

Start by creating a new Next.js application using create-next-app:

Copied

On installation, you’ll see various prompts. For this demo, we’ll be selecting those shown below in bold:

Step 2: Install and configure Transformers.js

You can install Transformers.js from NPM with the following command:

Copied

We also need to update the next.config.js file to ignore node-specific modules when bundling for the browser:

Copied

Next, we’ll create a new Web Worker script where we’ll place all ML-related code. This is to ensure that the main thread is not blocked while the model is loading and performing inference. For this application, we’ll be using Xenova/distilbert-base-uncased-finetuned-sst-2-english, a ~67M parameter model finetuned on the Stanford Sentiment Treebank dataset. Add the following code to ./src/app/worker.js:

Copied

Step 3: Design the user interface

We’ll now modify the default ./src/app/page.js file so that it connects to our worker thread. Since we’ll only be performing in-browser inference, we can opt-in to Client components using the 'use client' directive.

Copied

Initialise the following state variables at the beginning of the Home component:

Copied

and fill in the onMessageReceived function to update these variables when the worker thread sends a message:

Copied

Finally, we can add a simple UI to the Home component, consisting of an input textbox and a preformatted text element to display the classification result:

Copied

You can now run your application using the following command:

Copied

Visit the URL shown in the terminal (e.g., http://localhost:3000/) to see your application in action!

(Optional) Step 4: Build and deploy

To build your application, simply run:

Copied

This will bundle your application and output the static files to the out folder.

For this demo, we will deploy our application as a static BOINC AI Space, but you can deploy it anywhere you like! If you haven’t already, you can create a free BOINC AI account here.

  1. Visit https://boincai.com/new-space and fill in the form. Remember to select β€œStatic” as the space type.

  2. Click the β€œCreate space” button at the bottom of the page.

  3. Go to β€œFiles” β†’ β€œAdd file” β†’ β€œUpload files”. Drag the files from the out folder into the upload box and click β€œUpload”. After they have uploaded, scroll down to the button and click β€œCommit changes to main”.

That’s it! Your application should now be live at https://boincai.com/spaces/<your-username>/<your-space-name>!

Server-side inference

While there are many different ways to perform server-side inference, the simplest (which we will discuss in this tutorial) is using the new Route Handlers feature.

Step 1: Initialise the project

Start by creating a new Next.js application using create-next-app:

Copied

On installation, you’ll see various prompts. For this demo, we’ll be selecting those shown below in bold:

Step 2: Install and configure Transformers.js

You can install Transformers.js from NPM with the following command:

Copied

We also need to update the next.config.js file to prevent Webpack from bundling certain packages:

Copied

Next, let’s set up our Route Handler. We can do this by creating two files in a new ./src/app/classify/ directory:

  1. pipeline.js - to handle the construction of our pipeline.

    Copied

  2. route.js - to process requests made to the /classify route.

    Copied

Step 3: Design the user interface

We’ll now modify the default ./src/app/page.js file to make requests to our newly-created Route Handler.

Copied

You can now run your application using the following command:

Copied

Visit the URL shown in the terminal (e.g., http://localhost:3000/) to see your application in action!

(Optional) Step 4: Build and deploy

For this demo, we will build and deploy our application to BOINC AI Spaces. If you haven’t already, you can create a free BOINC AI account here.

  1. Create a new Dockerfile in your project’s root folder. You can use our example Dockerfile as a template.

  2. Visit https://boincai.com/new-space and fill in the form. Remember to select β€œDocker” as the space type (you can choose the β€œBlank” Docker template).

  3. Click the β€œCreate space” button at the bottom of the page.

  4. Go to β€œFiles” β†’ β€œAdd file” β†’ β€œUpload files”. Drag the files from your project folder (excluding node_modules and .next, if present) into the upload box and click β€œUpload”. After they have uploaded, scroll down to the button and click β€œCommit changes to main”.

  5. Add the following lines to the top of your README.md:Copied

That’s it! Your application should now be live at https://boincai.com/spaces/<your-username>/<your-space-name>!

Last updated