返回提交历史
Deleted
projects/text_to_speech/worker.js
+0
-105
XFEstudio/gpt4free
Delete worker.js
eb7c88bb
代码差异
1 个文件
+0
-105
@@ -1,105 +0,0 @@
1
import { env, Tensor, AutoTokenizer, SpeechT5ForTextToSpeech, SpeechT5HifiGan } from '@xenova/transformers';
2
import { encodeWAV } from './utils';
3
4
// Disable local model checks
5
env.allowLocalModels = false;
6
7
// Use the Singleton pattern to enable lazy construction of the pipeline.
8
class MyTextToSpeechPipeline {
9
10
static BASE_URL = 'https://huggingface.co/datasets/Xenova/cmu-arctic-xvectors-extracted/resolve/main/';
11
12
static model_id = 'Xenova/speecht5_tts';
13
static vocoder_id = 'Xenova/speecht5_hifigan';
14
15
static tokenizer_instance = null;
16
static model_instance = null;
17
static vocoder_instance = null;
18
19
static async getInstance(progress_callback = null) {
20
if (this.tokenizer_instance === null) {
21
this.tokenizer = AutoTokenizer.from_pretrained(this.model_id, { progress_callback });
22
}
23
24
if (this.model_instance === null) {
25
this.model_instance = SpeechT5ForTextToSpeech.from_pretrained(this.model_id, {
26
quantized: false,
27
progress_callback,
28
});
29
}
30
31
if (this.vocoder_instance === null) {
32
this.vocoder_instance = SpeechT5HifiGan.from_pretrained(this.vocoder_id, {
33
quantized: false,
34
progress_callback,
35
});
36
}
37
38
return new Promise(async (resolve, reject) => {
39
const result = await Promise.all([
40
this.tokenizer,
41
this.model_instance,
42
this.vocoder_instance,
43
]);
44
self.postMessage({
45
status: 'ready',
46
});
47
resolve(result);
48
});
49
}
50
51
static async getSpeakerEmbeddings(speaker_id) {
52
// e.g., `cmu_us_awb_arctic-wav-arctic_a0001`
53
const speaker_embeddings_url = `${this.BASE_URL}${speaker_id}.bin`;
54
const speaker_embeddings = new Tensor(
55
'float32',
56
new Float32Array(await (await fetch(speaker_embeddings_url)).arrayBuffer()),
57
[1, 512]
58
)
59
return speaker_embeddings;
60
}
61
}
62
63
// Mapping of cached speaker embeddings
64
const speaker_embeddings_cache = new Map();
65
66
// Listen for messages from the main thread
67
self.addEventListener('message', async (event) => {
68
// Load the pipeline
69
const [tokenizer, model, vocoder] = await MyTextToSpeechPipeline.getInstance(x => {
70
// We also add a progress callback so that we can track model loading.
71
self.postMessage(x);
72
});
73
74
// Tokenize the input
75
const { input_ids } = tokenizer(event.data.text);
76
77
// Load the speaker embeddings
78
let speaker_embeddings = speaker_embeddings_cache.get(event.data.speaker_id);
79
if (speaker_embeddings === undefined) {
80
speaker_embeddings = await MyTextToSpeechPipeline.getSpeakerEmbeddings(event.data.speaker_id);
81
speaker_embeddings_cache.set(event.data.speaker_id, speaker_embeddings);
82
}
83
84
// Generate the waveform
85
let response;
86
try {
87
response = await model.generate_speech(input_ids, speaker_embeddings, { vocoder });
88
} catch(e) {
89
self.postMessage({
90
status: 'error',
91
exception: e,
92
});
93
throw e;
94
}
95
const { waveform } = response;
96
97
// Encode the waveform as a WAV file
98
const wav = encodeWAV(waveform.data);
99
100
// Send the output back to the main thread
101
self.postMessage({
102
status: 'complete',
103
output: new Blob([wav], { type: 'audio/wav' }),
104
});
105
});