-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
403 lines (335 loc) · 14.3 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { AutoModel, AutoTokenizer, Tensor } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.2';
// Additional checkboxes for column selection
const maxSimCheckbox = document.getElementById('maxSimCheckbox');
const meanSimCheckbox = document.getElementById('meanSimCheckbox');
const maxSimChunkCheckbox = document.getElementById('maxSimChunkCheckbox');
const chunksCheckbox = document.getElementById('chunksCheckbox');
const embeddingsCheckbox = document.getElementById('embeddingsCheckbox')
let fileExtension = '';
// Theme management
const themeSelect = document.getElementById('themeSelect');
// Function to set theme
function setTheme(theme) {
if (theme === 'dark') {
document.documentElement.classList.add('dark');
} else if (theme === 'light') {
document.documentElement.classList.remove('dark');
} else if (theme === 'system') {
// Check system preference
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
}
// Save preference
localStorage.setItem('theme', theme);
}
// Initialize theme
const savedTheme = localStorage.getItem('theme') || 'system';
themeSelect.value = savedTheme;
setTheme(savedTheme);
// Listen for theme changes
themeSelect.addEventListener('change', (e) => setTheme(e.target.value));
// Listen for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (themeSelect.value === 'system') {
setTheme('system');
}
});
///////////////////////////////////
class TextSplitter {
constructor({
chunkSize = 4000,
lengthFunction = (text) => text.length,
keepSeparator = false,
} = {}) {
this.chunkSize = chunkSize;
this.lengthFunction = lengthFunction;
this.keepSeparator = keepSeparator;
}
splitText(text) {
throw new Error("Method 'splitText' should be implemented.");
}
_joinDocs(docs, separator) {
return docs.join(separator).trim() || null;
}
_mergeSplits(splits, separator) {
let docs = [], currentDoc = [], total = 0;
const separatorLen = this.lengthFunction(separator);
splits.forEach(d => {
const len = this.lengthFunction(d);
if (total + len + (currentDoc.length ? separatorLen : 0) > this.chunkSize) {
docs.push(this._joinDocs(currentDoc, separator));
currentDoc = [];
total = 0;
}
currentDoc.push(d);
total += len + (currentDoc.length > 1 ? separatorLen : 0);
});
const finalDoc = this._joinDocs(currentDoc, separator);
if (finalDoc) docs.push(finalDoc);
return docs;
}
}
const splitTextWithRegex = (text, separator, keepSeparator) => {
const splits = separator
? text.split(new RegExp(keepSeparator ? `(${separator})` : separator))
: [...text];
return splits.filter(Boolean);
};
class RecursiveCharacterTextSplitter extends TextSplitter {
constructor({
separators = [],
keepSeparator = true,
isSeparatorRegex = false,
...rest
} = {}) {
super({ keepSeparator, ...rest });
this.separators = separators;
this.isSeparatorRegex = isSeparatorRegex;
}
_splitText(text, separators) {
let finalChunks = [], goodSplits = [];
const separator = this._getSeparator(text, separators);
const finalSeparator = this.keepSeparator ? "" : separator;
const splits = splitTextWithRegex(text, this._escapeRegex(separator), this.keepSeparator);
const newSeparators = separators.slice(separators.indexOf(separator) + 1);
splits.forEach(s => {
if (this.lengthFunction(s) < this.chunkSize) {
goodSplits.push(s);
} else {
if (goodSplits.length) {
finalChunks.push(...this._mergeSplits(goodSplits, finalSeparator));
goodSplits = [];
}
finalChunks.push(...(newSeparators.length ? this._splitText(s, newSeparators) : [s]));
}
});
if (goodSplits.length) finalChunks.push(...this._mergeSplits(goodSplits, finalSeparator));
return finalChunks;
}
_getSeparator(text, separators) {
for (const sep of separators) {
const escapedSep = this.isSeparatorRegex ? sep : this._escapeRegex(sep);
if (new RegExp(escapedSep).test(text)) return sep;
}
return separators[separators.length - 1];
}
_escapeRegex(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
splitText(text) {
return this._splitText(text, this.separators);
}
}
function chunkText(text, chunkSize = 600, separators = ["\n\n", "\n", ".", "?", "!", ";", ",", ":", " ", ""]) {
const splitter = new RecursiveCharacterTextSplitter({
chunkSize,
separators,
keepSeparator: true, // Or false, depending on your needs
});
return splitter.splitText(text);
}
////////////////////////////////////
// Drag & Drop Handlers
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const columnSettings = document.getElementById('columnSettings');
const processButton = document.getElementById('processButton');
const status = document.getElementById('status');
const statusMessage = document.getElementById('statusMessage');
let workbook = null;
let fileName = '';
let jsonData = null;
dropZone.addEventListener('dragover', e => e.preventDefault());
dropZone.addEventListener('dragleave', e => dropZone.classList.remove('border-blue-500'));
dropZone.addEventListener('drop', e => {
e.preventDefault();
dropZone.classList.remove('border-blue-500');
handleFile(e.dataTransfer.files[0]);
});
dropZone.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', e => handleFile(e.target.files[0]));
async function handleFile(file) {
fileName = file.name;
fileExtension = fileName.split('.').pop().toLowerCase();
const reader = new FileReader();
reader.onload = async (e) => {
try {
const data = new Uint8Array(e.target.result);
if (fileExtension === 'csv') {
const csvText = new TextDecoder('utf-8').decode(data);
workbook = XLSX.read(csvText, { type: 'string' });
} else {
workbook = XLSX.read(data, { type: 'array' });
}
columnSettings.classList.remove('hidden');
showStatus('File loaded successfully!', 'success');
} catch {
showStatus('Error reading file. Please upload a valid Excel or CSV file.', 'error');
}
};
reader.readAsArrayBuffer(file);
}
async function processFile() {
try {
const columnName = document.getElementById('columnName').value.trim();
const queryText = document.getElementById('queryText').value.trim();
if (!workbook || !columnName || !queryText) {
showStatus('Please upload a file and fill all fields.', 'error');
return;
}
showStatus('Processing file...', 'success');
const sheet = workbook.Sheets[workbook.SheetNames[0]];
jsonData = XLSX.utils.sheet_to_json(sheet);
// Load model and tokenizer
//const model = await AutoModel.from_pretrained('minishlab/M2V_base_output', { revision: 'refs/pr/1' });
//const tokenizer = await AutoTokenizer.from_pretrained('minishlab/M2V_base_output', { revision: 'refs/pr/2' });
const selectedModel = document.getElementById('modelSelect').value;
const model = await AutoModel.from_pretrained(selectedModel, {
config: { model_type: 'model2vec' },
dtype: 'fp32'
});
const tokenizer = await AutoTokenizer.from_pretrained(selectedModel);
const queryEmbedding = await getEmbedding(queryText, tokenizer, model);
//console.log("Query embeddings:", queryEmbedding);
let chunkEmbeddings = [];
// Process each row sequentially
for (const row of jsonData) {
const chunks = chunkText(row[columnName]);
console.log("Number of chunks: ", chunks.length);
let maxSimilarity = -Infinity;
let maxSimilarityChunk = '';
let similaritySum = 0;
// Process each chunk sequentially
for (const chunk of chunks) {
//console.log(chunk.length)
if (chunk.length > 32000) { console.log(row) }
try {
const chunkEmbedding = await getEmbedding(chunk, tokenizer, model);
//console.log("Chunk embedding:", chunkEmbedding);
if (chunkEmbedding && queryEmbedding) {
const similarity = cosineSimilarity(queryEmbedding, chunkEmbedding);
similaritySum += similarity;
if (similarity > maxSimilarity) {
maxSimilarity = similarity;
maxSimilarityChunk = chunk;
}
// If embeddings checkbox is checked, store the chunk embedding
if (embeddingsCheckbox.checked) {
chunkEmbeddings.push(chunkEmbedding);
}
}
} catch (error) {
console.error(`Error processing chunk: ${chunk}`, error, row);
}
}
// Update row with results, dynamically adding queryText to the column names
// Conditionally add columns based on checkbox selection
if (maxSimCheckbox.checked) {
row[`max_sim ${queryText}`] = maxSimilarity;
}
if (meanSimCheckbox.checked) {
row[`mean_sim ${queryText}`] = chunks.length > 0 ? similaritySum / chunks.length : 0;
}
if (maxSimChunkCheckbox.checked) {
row[`max_sim chunk_${queryText}`] = maxSimilarityChunk;
}
if (chunksCheckbox.checked) {
row[`chunks ${queryText}`] = chunks.length;
}
if (embeddingsCheckbox.checked) {
console.log(chunkEmbeddings)
row[`embeddings ${queryText}`] = JSON.stringify(chunkEmbeddings);
}
}
const newSheet = XLSX.utils.json_to_sheet(jsonData);
workbook.Sheets[workbook.SheetNames[0]] = newSheet;
let blob, mimeType, downloadExtension;
if (fileExtension === 'csv') {
const csvOutput = XLSX.utils.sheet_to_csv(newSheet);
blob = new Blob([csvOutput], { type: 'text/csv' });
mimeType = 'text/csv';
downloadExtension = 'csv';
} else {
const xlsxOutput = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
blob = new Blob([xlsxOutput], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
downloadExtension = 'xlsx';
}
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `modified_${fileName.replace(/\.[^/.]+$/, '')}.${downloadExtension}`;
a.click();
window.URL.revokeObjectURL(url);
showStatus('File processed and downloaded successfully! You can add another query by simply hitting "Process File" again.', 'success');
} catch (error) {
console.error('Error processing file:', error);
showStatus('Error processing file. Please check console for details.', 'error');
}
}
// Cosine similarity calculation
function cosineSimilarity(vecA, vecB) {
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < vecA.length; i++) {
dotProduct += vecA[i] * vecB[i];
normA += vecA[i] * vecA[i];
normB += vecB[i] * vecB[i];
}
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
// Embedding helper
async function getEmbedding(text, tokenizer, model) {
try {
if (!text || typeof text !== 'string') {
console.error('Invalid text input:', text);
return null;
}
const texts = [text]; // Handle single text input as an array
//console.log('Tokenizing text:', texts);
const tokenized = await tokenizer(texts, {
add_special_tokens: false,
return_tensor: false
});
//console.log('Tokenized result:', tokenized);
if (!tokenized.input_ids || !tokenized.input_ids.length) {
console.error('Tokenization failed');
return null;
}
// Compute offsets using cumulative sum approach
const cumsum = arr => arr.reduce((acc, num, i) => [...acc, num + (acc[i - 1] || 0)], []);
const offsets = [0, ...cumsum(tokenized.input_ids.slice(0, -1).map(x => x.length))];
// Flatten input IDs for model input
const flattened_input_ids = tokenized.input_ids.flat();
// Prepare model inputs with flattened IDs and calculated offsets
const model_inputs = {
input_ids: new Tensor('int64', flattened_input_ids, [flattened_input_ids.length]),
offsets: new Tensor('int64', offsets, [offsets.length]),
};
//console.log('Model inputs:', model_inputs);
// Get embeddings from model
const { embeddings } = await model(model_inputs);
const embeddings_list = embeddings.tolist()[0];
//console.log('Generated embeddings:', embeddings_list);
return embeddings_list;
} catch (error) {
console.error('Error in getEmbedding:', error);
return null;
}
}
processButton.addEventListener('click', processFile);
function showStatus(message, type) {
status.classList.remove('hidden');
statusMessage.textContent = message;
if (type === 'error') {
status.classList.replace('bg-green-50', 'bg-red-50');
statusMessage.classList.replace('text-green-800', 'text-red-800');
} else {
status.classList.replace('bg-red-50', 'bg-green-50');
statusMessage.classList.replace('text-red-800', 'text-green-800');
}
}