Skip to content

Commit 83fd31f

Browse files
chmjkbjakmroMateusz Kopciński
authored
docs: Speech to Text (#111)
## Description <!-- Provide a concise and descriptive summary of the changes implemented in this PR. --> ### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] Documentation update (improves or adds clarity to existing documentation) ### Tested on - [ ] iOS - [ ] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues <!-- Link related issues here using #issue-number --> ### Checklist - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [ ] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. --> --------- Co-authored-by: jakmro <[email protected]> Co-authored-by: Jakub Mroz <[email protected]> Co-authored-by: Mateusz Kopciński <[email protected]>
1 parent 341d7a1 commit 83fd31f

File tree

11 files changed

+207
-6
lines changed

11 files changed

+207
-6
lines changed

docs/docs/benchmarks/_category_.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Benchmarks",
3-
"position": 7,
3+
"position": 8,
44
"link": {
55
"type": "generated-index"
66
}

docs/docs/benchmarks/memory-usage.md

+7
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ sidebar_position: 2
4747
| LLAMA3_2_3B | 7.1 | 7.3 |
4848
| LLAMA3_2_3B_SPINQUANT | 3.7 | 3.8 |
4949
| LLAMA3_2_3B_QLORA | 4 | 4.1 |
50+
51+
## Speech to text
52+
53+
| Model | Android (XNNPACK) [MB] | iOS (XNNPACK) [MB] |
54+
| -------------- | ---------------------- | ------------------ |
55+
| WHISPER_TINY | 900 | 600 |
56+
| MOONSHINE_TINY | 650 | 560 |

docs/docs/benchmarks/model-size.md

+7
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ sidebar_position: 1
5252
| LLAMA3_2_3B | 6.43 |
5353
| LLAMA3_2_3B_SPINQUANT | 2.55 |
5454
| LLAMA3_2_3B_QLORA | 2.65 |
55+
56+
## Speech to text
57+
58+
| Model | XNNPACK [MB] |
59+
| -------------- | ------------ |
60+
| WHISPER_TINY | 231.0 |
61+
| MOONSHINE_TINY | 148.9 |

docs/docs/computer-vision/_category_.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Computer Vision",
3-
"position": 3,
3+
"position": 4,
44
"link": {
55
"type": "generated-index"
66
}

docs/docs/hookless-api/LLMModule.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: LLMModule
33
sidebar_position: 3
44
---
55

6-
Hookless implementation of the [useLLM](../llms/running-llms.md) hook.
6+
Hookless implementation of the [useLLM](../llms/useLLM.md) hook.
77

88
## Reference
99

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: SpeechToTextModule
3+
sidebar_position: 6
4+
---
5+
6+
Hookless implementation of the [useSpeechToText](../speech-to-text/) hook.
7+
8+
## Reference
9+
10+
```typescript
11+
import { SpeechToTextModule } from 'react-native-executorch';
12+
13+
const audioUrl = 'https://www.your-url.com/cool-audio.mp3';
14+
15+
// Loading the model
16+
const onSequenceUpdate = (sequence) => {
17+
console.log(sequence);
18+
};
19+
await SpeechToTextModule.load('moonshine', onSequenceUpdate);
20+
21+
// Loading the audio and running the model
22+
await SpeechToTextModule.loadAudio(audioUrl);
23+
const transcribedText = await SpeechToTextModule.transcribe();
24+
```
25+
26+
### Methods
27+
28+
| Method | Type | Description |
29+
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
30+
| `load` | <code>(modelName: 'whisper' &#124 'moonshine, transcribeCallback?: (sequence: string) => void, modelDownloadProgressCalback?: (downloadProgress: number) => void, encoderSource?: ResourceSource, decoderSource?: ResourceSource, tokenizerSource?: ResourceSource)</code> | Loads the model specified with `modelName`, where `encoderSource`, `decoderSource`, `tokenizerSource` are strings specifying the location of the binaries for the models. `modelDownloadProgressCallback` allows you to monitor the current progress of the model download, while `transcribeCallback` is invoked with each generated token |
31+
| `transcribe` | `(waveform: number[]): Promise<string>` | Starts a transcription process for a given input array, which should be a waveform at 16kHz. When no input is provided, it uses an internal state which is set by calling `loadAudio`. Resolves a promise with the output transcription when the model is finished. |
32+
| `loadAudio` | `(url: string) => void` | Loads audio file from given url. It sets an internal state which serves as an input to `transcribe()`. |
33+
| `encode` | `(waveform: number[]) => Promise<number[]>` | Runs the encoding part of the model. Returns a float array representing the output of the encoder. |
34+
| `decode` | `(tokens: number[], encodings: number[]) => Promise<number[]>` | Runs the decoder of the model. Returns a single token representing a next token in the output sequence. |
35+
36+
<details>
37+
<summary>Type definitions</summary>
38+
39+
```typescript
40+
type ResourceSource = string | number;
41+
```
42+
43+
</details>
44+
45+
## Loading the model
46+
47+
To load the model, use the `load` method. The required argument is `modelName`, which serves as an identifier for which model to use. It also accepts accepts optional arguments such as `encoderSource`, `decoderSource`, `tokenizerSource` which are strings that specify the location of the binaries for the model. For more information, take a look at [loading models](../fundamentals/loading-models.md) page. This method returns a promise, which can resolve to an error or void.
48+
49+
## Running the model
50+
51+
To run the model, you can use the `transcribe` method. It accepts one argument, which is an array of numbers representing a waveform at 16kHz sampling rate. The method returns a promise, which can resolve either to an error or a string containing the output text.
52+
53+
## Obtaining the input
54+
55+
To get the input, you can use the `loadAudio` method, which sets the internal input state of the model. Then you can just call `transcribe` without passing any args. It is also possible to pass inputs from other sources, as long as it is a float array containing the aforementioned waveform.

docs/docs/hookless-api/_category_.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Hookless API",
3-
"position": 4,
3+
"position": 5,
44
"link": {
55
"type": "generated-index"
66
}

docs/docs/module-api/_category_.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Module API",
3-
"position": 5,
3+
"position": 6,
44
"link": {
55
"type": "generated-index"
66
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"label": "Speech To Text",
3+
"position": 3,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: useSpeechToText
3+
sidebar_position: 1
4+
---
5+
6+
With the latest `v0.3.0` release we introduce a new hook - `useSpeechToText`. Speech to text is a task that allows to transform spoken language to written text. It is commonly used to implement features such as transcription or voice assistants. As of now, [all supported STT models](#supported-models) run on the XNNPACK backend.
7+
8+
:::info
9+
Currently, we do not support direct microphone input streaming to the model. Instead, in v0.3.0, we provide a way to transcribe an audio file.
10+
:::
11+
12+
:::caution
13+
It is recommended to use models provided by us, which are available at our [Hugging Face repository](https://huggingface.co/software-mansion/react-native-executorch-moonshine-tiny). You can also use [constants](https://github.com/software-mansion/react-native-executorch/tree/main/src/constants/modelUrls.ts) shipped with our library
14+
:::
15+
16+
## Reference
17+
18+
```typescript
19+
import { useSpeechToText } from 'react-native-executorch';
20+
21+
const { transcribe, error, loadAudio } = useSpeechToText({
22+
modelName: 'moonshine',
23+
});
24+
25+
const audioUrl = ...; // URL with audio to transcribe
26+
27+
await loadAudio(audioUrl);
28+
const transcription = await transcribe();
29+
if (error) {
30+
console.log(error);
31+
} else {
32+
console.log(transcription);
33+
}
34+
```
35+
36+
### Streaming
37+
38+
Given that STT models can process audio no longer than 30 seconds, there is a need to chunk the input audio. Chunking audio may result in cutting speech mid-sentence, which might be hard to understand for the model. To make it work, we employed an algorithm (adapted for mobile devices from [whisper-streaming](https://aclanthology.org/2023.ijcnlp-demo.3.pdf)) that uses overlapping audio chunks. This might introduce some overhead, but allows for processing audio inputs of arbitrary length.
39+
40+
### Arguments
41+
42+
**`modelName`**
43+
A literal of `"moonshine" | "whisper"` which serves as an identifier for which model should be used.
44+
45+
**`encoderSource?`**
46+
A string that specifies the location of a .pte file for the encoder. For further information on passing model sources, check out [Loading Models](https://docs.swmansion.com/react-native-executorch/docs/fundamentals/loading-models). Defaults to [constants](https://github.com/software-mansion/react-native-executorch/blob/main/src/constants/modelUrls.ts) for given model.
47+
48+
**`decoderSource?`**
49+
Analogous to the encoderSource, this takes in a string which is a source for the decoder part of the model. Defaults to [constants](https://github.com/software-mansion/react-native-executorch/blob/main/src/constants/modelUrls.ts) for given model.
50+
51+
**`tokenizerSource?`**
52+
A string that specifies the location to the tokenizer for the model. This works just as the encoder and decoder do. Defaults to [constants](https://github.com/software-mansion/react-native-executorch/blob/main/src/constants/modelUrls.ts) for given model.
53+
54+
**`overlapSeconds?`**
55+
Specifies the length of overlap between consecutive audio chunks (expressed in seconds).
56+
57+
**`windowSize?`**
58+
Specifies the size of each audio chunk (expressed in seconds).
59+
60+
### Returns
61+
62+
| Field | Type | Description |
63+
| ------------------ | --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
64+
| `transcribe` | `(input?: number[]) => Promise<string>` | Starts a transcription process for a given input array, which should be a waveform at 16kHz. When no input is provided, it uses an internal state which is set by calling `loadAudio`. Resolves a promise with the output transcription when the model is finished. |
65+
| `loadAudio` | `(url: string) => void` | Loads audio file from given url. It sets an internal state which serves as an input to `transcribe()`. |
66+
| `error` | <code>Error &#124; undefined</code> | Contains the error message if the model failed to load. |
67+
| `sequence` | <code>string</code> | This property is updated with each generated token. If you're looking to obtain tokens as they're generated, you should use this property. |
68+
| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. |
69+
| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. |
70+
| `downloadProgress` | `number` | Tracks the progress of the model download process. |
71+
72+
## Running the model
73+
74+
Before running the model's `transcribe` method be sure to obtain waveform of the audio You wish to transcribe. You can either use `loadAudio` method to load audio from a url and save it in model's internal state or obtain the waveform on your own (remember to use sampling rate of 16kHz!). In the latter case just pass the obtained waveform as argument to the `transcribe` method which returns a promise resolving to the generated tokens when successful. If the model fails during inference the `error` property contains details of the error. If you want to obtain tokens in a streaming fashion, you can also use the sequence property, which is updated with each generated token, similar to the [useLLM](../llms/useLLM.md) hook.
75+
76+
## Example
77+
78+
```typescript
79+
import { Button, Text } from 'react-native';
80+
import { useSpeechToText } from 'react-native-executorch';
81+
82+
function App() {
83+
const { loadAudio, transcribe, sequence, error } = useSpeechToText({
84+
modelName: 'whisper',
85+
});
86+
87+
const audioUrl = ...; // URL with audio to transcribe
88+
89+
return (
90+
<View>
91+
<Button
92+
onPress={async () => {
93+
await loadAudio(audioUrl);
94+
await transcribe();
95+
}
96+
title="Transcribe"
97+
/>
98+
<Text>{error ? error : sequence}</Text>
99+
</View>
100+
);
101+
}
102+
```
103+
104+
## Supported models
105+
106+
| Model | Language |
107+
| --------------------------------------------------------------------- | -------- |
108+
| [Whisper tiny.en](https://huggingface.co/openai/whisper-tiny.en) | English |
109+
| [Moonshine tiny](https://huggingface.co/UsefulSensors/moonshine-tiny) | English |
110+
111+
## Benchmarks
112+
113+
### Model size
114+
115+
| Model | XNNPACK [MB] |
116+
| -------------- | ------------ |
117+
| WHISPER_TINY | 231.0 |
118+
| MOONSHINE_TINY | 148.9 |
119+
120+
### Memory usage
121+
122+
| Model | Android (XNNPACK) [MB] | iOS (XNNPACK) [MB] |
123+
| -------------- | ---------------------- | ------------------ |
124+
| WHISPER_TINY | 900 | 600 |
125+
| MOONSHINE_TINY | 650 | 560 |

docs/docs/utils/_category_.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Utils",
3-
"position": 6,
3+
"position": 7,
44
"link": {
55
"type": "generated-index"
66
}

0 commit comments

Comments
 (0)