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
Creating a single HTML file that:
- Loads your ONNX model and tokenizer from URLs,
- Lets you upload a JSON file with multiple texts and their target values,
- Calculates embeddings for each text,
- Finds the top 5 embedding dimensions most correlated with the target values,
- Fits a linear regression model on those 5 dimensions,
- Allows you to reorder those 5 features,
- Predicts a ranking for your own input text,
is quite complex but doable with some compromises.
Key points / constraints:
- ONNX runtime in browser: use onnxruntime-web
- Tokenizer: your tokenizer files are JSON-based, so we can load them and implement a minimal tokenizer in JS.
- Linear regression + correlation: implement in JS.
- Keep code under ~300 lines: will be tight but possible with concise code and minimal UI.
What I will do:
- Use onnxruntime-web to load the model.
- Implement a minimal tokenizer based on your tokenizer.json.
- Upload JSON file with
{text: value}pairs. - Compute embeddings for each text.
- Compute correlation (r²) of each embedding dimension with target values.
- Select top 5 dimensions.
- Fit linear regression on those 5 dimensions.
- Show UI to reorder those 5 features.
- Predict ranking for new input text.
Assumptions:
- Your JSON file format is
{ "text1": value1, "text2": value2, ... } - The model input is token ids, output is embedding vector.
- Embedding dimension is known from model output shape (we'll infer from output).
- The model expects input named
input_ids(common for transformers). - The tokenizer is a simple WordPiece/BPE tokenizer with vocab in tokenizer.json.
Here is the full HTML file:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Mini-GTE ONNX Embeddings + Linear Regression</title> <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js"></script> <style> body { font-family: Arial, sans-serif; margin: 20px; max-width: 900px; } textarea, input, select { width: 100%; margin: 5px 0 15px; font-size: 1em; } label { font-weight: bold; } button { font-size: 1em; padding: 8px 12px; margin: 5px 0; } .flex { display: flex; gap: 10px; flex-wrap: wrap; } .feature { cursor: grab; padding: 5px 10px; background: #eee; border: 1px solid #ccc; border-radius: 4px; user-select: none; } .feature.dragging { opacity: 0.5; } #status { margin: 10px 0; font-style: italic; } </style> </head> <body> <h1>Mini-GTE ONNX Embeddings + Linear Regression</h1> <label>Upload JSON file with texts and target values (format: {"text1": value1, ...}):</label> <input type="file" id="jsonFile" accept=".json" /> <div id="status">Load model and tokenizer first.</div> <button id="loadModelBtn">Load Model & Tokenizer</button> <div id="trainSection" style="display:none;"> <button id="trainBtn">Calculate Embeddings & Train Model</button> </div> <div id="featuresSection" style="display:none;"> <label>Top 5 embedding features (drag to reorder):</label> <div id="features" class="flex"></div> <button id="predictBtn">Predict Ranking for New Text</button> </div> <div id="predictSection" style="display:none;"> <label>Enter text to predict ranking:</label> <textarea id="inputText" rows="3"></textarea> <button id="runPredictBtn">Run Prediction</button> <div id="predictionResult"></div> </div> <script> (async () => { // URLs for model and tokenizer files const MODEL_URL = "https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/model.onnx"; const TOKENIZER_URL = "https://huggingface.co/RainbowPiBubbles/mini-gte-onnx/resolve/main/tokenizer.json"; let session = null; let tokenizer = null; let embeddings = []; let targets = []; let topFeatures = []; let linearModel = null; // {weights: [], bias: number} let embeddingDim = 0; const statusEl = document.getElementById("status"); const loadModelBtn = document.getElementById("loadModelBtn"); const trainBtn = document.getElementById("trainBtn"); const trainSection = document.getElementById("trainSection"); const featuresSection = document.getElementById("featuresSection"); const featuresDiv = document.getElementById("features"); const predictBtn = document.getElementById("predictBtn"); const predictSection = document.getElementById("predictSection"); const runPredictBtn = document.getElementById("runPredictBtn"); const predictionResult = document.getElementById("predictionResult"); const inputText = document.getElementById("inputText"); const jsonFileInput = document.getElementById("jsonFile"); // Minimal tokenizer based on tokenizer.json async function loadTokenizer() { const res = await fetch(TOKENIZER_URL); const tokJson = await res.json(); // tokenizer.json format from HF usually has vocab and merges or BPE merges // We'll implement a simple tokenizer that splits on whitespace and maps tokens to ids // fallback to unknown token id if not found const vocab = tokJson.vocab || tokJson.model?.vocab || {}; const unkToken = tokJson.unk_token || "<unk>"; const unkId = vocab[unkToken] ?? 0; function tokenize(text) { // Simple whitespace tokenizer + vocab lookup // This is a simplification; your tokenizer might be more complex const tokens = text.toLowerCase().split(/\s+/).filter(Boolean); const ids = tokens.map(t => vocab[t] ?? unkId); // Add special tokens if needed (e.g. CLS, SEP) // For mini-gte, assume no special tokens needed or add 0 at start return [0, ...ids]; // prepend CLS token id 0 } return { tokenize }; } // Load ONNX model async function loadModel() { statusEl.textContent = "Loading ONNX model..."; session = await ort.InferenceSession.create(MODEL_URL); statusEl.textContent = "Model loaded."; } // Compute embeddings for a single text async function getEmbedding(text) { const inputIds = tokenizer.tokenize(text); // ONNX model input shape: [1, seq_len] const inputTensor = new ort.Tensor("int64", BigInt64Array.from(inputIds.map(BigInt)), [1, inputIds.length]); const feeds = { input_ids: inputTensor }; const results = await session.run(feeds); // Assume output is named 'last_hidden_state' or first output const outputName = Object.keys(results)[0]; const outputTensor = results[outputName]; // outputTensor dims: [1, seq_len, hidden_size] // We take the embedding of CLS token (first token) const hiddenSize = outputTensor.dims[2]; embeddingDim = hiddenSize; const data = outputTensor.data; // CLS token embedding is first token: slice first hiddenSize elements const clsEmbedding = data.slice(0, hiddenSize); return clsEmbedding; } // Pearson correlation squared (r^2) between two arrays function rSquared(x, y) { const n = x.length; const meanX = x.reduce((a,b) => a+b,0)/n; const meanY = y.reduce((a,b) => a+b,0)/n; let num=0, denX=0, denY=0; for(let i=0; i<n; i++) { const dx = x[i]-meanX; const dy = y[i]-meanY; num += dx*dy; denX += dx*dx; denY += dy*dy; } if(denX===0 || denY===0) return 0; const r = num/Math.sqrt(denX*denY); return r*r; } // Linear regression fit: y = Xw + b // X: n x k matrix, y: n vector // returns {weights: k vector, bias: scalar} function linearRegression(X, y) { const n = X.length; const k = X[0].length; // Compute means const meanX = new Array(k).fill(0); for(let j=0; j<k; j++) { for(let i=0; i<n; i++) meanX[j]+=X[i][j]; meanX[j]/=n; } const meanY = y.reduce((a,b)=>a+b,0)/n; // Compute covariance matrix and covariance vector const covXX = Array(k).fill(0).map(()=>Array(k).fill(0)); const covXY = new Array(k).fill(0); for(let i=0; i<n; i++) { for(let j=0; j<k; j++) { const dxj = X[i][j]-meanX[j]; covXY[j] += dxj*(y[i]-meanY); for(let l=0; l<k; l++) { const dxl = X[i][l]-meanX[l]; covXX[j][l] += dxj*dxl; } } } // Solve linear system covXX * w = covXY // Use simple Gaussian elimination function gaussSolve(A,b) { const m = A.length; A = A.map(row => row.slice()); b = b.slice(); for(let i=0; i<m; i++) { // pivot let maxRow = i; for(let r=i+1; r<m; r++) if(Math.abs(A[r][i])>Math.abs(A[maxRow][i])) maxRow=r; [A[i],A[maxRow]]=[A[maxRow],A[i]]; [b[i],b[maxRow]]=[b[maxRow],b[i]]; if(Math.abs(A[i][i])<1e-12) return null; // singular // eliminate for(let r=i+1; r<m; r++) { const f = A[r][i]/A[i][i]; for(let c=i; c<m; c++) A[r][c]-=f*A[i][c]; b[r]-=f*b[i]; } } // back substitution const x = new Array(m).fill(0); for(let i=m-1; i>=0; i--) { let s = b[i]; for(let c=i+1; c<m; c++) s-=A[i][c]*x[c]; x[i]=s/A[i][i]; } return x; } const w = gaussSolve(covXX, covXY); if(!w) return null; // Compute bias let b = meanY; for(let j=0; j<k; j++) b -= w[j]*meanX[j]; return {weights: w, bias: b}; } // Predict y from x vector and linear model function predict(x, model) { let y = model.bias; for(let i=0; i<x.length; i++) y += x[i]*model.weights[i]; return y; } // Drag and drop reorder for features function makeDraggable(container) { let dragSrcEl = null; function handleDragStart(e) { dragSrcEl = this; e.dataTransfer.effectAllowed = 'move'; e.dataTransfer.setData('text/html', this.innerHTML); this.classList.add('dragging'); } function handleDragOver(e) { if(e.preventDefault) e.preventDefault(); e.dataTransfer.dropEffect = 'move'; return false; } function handleDragEnter() { this.classList.add('over'); } function handleDragLeave() { this.classList.remove('over'); } function handleDrop(e) { if(e.stopPropagation) e.stopPropagation(); if(dragSrcEl !== this) { dragSrcEl.innerHTML = this.innerHTML; this.innerHTML = e.dataTransfer.getData('text/html'); } return false; } function handleDragEnd() { this.classList.remove('dragging'); [...container.children].forEach(c => c.classList.remove('over')); } [...container.children].forEach(item => { item.setAttribute('draggable', 'true'); item.addEventListener('dragstart', handleDragStart); item.addEventListener('dragenter', handleDragEnter); item.addEventListener('dragover', handleDragOver); item.addEventListener('dragleave', handleDragLeave); item.addEventListener('drop', handleDrop); item.addEventListener('dragend', handleDragEnd); }); } // Load model and tokenizer on button click loadModelBtn.onclick = async () => { loadModelBtn.disabled = true; statusEl.textContent = "Loading tokenizer..."; tokenizer = await loadTokenizer(); statusEl.textContent = "Loading model..."; await loadModel(); statusEl.textContent = "Model and tokenizer loaded. Upload JSON file and click 'Calculate Embeddings & Train Model'."; trainSection.style.display = "block"; }; // Read JSON file and parse function readJsonFile(file) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = e => { try { const obj = JSON.parse(e.target.result); resolve(obj); } catch(e) { reject(e); } }; reader.onerror = e => reject(e); reader.readAsText(file); }); } // Train model: compute embeddings, find top 5 features, fit linear regression trainBtn.onclick = async () => { if(!jsonFileInput.files.length) { alert("Please upload a JSON file first."); return; } trainBtn.disabled = true; statusEl.textContent = "Reading JSON file..."; let data; try { data = await readJsonFile(jsonFileInput.files[0]); } catch(e) { alert("Invalid JSON file."); trainBtn.disabled = false; return; } // data is {text: value} const texts = Object.keys(data); targets = texts.map(t => data[t]); statusEl.textContent = `Computing embeddings for ${texts.length} texts...`; embeddings = []; for(let i=0; i<texts.length; i++) { const emb = await getEmbedding(texts[i]); embeddings.push(emb); statusEl.textContent = `Computed embeddings: ${i+1}/${texts.length}`; await new Promise(r => setTimeout(r, 10)); // yield } // Compute r^2 for each embedding dimension const dim = embeddingDim; const r2s = []; for(let d=0; d<dim; d++) { const vals = embeddings.map(e => e[d]); r2s.push({dim: d, r2: rSquared(vals, targets)}); } r2s.sort((a,b) => b.r2 - a.r2); topFeatures = r2s.slice(0,5).map(f => f.dim); statusEl.textContent = `Top 5 features selected: ${topFeatures.join(", ")}`; // Prepare X matrix for linear regression const X = embeddings.map(e => topFeatures.map(d => e[d])); linearModel = linearRegression(X, targets); if(!linearModel) { alert("Failed to fit linear regression model."); trainBtn.disabled = false; return; } statusEl.textContent = "Linear regression model trained."; trainSection.style.display = "none"; featuresSection.style.display = "block"; // Show features with drag and drop featuresDiv.innerHTML = ""; topFeatures.forEach(d => { const div = document.createElement("div"); div.className = "feature"; div.textContent = `Feature ${d}`; featuresDiv.appendChild(div); }); makeDraggable(featuresDiv); predictSection.style.display = "none"; predictionResult.textContent = ""; trainBtn.disabled = false; }; // Predict button shows input box predictBtn.onclick = () => { predictSection.style.display = "block"; predictionResult.textContent = ""; }; // Run prediction on input text runPredictBtn.onclick = async () => { const text = inputText.value.trim(); if(!text) { alert("Enter some text to predict."); return; } runPredictBtn.disabled = true; statusEl.textContent = "Computing embedding for input text..."; const emb = await getEmbedding(text); // Get current feature order const order = [...featuresDiv.children].map(div => { const m = div.textContent.match(/Feature (\d+)/); return m ? parseInt(m[1]) : null; }); const x = order.map(d => emb[d]); const y = predict(x, linearModel); predictionResult.textContent = `Predicted ranking: ${y.toFixed(4)}`; statusEl.textContent = "Prediction done."; runPredictBtn.disabled = false; }; })(); </script> </body> </html>
How to use:
- Open this HTML file in a modern browser (Chrome/Firefox).
- Click Load Model & Tokenizer (loads ONNX model and tokenizer).
- Upload your JSON file with texts and target values (format:
{"text1": 1, "text2": 0.8, ...}). - Click Calculate Embeddings & Train Model.
- After training, drag and reorder the top 5 features if you want.
- Click Predict Ranking for New Text, enter your text, and click Run Prediction.
Notes:
- The tokenizer is a very simplified whitespace + vocab lookup tokenizer. If your tokenizer uses BPE merges or more complex tokenization, this will be approximate.
- The ONNX model input is assumed to be
input_idsof int64 tensor. - The output is assumed to be
[1, seq_len, hidden_size]and we take the first token embedding as the sentence embedding. - The linear regression is done with a simple covariance matrix solve.
- Drag and drop reorder is implemented for the 5 features.
- The entire app runs fully client-side.
If you want me to help with a more accurate tokenizer or a more complex UI, just ask!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.