srish327 commited on
Commit
1b38872
·
verified ·
1 Parent(s): ff23f1c

In-browser demo

Browse files
Files changed (3) hide show
  1. README.md +8 -5
  2. extract.js +84 -0
  3. index.html +158 -17
README.md CHANGED
@@ -1,10 +1,13 @@
1
  ---
2
- title: Skill Extractor
3
- emoji: 📚
4
- colorFrom: indigo
5
- colorTo: red
6
  sdk: static
7
  pinned: false
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
  ---
2
+ title: Job Posting Skill Extractor
3
+ emoji: 🧰
4
+ colorFrom: green
5
+ colorTo: blue
6
  sdk: static
7
  pinned: false
8
+ license: apache-2.0
9
  ---
10
 
11
+ Extracts skill and knowledge spans from job postings. It runs **entirely in the browser**: the int8 ONNX model runs on onnxruntime-web,
12
+ the tokenizer is transformers.js, and Viterbi decoding with the learned CRF is written in JavaScript.
13
+ Model: https://huggingface.co/srish327/deberta-v3-skillspan-crf · Code: https://github.com/MSR327/skillspan-ner
extract.js ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Core extraction logic, shared by index.html and the Node parity test (tests/js_parity.mjs).
2
+ // Same word/sentence splitting as skillex/predict.py (matches SkillSpan's training tokens).
3
+ const WORD = /[\p{L}\p{N}_\p{M}]+(?:['’.\-][\p{L}\p{N}_\p{M}]+)*|\S/gu;
4
+ export const splitWords = (text) => [...text.matchAll(WORD)].map((m) => [m.index, m.index + m[0].length]);
5
+ export function splitSentences(text, words) {
6
+ const sents = []; let cur = [];
7
+ words.forEach(([a, b], i) => {
8
+ if (cur.length && text.slice(words[cur.at(-1)][1], a).includes("\n")) { sents.push(cur); cur = []; }
9
+ cur.push(i);
10
+ if ([".", "!", "?"].includes(text.slice(a, b))) { sents.push(cur); cur = []; }
11
+ });
12
+ if (cur.length) sents.push(cur);
13
+ return sents;
14
+ }
15
+
16
+ // Viterbi with the learned CRF transitions (identical to skillex.decode.viterbi_bio).
17
+ export function viterbi(emit, trans, start) {
18
+ const T = emit.length; if (!T) return [];
19
+ let best = [0, 1, 2].map((j) => start[j] + emit[0][j]);
20
+ const back = [];
21
+ for (let t = 1; t < T; t++) {
22
+ const bt = [], nb = [];
23
+ for (let j = 0; j < 3; j++) {
24
+ let arg = 0, val = -Infinity;
25
+ for (let i = 0; i < 3; i++) { const v = best[i] + trans[i][j]; if (v > val) { val = v; arg = i; } }
26
+ bt.push(arg); nb.push(val + emit[t][j]);
27
+ }
28
+ back.push(bt); best = nb;
29
+ }
30
+ let k = best.indexOf(Math.max(...best)); const path = [k];
31
+ for (let t = T - 1; t > 0; t--) { k = back[t - 1][k]; path.push(k); }
32
+ return path.reverse();
33
+ }
34
+
35
+
36
+ const MAX_SUBWORDS = 510, BATCH = 16, TYPES = ["SKILL", "KNOWLEDGE"];
37
+
38
+ // Split a sentence (list of global word ids) into chunks that fit the model.
39
+ function encodeSentence(tok, CLS, SEP, text, words, sent) {
40
+ const chunks = []; let ids = [CLS], first = [], wids = [];
41
+ for (const w of sent) {
42
+ // Leading space = SentencePiece word-start marker "▁". Python's is_split_into_words adds it;
43
+ // transformers.js does not (without it, IDs differ for every word: 0/897 matched).
44
+ const sub = tok.encode(" " + text.slice(...words[w]), { add_special_tokens: false });
45
+ if (ids.length + sub.length > MAX_SUBWORDS && wids.length) {
46
+ chunks.push({ ids: [...ids, SEP], first, wids }); ids = [CLS]; first = []; wids = [];
47
+ }
48
+ if (!sub.length) continue;
49
+ first.push(ids.length); wids.push(w); ids.push(...sub.slice(0, MAX_SUBWORDS - 1));
50
+ }
51
+ if (wids.length) chunks.push({ ids: [...ids, SEP], first, wids });
52
+ return chunks;
53
+ }
54
+
55
+ export async function extract({ ort, tok, session, crfs }, text) {
56
+ const [CLS, SEP] = tok.encode("", { add_special_tokens: true });
57
+ const words = splitWords(text);
58
+ const chunks = splitSentences(text, words).flatMap((s) => encodeSentence(tok, CLS, SEP, text, words, s));
59
+ const spans = [];
60
+ for (let b = 0; b < chunks.length; b += BATCH) {
61
+ const batch = chunks.slice(b, b + BATCH), L = Math.max(...batch.map((c) => c.ids.length));
62
+ const ids = new BigInt64Array(batch.length * L), am = new BigInt64Array(batch.length * L);
63
+ batch.forEach((c, r) => c.ids.forEach((v, j) => { ids[r * L + j] = BigInt(v); am[r * L + j] = 1n; }));
64
+ const out = await session.run({
65
+ input_ids: new ort.Tensor("int64", ids, [batch.length, L]),
66
+ attention_mask: new ort.Tensor("int64", am, [batch.length, L]),
67
+ });
68
+ const lg = out.logits.data;
69
+ batch.forEach((c, r) => {
70
+ TYPES.forEach((type, h) => {
71
+ const emit = c.first.map((p) => [0, 1, 2].map((k) => lg[(r * L + p) * 6 + h * 3 + k]));
72
+ const path = viterbi(emit, crfs[h].transitions, crfs[h].start);
73
+ let s = -1;
74
+ for (let i = 0; i <= path.length; i++) {
75
+ const tag = i < path.length ? path[i] : 0; // 0=O 1=B 2=I
76
+ if (s >= 0 && tag !== 2) { spans.push({ type, start: words[c.wids[s]][0], end: words[c.wids[i - 1]][1] }); s = -1; }
77
+ if (tag === 1 || (tag === 2 && s < 0)) s = i;
78
+ }
79
+ });
80
+ });
81
+ }
82
+ return spans;
83
+ }
84
+
index.html CHANGED
@@ -1,19 +1,160 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
  <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>Skill Extractor</title>
7
+ <style>
8
+ :root {
9
+ --bg: #fafaf9; --panel: #ffffff; --text: #1c1917; --muted: #78716c; --border: #e7e5e4;
10
+ --skill: #15803d; --skill-bg: #dcfce7; --know: #1d4ed8; --know-bg: #dbeafe; --accent: #1c1917;
11
+ }
12
+ @media (prefers-color-scheme: dark) {
13
+ :root:not([data-theme="light"]) {
14
+ --bg: #0c0a09; --panel: #1c1917; --text: #f5f5f4; --muted: #a8a29e; --border: #292524;
15
+ --skill: #86efac; --skill-bg: #14532d; --know: #93c5fd; --know-bg: #1e3a8a; --accent: #f5f5f4;
16
+ }
17
+ }
18
+ * { box-sizing: border-box; }
19
+ body { margin: 0; background: var(--bg); color: var(--text);
20
+ font: 15px/1.55 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif; }
21
+ main { max-width: 860px; margin: 0 auto; padding: 32px 16px 64px; }
22
+ h1 { font-size: 26px; margin: 0 0 4px; letter-spacing: -0.01em; }
23
+ .sub { color: var(--muted); margin: 0 0 24px; }
24
+ .sub a { color: inherit; }
25
+ textarea { width: 100%; min-height: 170px; padding: 12px; border: 1px solid var(--border); border-radius: 10px;
26
+ background: var(--panel); color: var(--text); font: inherit; resize: vertical; }
27
+ .row { display: flex; gap: 12px; align-items: center; margin: 12px 0 24px; flex-wrap: wrap; }
28
+ button { background: var(--accent); color: var(--bg); border: 0; border-radius: 8px; padding: 10px 18px;
29
+ font: inherit; font-weight: 600; cursor: pointer; }
30
+ button:disabled { opacity: .45; cursor: default; }
31
+ #status { color: var(--muted); font-size: 14px; }
32
+ .bar { height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; flex: 1 1 160px; max-width: 260px; }
33
+ .bar > div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
34
+ .card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 16px; margin-bottom: 16px; }
35
+ .card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 10px; }
36
+ .hl { white-space: pre-wrap; }
37
+ mark { border-radius: 4px; padding: 1px 2px; }
38
+ mark.SKILL { background: var(--skill-bg); color: var(--skill); }
39
+ mark.KNOWLEDGE { background: var(--know-bg); color: var(--know); }
40
+ .chips { display: flex; flex-wrap: wrap; gap: 6px; }
41
+ .chip { border-radius: 999px; padding: 3px 10px; font-size: 14px; }
42
+ .chip.SKILL { background: var(--skill-bg); color: var(--skill); }
43
+ .chip.KNOWLEDGE { background: var(--know-bg); color: var(--know); }
44
+ .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
45
+ @media (max-width: 640px) { .grid { grid-template-columns: 1fr; } }
46
+ .empty { color: var(--muted); font-size: 14px; }
47
+ footer { color: var(--muted); font-size: 13px; margin-top: 32px; }
48
+ </style>
49
+ </head>
50
+ <body>
51
+ <main>
52
+ <h1>Job Posting Skill Extractor</h1>
53
+ <p class="sub">
54
+ Finds <b style="color:var(--skill)">skills</b> (what you can do) and
55
+ <b style="color:var(--know)">knowledge</b> (what you know) in a job posting. The model is DeBERTa-v3 with two BIO heads and a learned CRF,
56
+ fine-tuned on SkillSpan (test span F1 0.645 ± 0.005).
57
+ <b>It runs entirely in your browser</b>, so your text never leaves this page.
58
+ <a href="https://huggingface.co/srish327/deberta-v3-skillspan-crf">Model</a> ·
59
+ <a href="https://github.com/MSR327/skillspan-ner">Code</a>
60
+ </p>
61
+
62
+ <textarea id="text">We are looking for a Data Engineer with strong Python and SQL experience. You will design ETL pipelines on AWS, collaborate with stakeholders, and mentor junior engineers.
63
+ Requirements:
64
+ - 3+ years of experience with Kubernetes and Terraform
65
+ - Excellent communication skills
66
+ - Ability to prioritize tasks in a fast-paced environment
67
+ Nice to have: Kafka, dbt</textarea>
68
+ <div class="row">
69
+ <button id="go" disabled>Extract</button>
70
+ <div class="bar"><div id="prog"></div></div>
71
+ <span id="status">Loading model (243 MB, cached after the first visit)…</span>
72
+ </div>
73
+
74
+ <div class="grid">
75
+ <div class="card"><h2>Skills</h2><div id="skills" class="chips"></div></div>
76
+ <div class="card"><h2>Knowledge</h2><div id="knowledge" class="chips"></div></div>
77
+ </div>
78
+ <div class="card"><h2>Skills in context</h2><div id="hl-skill" class="hl empty">—</div></div>
79
+ <div class="card"><h2>Knowledge in context</h2><div id="hl-know" class="hl empty">—</div></div>
80
+
81
+ <footer>Skill and knowledge spans can overlap, so each is shown separately. Model is int8-quantized ONNX, run with onnxruntime-web.
82
+ Not intended for automated hiring decisions.</footer>
83
+ </main>
84
+
85
+ <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/ort.min.js"></script>
86
+ <script type="module">
87
+ import { AutoTokenizer } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.5.1";
88
+ import { extract } from "./extract.js";
89
+
90
+ const REPO = "srish327/deberta-v3-skillspan-crf";
91
+ const BASE = `https://huggingface.co/${REPO}/resolve/main/`;
92
+ const MODEL_URL = BASE + "onnx/model_quantized.onnx";
93
+
94
+ const $ = (id) => document.getElementById(id);
95
+ const status = (s) => ($("status").textContent = s);
96
+
97
+ async function fetchWithProgress(url) {
98
+ let cache = null;
99
+ try { cache = await caches.open("skill-extractor-v1"); const hit = await cache.match(url);
100
+ if (hit) { $("prog").style.width = "100%"; return await hit.arrayBuffer(); } } catch {}
101
+ const res = await fetch(url);
102
+ if (!res.ok) throw new Error(`download failed: ${res.status}`);
103
+ const total = +res.headers.get("content-length") || 0, reader = res.body.getReader(), parts = [];
104
+ let got = 0;
105
+ for (;;) {
106
+ const { done, value } = await reader.read(); if (done) break;
107
+ parts.push(value); got += value.length;
108
+ if (total) { $("prog").style.width = `${(100 * got) / total}%`;
109
+ status(`Loading model… ${Math.round(got / 1e6)} / ${Math.round(total / 1e6)} MB`); }
110
+ }
111
+ const buf = await new Blob(parts).arrayBuffer();
112
+ try { await cache?.put(url, new Response(buf.slice(0))); } catch {}
113
+ return buf;
114
+ }
115
+
116
+ let tok, session, crfs;
117
+ async function load() {
118
+ ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/";
119
+ [tok, crfs] = await Promise.all([
120
+ AutoTokenizer.from_pretrained(REPO),
121
+ fetch(BASE + "skillex_crf.json").then((r) => r.json()),
122
+ ]);
123
+ const buf = await fetchWithProgress(MODEL_URL);
124
+ status("Starting model…");
125
+ session = await ort.InferenceSession.create(buf, { executionProviders: ["wasm"] });
126
+ status("Ready. Paste a job posting and click Extract.");
127
+ $("go").disabled = false;
128
+ }
129
+
130
+ const esc = (s) => s.replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
131
+ function highlight(text, spans, type) {
132
+ let html = "", pos = 0;
133
+ for (const s of spans.filter((x) => x.type === type).sort((a, b) => a.start - b.start)) {
134
+ html += esc(text.slice(pos, s.start)) + `<mark class="${type}">${esc(text.slice(s.start, s.end))}</mark>`;
135
+ pos = s.end;
136
+ }
137
+ return html + esc(text.slice(pos));
138
+ }
139
+ function chips(el, text, spans, type) {
140
+ const uniq = [...new Set(spans.filter((s) => s.type === type).map((s) => text.slice(s.start, s.end)))];
141
+ el.innerHTML = uniq.length ? uniq.map((u) => `<span class="chip ${type}">${esc(u)}</span>`).join("")
142
+ : `<span class="empty">none found</span>`;
143
+ }
144
+
145
+ $("go").onclick = async () => {
146
+ const text = $("text").value; $("go").disabled = true; status("Running…");
147
+ try {
148
+ const t0 = performance.now(), spans = await extract({ ort, tok, session, crfs }, text);
149
+ chips($("skills"), text, spans, "SKILL"); chips($("knowledge"), text, spans, "KNOWLEDGE");
150
+ for (const [id, type] of [["hl-skill", "SKILL"], ["hl-know", "KNOWLEDGE"]]) {
151
+ $(id).classList.remove("empty"); $(id).innerHTML = highlight(text, spans, type);
152
+ }
153
+ status(`Done in ${Math.round(performance.now() - t0)} ms, ${spans.length} spans.`);
154
+ } catch (e) { status("Error: " + e.message); console.error(e); }
155
+ $("go").disabled = false;
156
+ };
157
+ load().catch((e) => { status("Failed to load model: " + e.message); console.error(e); });
158
+ </script>
159
+ </body>
160
  </html>