Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,67 @@
|
|
1 |
---
|
2 |
library_name: transformers.js
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
library_name: transformers.js
|
3 |
+
---
|
4 |
+
|
5 |
+
https://huggingface.co/microsoft/speecht5_hifigan with ONNX weights to be compatible with Transformers.js.
|
6 |
+
|
7 |
+
|
8 |
+
## Usage (Transformers.js)
|
9 |
+
|
10 |
+
If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@xenova/transformers) using:
|
11 |
+
```bash
|
12 |
+
npm i @xenova/transformers
|
13 |
+
```
|
14 |
+
|
15 |
+
**Example:** Generate speech from text.
|
16 |
+
```js
|
17 |
+
import { AutoTokenizer, AutoProcessor, SpeechT5ForTextToSpeech, SpeechT5HifiGan, Tensor } from '@xenova/transformers';
|
18 |
+
|
19 |
+
// Load the tokenizer and processor
|
20 |
+
const tokenizer = await AutoTokenizer.from_pretrained('Xenova/speecht5_tts');
|
21 |
+
const processor = await AutoProcessor.from_pretrained('Xenova/speecht5_tts');
|
22 |
+
|
23 |
+
// Load the models
|
24 |
+
// NOTE: We use the unquantized versions as they are more accurate
|
25 |
+
const model = await SpeechT5ForTextToSpeech.from_pretrained('Xenova/speecht5_tts', { quantized: false });
|
26 |
+
const vocoder = await SpeechT5HifiGan.from_pretrained('Xenova/speecht5_hifigan', { quantized: false });
|
27 |
+
|
28 |
+
// Load speaker embeddings from URL
|
29 |
+
const speaker_embeddings_data = new Float32Array(
|
30 |
+
await (await fetch('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin')).arrayBuffer()
|
31 |
+
);
|
32 |
+
const speaker_embeddings = new Tensor(
|
33 |
+
'float32',
|
34 |
+
speaker_embeddings_data,
|
35 |
+
[1, speaker_embeddings_data.length]
|
36 |
+
)
|
37 |
+
|
38 |
+
// Run tokenization
|
39 |
+
const { input_ids } = tokenizer('Hello, my dog is cute');
|
40 |
+
|
41 |
+
// Generate waveform
|
42 |
+
const { waveform } = await model.generate_speech(input_ids, speaker_embeddings, { vocoder });
|
43 |
+
console.log(waveform)
|
44 |
+
// Tensor {
|
45 |
+
// dims: [ 26112 ],
|
46 |
+
// type: 'float32',
|
47 |
+
// size: 26112,
|
48 |
+
// data: Float32Array(26112) [ -0.00043630177970044315, -0.00018082228780258447, ... ],
|
49 |
+
// }
|
50 |
+
```
|
51 |
+
|
52 |
+
Optionally, save the audio to a wav file (Node.js):
|
53 |
+
```js
|
54 |
+
// Write to file (Node.js)
|
55 |
+
import wavefile from 'wavefile';
|
56 |
+
import fs from 'fs';
|
57 |
+
|
58 |
+
const wav = new wavefile.WaveFile();
|
59 |
+
wav.fromScratch(1, processor.feature_extractor.config.sampling_rate, '32f', waveform.data);
|
60 |
+
fs.writeFileSync('out.wav', wav.toBuffer());
|
61 |
+
```
|
62 |
+
|
63 |
+
<audio controls src="https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/on1ij9Y269ne9zlYN9mdb.wav"></audio>
|
64 |
+
|
65 |
+
---
|
66 |
+
|
67 |
+
Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using [🤗 Optimum](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`).
|