YMRohit Codex commited on
Commit
281cd85
·
1 Parent(s): e55beda

Make leaderboard refresh visible

Browse files

Co-authored-by: Codex <codex@openai.com>

Files changed (2) hide show
  1. app.py +99 -17
  2. style.css +6 -0
app.py CHANGED
@@ -497,14 +497,29 @@ def do_mint(recipe_json: str) -> str:
497
 
498
 
499
  def fetch_lb(_=None):
 
 
 
 
500
  for u in (ONE_B, PRO):
501
  try:
502
- rows = requests.get(lb_url(u), timeout=20).json().get("rows", [])
503
- if rows:
504
- return _blob({"rows": rows})
505
- except Exception:
 
 
 
 
 
 
 
 
 
506
  continue
507
- return _blob({"rows": []})
 
 
508
 
509
 
510
  CSS = open(os.path.join(os.path.dirname(__file__), "style.css")).read() if os.path.exists(
@@ -518,7 +533,9 @@ except Exception:
518
  REPLAYS_JSON = "{}"
519
 
520
  APP_HTML = r"""
521
- <div id="km" data-build="2026-06-15-mint-reset-lb-live">
 
 
522
  <div id="km-top">
523
  <div id="km-brand">
524
  <span class="mark"><span class="dot"></span>Kernel Mint</span>
@@ -656,7 +673,10 @@ APP_HTML = r"""
656
  <div class="km-h">Leaderboard</div>
657
  <p class="km-lede">The best verified kernel per operation. Crowned rows were minted by the 27B model. Every speedup here was measured by the referee, not reported by the model.</p>
658
  <div id="km-lb-view" class="km-dim" style="margin-top:16px">Loading.</div>
659
- <button id="km-lb-refresh" class="km-mini">Refresh</button>
 
 
 
660
  </section>
661
  </div>
662
 
@@ -871,7 +891,10 @@ print(result.to_dict())</div>
871
  </div>
872
  """
873
 
874
- APP_HTML = APP_HTML.replace("__KM_REPLAYS__", REPLAYS_JSON)
 
 
 
875
 
876
  APP_JS = r"""
877
  () => {
@@ -906,6 +929,7 @@ APP_JS = r"""
906
  if(!root||root.dataset.init)return;
907
  root.dataset.init='1';
908
  const $=s=>root.querySelector(s), $$=s=>Array.from(root.querySelectorAll(s));
 
909
  const cleanText=s=>String(s??'').replace(/\u2013|\u2014/g,' - ').replace(/\u2192/g,'->').replace(/\u00d7/g,'x').replace(/[\u2600-\u27BF]/g,'').replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,'').replace(/\s+/g,' ').trim();
910
  const esc=s=>cleanText(s).replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
911
  let REPLAYS={};
@@ -919,7 +943,7 @@ APP_JS = r"""
919
 
920
  const state={tab:'build',mode:'build',slots:{mem:false,norm:'rmsnorm',act:'gelu',act2:null},classic:'softmax'};
921
  let buildTries=4, expertTries=5, resolvedExpert=null, lastMintedMode='build';
922
- let activeMintNonce=null, awaitingMint=false, pendingLBRefresh=false, lbRefreshTimer=null, lbPollTimer=null;
923
  const HIST=[];
924
 
925
  function animate(el,frames,opts){ if(el&&el.animate)el.animate(frames,opts); }
@@ -961,7 +985,7 @@ APP_JS = r"""
961
  $$('.km-page').forEach(p=>{p.hidden=p.dataset.page!==tab;});
962
  requestAnimationFrame(positionTabPill);
963
  if(tab==='expert')resolveExpert();
964
- if(tab==='lb'){refreshLB(); setLBPolling(true);}
965
  else setLBPolling(false);
966
  }
967
  $$('.km-tab').forEach(b=>b.addEventListener('click',()=>showTab(b.dataset.tab)));
@@ -1236,19 +1260,71 @@ APP_JS = r"""
1236
  if(g)g.click();
1237
  },80);
1238
  }
1239
- function refreshLB(){
1240
- if(awaitingMint){pendingLBRefresh=true;return;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1241
  const btn=document.querySelector('#km_lb_btn button')||document.querySelector('#km_lb_btn');
1242
  if(btn)btn.click();
 
 
 
 
1243
  }
1244
  function scheduleLBRefresh(delay=700){
1245
  pendingLBRefresh=false;
1246
  if(lbRefreshTimer)clearTimeout(lbRefreshTimer);
1247
- lbRefreshTimer=setTimeout(refreshLB,delay);
1248
  }
1249
  function setLBPolling(on){
1250
  if(lbPollTimer){clearInterval(lbPollTimer);lbPollTimer=null;}
1251
- if(on)lbPollTimer=setInterval(()=>{if(state.tab==='lb')refreshLB();},15000);
1252
  }
1253
  let mintSafetyTimer=null;
1254
  function setMinting(on){
@@ -1482,9 +1558,15 @@ APP_JS = r"""
1482
  render();
1483
  }
1484
 
1485
- function renderLB(rows){
1486
  const el=$('#km-lb-view');
1487
  if(!el)return;
 
 
 
 
 
 
1488
  if(!rows.length){el.innerHTML='<i>No kernels yet. Be the first.</i>';return;}
1489
  el.innerHTML="<table class='km-lbt'><thead><tr><th>#</th><th>machine</th><th>builder</th><th>vs compiler</th><th>vs PyTorch</th></tr></thead><tbody>"+
1490
  rows.slice(0,25).map((r,i)=>"<tr class='"+(r.champion?'champ':'')+"'><td class='km-rank'>"+(i+1)+"</td><td><code>"+esc(r.op||'kernel')+"</code></td><td>"+esc(r.name||'anonymous')+"</td><td class='hot'>"+sp2(r.speedup_compile)+"x</td><td>"+sp(r.speedup_eager)+"x</td></tr>").join('')+"</tbody></table>";
@@ -1499,9 +1581,9 @@ APP_JS = r"""
1499
  new MutationObserver(read).observe(el,{childList:true,subtree:true});
1500
  read();
1501
  }
1502
- setTimeout(()=>{watch('#km_result',showResult);watch('#km_lb',d=>renderLB(d.rows||[]));},250);
1503
  const rf=$('#km-lb-refresh');
1504
- if(rf)rf.addEventListener('click',refreshLB);
1505
 
1506
  const CLASSIC_TERMS=[
1507
  {key:'softmax_scale',terms:['scaled softmax','scale softmax','attention scale']},
 
497
 
498
 
499
  def fetch_lb(_=None):
500
+ rows = []
501
+ seen = set()
502
+ sources = []
503
+ errors = []
504
  for u in (ONE_B, PRO):
505
  try:
506
+ url = lb_url(u)
507
+ got = requests.get(url, timeout=20).json().get("rows", [])
508
+ if got:
509
+ sources.append(url)
510
+ for row in got:
511
+ key = (row.get("op"), row.get("name"), row.get("ts"),
512
+ row.get("speedup_compile"), row.get("speedup_eager"))
513
+ if key in seen:
514
+ continue
515
+ seen.add(key)
516
+ rows.append(row)
517
+ except Exception as e:
518
+ errors.append(f"{lb_url(u)}: {type(e).__name__}")
519
  continue
520
+ # Always include fetched_at so the hidden Gradio HTML changes even when the rows are identical.
521
+ # That makes the visible Refresh button observably work.
522
+ return _blob({"rows": rows, "fetched_at": time.time(), "sources": sources, "errors": errors[:2]})
523
 
524
 
525
  CSS = open(os.path.join(os.path.dirname(__file__), "style.css")).read() if os.path.exists(
 
533
  REPLAYS_JSON = "{}"
534
 
535
  APP_HTML = r"""
536
+ <div id="km" data-build="2026-06-15-leaderboard-refresh-fallback"
537
+ data-lb-one="__KM_LB_ONE__"
538
+ data-lb-pro="__KM_LB_PRO__">
539
  <div id="km-top">
540
  <div id="km-brand">
541
  <span class="mark"><span class="dot"></span>Kernel Mint</span>
 
673
  <div class="km-h">Leaderboard</div>
674
  <p class="km-lede">The best verified kernel per operation. Crowned rows were minted by the 27B model. Every speedup here was measured by the referee, not reported by the model.</p>
675
  <div id="km-lb-view" class="km-dim" style="margin-top:16px">Loading.</div>
676
+ <div class="km-lb-tools">
677
+ <button id="km-lb-refresh" class="km-mini">Refresh</button>
678
+ <span id="km-lb-status" class="km-lb-status">Auto-refreshes while this tab is open.</span>
679
+ </div>
680
  </section>
681
  </div>
682
 
 
891
  </div>
892
  """
893
 
894
+ APP_HTML = (APP_HTML
895
+ .replace("__KM_REPLAYS__", REPLAYS_JSON)
896
+ .replace("__KM_LB_ONE__", lb_url(ONE_B))
897
+ .replace("__KM_LB_PRO__", lb_url(PRO)))
898
 
899
  APP_JS = r"""
900
  () => {
 
929
  if(!root||root.dataset.init)return;
930
  root.dataset.init='1';
931
  const $=s=>root.querySelector(s), $$=s=>Array.from(root.querySelectorAll(s));
932
+ const LB_URLS=[root.dataset.lbOne,root.dataset.lbPro].filter(Boolean);
933
  const cleanText=s=>String(s??'').replace(/\u2013|\u2014/g,' - ').replace(/\u2192/g,'->').replace(/\u00d7/g,'x').replace(/[\u2600-\u27BF]/g,'').replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,'').replace(/\s+/g,' ').trim();
934
  const esc=s=>cleanText(s).replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
935
  let REPLAYS={};
 
943
 
944
  const state={tab:'build',mode:'build',slots:{mem:false,norm:'rmsnorm',act:'gelu',act2:null},classic:'softmax'};
945
  let buildTries=4, expertTries=5, resolvedExpert=null, lastMintedMode='build';
946
+ let activeMintNonce=null, awaitingMint=false, pendingLBRefresh=false, lbRefreshTimer=null, lbPollTimer=null, lbUiTimer=null;
947
  const HIST=[];
948
 
949
  function animate(el,frames,opts){ if(el&&el.animate)el.animate(frames,opts); }
 
985
  $$('.km-page').forEach(p=>{p.hidden=p.dataset.page!==tab;});
986
  requestAnimationFrame(positionTabPill);
987
  if(tab==='expert')resolveExpert();
988
+ if(tab==='lb'){refreshLB(true); setLBPolling(true);}
989
  else setLBPolling(false);
990
  }
991
  $$('.km-tab').forEach(b=>b.addEventListener('click',()=>showTab(b.dataset.tab)));
 
1260
  if(g)g.click();
1261
  },80);
1262
  }
1263
+ function setLBStatus(text,kind){
1264
+ const s=$('#km-lb-status');
1265
+ if(!s)return;
1266
+ s.textContent=text;
1267
+ s.classList.toggle('bad',kind==='bad');
1268
+ s.classList.toggle('warn',kind==='warn');
1269
+ }
1270
+ function setLBButton(on){
1271
+ const b=$('#km-lb-refresh');
1272
+ if(!b)return;
1273
+ b.disabled=on;
1274
+ b.textContent=on?'Refreshing...':'Refresh';
1275
+ }
1276
+ async function refreshLBDirect(){
1277
+ const rows=[], seen=new Set(), sources=[];
1278
+ for(const url of LB_URLS){
1279
+ try{
1280
+ const res=await fetch(url,{cache:'no-store'});
1281
+ if(!res.ok)continue;
1282
+ const data=await res.json();
1283
+ const got=Array.isArray(data.rows)?data.rows:[];
1284
+ if(got.length)sources.push(url);
1285
+ got.forEach(row=>{
1286
+ const key=[row.op,row.name,row.ts,row.speedup_compile,row.speedup_eager].join('|');
1287
+ if(seen.has(key))return;
1288
+ seen.add(key);
1289
+ rows.push(row);
1290
+ });
1291
+ }catch(e){}
1292
+ }
1293
+ if(!sources.length)throw new Error('direct leaderboard fetch failed');
1294
+ renderLB(rows,{fetched_at:Date.now()/1000,sources});
1295
+ }
1296
+ function refreshLB(manual=false){
1297
+ if(awaitingMint){
1298
+ pendingLBRefresh=true;
1299
+ if(manual)setLBStatus('Queued until the current mint finishes.','warn');
1300
+ return;
1301
+ }
1302
+ if(manual||state.tab==='lb'){
1303
+ setLBButton(true);
1304
+ setLBStatus('Checking leaderboard...','');
1305
+ if(lbUiTimer)clearTimeout(lbUiTimer);
1306
+ lbUiTimer=setTimeout(()=>{
1307
+ refreshLBDirect().catch(()=>{
1308
+ setLBButton(false);
1309
+ setLBStatus('Still waiting for the Gradio refresh bridge. Direct browser fetch was blocked. Try once more or reopen the tab.','warn');
1310
+ });
1311
+ },8000);
1312
+ }
1313
  const btn=document.querySelector('#km_lb_btn button')||document.querySelector('#km_lb_btn');
1314
  if(btn)btn.click();
1315
+ else{
1316
+ setLBButton(false);
1317
+ setLBStatus('Refresh bridge is not mounted yet. Reopen the Leaderboard tab.','bad');
1318
+ }
1319
  }
1320
  function scheduleLBRefresh(delay=700){
1321
  pendingLBRefresh=false;
1322
  if(lbRefreshTimer)clearTimeout(lbRefreshTimer);
1323
+ lbRefreshTimer=setTimeout(()=>refreshLB(false),delay);
1324
  }
1325
  function setLBPolling(on){
1326
  if(lbPollTimer){clearInterval(lbPollTimer);lbPollTimer=null;}
1327
+ if(on)lbPollTimer=setInterval(()=>{if(state.tab==='lb')refreshLB(false);},15000);
1328
  }
1329
  let mintSafetyTimer=null;
1330
  function setMinting(on){
 
1558
  render();
1559
  }
1560
 
1561
+ function renderLB(rows,meta={}){
1562
  const el=$('#km-lb-view');
1563
  if(!el)return;
1564
+ if(lbUiTimer){clearTimeout(lbUiTimer);lbUiTimer=null;}
1565
+ setLBButton(false);
1566
+ const fetched=Number(meta.fetched_at||0);
1567
+ const stamp=fetched?new Date(fetched*1000).toLocaleTimeString([], {hour:'2-digit', minute:'2-digit', second:'2-digit'}):'just now';
1568
+ const sourceCount=Array.isArray(meta.sources)?meta.sources.length:0;
1569
+ setLBStatus('Last checked '+stamp+(sourceCount?' from '+sourceCount+' endpoint'+(sourceCount>1?'s':'')+'.':'.'),'');
1570
  if(!rows.length){el.innerHTML='<i>No kernels yet. Be the first.</i>';return;}
1571
  el.innerHTML="<table class='km-lbt'><thead><tr><th>#</th><th>machine</th><th>builder</th><th>vs compiler</th><th>vs PyTorch</th></tr></thead><tbody>"+
1572
  rows.slice(0,25).map((r,i)=>"<tr class='"+(r.champion?'champ':'')+"'><td class='km-rank'>"+(i+1)+"</td><td><code>"+esc(r.op||'kernel')+"</code></td><td>"+esc(r.name||'anonymous')+"</td><td class='hot'>"+sp2(r.speedup_compile)+"x</td><td>"+sp(r.speedup_eager)+"x</td></tr>").join('')+"</tbody></table>";
 
1581
  new MutationObserver(read).observe(el,{childList:true,subtree:true});
1582
  read();
1583
  }
1584
+ setTimeout(()=>{watch('#km_result',showResult);watch('#km_lb',d=>renderLB(d.rows||[],d));},250);
1585
  const rf=$('#km-lb-refresh');
1586
+ if(rf)rf.addEventListener('click',()=>refreshLB(true));
1587
 
1588
  const CLASSIC_TERMS=[
1589
  {key:'softmax_scale',terms:['scaled softmax','scale softmax','attention scale']},
style.css CHANGED
@@ -370,6 +370,11 @@ input[type=range].km-range::-moz-range-thumb { width: 16px; height: 16px; border
370
  .km-lbt .hot { color: var(--accent); font-family: var(--mono); font-weight: 600; font-variant-numeric: tabular-nums; }
371
  .km-lbt td:nth-child(5) { color: var(--muted); font-family: var(--mono); font-variant-numeric: tabular-nums; }
372
  .km-rank { color: var(--faint); font-family: var(--mono); }
 
 
 
 
 
373
 
374
  /* ---------- run + verify docs ---------- */
375
  .km-docs { width: 100%; }
@@ -441,6 +446,7 @@ input[type=range].km-range::-moz-range-thumb { width: 16px; height: 16px; border
441
  font: 500 12.5px var(--sans); cursor: pointer; padding: 8px 13px; border-radius: var(--r-sm); margin-top: 14px;
442
  transition: all 0.18s var(--ease); }
443
  .km-mini:hover { color: var(--text); border-color: var(--line-2); }
 
444
 
445
  @media (prefers-reduced-motion: reduce) {
446
  #km *, #km *::before, #km *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
 
370
  .km-lbt .hot { color: var(--accent); font-family: var(--mono); font-weight: 600; font-variant-numeric: tabular-nums; }
371
  .km-lbt td:nth-child(5) { color: var(--muted); font-family: var(--mono); font-variant-numeric: tabular-nums; }
372
  .km-rank { color: var(--faint); font-family: var(--mono); }
373
+ .km-lb-tools { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 14px; }
374
+ .km-lb-tools .km-mini { margin-top: 0; }
375
+ .km-lb-status { color: var(--faint); font-size: 12.5px; line-height: 1.45; }
376
+ .km-lb-status.warn { color: var(--amber); }
377
+ .km-lb-status.bad { color: var(--danger); }
378
 
379
  /* ---------- run + verify docs ---------- */
380
  .km-docs { width: 100%; }
 
446
  font: 500 12.5px var(--sans); cursor: pointer; padding: 8px 13px; border-radius: var(--r-sm); margin-top: 14px;
447
  transition: all 0.18s var(--ease); }
448
  .km-mini:hover { color: var(--text); border-color: var(--line-2); }
449
+ .km-mini:disabled { opacity: 0.62; cursor: wait; transform: none; }
450
 
451
  @media (prefers-reduced-motion: reduce) {
452
  #km *, #km *::before, #km *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }