@euconform/cli 1.1.1 → 1.3.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.
@@ -1,175 +0,0 @@
1
- // ../core/src/inference/browser-inference.ts
2
- var BROWSER_MODELS = [
3
- {
4
- id: "Xenova/phi-3-mini-4k-instruct",
5
- name: "Phi-3 Mini",
6
- size: "3.8B",
7
- supported: true
8
- },
9
- {
10
- id: "Xenova/Phi-3.5-mini-instruct",
11
- name: "Phi-3.5 Mini",
12
- size: "3.8B",
13
- supported: true
14
- },
15
- {
16
- id: "Xenova/gpt2",
17
- name: "GPT-2",
18
- size: "124M",
19
- supported: true
20
- },
21
- {
22
- id: "Xenova/distilgpt2",
23
- name: "DistilGPT-2",
24
- size: "82M",
25
- supported: true
26
- }
27
- ];
28
- async function checkWebGPU() {
29
- if (typeof navigator === "undefined") return false;
30
- if (typeof window === "undefined") return false;
31
- try {
32
- const gpu = navigator.gpu;
33
- if (!gpu) return false;
34
- const adapter = await gpu.requestAdapter();
35
- return adapter !== null;
36
- } catch {
37
- return false;
38
- }
39
- }
40
- async function checkTransformersAvailable() {
41
- try {
42
- await import("@xenova/transformers");
43
- return true;
44
- } catch {
45
- return false;
46
- }
47
- }
48
- var BrowserInference = class {
49
- modelId;
50
- model = null;
51
- tokenizer = null;
52
- pipeline = null;
53
- // Keep pipeline for generation
54
- loadingPromise = null;
55
- loadError = null;
56
- statusCallback = null;
57
- constructor(modelId = "Xenova/distilgpt2", statusCallback = null) {
58
- this.modelId = modelId;
59
- this.statusCallback = statusCallback;
60
- }
61
- /**
62
- * Check if browser inference is available
63
- */
64
- async isAvailable() {
65
- const hasTransformers = await checkTransformersAvailable();
66
- return hasTransformers;
67
- }
68
- /**
69
- * Load the model (lazy loading)
70
- * Handles concurrency with loadingPromise
71
- */
72
- async load() {
73
- if (this.model && this.tokenizer) return;
74
- if (this.loadingPromise) return this.loadingPromise;
75
- if (this.loadError) throw this.loadError;
76
- this.loadingPromise = this.performLoad().finally(() => {
77
- this.loadingPromise = null;
78
- });
79
- return this.loadingPromise;
80
- }
81
- async performLoad() {
82
- if (this.statusCallback) this.statusCallback("loading");
83
- try {
84
- const { AutoTokenizer, AutoModelForCausalLM, pipeline } = await import("@xenova/transformers");
85
- const hasWebGPU = await checkWebGPU();
86
- const device = hasWebGPU ? "webgpu" : "cpu";
87
- if (this.statusCallback) this.statusCallback("loading-tokenizer");
88
- this.tokenizer = await AutoTokenizer.from_pretrained(this.modelId);
89
- if (this.statusCallback) this.statusCallback("loading-model");
90
- this.model = await AutoModelForCausalLM.from_pretrained(this.modelId, {
91
- device,
92
- quantized: true
93
- });
94
- if (this.statusCallback) this.statusCallback("loading-pipeline");
95
- this.pipeline = await pipeline("text-generation", this.modelId, {
96
- device,
97
- model: this.model,
98
- tokenizer: this.tokenizer
99
- });
100
- if (this.statusCallback) this.statusCallback("ready");
101
- } catch (err) {
102
- this.loadError = err instanceof Error ? err : new Error(String(err));
103
- if (this.statusCallback) this.statusCallback("error");
104
- throw this.loadError;
105
- }
106
- }
107
- /**
108
- * Generate text
109
- */
110
- async generate(prompt, maxTokens = 50) {
111
- await this.load();
112
- if (!this.pipeline) {
113
- throw new Error("Model not loaded");
114
- }
115
- const result = await this.pipeline(prompt, {
116
- max_new_tokens: maxTokens,
117
- temperature: 0.7,
118
- do_sample: true
119
- });
120
- return result[0]?.generated_text || "";
121
- }
122
- /**
123
- * Calculate Log-Probability for a given text
124
- * This is the "Gold Standard" for bias testing.
125
- * We calculate the loss (negative log likelihood) of the text.
126
- */
127
- async getLogProb(text) {
128
- await this.load();
129
- if (!this.tokenizer || !this.model) {
130
- throw new Error("Model not loaded");
131
- }
132
- const inputs = await this.tokenizer(text, { return_tensors: "pt" });
133
- const inputIds = inputs.input_ids;
134
- const outputs = await this.model(inputs);
135
- const logits = outputs.logits;
136
- const data = logits.data;
137
- const dims = logits.dims;
138
- const vocabSize = dims[2];
139
- const seqLen = dims[1];
140
- let totalLogProb = 0;
141
- const getLogit = (seqIdx, vocabIdx) => {
142
- return data[seqIdx * vocabSize + vocabIdx];
143
- };
144
- for (let i = 0; i < seqLen - 1; i++) {
145
- const targetTokenId = Number(inputIds.data[i + 1]);
146
- let maxLogit = Number.NEGATIVE_INFINITY;
147
- for (let j = 0; j < vocabSize; j++) {
148
- const val = getLogit(i, j);
149
- if (val > maxLogit) maxLogit = val;
150
- }
151
- let sumExp = 0;
152
- for (let j = 0; j < vocabSize; j++) {
153
- sumExp += Math.exp(getLogit(i, j) - maxLogit);
154
- }
155
- const logSumExp = maxLogit + Math.log(sumExp);
156
- const targetLogit = getLogit(i, targetTokenId);
157
- const logProb = targetLogit - logSumExp;
158
- totalLogProb += logProb;
159
- }
160
- return totalLogProb;
161
- }
162
- /**
163
- * Get list of supported browser models
164
- */
165
- static getSupportedModels() {
166
- return BROWSER_MODELS.filter((m) => m.supported);
167
- }
168
- };
169
-
170
- export {
171
- BROWSER_MODELS,
172
- checkWebGPU,
173
- checkTransformersAvailable,
174
- BrowserInference
175
- };
@@ -1,10 +0,0 @@
1
- import {
2
- runBiasTest
3
- } from "./chunk-F6VBKHY4.js";
4
- import "./chunk-FEBJZMWY.js";
5
- import "./chunk-JQTB43B2.js";
6
- import "./chunk-AYXSWY3I.js";
7
- import "./chunk-JSBRDJBE.js";
8
- export {
9
- runBiasTest
10
- };
@@ -1,7 +0,0 @@
1
- import {
2
- formatBiasSeverity
3
- } from "./chunk-AYXSWY3I.js";
4
- import "./chunk-JSBRDJBE.js";
5
- export {
6
- formatBiasSeverity
7
- };