File size: 5,752 Bytes
006b9f1 | 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 |
(function(){
const cfg = window.EXPLORER_CONFIG;
const statusEl = document.getElementById("status");
const errEl = document.getElementById("error");
const tableEl = document.getElementById("table");
const searchEl = document.getElementById("search");
const filterEl = document.getElementById("filter");
const sourceEl = document.getElementById("source");
const countEl = document.getElementById("count");
function parseCSV(text){
const rows=[];
let row=[], field="", i=0, inQuotes=false;
const s=text.replace(/^\uFEFF/, "");
while(i<s.length){
const c=s[i];
if(inQuotes){
if(c==='"'){
if(s[i+1]==='"'){ field+='"'; i+=2; continue; }
inQuotes=false; i++; continue;
}
field+=c; i++; continue;
}
if(c==='"'){ inQuotes=true; i++; continue; }
if(c===","){ row.push(field); field=""; i++; continue; }
if(c==="\n" || (c==="\r" && s[i+1]==="\n")){
row.push(field); field="";
if(row.length>1 || row[0]!=="") rows.push(row);
row=[];
i += (c==="\r") ? 2 : 1;
continue;
}
if(c==="\r"){ row.push(field); field=""; if(row.length>1||row[0]!=="") rows.push(row); row=[]; i++; continue; }
field+=c; i++;
}
if(field!=="" || row.length){ row.push(field); rows.push(row); }
if(!rows.length) return {headers:[], records:[]};
const headers=rows[0].map(h=>h.trim());
const records=rows.slice(1).map(r=>{
const o={};
headers.forEach((h,idx)=>{ o[h]=r[idx]??""; });
return o;
});
return {headers, records};
}
function isUrl(v){ return /^https?:\/\//i.test(v||""); }
function render(headers, records){
const q=(searchEl.value||"").trim().toLowerCase();
const fv=filterEl && filterEl.value;
const filtered=records.filter(rec=>{
if(fv && filterEl.dataset.col && String(rec[filterEl.dataset.col]||"")!==fv) return false;
if(!q) return true;
return headers.some(h=>String(rec[h]??"").toLowerCase().includes(q));
});
countEl.textContent = `${filtered.length.toLocaleString()} of ${records.length.toLocaleString()} rows`;
const show=filtered.slice(0, 500);
let html="<table><thead><tr>"+headers.map(h=>`<th>${escapeHtml(h)}</th>`).join("")+"</tr></thead><tbody>";
for(const rec of show){
html+="<tr>";
for(const h of headers){
const v=String(rec[h]??"");
if(isUrl(v)) html+=`<td><a href="${escapeAttr(v)}" target="_blank" rel="noopener noreferrer">${escapeHtml(v)}</a></td>`;
else html+=`<td>${escapeHtml(v)}</td>`;
}
html+="</tr>";
}
html+="</tbody></table>";
if(filtered.length>500) html+=`<p class="meta">Showing the first 500 matching rows.</p>`;
tableEl.innerHTML=html;
}
function escapeHtml(s){
return String(s).replace(/[&<>"']/g, ch=>({"&":"&","<":"<",">":">","\"":""","'":"'"}[ch]));
}
function escapeAttr(s){ return escapeHtml(s).replace(/`/g,"`"); }
function fillFilter(headers, records){
const col=cfg.filterColumn;
if(!col || !filterEl) return;
if(!headers.includes(col)){ filterEl.style.display="none"; return; }
filterEl.dataset.col=col;
const vals=[...new Set(records.map(r=>String(r[col]??"")).filter(Boolean))].sort();
filterEl.innerHTML=`<option value="">All ${escapeHtml(col)}</option>`+vals.map(v=>`<option value="${escapeAttr(v)}">${escapeHtml(v)}</option>`).join("");
}
async function loadOne(ds){
const url=`https://huggingface.co/datasets/${ds.id}/resolve/main/${ds.file}`;
const res=await fetch(url, {headers:{"Accept":"text/csv,*/*"}});
if(!res.ok) throw new Error(`Could not load ${ds.id} (${res.status})`);
const text=await res.text();
const parsed=parseCSV(text);
const tag=ds.label || ds.id;
return parsed.records.map(r=>({...r, _dataset: tag}));
}
async function loadAll(){
errEl.hidden=true;
statusEl.textContent="Loading public Hugging Face dataset…";
tableEl.innerHTML="";
const selected = sourceEl ? sourceEl.value : "all";
const sources = (selected==="all") ? cfg.datasets : cfg.datasets.filter(d=>d.id===selected);
try{
const batches=await Promise.all(sources.map(loadOne));
let records=batches.flat();
let headers=[];
const seen=new Set();
for(const rec of records){
for(const k of Object.keys(rec)){
if(!seen.has(k)){ seen.add(k); headers.push(k); }
}
}
if(headers.includes("_dataset") && cfg.datasets.length===1){
headers=headers.filter(h=>h!=="_dataset");
records=records.map(r=>{ const o={...r}; delete o._dataset; return o; });
}
window.__ROWS={headers, records};
fillFilter(headers, records);
statusEl.textContent="Loaded from Hugging Face (public, no login).";
render(headers, records);
}catch(e){
statusEl.textContent="";
errEl.hidden=false;
errEl.textContent="Could not load the public dataset: "+e.message;
}
}
if(sourceEl && cfg.datasets.length>1){
sourceEl.innerHTML=`<option value="all">All datasets in this family</option>`+
cfg.datasets.map(d=>`<option value="${escapeAttr(d.id)}">${escapeHtml(d.label||d.id)}</option>`).join("");
sourceEl.addEventListener("change", loadAll);
}else if(sourceEl){
sourceEl.style.display="none";
const lab=document.getElementById("source-label");
if(lab) lab.style.display="none";
}
searchEl.addEventListener("input", ()=>{ if(window.__ROWS) render(window.__ROWS.headers, window.__ROWS.records); });
if(filterEl) filterEl.addEventListener("change", ()=>{ if(window.__ROWS) render(window.__ROWS.headers, window.__ROWS.records); });
loadAll();
})();
|