Upload utils.js
Browse files
utils.js
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export async function extractEmbeddings(
|
2 |
+
worker,
|
3 |
+
weightsURL,
|
4 |
+
tokenizerURL,
|
5 |
+
configURL,
|
6 |
+
modelID,
|
7 |
+
sentences,
|
8 |
+
updateStatus,
|
9 |
+
normalize_embeddings
|
10 |
+
) {
|
11 |
+
return new Promise((resolve, reject) => {
|
12 |
+
worker.postMessage({
|
13 |
+
weightsURL,
|
14 |
+
tokenizerURL,
|
15 |
+
configURL,
|
16 |
+
modelID,
|
17 |
+
sentences,
|
18 |
+
normalize_embeddings,
|
19 |
+
});
|
20 |
+
function messageHandler(event) {
|
21 |
+
if ("error" in event.data) {
|
22 |
+
worker.removeEventListener("message", messageHandler);
|
23 |
+
reject(new Error(event.data.error));
|
24 |
+
}
|
25 |
+
if (event.data.status === "complete") {
|
26 |
+
worker.removeEventListener("message", messageHandler);
|
27 |
+
resolve(event.data);
|
28 |
+
}
|
29 |
+
if (updateStatus) updateStatus(event.data);
|
30 |
+
}
|
31 |
+
worker.addEventListener("message", messageHandler);
|
32 |
+
});
|
33 |
+
}
|
34 |
+
|
35 |
+
export async function generateText(
|
36 |
+
worker,
|
37 |
+
weightsURL,
|
38 |
+
tokenizerURL,
|
39 |
+
configURL,
|
40 |
+
modelID,
|
41 |
+
prompt,
|
42 |
+
params,
|
43 |
+
updateStatus
|
44 |
+
) {
|
45 |
+
return new Promise((resolve, reject) => {
|
46 |
+
worker.postMessage({
|
47 |
+
weightsURL,
|
48 |
+
tokenizerURL,
|
49 |
+
configURL,
|
50 |
+
modelID,
|
51 |
+
prompt,
|
52 |
+
params,
|
53 |
+
});
|
54 |
+
function messageHandler(event) {
|
55 |
+
if ("error" in event.data) {
|
56 |
+
worker.removeEventListener("message", messageHandler);
|
57 |
+
reject(new Error(event.data.error));
|
58 |
+
}
|
59 |
+
if (event.data.status === "complete") {
|
60 |
+
worker.removeEventListener("message", messageHandler);
|
61 |
+
resolve(event.data);
|
62 |
+
}
|
63 |
+
if (updateStatus) updateStatus(event.data);
|
64 |
+
}
|
65 |
+
worker.addEventListener("message", messageHandler);
|
66 |
+
});
|
67 |
+
}
|
68 |
+
export const MODELS = {
|
69 |
+
t5_small_quantized: {
|
70 |
+
size: "102 MB",
|
71 |
+
base_url: "https://huggingface.co/lmz/candle-quantized-t5/resolve/main/",
|
72 |
+
model: "model.gguf",
|
73 |
+
tokenizer: "tokenizer.json",
|
74 |
+
config: "config.json",
|
75 |
+
tasks: {
|
76 |
+
translation_en_to_de: {
|
77 |
+
prefix: "translate English to German: ",
|
78 |
+
max_length: 300,
|
79 |
+
},
|
80 |
+
translation_en_to_fr: {
|
81 |
+
prefix: "translate English to French: ",
|
82 |
+
max_length: 300,
|
83 |
+
},
|
84 |
+
translation_en_to_ro: {
|
85 |
+
prefix: "translate English to Romanian: ",
|
86 |
+
max_length: 300,
|
87 |
+
},
|
88 |
+
summarization: { prefix: "summarize: ", max_length: 200 },
|
89 |
+
},
|
90 |
+
},
|
91 |
+
t5_small: {
|
92 |
+
size: "242 MB",
|
93 |
+
base_url: "https://huggingface.co/t5-small/resolve/main/",
|
94 |
+
model: "model.safetensors",
|
95 |
+
tokenizer: "tokenizer.json",
|
96 |
+
config: "config.json",
|
97 |
+
tasks: {
|
98 |
+
translation_en_to_de: {
|
99 |
+
prefix: "translate English to German: ",
|
100 |
+
max_length: 300,
|
101 |
+
},
|
102 |
+
translation_en_to_fr: {
|
103 |
+
prefix: "translate English to French: ",
|
104 |
+
max_length: 300,
|
105 |
+
},
|
106 |
+
translation_en_to_ro: {
|
107 |
+
prefix: "translate English to Romanian: ",
|
108 |
+
max_length: 300,
|
109 |
+
},
|
110 |
+
summarization: { prefix: "summarize: ", max_length: 200 },
|
111 |
+
},
|
112 |
+
},
|
113 |
+
flan_t5_small: {
|
114 |
+
size: "308 MB",
|
115 |
+
base_url:
|
116 |
+
"https://huggingface.co/google/flan-t5-small/resolve/refs%2Fpr%2F14/",
|
117 |
+
model: "model.safetensors",
|
118 |
+
tokenizer: "tokenizer.json",
|
119 |
+
config: "config.json",
|
120 |
+
tasks: {
|
121 |
+
translation_en_to_de: {
|
122 |
+
prefix: "translate English to German: ",
|
123 |
+
max_length: 300,
|
124 |
+
},
|
125 |
+
translation_en_to_fr: {
|
126 |
+
prefix: "translate English to French: ",
|
127 |
+
max_length: 300,
|
128 |
+
},
|
129 |
+
translation_en_to_ro: {
|
130 |
+
prefix: "translate English to Romanian: ",
|
131 |
+
max_length: 300,
|
132 |
+
},
|
133 |
+
summarization: { prefix: "summarize: ", max_length: 200 },
|
134 |
+
},
|
135 |
+
},
|
136 |
+
|
137 |
+
flan_t5_base_quantized: {
|
138 |
+
size: "360 MB",
|
139 |
+
base_url: "https://huggingface.co/lmz/candle-quantized-t5/resolve/main/",
|
140 |
+
model: "model-flan-t5-base.gguf",
|
141 |
+
tokenizer: "tokenizer.json",
|
142 |
+
config: "config-flan-t5-base.json",
|
143 |
+
tasks: {
|
144 |
+
translation_en_to_de: {
|
145 |
+
prefix: "translate English to German: ",
|
146 |
+
max_length: 300,
|
147 |
+
},
|
148 |
+
translation_en_to_fr: {
|
149 |
+
prefix: "translate English to French: ",
|
150 |
+
max_length: 300,
|
151 |
+
},
|
152 |
+
translation_en_to_ro: {
|
153 |
+
prefix: "translate English to Romanian: ",
|
154 |
+
max_length: 300,
|
155 |
+
},
|
156 |
+
summarization: { prefix: "summarize: ", max_length: 200 },
|
157 |
+
},
|
158 |
+
},
|
159 |
+
};
|
160 |
+
export function getModelInfo(id, taskID) {
|
161 |
+
const model = MODELS[id];
|
162 |
+
return {
|
163 |
+
modelURL: model.base_url + model.model,
|
164 |
+
configURL: model.base_url + model.config,
|
165 |
+
tokenizerURL: model.base_url + model.tokenizer,
|
166 |
+
maxLength: model.tasks[taskID].max_length,
|
167 |
+
};
|
168 |
+
}
|