Spaces:
Running
Running
reasoning-parser advisor
Browse files- app.js +113 -107
- progress.js +123 -0
app.js
CHANGED
|
@@ -1,107 +1,113 @@
|
|
| 1 |
-
/**
|
| 2 |
-
* The page: one repo id in, one serving decision out.
|
| 3 |
-
*
|
| 4 |
-
* All rules live in parser_advice.js, which the parity gate holds to the
|
| 5 |
-
* Python implementation. Nothing here decides anything — it fetches,
|
| 6 |
-
* calls, and renders, so a rule can never be quietly different in the UI
|
| 7 |
-
* from the one that was tested.
|
| 8 |
-
*/
|
| 9 |
-
import { ALL_MARKERS, advise, fetchTemplate } from './parser_advice.js';
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
const
|
| 13 |
-
const
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
seen.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
`<td><code>${
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
<span>
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
<code>
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
<
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
//
|
| 60 |
-
//
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
const
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
${
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
go
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* The page: one repo id in, one serving decision out.
|
| 3 |
+
*
|
| 4 |
+
* All rules live in parser_advice.js, which the parity gate holds to the
|
| 5 |
+
* Python implementation. Nothing here decides anything — it fetches,
|
| 6 |
+
* calls, and renders, so a rule can never be quietly different in the UI
|
| 7 |
+
* from the one that was tested.
|
| 8 |
+
*/
|
| 9 |
+
import { ALL_MARKERS, advise, fetchTemplate } from './parser_advice.js';
|
| 10 |
+
import { progress } from './progress.js';
|
| 11 |
+
|
| 12 |
+
const $ = (id) => document.getElementById(id);
|
| 13 |
+
const out = $('out');
|
| 14 |
+
const esc = (s) => String(s).replace(/[&<>"]/g, (c) =>
|
| 15 |
+
({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
|
| 16 |
+
|
| 17 |
+
function renderRules() {
|
| 18 |
+
const seen = new Map();
|
| 19 |
+
for (const [parser, marks] of Object.entries(ALL_MARKERS)) {
|
| 20 |
+
const key = marks.join(' ');
|
| 21 |
+
if (!seen.has(key)) seen.set(key, []);
|
| 22 |
+
seen.get(key).push(parser);
|
| 23 |
+
}
|
| 24 |
+
$('rules').innerHTML = [...seen.entries()].map(([marks, parsers]) =>
|
| 25 |
+
`<tr><td><code>${parsers.map(esc).join('</code>, <code>')}</code></td>` +
|
| 26 |
+
`<td><code>${marks.split(' ').map(esc).join('</code> <code>')}</code></td></tr>`
|
| 27 |
+
).join('');
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
function card(cls, title, body) {
|
| 31 |
+
return `<div class="card ${cls}"><h3>${title}</h3>${body}</div>`;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
function render(repo, source, result) {
|
| 35 |
+
const meta = `<div class="meta">
|
| 36 |
+
<span>repo <code>${esc(repo)}</code></span>
|
| 37 |
+
<span>template from <code>${esc(source)}</code></span>
|
| 38 |
+
</div>`;
|
| 39 |
+
|
| 40 |
+
if (result.verdict === 'unknown') {
|
| 41 |
+
return meta + card('check', 'No chat template published',
|
| 42 |
+
`<p>Nothing here can be decided from metadata. The repo publishes neither
|
| 43 |
+
<code>chat_template.jinja</code> nor a <code>chat_template</code> in
|
| 44 |
+
<code>tokenizer_config.json</code>, so there is no assistant turn to read.
|
| 45 |
+
Serving it will still work — the decision just has to come from somewhere
|
| 46 |
+
else.</p>`);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
if (result.verdict === 'none') {
|
| 50 |
+
return meta + card('clear', 'Serve it without a reasoning parser',
|
| 51 |
+
`<div class="flag">vllm serve ${esc(repo)}</div>
|
| 52 |
+
<p>The template carries no marker any parser closes on. Adding
|
| 53 |
+
<code>--reasoning-parser</code> here is the silent failure: the parser
|
| 54 |
+
would claim the whole answer, the request would finish with
|
| 55 |
+
<code>stop</code>, and <code>content</code> would come back empty.</p>`);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
const parsers = Object.keys(result.candidates).sort();
|
| 59 |
+
// Parsers sharing one marker set are interchangeable for this model.
|
| 60 |
+
// Naming the family one first is a convenience, not a rule the data
|
| 61 |
+
// supports, so it is offered as a preference rather than a verdict.
|
| 62 |
+
const preferred = parsers.find((p) => repo.toLowerCase().includes(p.split('_')[0]))
|
| 63 |
+
|| parsers[0];
|
| 64 |
+
const marks = result.candidates[preferred];
|
| 65 |
+
|
| 66 |
+
const others = parsers.filter((p) => p !== preferred);
|
| 67 |
+
const alt = others.length
|
| 68 |
+
? `<p>Interchangeable for this model, because they close on the same marker:
|
| 69 |
+
<code>${others.map(esc).join('</code>, <code>')}</code>. Pick the one named
|
| 70 |
+
for the model's family if there is one.</p>`
|
| 71 |
+
: '';
|
| 72 |
+
|
| 73 |
+
return meta + card('check', 'This model thinks — it needs a parser',
|
| 74 |
+
`<div class="flag">vllm serve ${esc(repo)} --reasoning-parser ${esc(preferred)}</div>
|
| 75 |
+
<p>Its template emits ${marks.map((m) => `<code>${esc(m)}</code>`).join(' and ')},
|
| 76 |
+
which <code>${esc(preferred)}</code> closes on.</p>
|
| 77 |
+
${alt}
|
| 78 |
+
<p>Without the flag that section reaches the user as the answer.</p>`);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
async function run(repo) {
|
| 82 |
+
const go = $('go');
|
| 83 |
+
go.disabled = true;
|
| 84 |
+
// Two small files at most, so this stays indeterminate — there is
|
| 85 |
+
// nothing to count. It is here for the same reason as on the other
|
| 86 |
+
// two pages: a page that shows nothing while it works reads as a page
|
| 87 |
+
// that does not work, and the wait is not always short.
|
| 88 |
+
const pg = progress(out).start(`reading the chat template of ${repo}`);
|
| 89 |
+
try {
|
| 90 |
+
const { template, source } = await fetchTemplate(repo);
|
| 91 |
+
pg.done();
|
| 92 |
+
out.innerHTML = render(repo, source, advise(template));
|
| 93 |
+
} catch (e) {
|
| 94 |
+
out.innerHTML = card('problem', 'Could not read the repo',
|
| 95 |
+
`<p>${esc(e.message || e)}</p>
|
| 96 |
+
<p>Gated and private repos cannot be read from the browser.</p>`);
|
| 97 |
+
} finally {
|
| 98 |
+
go.disabled = false;
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
$('form').addEventListener('submit', (e) => {
|
| 103 |
+
e.preventDefault();
|
| 104 |
+
const v = $('repo').value.trim();
|
| 105 |
+
if (v) run(v);
|
| 106 |
+
});
|
| 107 |
+
document.querySelectorAll('[data-ex]').forEach((b) =>
|
| 108 |
+
b.addEventListener('click', () => {
|
| 109 |
+
$('repo').value = b.dataset.ex;
|
| 110 |
+
run(b.dataset.ex);
|
| 111 |
+
}));
|
| 112 |
+
|
| 113 |
+
renderRules();
|
progress.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* The progress bar the three Spaces share.
|
| 3 |
+
*
|
| 4 |
+
* Every one of them reads safetensors headers over ranged requests — two
|
| 5 |
+
* per shard, and a large model has a dozen shards. That is seconds of
|
| 6 |
+
* silence behind a line of static text, which reads as a page that does
|
| 7 |
+
* not work. The bar exists so waiting looks like waiting.
|
| 8 |
+
*
|
| 9 |
+
* One file, copied into each Space at deploy time rather than written
|
| 10 |
+
* three times: three copies of a progress bar drift into three different
|
| 11 |
+
* behaviours, and the one that matters here is subtle — a bar that goes
|
| 12 |
+
* backwards, or sits at 100% while work continues, is worse than no bar,
|
| 13 |
+
* because it is believed.
|
| 14 |
+
*
|
| 15 |
+
* Two modes, and which one is honest depends on what is known:
|
| 16 |
+
*
|
| 17 |
+
* start(label) indeterminate — something is happening, and
|
| 18 |
+
* nobody knows how much of it there is yet
|
| 19 |
+
* total(n) switches to determinate once the shard count
|
| 20 |
+
* is known
|
| 21 |
+
* step(label) one unit done
|
| 22 |
+
* done() / fail() release it
|
| 23 |
+
*/
|
| 24 |
+
|
| 25 |
+
const BAR_CSS = `
|
| 26 |
+
.pg { margin: 1.25rem 0 .5rem; }
|
| 27 |
+
.pg-label { font-size: .88rem; color: var(--muted); margin-bottom: .45rem;
|
| 28 |
+
display: flex; justify-content: space-between; gap: 1rem; }
|
| 29 |
+
.pg-count { font-family: var(--mono); font-size: .82rem; white-space: nowrap; }
|
| 30 |
+
.pg-track { height: .4rem; border-radius: .2rem; background: var(--line);
|
| 31 |
+
overflow: hidden; position: relative; }
|
| 32 |
+
.pg-fill { height: 100%; background: var(--accent); border-radius: .2rem;
|
| 33 |
+
width: 0; transition: width .18s ease-out; }
|
| 34 |
+
.pg-track.indeterminate .pg-fill {
|
| 35 |
+
width: 35%; animation: pg-slide 1.1s ease-in-out infinite;
|
| 36 |
+
}
|
| 37 |
+
@keyframes pg-slide {
|
| 38 |
+
0% { transform: translateX(-100%); }
|
| 39 |
+
100% { transform: translateX(300%); }
|
| 40 |
+
}
|
| 41 |
+
@media (prefers-reduced-motion: reduce) {
|
| 42 |
+
.pg-track.indeterminate .pg-fill { animation: none; width: 100%; opacity: .45; }
|
| 43 |
+
.pg-fill { transition: none; }
|
| 44 |
+
}`;
|
| 45 |
+
|
| 46 |
+
let styled = false;
|
| 47 |
+
|
| 48 |
+
function ensureStyle() {
|
| 49 |
+
if (styled) return;
|
| 50 |
+
const el = document.createElement('style');
|
| 51 |
+
el.textContent = BAR_CSS;
|
| 52 |
+
document.head.appendChild(el);
|
| 53 |
+
styled = true;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
/**
|
| 57 |
+
* A progress bar mounted in `host`.
|
| 58 |
+
*
|
| 59 |
+
* It replaces the host's contents, so the caller renders the result into
|
| 60 |
+
* the same element when it finishes and the bar disappears with it.
|
| 61 |
+
*/
|
| 62 |
+
export function progress(host) {
|
| 63 |
+
ensureStyle();
|
| 64 |
+
let done = 0;
|
| 65 |
+
let total = 0;
|
| 66 |
+
|
| 67 |
+
host.innerHTML = `<div class="pg">
|
| 68 |
+
<div class="pg-label"><span class="pg-text"></span><span class="pg-count"></span></div>
|
| 69 |
+
<div class="pg-track indeterminate"><div class="pg-fill"></div></div>
|
| 70 |
+
</div>`;
|
| 71 |
+
|
| 72 |
+
const text = host.querySelector('.pg-text');
|
| 73 |
+
const count = host.querySelector('.pg-count');
|
| 74 |
+
const track = host.querySelector('.pg-track');
|
| 75 |
+
const fill = host.querySelector('.pg-fill');
|
| 76 |
+
|
| 77 |
+
const paint = () => {
|
| 78 |
+
if (!total) return;
|
| 79 |
+
// Never let the bar reach the end before the work does: a full bar
|
| 80 |
+
// with the page still frozen is exactly the impression this is here
|
| 81 |
+
// to remove. It closes only in done().
|
| 82 |
+
const pctDone = Math.min(99, Math.round((done / total) * 100));
|
| 83 |
+
fill.style.width = `${pctDone}%`;
|
| 84 |
+
count.textContent = `${done} / ${total}`;
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
return {
|
| 88 |
+
/** Say what is happening now, without claiming to know how long. */
|
| 89 |
+
start(label) {
|
| 90 |
+
text.textContent = label;
|
| 91 |
+
return this;
|
| 92 |
+
},
|
| 93 |
+
/**
|
| 94 |
+
* Begin a determinate phase of `n` units.
|
| 95 |
+
*
|
| 96 |
+
* Resets the count, because a second call means a second phase — the
|
| 97 |
+
* integrity checker reads the pack and then the source model, and
|
| 98 |
+
* without the reset the bar would show "12 / 5" and run past its own
|
| 99 |
+
* end.
|
| 100 |
+
*/
|
| 101 |
+
total(n) {
|
| 102 |
+
total = n;
|
| 103 |
+
done = 0;
|
| 104 |
+
track.classList.remove('indeterminate');
|
| 105 |
+
paint();
|
| 106 |
+
return this;
|
| 107 |
+
},
|
| 108 |
+
/** One unit finished. Safe to call before total() — it just counts. */
|
| 109 |
+
step(label) {
|
| 110 |
+
done += 1;
|
| 111 |
+
if (label) text.textContent = label;
|
| 112 |
+
paint();
|
| 113 |
+
return this;
|
| 114 |
+
},
|
| 115 |
+
/** Fill it before the caller replaces the host with the result. */
|
| 116 |
+
done() {
|
| 117 |
+
track.classList.remove('indeterminate');
|
| 118 |
+
fill.style.width = '100%';
|
| 119 |
+
if (total) count.textContent = `${total} / ${total}`;
|
| 120 |
+
return this;
|
| 121 |
+
},
|
| 122 |
+
};
|
| 123 |
+
}
|