https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/model.onnx…
- User
https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/model.onnx https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/config.json https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/special_tokens_map.json https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/tokenizer.json https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/tokenizer_config.json https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/tokenizer_config.json
Can you make a single html file that lets me upload a json with several texts, calculate embeddings(from the model above) for each of them, and calculate a linear regression model(line of best fit) to predict based on the top 5 most correlated (r squared) variables from the embeddings to corresponding the number in the json. It only uses 5 parameters so it doesnt overfit. I should be able to edit the order Then, it should let me predict a ranking of my own text
Preferably like around 300 or less lines of code
Here is an example of what the json looks like: { "a": 1, "b": 0.8, "c": 0.6666666666666666, "d": 0, "e": 1, "f": 0.5714285714285714, "g": 0, "h": 0 }
- Assistant
<!DOCTYPE html> <html> <head> <title>MiniGTE Embedding Regressor</title> <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@0.5.0/dist/ort.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@tokenizer/bert@0.0.1/dist/index.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.0.3/math.min.js"></script> </head> <body> <h1>Upload JSON</h1> <input type="file" id="jsonFile" accept=".json"> <pre id="status"></pre> <div id="features" style="display:none;"> <h2>Top Features</h2> <ul id="featureList"></ul> <button onclick="trainModel()">Retrain Model</button> </div> <div id="predictor" style="display:none;"> <h2>Predict New Text</h2> <input type="text" id="newText"> <button onclick="predictRanking()">Predict</button> <p id="predictionResult"></p> </div> <script> let tokenizer, session; let embeddings = [], labels = [], topFeatures = []; let coefficients = [], intercept = 0; async function loadModelAndTokenizer() { const tokenizerResponse = await fetch('https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/tokenizer.json'); const tokenizerJson = await tokenizerResponse.json(); tokenizer = BertTokenizer.fromTokenizerJson(tokenizerJson); session = new ort.InferenceSession(); await session.loadModel('https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/model.onnx'); log('Model and tokenizer loaded.'); } function log(message) { document.getElementById('status').textContent += message + '\n'; } document.getElementById('jsonFile').addEventListener('change', handleFileUpload); async function handleFileUpload(event) { const file = event.target.files[0]; const reader = new FileReader(); reader.onload = async function(e) { try { const data = JSON.parse(e.target.result); const keys = Object.keys(data); const texts = keys; labels = keys.map(k => data[k]); embeddings = []; log(`Processing ${texts.length} texts...`); for (let i = 0; i < texts.length; i++) { const text = texts[i]; const encoded = tokenizer.encode(text, { add_special_tokens: true, max_length: 512, padding: 'max_length', truncation: true }); const inputIds = new ort.Tensor('int64', encoded.ids, [1, encoded.ids.length]); const attentionMask = new ort.Tensor('int64', encoded.attention_mask, [1, encoded.attention_mask.length]); const outputs = await session.run({ input_ids: inputIds, attention_mask: attentionMask }); const outputName = Object.keys(outputs)[0]; const embedding = outputs[outputName].data; embeddings.push(Array.from(embedding)); } log('Embeddings generated. Calculating correlations...'); const numFeatures = embeddings[0].length; const featureStats = []; for (let f = 0; f < numFeatures; f++) { const x = embeddings.map(e => e[f]); const { r } = computeCorrelation(x, labels); featureStats.push({ feature: f, rSquared: r * r, correlation: r }); } featureStats.sort((a, b) => Math.abs(b.rSquared) - Math.abs(a.rSquared)); topFeatures = featureStats.slice(0, 5); displayFeatures(); await trainModel(); } catch (err) { log('Error: ' + err.message); } }; reader.readAsText(file); } function computeCorrelation(x, y) { const n = x.length; let sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0, sumY2 = 0; for (let i = 0; i < n; i++) { sumX += x[i]; sumY += y[i]; sumXY += x[i] * y[i]; sumX2 += x[i] * x[i]; sumY2 += y[i] * y[i]; } const numerator = (n * sumXY) - (sumX * sumY); const denominator = Math.sqrt((n * sumX2 - sumX * sumX) * (n * sumY2 - sumY * sumY)); return { r: denominator === 0 ? 0 : numerator / denominator }; } function displayFeatures() { const list = document.getElementById('featureList'); list.innerHTML = ''; topFeatures.forEach((f, index) => { const li = document.createElement('li'); li.innerHTML = `Feature ${f.feature} (R²: ${f.rSquared.toFixed(4)}) <button onclick="moveUp(${index})">↑</button> <button onclick="moveDown(${index})">↓</button>`; list.appendChild(li); }); document.getElementById('features').style.display = 'block'; } function moveUp(index) { if (index > 0) { [topFeatures[index], topFeatures[index-1]] = [topFeatures[index-1], topFeatures[index]]; displayFeatures(); } } function moveDown(index) { if (index < topFeatures.length - 1) { [topFeatures[index], topFeatures[index+1]] = [topFeatures[index+1], topFeatures[index]]; displayFeatures(); } } async function trainModel() { if (!embeddings.length || !topFeatures.length) return; const selectedFeatures = topFeatures.map(f => f.feature); const X = embeddings.map(embed => selectedFeatures.map(f => embed[f])); const XWithIntercept = X.map(row => [1.0, ...row]); const y = labels.map(v => [v]); try { const Xt = math.matrix(math.transpose(XWithIntercept)); const X = math.matrix(XWithIntercept); const yVec = math.matrix(y); const XtX = math.multiply(Xt, X); const Xty = math.multiply(Xt, yVec); const invXtX = math.inv(XtX); const beta = math.multiply(invXtX, Xty); coefficients = beta._data.map(d => d[0]); intercept = coefficients[0]; featureCoefficients = coefficients.slice(1).map((coef, i) => ({ feature: selectedFeatures[i], coefficient: coef, correlation: topFeatures[i].correlation })); log('Model trained. Coefficients: ' + JSON.stringify(featureCoefficients)); document.getElementById('predictor').style.display = 'block'; } catch (err) { log('Training failed: ' + err.message); } } async function predictRanking() { const text = document.getElementById('newText').value; if (!text) return alert('Enter text to predict.'); const encoded = tokenizer.encode(text, { add_special_tokens: true, max_length: 512, padding: 'max_length', truncation: true }); const inputIds = new ort.Tensor('int64', encoded.ids, [1, encoded.ids.length]); const attentionMask = new ort.Tensor('int64', encoded.attention_mask, [1, encoded.attention_mask.length]); const outputs = await session.run({ input_ids: inputIds, attention_mask: attentionMask }); const embedding = Array.from(outputs[Object.keys(outputs)[0]].data); const features = topFeatures.map(f => embedding[f.feature]); let score = intercept; for (let i = 0; i < features.length; i++) { score += features[i] * featureCoefficients[i].coefficient; } document.getElementById('predictionResult').innerText = `Predicted Score: ${score.toFixed(4)}`; } loadModelAndTokenizer(); </script> </body> </html>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.