@p2arthur/webinference-sdk 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,121 @@
1
- # Temporary Holding Version
1
+ # WebInference SDK
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Run browser-ready Hugging Face models for chat, image generation, and text classification. The SDK selects a runtime for the model, checks device requirements, downloads weights on first use, and runs inference in a browser worker.
4
+
5
+ ## Quickstart
6
+
7
+ The first chat uses SmolLM2 135M Q4, which downloads about 184 MB. Use a browser with WebGPU enabled and serve the app on HTTPS or localhost. The SDK is an ES module with TypeScript declarations.
8
+
9
+ In a new app directory, install WebInference SDK and Vite.
10
+
11
+ ```sh
12
+ npm init -y
13
+ npm install @p2arthur/webinference-sdk
14
+ npm install -D vite
15
+ ```
16
+
17
+ Save this as `index.html`.
18
+
19
+ ```html
20
+ <!doctype html>
21
+ <html lang="en">
22
+ <meta charset="utf-8">
23
+ <title>WebInference SDK quickstart</title>
24
+ <button type="button">Run local chat</button>
25
+ <pre aria-live="polite"></pre>
26
+ <script type="module">
27
+ import { openModel } from '@p2arthur/webinference-sdk'
28
+
29
+ const button = document.querySelector('button')
30
+ const output = document.querySelector('pre')
31
+ button.onclick = async () => {
32
+ button.disabled = true
33
+ let model
34
+ try {
35
+ model = await openModel('https://huggingface.co/HuggingFaceTB/SmolLM2-135M-Instruct', {
36
+ task: 'chat',
37
+ onProgress: ({ stage, loadedBytes, totalBytes }) => {
38
+ output.textContent = stage === 'download'
39
+ ? `Downloading ${Math.round(100 * loadedBytes / totalBytes)}%`
40
+ : stage
41
+ },
42
+ })
43
+ output.textContent = ''
44
+ await model.chat('Write one sentence greeting a new developer.', {
45
+ onToken: (text) => { output.textContent += text },
46
+ })
47
+ } catch (error) {
48
+ output.textContent = error.message
49
+ } finally {
50
+ model?.dispose()
51
+ button.disabled = false
52
+ }
53
+ }
54
+ </script>
55
+ </html>
56
+ ```
57
+
58
+ Run `npx vite`, open the localhost URL, and click **Run local chat**.
59
+
60
+ ## Inspect or select a model
61
+
62
+ ```js
63
+ import { inspectModel, listModels, openModel } from '@p2arthur/webinference-sdk'
64
+
65
+ const report = await inspectModel('https://huggingface.co/HuggingFaceTB/SmolLM2-135M-Instruct')
66
+ console.log(report.status, report.issues, report.alternatives)
67
+
68
+ const [candidate] = await listModels({ task: 'chat', search: 'SmolLM2-135M', maxDownloadBytes: 500e6 })
69
+ const model = await openModel(candidate)
70
+ try {
71
+ console.log(await model.chat('Say hello.'))
72
+ } finally {
73
+ model.dispose()
74
+ }
75
+ ```
76
+
77
+ `listModels({ task, search, maxDownloadBytes })` returns matching entries in the 100-repository catalog. `bytes` is the size of the chosen model files. `getDeviceInfo()` reports WebGPU features, available browser storage, and browser-reported RAM when present. Actual fit is established by loading the export.
78
+
79
+ `inspectModel(url, { signal, findRelated })` reads repository metadata and small configuration files. It returns a status, pinned compatible descriptors, explanations, and any related public exports it finds. Supported URLs include repository, revision, and file links. The exported `HUGGING_FACE_BROWSE_URL` opens a filtered Hugging Face search for more ONNX chat exports.
80
+
81
+ `openModel(urlOrDescriptor, { task, signal, onProgress })` loads an exact descriptor or selects the first compatible export at the URL. To pick a quantization or a related repository, pass its descriptor from the inspection report. Public repositories can be inspected and loaded without a Hugging Face account.
82
+
83
+ ## Run a task
84
+
85
+ - **Chat** — `model.chat(prompt, { onToken, signal })` returns the complete reply. `onToken` receives text chunks as they become available. A session keeps conversation history. Call `model.reset()` to clear it.
86
+ - **Images** — `model.generateImage(prompt, { seed, signal, onProgress })` returns a PNG `blob`, width, and height. SD-Turbo accepts an optional unsigned 32-bit seed and returns the seed used. Janus chooses its own seed.
87
+ - **Classification** — `model.classify(texts, choices, { question, signal, onProgress })` accepts three to five English choices as label strings or `{ label, description }` objects. It returns the predicted label, choice index, scores, and truncation flag for each text. Results follow input order. Progress reports how many texts have been processed.
88
+
89
+ ```js
90
+ const model = await openModel('https://huggingface.co/onnx-community/laya-ONNX', { task: 'classification' })
91
+ try {
92
+ const predictions = await model.classify(
93
+ ['I was charged twice.', 'I forgot my password and cannot sign in to my account.'],
94
+ [
95
+ { label: 'Billing', description: 'Payments, invoices, refunds, or duplicate charges.' },
96
+ { label: 'Account access', description: 'Sign-in, password resets, or locked accounts.' },
97
+ { label: 'Technical issue', description: 'An existing product feature crashes or loses data.' },
98
+ ],
99
+ { question: 'Which support team should handle this message?' },
100
+ )
101
+ console.log(predictions)
102
+ } finally {
103
+ model.dispose()
104
+ }
105
+ ```
106
+
107
+ Each loaded model handles one operation at a time. Call `dispose()` to release its worker and GPU resources. Aborting an in-flight load or operation closes the session. A signal already aborted before an operation rejects that call.
108
+
109
+ ## Errors and hosting
110
+
111
+ `ModelError` has a stable `code`, a readable message, and the operation and repository when available. Useful cases include `ACCESS_REQUIRED` for gated models, `UNSUPPORTED_MODEL` for an unmatched export, `DEVICE_UNSUPPORTED` for missing browser features, `LOAD_FAILED` for download or initialization failures, and `ABORTED` for cancellation.
112
+
113
+ The SDK loads the relevant runtime worker on demand. If your app sets a Content Security Policy, allow blob workers and the downloads from Hugging Face and the runtime CDN. Model downloads and runtime assets are fetched on first use. Prompts and classification input are processed in the browser.
114
+
115
+ The adapter set covers LiteRT-LM browser exports, compatible Transformers.js causal ONNX models, Janus and SD-Turbo image exports, and the English Laya choice layout. The SDK’s model inspection, catalog, and device probe are available independently of loading an inference session.
116
+
117
+ ## Build and maintain the package
118
+
119
+ Run `npm ci`, `npm test`, and `npm pack` from the SDK checkout. To try a local build, install the resulting `p2arthur-webinference-sdk-*.tgz` in your app. Run `npm run refresh:catalog` to regenerate the pinned catalog using the same model checks as the public importer.
120
+
121
+ Bundled dependency licenses are included in `third-party-licenses/`.