Building a React Application

Building a React application

In this tutorial, we’ll be building a simple React application that performs multilingual translation using Transformers.js! The final product will look something like this:

Demo

Useful links:

Prerequisites

Step 1: Initialise the project

For this tutorial, we will use Vite to initialise our project. Vite is a build tool that allows us to quickly set up a React application with minimal configuration. Run the following command in your terminal:

Copied

If prompted to install create-vite, type y and press Enter.

Next, enter the project directory and install the necessary development dependencies:

Copied

To test that our application is working, we can run the following command:

Copied

Visiting the URL shown in the terminal (e.g., http://localhost:5173/) should show the default β€œReact + Vite” landing page. You can stop the development server by pressing Ctrl + C in the terminal.

Step 2: Install and configure Transformers.js

Now we get to the fun part: adding machine learning to our application! First, install Transformers.js from NPM with the following command:

Copied

For this application, we will use the Xenova/nllb-200-distilled-600M model, which can perform multilingual translation among 200 languages. Before we start, there are 2 things we need to take note of:

  1. ML inference can be quite computationally intensive, so it’s better to load and run the models in a separate thread from the main (UI) thread.

  2. Since the model is quite large (>1 GB), we don’t want to download it until the user clicks the β€œTranslate” button.

We can achieve both of these goals by using a Web Worker and some React hooks.

  1. Create a file called worker.js in the src directory. This script will do all the heavy-lifing for us, including loading and running of the translation pipeline. To ensure the model is only loaded once, we will create the MyTranslationPipeline class which use the singleton pattern to lazily create a single instance of the pipeline when getInstance is first called, and use this pipeline for all subsequent calls:

    Copied

  2. Modify App.jsx in the src directory. This file is automatically created when initializing our React project, and will contain some boilerplate code. Inside the App function, let’s create the web worker and store a reference to it using the useRef hook:

    Copied

Step 3: Design the user interface

We recommend starting the development server again with npm run dev (if not already running) so that you can see your changes in real-time.

First, let’s define our components. Create a folder called components in the src directory, and create the following files:

  1. LanguageSelector.jsx: This component will allow the user to select the input and output languages. Check out the full list of languages here.

    Copied

  2. Progress.jsx: This component will display the progress for downloading each model file.Copied

We can now use these components in App.jsx by adding these imports to the top of the file:

Copied

Let’s also add some state variables to keep track of a few things in our application, like model loading, languages, input text, and output text. Add the following code to the beginning of the App function in src/App.jsx:

Copied

Next, we can add our custom components to the main App component. We will also add two textarea elements for input and output text, and a button to trigger the translation. Modify the return statement to look like this:

Copied

Don’t worry about the translate function for now. We will define it in the next section.

Finally, we can add some CSS to make our app look a little nicer. Modify the following files in the src directory:

  1. index.css:

  2. App.css

Step 4: Connecting everything together

Now that we have a basic user interface set up, we can finally connect everything together.

First, let’s define the translate function, which will be called when the user clicks the Translate button. This sends a message (containing the input text, source language, and target language) to the worker thread for processing. We will also disable the button so the user doesn’t click it multiple times. Add the following code just before the return statement in the App function:

Copied

Now, let’s add an event listener in src/worker.js to listen for messages from the main thread. We will send back messages (e.g., for model loading progress and text streaming) to the main thread with self.postMessage.

Copied

Finally, let’s fill in our onMessageReceived function, which will update the application state in response to messages from the worker thread. Add the following code inside the useEffect hook we defined earlier:

Copied

You can now run the application with npm run dev and perform multilingual translation directly in your browser!

(Optional) Step 5: Build and deploy

To build your application, simply run npm run build. This will bundle your application and output the static files to the dist 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. Go to β€œFiles” β†’ β€œAdd file” β†’ β€œUpload files”. Drag the index.html file and public/ folder from the dist 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>!

Last updated