Skip to content

Commit f126091

Browse files
authored
Update React guide for v3 (#1128)
* Update react guide for v3 * Have the react side keep the full output
1 parent eb83819 commit f126091

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

docs/source/tutorials/react.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ We can achieve both of these goals by using a [Web Worker](https://developer.moz
5858

5959
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](https://en.wikipedia.org/wiki/Singleton_pattern) to lazily create a single instance of the pipeline when `getInstance` is first called, and use this pipeline for all subsequent calls:
6060
```javascript
61-
import { pipeline } from '@huggingface/transformers';
61+
import { pipeline, TextStreamer } from '@huggingface/transformers';
6262

6363
class MyTranslationPipeline {
6464
static task = 'translation';
@@ -423,18 +423,25 @@ self.addEventListener('message', async (event) => {
423423
self.postMessage(x);
424424
});
425425

426+
// Capture partial output as it streams from the pipeline
427+
const streamer = new TextStreamer(pipeline.tokenizer, {
428+
skip_prompt: true,
429+
skip_special_tokens: true,
430+
callback_function: function (text) {
431+
self.postMessage({
432+
status: 'update',
433+
output: text
434+
});
435+
}
436+
});
437+
426438
// Actually perform the translation
427439
let output = await translator(event.data.text, {
428440
tgt_lang: event.data.tgt_lang,
429441
src_lang: event.data.src_lang,
430442

431-
// Allows for partial output
432-
callback_function: x => {
433-
self.postMessage({
434-
status: 'update',
435-
output: translator.tokenizer.decode(x[0].output_token_ids, { skip_special_tokens: true })
436-
});
437-
}
443+
// Allows for partial output to be captured
444+
streamer
438445
});
439446

440447
// Send the output back to the main thread
@@ -482,7 +489,7 @@ const onMessageReceived = (e) => {
482489

483490
case 'update':
484491
// Generation update: update the output text.
485-
setOutput(e.data.output);
492+
setOutput((o => o + e.data.output);
486493
break;
487494

488495
case 'complete':

0 commit comments

Comments
 (0)