Qrzysztof commited on
Commit
8bfd5c7
·
verified ·
1 Parent(s): bd2bf82

add in-browser demo

Browse files
Files changed (1) hide show
  1. index.html +110 -18
index.html CHANGED
@@ -1,19 +1,111 @@
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>get-name · in-browser demo</title>
7
+ <style>
8
+ body{font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;max-width:720px;margin:2rem auto;padding:0 1rem;color:#1f2328;line-height:1.5}
9
+ h1{font-size:1.4rem}
10
+ textarea{width:100%;box-sizing:border-box;min-height:4rem;font:inherit;padding:.6rem;border:1px solid #d0d7de;border-radius:8px}
11
+ button{font:inherit;padding:.5rem 1rem;border:0;border-radius:8px;background:#0969da;color:#fff;cursor:pointer}
12
+ button:disabled{background:#8bb3dd;cursor:wait}
13
+ pre{background:#f6f8fa;border:1px solid #d0d7de;border-radius:8px;padding:.8rem;overflow:auto;white-space:pre-wrap;word-break:break-word}
14
+ .ok{color:#1a7f37}.err{color:#cf222e}.muted{color:#57606a;font-size:.85rem}
15
+ </style>
16
+ </head>
17
+ <body>
18
+ <h1>get-name — extract the intended person's first name (in-browser)</h1>
19
+ <p class="muted">
20
+ Loads the <code>cactus-needle</code> WebAssembly engine + the
21
+ <code>Qrzysztof/get-name</code> tuned weights <em>directly from Hugging Face</em>.
22
+ No server. Everything runs in this tab.
23
+ </p>
24
+ <p><textarea id="q" placeholder='Type a sentence… e.g. Nice to meet you, Alex'>By the way, Alice, if you don't know, how old am I?</textarea></p>
25
+ <p><button id="go" onclick="run()" disabled>Extract name</button>
26
+ <span id="status" class="muted"></span></p>
27
+ <pre id="out" class="muted">waiting…</pre>
28
+
29
+ <script src="https://huggingface.co/Cactus-Compute/needle2/resolve/main/wasm/needle.js"></script>
30
+ <script>
31
+ const WASM_URL = "https://huggingface.co/Cactus-Compute/needle2/resolve/main/wasm/needle.wasm";
32
+ const CACT_URL = "https://huggingface.co/Qrzysztof/get-name/resolve/main/tuned.cact";
33
+ const SYSTEM = "You extract the given (first) name of the intended person. If the text has no intended person, do not call the tool.";
34
+ const TOOLS = [{ name: "extract_name", parameters: { type: "object", properties: { name: { type: "string" } }, required: ["name"] } }];
35
+
36
+ let mod = null;
37
+
38
+ const enc = s => { const b = new TextEncoder().encode(s + "\0"); const p = mod._malloc(b.length); mod.HEAPU8.set(b, p); return p; };
39
+
40
+ async function init() {
41
+ const status = document.getElementById("status");
42
+ const btn = document.getElementById("go");
43
+ status.textContent = "downloading engine + weights…"; btn.disabled = true;
44
+
45
+ const [wasm, cact] = await Promise.all([
46
+ fetch(WASM_URL).then(r => r.arrayBuffer()),
47
+ fetch(CACT_URL).then(r => r.arrayBuffer()),
48
+ ]);
49
+
50
+ status.textContent = "compiling engine…";
51
+ mod = await createNeedle({ wasmBinary: wasm });
52
+
53
+ const p = mod._malloc(cact.byteLength);
54
+ mod.HEAPU8.set(new Uint8Array(cact), p);
55
+ if (mod._needle_load(p, BigInt(cact.byteLength)) !== 0) throw new Error("needle_load failed");
56
+ mod._free(p);
57
+
58
+ const sysP = enc(SYSTEM);
59
+ const toolsP = enc(JSON.stringify(TOOLS));
60
+ if (mod._needle_init(sysP, toolsP, 0) < 0) throw new Error("needle_init failed");
61
+
62
+ status.textContent = "ready ✓"; btn.disabled = false;
63
+ }
64
+
65
+ async function run() {
66
+ const q = document.getElementById("q").value.trim();
67
+ const out = document.getElementById("out");
68
+ const status = document.getElementById("status");
69
+ if (!q) return;
70
+ const btn = document.getElementById("go"); btn.disabled = true;
71
+ status.textContent = "generating…";
72
+ try {
73
+ await (mod ? Promise.resolve() : init());
74
+
75
+ const qP = enc(q);
76
+ const cap = 1 << 20, outP = mod._malloc(cap);
77
+ const rc = mod._needle_complete(qP, 512, outP, cap);
78
+ const bytes = mod.HEAPU8.subarray(outP, outP + Math.min(rc, cap));
79
+ let end = bytes.indexOf(0); if (end < 0) end = bytes.length;
80
+ const response = new TextDecoder().decode(bytes.subarray(0, end));
81
+
82
+ out.textContent = response;
83
+ out.className = "";
84
+ try {
85
+ const obj = JSON.parse(response);
86
+ const calls = obj.function_calls || [];
87
+ if (calls.length) {
88
+ status.innerHTML = `<span class="ok">intended person: <b>${calls[0].arguments.name}</b></span>`;
89
+ } else {
90
+ status.textContent = "no tool call (no intended person detected)";
91
+ }
92
+ } catch { status.textContent = "raw response above (unparseable JSON)."; }
93
+ mod._free(qP); mod._free(outP);
94
+ } catch (e) {
95
+ out.textContent = String(e);
96
+ out.className = "err";
97
+ status.textContent = "error";
98
+ }
99
+ btn.disabled = false;
100
+ }
101
+
102
+ init().catch(e => { document.getElementById("status").textContent = "error: " + e.message; });
103
+ </script>
104
+ <div class="muted"><p>Tips:</p>
105
+ <ul>
106
+ <li>Runs fully client-side (WASM ~325 KB, weights ~13.7 MB, ~700 MB peak RAM).</li>
107
+ <li>CORS: works from any <code>http(s)</code> page; for a local file open over <code>python3 -m http.server</code>.</li>
108
+ <li>Gives one answer per <code>complete()</code> call — for batches, call the engine fresh per text (see README).</li>
109
+ </ul></div>
110
+ </body>
111
+ </html>