Skip to content

Commit 761f257

Browse files
authored
Update react tutorial sample code (#1152)
1 parent e1753ac commit 761f257

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

docs/source/tutorials/react.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ In this tutorial, we'll be building a simple React application that performs mul
77

88
Useful links:
99
- [Demo site](https://huggingface.co/spaces/Xenova/react-translator)
10-
- [Source code](https://github.com/huggingface/transformers.js/tree/main/examples/react-translator)
10+
- [Source code](https://github.com/huggingface/transformers.js-examples/tree/main/react-translator)
1111

1212

1313
## Prerequisites
@@ -66,10 +66,7 @@ We can achieve both of these goals by using a [Web Worker](https://developer.moz
6666
static instance = null;
6767

6868
static async getInstance(progress_callback = null) {
69-
if (this.instance === null) {
70-
this.instance = pipeline(this.task, this.model, { progress_callback });
71-
}
72-
69+
this.instance ??= pipeline(this.task, this.model, { progress_callback });
7370
return this.instance;
7471
}
7572
}
@@ -79,19 +76,18 @@ We can achieve both of these goals by using a [Web Worker](https://developer.moz
7976
```jsx
8077
// Remember to import the relevant hooks
8178
import { useEffect, useRef, useState } from 'react'
79+
import './App.css'
8280

8381
function App() {
8482
// Create a reference to the worker object.
8583
const worker = useRef(null);
8684

8785
// We use the `useEffect` hook to setup the worker as soon as the `App` component is mounted.
8886
useEffect(() => {
89-
if (!worker.current) {
90-
// Create the worker if it does not yet exist.
91-
worker.current = new Worker(new URL('./worker.js', import.meta.url), {
92-
type: 'module'
93-
});
94-
}
87+
// Create the worker if it does not yet exist.
88+
worker.current ??= new Worker(new URL('./worker.js', import.meta.url), {
89+
type: 'module'
90+
});
9591

9692
// Create a callback function for messages from the worker thread.
9793
const onMessageReceived = (e) => {
@@ -127,7 +123,7 @@ We recommend starting the development server again with `npm run dev`
127123
128124
129125
First, let's define our components. Create a folder called `components` in the `src` directory, and create the following files:
130-
1. `LanguageSelector.jsx`: This component will allow the user to select the input and output languages. Check out the full list of languages [here](https://github.com/huggingface/transformers.js/blob/main/examples/react-translator/src/components/LanguageSelector.jsx).
126+
1. `LanguageSelector.jsx`: This component will allow the user to select the input and output languages. Check out the full list of languages [here](https://github.com/huggingface/transformers.js-examples/tree/main/react-translator/src/components/LanguageSelector.jsx).
131127
```jsx
132128
const LANGUAGES = {
133129
"Acehnese (Arabic script)": "ace_Arab",
@@ -363,7 +359,6 @@ Finally, we can add some CSS to make our app look a little nicer. Modify the fol
363359
z-index: 0;
364360
top: 0;
365361
width: 1%;
366-
height: 100%;
367362
overflow: hidden;
368363
background-color: #007bff;
369364
white-space: nowrap;
@@ -402,6 +397,7 @@ First, let's define the `translate` function, which will be called when the user
402397
```jsx
403398
const translate = () => {
404399
setDisabled(true);
400+
setOutput('');
405401
worker.current.postMessage({
406402
text: input,
407403
src_lang: sourceLanguage,
@@ -417,14 +413,14 @@ Now, let's add an event listener in `src/worker.js` to listen for messages from
417413
self.addEventListener('message', async (event) => {
418414
// Retrieve the translation pipeline. When called for the first time,
419415
// this will load the pipeline and save it for future use.
420-
let translator = await MyTranslationPipeline.getInstance(x => {
416+
const translator = await MyTranslationPipeline.getInstance(x => {
421417
// We also add a progress callback to the pipeline so that we can
422418
// track model loading.
423419
self.postMessage(x);
424420
});
425421

426422
// Capture partial output as it streams from the pipeline
427-
const streamer = new TextStreamer(pipeline.tokenizer, {
423+
const streamer = new TextStreamer(translator.tokenizer, {
428424
skip_prompt: true,
429425
skip_special_tokens: true,
430426
callback_function: function (text) {
@@ -436,23 +432,23 @@ self.addEventListener('message', async (event) => {
436432
});
437433

438434
// Actually perform the translation
439-
let output = await translator(event.data.text, {
435+
const output = await translator(event.data.text, {
440436
tgt_lang: event.data.tgt_lang,
441437
src_lang: event.data.src_lang,
442438

443439
// Allows for partial output to be captured
444-
streamer
440+
streamer,
445441
});
446442

447443
// Send the output back to the main thread
448444
self.postMessage({
449445
status: 'complete',
450-
output: output,
446+
output,
451447
});
452448
});
453449
```
454450
455-
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:
451+
Finally, let's fill in our `onMessageReceived` function in `src/App.jsx`, 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:
456452
457453
```jsx
458454
const onMessageReceived = (e) => {
@@ -489,7 +485,7 @@ const onMessageReceived = (e) => {
489485

490486
case 'update':
491487
// Generation update: update the output text.
492-
setOutput((o => o + e.data.output);
488+
setOutput(o => o + e.data.output);
493489
break;
494490

495491
case 'complete':

0 commit comments

Comments
 (0)