@min-pack/tfjs-node 2.17.1
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/LICENSE +202 -0
- package/README.md +9 -0
- package/addon-node_modules/onnxruntime-node/bin/napi-v3/linux/arm64/libonnxruntime.so.1.14.0 +0 -0
- package/addon-node_modules/onnxruntime-node/bin/napi-v3/linux/arm64/onnxruntime_binding.node +0 -0
- package/addon-node_modules/onnxruntime-node/bin/napi-v3/linux/x64/libonnxruntime.so.1.14.0 +0 -0
- package/addon-node_modules/onnxruntime-node/bin/napi-v3/linux/x64/onnxruntime_binding.node +0 -0
- package/index.js +3605 -0
- package/package.json +10 -0
package/index.js
ADDED
|
@@ -0,0 +1,3605 @@
|
|
|
1
|
+
(()=>{var e={745:(e,t,n)=>{var s={"./darwin/arm64/onnxruntime_binding.node":968,"./darwin/x64/onnxruntime_binding.node":710,"./linux/arm64/onnxruntime_binding.node":225,"./linux/x64/onnxruntime_binding.node":239,"./win32/arm64/onnxruntime_binding.node":587,"./win32/x64/onnxruntime_binding.node":598}
|
|
2
|
+
function r(e){var t=o(e)
|
|
3
|
+
return n(t)}function o(e){if(!n.o(s,e)){var t=new Error("Cannot find module '"+e+"'")
|
|
4
|
+
t.code="MODULE_NOT_FOUND"
|
|
5
|
+
throw t}return s[e]}r.keys=function(){return Object.keys(s)}
|
|
6
|
+
r.resolve=o
|
|
7
|
+
e.exports=r
|
|
8
|
+
r.id=745},833:(e,t,n)=>{n.r(t)
|
|
9
|
+
n.d(t,{InferenceSession:()=>p,Tensor:()=>h,env:()=>i,registerBackend:()=>o})
|
|
10
|
+
const s={}
|
|
11
|
+
const r=[]
|
|
12
|
+
const o=(e,t,n)=>{if(!t||"function"!=typeof t.init||"function"!=typeof t.createSessionHandler)throw new TypeError("not a valid backend")
|
|
13
|
+
{const o=s[e]
|
|
14
|
+
if(void 0===o)s[e]={backend:t,priority:n}
|
|
15
|
+
else{if(o.priority>n)return
|
|
16
|
+
if(o.priority===n&&o.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${n}`)}if(n>=0){const t=r.indexOf(e);-1!==t&&r.splice(t,1)
|
|
17
|
+
for(let t=0;t<r.length;t++)if(s[r[t]].priority<=n){r.splice(t,0,e)
|
|
18
|
+
return}r.push(e)}}}
|
|
19
|
+
const i=new class{constructor(){this.wasm={}
|
|
20
|
+
this.webgl={}
|
|
21
|
+
this.logLevelInternal="warning"}set logLevel(e){if(void 0!==e){if("string"!=typeof e||-1===["verbose","info","warning","error","fatal"].indexOf(e))throw new Error(`Unsupported logging level: ${e}`)
|
|
22
|
+
this.logLevelInternal=e}}get logLevel(){return this.logLevelInternal}}
|
|
23
|
+
const a="undefined"!=typeof BigInt64Array&&"function"==typeof BigInt64Array.from
|
|
24
|
+
const l="undefined"!=typeof BigUint64Array&&"function"==typeof BigUint64Array.from
|
|
25
|
+
const c=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array]])
|
|
26
|
+
const d=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]])
|
|
27
|
+
if(a){c.set("int64",BigInt64Array)
|
|
28
|
+
d.set(BigInt64Array,"int64")}if(l){c.set("uint64",BigUint64Array)
|
|
29
|
+
d.set(BigUint64Array,"uint64")}class u{constructor(e,t,n){let s
|
|
30
|
+
let r
|
|
31
|
+
let o
|
|
32
|
+
if("string"==typeof e){s=e
|
|
33
|
+
o=n
|
|
34
|
+
if("string"===e){if(!Array.isArray(t))throw new TypeError("A string tensor's data must be a string array.")
|
|
35
|
+
r=t}else{const n=c.get(e)
|
|
36
|
+
if(void 0===n)throw new TypeError(`Unsupported tensor type: ${e}.`)
|
|
37
|
+
if(Array.isArray(t))r=n.from(t)
|
|
38
|
+
else{if(!(t instanceof n))throw new TypeError(`A ${s} tensor's data must be type of ${n}`)
|
|
39
|
+
r=t}}}else{o=t
|
|
40
|
+
if(Array.isArray(e)){if(0===e.length)throw new TypeError("Tensor type cannot be inferred from an empty array.")
|
|
41
|
+
const t=typeof e[0]
|
|
42
|
+
if("string"===t){s="string"
|
|
43
|
+
r=e}else{if("boolean"!==t)throw new TypeError(`Invalid element type of data array: ${t}.`)
|
|
44
|
+
s="bool"
|
|
45
|
+
r=Uint8Array.from(e)}}else{const t=d.get(e.constructor)
|
|
46
|
+
if(void 0===t)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`)
|
|
47
|
+
s=t
|
|
48
|
+
r=e}}if(void 0===o)o=[r.length]
|
|
49
|
+
else if(!Array.isArray(o))throw new TypeError("A tensor's dims must be a number array")
|
|
50
|
+
const i=(e=>{let t=1
|
|
51
|
+
for(let n=0;n<e.length;n++){const s=e[n]
|
|
52
|
+
if("number"!=typeof s||!Number.isSafeInteger(s))throw new TypeError(`dims[${n}] must be an integer, got: ${s}`)
|
|
53
|
+
if(s<0)throw new RangeError(`dims[${n}] must be a non-negative integer, got: ${s}`)
|
|
54
|
+
t*=s}return t})(o)
|
|
55
|
+
if(i!==r.length)throw new Error(`Tensor's size(${i}) does not match data length(${r.length}).`)
|
|
56
|
+
this.dims=o
|
|
57
|
+
this.type=s
|
|
58
|
+
this.data=r
|
|
59
|
+
this.size=i}static bufferToTensor(e,t){if(void 0===e)throw new Error("Image buffer must be defined")
|
|
60
|
+
if(void 0===t.height||void 0===t.width)throw new Error("Image height and width must be defined")
|
|
61
|
+
const{height:n,width:s}=t
|
|
62
|
+
const r=t.norm
|
|
63
|
+
let o
|
|
64
|
+
let i
|
|
65
|
+
o=void 0===r||void 0===r.mean?255:r.mean
|
|
66
|
+
i=void 0===r||void 0===r.bias?0:r.bias
|
|
67
|
+
const a=void 0!==t.bitmapFormat?t.bitmapFormat:"RGBA"
|
|
68
|
+
const l=void 0!==t.tensorFormat&&void 0!==t.tensorFormat?t.tensorFormat:"RGB"
|
|
69
|
+
const c=n*s
|
|
70
|
+
const d="RGBA"===l?new Float32Array(4*c):new Float32Array(3*c)
|
|
71
|
+
let h=4,_=0,p=1,f=2,m=3
|
|
72
|
+
let g=0,w=c,y=2*c,x=-1
|
|
73
|
+
if("RGB"===a){h=3
|
|
74
|
+
_=0
|
|
75
|
+
p=1
|
|
76
|
+
f=2
|
|
77
|
+
m=-1}if("RGBA"===l)x=3*c
|
|
78
|
+
else if("RBG"===l){g=0
|
|
79
|
+
y=c
|
|
80
|
+
w=2*c}else if("BGR"===l){y=0
|
|
81
|
+
w=c
|
|
82
|
+
g=2*c}for(let t=0;t<c;t++,_+=h,f+=h,p+=h,m+=h){d[g++]=(e[_]+i)/o
|
|
83
|
+
d[w++]=(e[p]+i)/o
|
|
84
|
+
d[y++]=(e[f]+i)/o;-1!==x&&-1!==m&&(d[x++]=(e[m]+i)/o)}return new u("float32",d,"RGBA"===l?[1,4,n,s]:[1,3,n,s])}static async fromImage(e,t){const n="undefined"!=typeof HTMLImageElement&&e instanceof HTMLImageElement
|
|
85
|
+
const s="undefined"!=typeof ImageData&&e instanceof ImageData
|
|
86
|
+
const r="undefined"!=typeof ImageBitmap&&e instanceof ImageBitmap
|
|
87
|
+
const o="undefined"!=typeof String&&(e instanceof String||"string"==typeof e)
|
|
88
|
+
let i
|
|
89
|
+
let a={}
|
|
90
|
+
if(n){const n=document.createElement("canvas")
|
|
91
|
+
const s=n.getContext("2d")
|
|
92
|
+
if(null==s)throw new Error("Can not access image data")
|
|
93
|
+
{let r=e.naturalHeight
|
|
94
|
+
let o=e.naturalWidth
|
|
95
|
+
if(void 0!==t&&void 0!==t.resizedHeight&&void 0!==t.resizedWidth){r=t.resizedHeight
|
|
96
|
+
o=t.resizedWidth}if(void 0!==t){a=t
|
|
97
|
+
if(void 0!==t.tensorFormat)throw new Error("Image input config format must be RGBA for HTMLImageElement")
|
|
98
|
+
a.tensorFormat="RGBA"
|
|
99
|
+
if(void 0!==t.height&&t.height!==r)throw new Error("Image input config height doesn't match HTMLImageElement height")
|
|
100
|
+
a.height=r
|
|
101
|
+
if(void 0!==t.width&&t.width!==o)throw new Error("Image input config width doesn't match HTMLImageElement width")
|
|
102
|
+
a.width=o}else{a.tensorFormat="RGBA"
|
|
103
|
+
a.height=r
|
|
104
|
+
a.width=o}n.width=o
|
|
105
|
+
n.height=r
|
|
106
|
+
s.drawImage(e,0,0,o,r)
|
|
107
|
+
i=s.getImageData(0,0,o,r).data}}else{if(!s){if(r){if(void 0===t)throw new Error("Please provide image config with format for Imagebitmap")
|
|
108
|
+
if(void 0!==t.bitmapFormat)throw new Error("Image input config format must be defined for ImageBitmap")
|
|
109
|
+
const n=document.createElement("canvas").getContext("2d")
|
|
110
|
+
if(null!=n){const s=e.height
|
|
111
|
+
const r=e.width
|
|
112
|
+
n.drawImage(e,0,0,r,s)
|
|
113
|
+
i=n.getImageData(0,0,r,s).data
|
|
114
|
+
if(void 0!==t){if(void 0!==t.height&&t.height!==s)throw new Error("Image input config height doesn't match ImageBitmap height")
|
|
115
|
+
a.height=s
|
|
116
|
+
if(void 0!==t.width&&t.width!==r)throw new Error("Image input config width doesn't match ImageBitmap width")
|
|
117
|
+
a.width=r}else{a.height=s
|
|
118
|
+
a.width=r}return u.bufferToTensor(i,a)}throw new Error("Can not access image data")}if(o)return new Promise(((n,s)=>{const r=document.createElement("canvas")
|
|
119
|
+
const o=r.getContext("2d")
|
|
120
|
+
if(!e||!o)return s()
|
|
121
|
+
const i=new Image
|
|
122
|
+
i.crossOrigin="Anonymous"
|
|
123
|
+
i.src=e
|
|
124
|
+
i.onload=()=>{r.width=i.width
|
|
125
|
+
r.height=i.height
|
|
126
|
+
o.drawImage(i,0,0,r.width,r.height)
|
|
127
|
+
const e=o.getImageData(0,0,r.width,r.height)
|
|
128
|
+
if(void 0!==t){if(void 0!==t.height&&t.height!==r.height)throw new Error("Image input config height doesn't match ImageBitmap height")
|
|
129
|
+
a.height=r.height
|
|
130
|
+
if(void 0!==t.width&&t.width!==r.width)throw new Error("Image input config width doesn't match ImageBitmap width")
|
|
131
|
+
a.width=r.width}else{a.height=r.height
|
|
132
|
+
a.width=r.width}n(u.bufferToTensor(e.data,a))}}))
|
|
133
|
+
throw new Error("Input data provided is not supported - aborted tensor creation")}{const n="RGBA"
|
|
134
|
+
let s
|
|
135
|
+
let r
|
|
136
|
+
if(void 0!==t&&void 0!==t.resizedWidth&&void 0!==t.resizedHeight){s=t.resizedHeight
|
|
137
|
+
r=t.resizedWidth}else{s=e.height
|
|
138
|
+
r=e.width}if(void 0!==t){a=t
|
|
139
|
+
if(void 0!==t.bitmapFormat&&t.bitmapFormat!==n)throw new Error("Image input config format must be RGBA for ImageData")
|
|
140
|
+
a.bitmapFormat="RGBA"}else a.bitmapFormat="RGBA"
|
|
141
|
+
a.height=s
|
|
142
|
+
a.width=r
|
|
143
|
+
if(void 0!==t){const t=document.createElement("canvas")
|
|
144
|
+
t.width=r
|
|
145
|
+
t.height=s
|
|
146
|
+
const n=t.getContext("2d")
|
|
147
|
+
if(null==n)throw new Error("Can not access image data")
|
|
148
|
+
n.putImageData(e,0,0)
|
|
149
|
+
i=n.getImageData(0,0,r,s).data}else i=e.data}}if(void 0!==i)return u.bufferToTensor(i,a)
|
|
150
|
+
throw new Error("Input data provided is not supported - aborted tensor creation")}toImageData(e){var t,n
|
|
151
|
+
const s=document.createElement("canvas").getContext("2d")
|
|
152
|
+
let r
|
|
153
|
+
if(null==s)throw new Error("Can not access image data")
|
|
154
|
+
{const o=this.dims[3]
|
|
155
|
+
const i=this.dims[2]
|
|
156
|
+
const a=this.dims[1]
|
|
157
|
+
const l=void 0!==e&&void 0!==e.format?e.format:"RGB"
|
|
158
|
+
const c=void 0!==e&&void 0!==(null===(t=e.norm)||void 0===t?void 0:t.mean)?e.norm.mean:255
|
|
159
|
+
const d=void 0!==e&&void 0!==(null===(n=e.norm)||void 0===n?void 0:n.bias)?e.norm.bias:0
|
|
160
|
+
const u=i*o
|
|
161
|
+
if(void 0!==e){if(void 0!==e.height&&e.height!==i)throw new Error("Image output config height doesn't match tensor height")
|
|
162
|
+
if(void 0!==e.width&&e.width!==o)throw new Error("Image output config width doesn't match tensor width")
|
|
163
|
+
if(void 0!==e.format&&4===a&&"RGBA"!==e.format||3===a&&"RGB"!==e.format&&"BGR"!==e.format)throw new Error("Tensor format doesn't match input tensor dims")}const h=4
|
|
164
|
+
let _=0,p=1,f=2,m=3
|
|
165
|
+
let g=0,w=u,y=2*u,x=-1
|
|
166
|
+
if("RGBA"===l){g=0
|
|
167
|
+
w=u
|
|
168
|
+
y=2*u
|
|
169
|
+
x=3*u}else if("RGB"===l){g=0
|
|
170
|
+
w=u
|
|
171
|
+
y=2*u}else if("RBG"===l){g=0
|
|
172
|
+
y=u
|
|
173
|
+
w=2*u}r=s.createImageData(o,i)
|
|
174
|
+
for(let e=0;e<i*o;_+=h,p+=h,f+=h,m+=h,e++){r.data[_]=(this.data[g++]-d)*c
|
|
175
|
+
r.data[p]=(this.data[w++]-d)*c
|
|
176
|
+
r.data[f]=(this.data[y++]-d)*c
|
|
177
|
+
r.data[m]=-1===x?255:(this.data[x++]-d)*c}}return r}reshape(e){return new u(this.type,this.data,e)}}const h=u
|
|
178
|
+
class _{constructor(e){this.handler=e}async run(e,t,n){const s={}
|
|
179
|
+
let r={}
|
|
180
|
+
if("object"!=typeof e||null===e||e instanceof h||Array.isArray(e))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.")
|
|
181
|
+
let o=!0
|
|
182
|
+
if("object"==typeof t){if(null===t)throw new TypeError("Unexpected argument[1]: cannot be null.")
|
|
183
|
+
if(t instanceof h)throw new TypeError("'fetches' cannot be a Tensor")
|
|
184
|
+
if(Array.isArray(t)){if(0===t.length)throw new TypeError("'fetches' cannot be an empty array.")
|
|
185
|
+
o=!1
|
|
186
|
+
for(const e of t){if("string"!=typeof e)throw new TypeError("'fetches' must be a string array or an object.")
|
|
187
|
+
if(-1===this.outputNames.indexOf(e))throw new RangeError(`'fetches' contains invalid output name: ${e}.`)
|
|
188
|
+
s[e]=null}if("object"==typeof n&&null!==n)r=n
|
|
189
|
+
else if(void 0!==n)throw new TypeError("'options' must be an object.")}else{let e=!1
|
|
190
|
+
const i=Object.getOwnPropertyNames(t)
|
|
191
|
+
for(const n of this.outputNames)if(-1!==i.indexOf(n)){const r=t[n]
|
|
192
|
+
if(null===r||r instanceof h){e=!0
|
|
193
|
+
o=!1
|
|
194
|
+
s[n]=r}}if(e){if("object"==typeof n&&null!==n)r=n
|
|
195
|
+
else if(void 0!==n)throw new TypeError("'options' must be an object.")}else r=t}}else if(void 0!==t)throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.")
|
|
196
|
+
for(const t of this.inputNames)if(void 0===e[t])throw new Error(`input '${t}' is missing in 'feeds'.`)
|
|
197
|
+
if(o)for(const e of this.outputNames)s[e]=null
|
|
198
|
+
const i=await this.handler.run(e,s,r)
|
|
199
|
+
const a={}
|
|
200
|
+
for(const e in i)Object.hasOwnProperty.call(i,e)&&(a[e]=new h(i[e].type,i[e].data,i[e].dims))
|
|
201
|
+
return a}static async create(e,t,n,o){let i
|
|
202
|
+
let a={}
|
|
203
|
+
if("string"==typeof e){i=e
|
|
204
|
+
if("object"==typeof t&&null!==t)a=t
|
|
205
|
+
else if(void 0!==t)throw new TypeError("'options' must be an object.")}else if(e instanceof Uint8Array){i=e
|
|
206
|
+
if("object"==typeof t&&null!==t)a=t
|
|
207
|
+
else if(void 0!==t)throw new TypeError("'options' must be an object.")}else{if(!(e instanceof ArrayBuffer||"undefined"!=typeof SharedArrayBuffer&&e instanceof SharedArrayBuffer))throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.")
|
|
208
|
+
{const s=e
|
|
209
|
+
let r=0
|
|
210
|
+
let l=e.byteLength
|
|
211
|
+
if("object"==typeof t&&null!==t)a=t
|
|
212
|
+
else if("number"==typeof t){r=t
|
|
213
|
+
if(!Number.isSafeInteger(r))throw new RangeError("'byteOffset' must be an integer.")
|
|
214
|
+
if(r<0||r>=s.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${s.byteLength}).`)
|
|
215
|
+
l=e.byteLength-r
|
|
216
|
+
if("number"==typeof n){l=n
|
|
217
|
+
if(!Number.isSafeInteger(l))throw new RangeError("'byteLength' must be an integer.")
|
|
218
|
+
if(l<=0||r+l>s.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${s.byteLength-r}].`)
|
|
219
|
+
if("object"==typeof o&&null!==o)a=o
|
|
220
|
+
else if(void 0!==o)throw new TypeError("'options' must be an object.")}else if(void 0!==n)throw new TypeError("'byteLength' must be a number.")}else if(void 0!==t)throw new TypeError("'options' must be an object.")
|
|
221
|
+
i=new Uint8Array(s,r,l)}}const l=(a.executionProviders||[]).map((e=>"string"==typeof e?e:e.name))
|
|
222
|
+
const c=await(async e=>{const t=0===e.length?r:e
|
|
223
|
+
const n=[]
|
|
224
|
+
for(const e of t){const t=s[e]
|
|
225
|
+
if(t){if(t.initialized)return t.backend
|
|
226
|
+
if(t.aborted)continue
|
|
227
|
+
const s=!!t.initPromise
|
|
228
|
+
try{s||(t.initPromise=t.backend.init())
|
|
229
|
+
await t.initPromise
|
|
230
|
+
t.initialized=!0
|
|
231
|
+
return t.backend}catch(r){s||n.push({name:e,err:r})
|
|
232
|
+
t.aborted=!0}finally{delete t.initPromise}}}throw new Error(`no available backend found. ERR: ${n.map((e=>`[${e.name}] ${e.err}`)).join(", ")}`)})(l)
|
|
233
|
+
const d=await c.createSessionHandler(i,a)
|
|
234
|
+
return new _(d)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}const p=_},204:function(e,t,n){var s=this&&this.__classPrivateFieldSet||function(e,t,n,s,r){if("m"===s)throw new TypeError("Private method is not writable")
|
|
235
|
+
if("a"===s&&!r)throw new TypeError("Private accessor was defined without a setter")
|
|
236
|
+
if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it")
|
|
237
|
+
return"a"===s?r.call(e,n):r?r.value=n:t.set(e,n),n}
|
|
238
|
+
var r=this&&this.__classPrivateFieldGet||function(e,t,n,s){if("a"===n&&!s)throw new TypeError("Private accessor was defined without a getter")
|
|
239
|
+
if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it")
|
|
240
|
+
return"m"===n?s:"a"===n?s.call(e):s?s.value:t.get(e)}
|
|
241
|
+
var o
|
|
242
|
+
Object.defineProperty(t,"__esModule",{value:!0})
|
|
243
|
+
t.onnxruntimeBackend=void 0
|
|
244
|
+
const i=n(389)
|
|
245
|
+
class a{constructor(e,t){o.set(this,void 0)
|
|
246
|
+
s(this,o,new i.binding.InferenceSession,"f")
|
|
247
|
+
"string"==typeof e?r(this,o,"f").loadModel(e,t):r(this,o,"f").loadModel(e.buffer,e.byteOffset,e.byteLength,t)
|
|
248
|
+
this.inputNames=r(this,o,"f").inputNames
|
|
249
|
+
this.outputNames=r(this,o,"f").outputNames}async dispose(){return Promise.resolve()}startProfiling(){}endProfiling(){}async run(e,t,n){return new Promise(((s,i)=>{process.nextTick((()=>{try{s(r(this,o,"f").run(e,t,n))}catch(e){i(e)}}))}))}}o=new WeakMap
|
|
250
|
+
t.onnxruntimeBackend=new class{async init(){return Promise.resolve()}async createSessionHandler(e,t){return new Promise(((n,s)=>{process.nextTick((()=>{try{n(new a(e,t||{}))}catch(e){s(e)}}))}))}}},389:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0})
|
|
251
|
+
t.binding=void 0
|
|
252
|
+
t.binding=n(745)(`./${process.platform}/${process.arch}/onnxruntime_binding.node`)},536:function(e,t,n){var s=this&&this.__createBinding||(Object.create?function(e,t,n,s){void 0===s&&(s=n)
|
|
253
|
+
var r=Object.getOwnPropertyDescriptor(t,n)
|
|
254
|
+
r&&!("get"in r?!t.__esModule:r.writable||r.configurable)||(r={enumerable:!0,get:function(){return t[n]}})
|
|
255
|
+
Object.defineProperty(e,s,r)}:function(e,t,n,s){void 0===s&&(s=n)
|
|
256
|
+
e[s]=t[n]})
|
|
257
|
+
var r=this&&this.__exportStar||function(e,t){for(var n in e)"default"===n||Object.prototype.hasOwnProperty.call(t,n)||s(t,e,n)}
|
|
258
|
+
Object.defineProperty(t,"__esModule",{value:!0})
|
|
259
|
+
r(n(833),t)
|
|
260
|
+
const o=n(833)
|
|
261
|
+
const i=n(204);(0,o.registerBackend)("cpu",i.onnxruntimeBackend,100)},71:()=>{},968:(e,t,n)=>{e=n.nmd(e)
|
|
262
|
+
try{process.dlopen(e,__dirname+n(928).sep+n.p+"addon-node_modules/onnxruntime-node/bin/napi-v3/darwin/arm64/onnxruntime_binding.node")}catch(e){throw new Error("node-loader:\n"+e)}},710:(e,t,n)=>{e=n.nmd(e)
|
|
263
|
+
try{process.dlopen(e,__dirname+n(928).sep+n.p+"addon-node_modules/onnxruntime-node/bin/napi-v3/darwin/x64/onnxruntime_binding.node")}catch(e){throw new Error("node-loader:\n"+e)}},225:(e,t,n)=>{e=n.nmd(e)
|
|
264
|
+
try{process.dlopen(e,__dirname+n(928).sep+n.p+"addon-node_modules/onnxruntime-node/bin/napi-v3/linux/arm64/onnxruntime_binding.node")}catch(e){throw new Error("node-loader:\n"+e)}},239:(e,t,n)=>{e=n.nmd(e)
|
|
265
|
+
try{process.dlopen(e,__dirname+n(928).sep+n.p+"addon-node_modules/onnxruntime-node/bin/napi-v3/linux/x64/onnxruntime_binding.node")}catch(e){throw new Error("node-loader:\n"+e)}},587:(e,t,n)=>{e=n.nmd(e)
|
|
266
|
+
try{process.dlopen(e,__dirname+n(928).sep+n.p+"addon-node_modules/onnxruntime-node/bin/napi-v3/win32/arm64/onnxruntime_binding.node")}catch(e){throw new Error("node-loader:\n"+e)}},598:(e,t,n)=>{e=n.nmd(e)
|
|
267
|
+
try{process.dlopen(e,__dirname+n(928).sep+n.p+"addon-node_modules/onnxruntime-node/bin/napi-v3/win32/x64/onnxruntime_binding.node")}catch(e){throw new Error("node-loader:\n"+e)}},928:e=>{e.exports=require("path")}}
|
|
268
|
+
var t={}
|
|
269
|
+
function n(s){var r=t[s]
|
|
270
|
+
if(void 0!==r)return r.exports
|
|
271
|
+
var o=t[s]={id:s,loaded:!1,exports:{}}
|
|
272
|
+
e[s].call(o.exports,o,o.exports,n)
|
|
273
|
+
o.loaded=!0
|
|
274
|
+
return o.exports}s=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,n.t=function(e,t){1&t&&(e=this(e))
|
|
275
|
+
if(8&t)return e
|
|
276
|
+
if("object"==typeof e&&e){if(4&t&&e.__esModule)return e
|
|
277
|
+
if(16&t&&"function"==typeof e.then)return e}var o=Object.create(null)
|
|
278
|
+
n.r(o)
|
|
279
|
+
var i={}
|
|
280
|
+
r=r||[null,s({}),s([]),s(s)]
|
|
281
|
+
for(var a=2&t&&e;"object"==typeof a&&!~r.indexOf(a);a=s(a))Object.getOwnPropertyNames(a).forEach((t=>i[t]=()=>e[t]))
|
|
282
|
+
i.default=()=>e
|
|
283
|
+
n.d(o,i)
|
|
284
|
+
return o}
|
|
285
|
+
var s,r
|
|
286
|
+
n.d=(e,t)=>{for(var s in t)n.o(t,s)&&!n.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:t[s]})}
|
|
287
|
+
n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t)
|
|
288
|
+
n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})
|
|
289
|
+
Object.defineProperty(e,"__esModule",{value:!0})}
|
|
290
|
+
n.nmd=e=>{e.paths=[]
|
|
291
|
+
e.children||(e.children=[])
|
|
292
|
+
return e}
|
|
293
|
+
n.p=""
|
|
294
|
+
var o={};(()=>{n.r(o)
|
|
295
|
+
n.d(o,{TFJS:()=>e})
|
|
296
|
+
var e={}
|
|
297
|
+
n.r(e)
|
|
298
|
+
n.d(e,{ASTFeatureExtractor:()=>_h,ASTForAudioClassification:()=>zi,ASTModel:()=>Fi,ASTPreTrainedModel:()=>Si,AlbertForMaskedLM:()=>Do,AlbertForQuestionAnswering:()=>jo,AlbertForSequenceClassification:()=>$o,AlbertModel:()=>No,AlbertPreTrainedModel:()=>Oo,AlbertTokenizer:()=>In,AudioClassificationPipeline:()=>jh,AutoConfig:()=>Ss,AutoModel:()=>zd,AutoModelForAudioClassification:()=>Qd,AutoModelForAudioFrameClassification:()=>Yd,AutoModelForCTC:()=>Hd,AutoModelForCausalLM:()=>$d,AutoModelForDepthEstimation:()=>tu,AutoModelForDocumentQuestionAnswering:()=>Jd,AutoModelForImageClassification:()=>qd,AutoModelForImageFeatureExtraction:()=>nu,AutoModelForImageMatting:()=>Zd,AutoModelForImageSegmentation:()=>Ud,AutoModelForImageToImage:()=>eu,AutoModelForMaskGeneration:()=>Xd,AutoModelForMaskedLM:()=>jd,AutoModelForObjectDetection:()=>Gd,AutoModelForQuestionAnswering:()=>Dd,AutoModelForSemanticSegmentation:()=>Vd,AutoModelForSeq2SeqLM:()=>Ld,AutoModelForSequenceClassification:()=>Pd,AutoModelForSpeechSeq2Seq:()=>Bd,AutoModelForTextToSpectrogram:()=>Od,AutoModelForTextToWaveform:()=>Nd,AutoModelForTokenClassification:()=>Id,AutoModelForVision2Seq:()=>Rd,AutoModelForXVector:()=>Kd,AutoModelForZeroShotObjectDetection:()=>Wd,AutoProcessor:()=>vh,AutoTokenizer:()=>Cs,AutomaticSpeechRecognitionPipeline:()=>Rh,BartForConditionalGeneration:()=>Jo,BartForSequenceClassification:()=>Zo,BartModel:()=>Yo,BartPretrainedModel:()=>Ko,BartTokenizer:()=>Xn,BaseModelOutput:()=>gr,BeitFeatureExtractor:()=>nh,BeitForImageClassification:()=>Xa,BeitModel:()=>Wa,BeitPreTrainedModel:()=>Ga,BertForMaskedLM:()=>xr,BertForQuestionAnswering:()=>kr,BertForSequenceClassification:()=>br,BertForTokenClassification:()=>vr,BertModel:()=>yr,BertPreTrainedModel:()=>wr,BertTokenizer:()=>Pn,BitImageProcessor:()=>qu,BlenderbotForConditionalGeneration:()=>ai,BlenderbotModel:()=>ii,BlenderbotPreTrainedModel:()=>oi,BlenderbotSmallForConditionalGeneration:()=>di,BlenderbotSmallModel:()=>ci,BlenderbotSmallPreTrainedModel:()=>li,BlenderbotSmallTokenizer:()=>vs,BlenderbotTokenizer:()=>bs,BloomForCausalLM:()=>Ma,BloomModel:()=>ka,BloomPreTrainedModel:()=>va,BloomTokenizer:()=>Yn,CLIPFeatureExtractor:()=>Vu,CLIPModel:()=>Ni,CLIPPreTrainedModel:()=>Oi,CLIPSegForImageSegmentation:()=>Hi,CLIPSegModel:()=>Xi,CLIPSegPreTrainedModel:()=>Wi,CLIPTextModelWithProjection:()=>$i,CLIPTokenizer:()=>gs,CLIPVisionModelWithProjection:()=>ji,CamembertForMaskedLM:()=>Wr,CamembertForQuestionAnswering:()=>Qr,CamembertForSequenceClassification:()=>Xr,CamembertForTokenClassification:()=>Hr,CamembertModel:()=>Gr,CamembertPreTrainedModel:()=>Vr,CamembertTokenizer:()=>qn,CausalLMOutput:()=>cu,CausalLMOutputWithPast:()=>du,ChineseCLIPFeatureExtractor:()=>Gu,ChineseCLIPModel:()=>Gi,ChineseCLIPPreTrainedModel:()=>Vi,ClapAudioModelWithProjection:()=>Rc,ClapFeatureExtractor:()=>ph,ClapModel:()=>jc,ClapPreTrainedModel:()=>$c,ClapTextModelWithProjection:()=>Dc,CodeGenForCausalLM:()=>ha,CodeGenModel:()=>ua,CodeGenPreTrainedModel:()=>da,CodeGenTokenizer:()=>ms,CodeLlamaTokenizer:()=>es,CohereTokenizer:()=>Es,ConvBertForMaskedLM:()=>Lr,ConvBertForQuestionAnswering:()=>Nr,ConvBertForSequenceClassification:()=>Br,ConvBertForTokenClassification:()=>Or,ConvBertModel:()=>Ir,ConvBertPreTrainedModel:()=>Pr,ConvBertTokenizer:()=>jn,ConvNextFeatureExtractor:()=>Xu,ConvNextForImageClassification:()=>Al,ConvNextImageProcessor:()=>Hu,ConvNextModel:()=>Cl,ConvNextPreTrainedModel:()=>El,ConvNextV2ForImageClassification:()=>zl,ConvNextV2Model:()=>Fl,ConvNextV2PreTrainedModel:()=>Sl,DPTFeatureExtractor:()=>Du,DPTForDepthEstimation:()=>wl,DPTImageProcessor:()=>Ru,DPTModel:()=>gl,DPTPreTrainedModel:()=>ml,DebertaForMaskedLM:()=>Jr,DebertaForQuestionAnswering:()=>to,DebertaForSequenceClassification:()=>Zr,DebertaForTokenClassification:()=>eo,DebertaModel:()=>Yr,DebertaPreTrainedModel:()=>Kr,DebertaTokenizer:()=>On,DebertaV2ForMaskedLM:()=>ro,DebertaV2ForQuestionAnswering:()=>ao,DebertaV2ForSequenceClassification:()=>oo,DebertaV2ForTokenClassification:()=>io,DebertaV2Model:()=>so,DebertaV2PreTrainedModel:()=>no,DebertaV2Tokenizer:()=>Nn,DeiTFeatureExtractor:()=>th,DeiTForImageClassification:()=>il,DeiTModel:()=>ol,DeiTPreTrainedModel:()=>rl,DepthAnythingForDepthEstimation:()=>xl,DepthAnythingPreTrainedModel:()=>yl,DepthEstimationPipeline:()=>Yh,DetrFeatureExtractor:()=>oh,DetrForObjectDetection:()=>Ka,DetrForSegmentation:()=>Ya,DetrModel:()=>Qa,DetrObjectDetectionOutput:()=>Ja,DetrPreTrainedModel:()=>Ha,DetrSegmentationOutput:()=>Za,Dinov2ForImageClassification:()=>Ll,Dinov2Model:()=>Il,Dinov2PreTrainedModel:()=>Pl,DistilBertForMaskedLM:()=>po,DistilBertForQuestionAnswering:()=>_o,DistilBertForSequenceClassification:()=>uo,DistilBertForTokenClassification:()=>ho,DistilBertModel:()=>co,DistilBertPreTrainedModel:()=>lo,DistilBertTokenizer:()=>Rn,DocumentQuestionAnsweringPipeline:()=>Hh,DonutFeatureExtractor:()=>sh,DonutSwinModel:()=>Tl,DonutSwinPreTrainedModel:()=>Ml,EfficientNetForImageClassification:()=>Zc,EfficientNetImageProcessor:()=>Yu,EfficientNetModel:()=>Jc,EfficientNetPreTrainedModel:()=>Yc,ElectraForMaskedLM:()=>Dr,ElectraForQuestionAnswering:()=>Ur,ElectraForSequenceClassification:()=>Rr,ElectraForTokenClassification:()=>qr,ElectraModel:()=>jr,ElectraPreTrainedModel:()=>$r,ElectraTokenizer:()=>Vn,EsmForMaskedLM:()=>go,EsmForSequenceClassification:()=>wo,EsmForTokenClassification:()=>yo,EsmModel:()=>mo,EsmPreTrainedModel:()=>fo,EsmTokenizer:()=>os,FFT:()=>re,FalconForCausalLM:()=>Nc,FalconModel:()=>Oc,FalconPreTrainedModel:()=>Bc,FalconTokenizer:()=>ss,FeatureExtractionPipeline:()=>Nh,FeatureExtractor:()=>Nu,FillMaskPipeline:()=>Fh,GLPNFeatureExtractor:()=>Uu,GLPNForDepthEstimation:()=>kl,GLPNModel:()=>vl,GLPNPreTrainedModel:()=>bl,GPT2LMHeadModel:()=>Yi,GPT2Model:()=>Ki,GPT2PreTrainedModel:()=>Qi,GPT2Tokenizer:()=>Wn,GPTBigCodeForCausalLM:()=>ca,GPTBigCodeModel:()=>la,GPTBigCodePreTrainedModel:()=>aa,GPTJForCausalLM:()=>ia,GPTJModel:()=>oa,GPTJPreTrainedModel:()=>ra,GPTNeoForCausalLM:()=>ea,GPTNeoModel:()=>Zi,GPTNeoPreTrainedModel:()=>Ji,GPTNeoXForCausalLM:()=>sa,GPTNeoXModel:()=>na,GPTNeoXPreTrainedModel:()=>ta,GPTNeoXTokenizer:()=>rs,GemmaTokenizer:()=>as,Grok1Tokenizer:()=>ls,HerbertTokenizer:()=>$n,HubertForCTC:()=>pc,HubertForSequenceClassification:()=>fc,HubertModel:()=>_c,HubertPreTrainedModel:()=>hc,ImageClassificationPipeline:()=>Uh,ImageFeatureExtractionPipeline:()=>$h,ImageFeatureExtractor:()=>$u,ImageMattingOutput:()=>uu,ImageSegmentationPipeline:()=>Vh,ImageToImagePipeline:()=>Kh,ImageToTextPipeline:()=>qh,LlamaForCausalLM:()=>fa,LlamaModel:()=>pa,LlamaPreTrainedModel:()=>_a,LlamaTokenizer:()=>Zn,LongT5ForConditionalGeneration:()=>Wo,LongT5Model:()=>Go,LongT5PreTrainedModel:()=>Vo,M2M100ForConditionalGeneration:()=>Xl,M2M100Model:()=>Wl,M2M100PreTrainedModel:()=>Gl,M2M100Tokenizer:()=>us,MBart50Tokenizer:()=>Qn,MBartForCausalLM:()=>ri,MBartForConditionalGeneration:()=>ni,MBartForSequenceClassification:()=>si,MBartModel:()=>ti,MBartPreTrainedModel:()=>ei,MBartTokenizer:()=>Hn,MPNetForMaskedLM:()=>Co,MPNetForQuestionAnswering:()=>Fo,MPNetForSequenceClassification:()=>Ao,MPNetForTokenClassification:()=>So,MPNetModel:()=>Eo,MPNetPreTrainedModel:()=>To,MPNetTokenizer:()=>ns,MT5ForConditionalGeneration:()=>Qo,MT5Model:()=>Ho,MT5PreTrainedModel:()=>Xo,MarianMTModel:()=>Vl,MarianModel:()=>Ul,MarianPreTrainedModel:()=>ql,MarianTokenizer:()=>ys,MaskedLMOutput:()=>au,MistralForCausalLM:()=>zc,MistralModel:()=>Fc,MistralPreTrainedModel:()=>Sc,MobileBertForMaskedLM:()=>vo,MobileBertForQuestionAnswering:()=>Mo,MobileBertForSequenceClassification:()=>ko,MobileBertModel:()=>bo,MobileBertPreTrainedModel:()=>xo,MobileBertTokenizer:()=>Ln,MobileViTFeatureExtractor:()=>Ju,MobileViTForImageClassification:()=>$a,MobileViTModel:()=>Na,MobileViTPreTrainedModel:()=>Oa,ModelOutput:()=>mr,MptForCausalLM:()=>Ca,MptModel:()=>Ea,MptPreTrainedModel:()=>Ta,NllbTokenizer:()=>ds,NomicBertModel:()=>Tr,NomicBertPreTrainedModel:()=>Mr,NougatImageProcessor:()=>rh,NougatTokenizer:()=>Ms,OPTForCausalLM:()=>Fa,OPTModel:()=>Sa,OPTPreTrainedModel:()=>Aa,ObjectDetectionPipeline:()=>Wh,OwlViTFeatureExtractor:()=>Zu,OwlViTForObjectDetection:()=>Ra,OwlViTModel:()=>Da,OwlViTPreTrainedModel:()=>ja,OwlViTProcessor:()=>bh,Owlv2ForObjectDetection:()=>Va,Owlv2ImageProcessor:()=>eh,Owlv2Model:()=>Ua,Owlv2PreTrainedModel:()=>qa,PhiForCausalLM:()=>ba,PhiModel:()=>xa,PhiPreTrainedModel:()=>ya,Pipeline:()=>Eh,PreTrainedModel:()=>fr,PreTrainedTokenizer:()=>zn,PretrainedConfig:()=>As,PretrainedMixin:()=>ed,Processor:()=>mh,QuestionAnsweringModelOutput:()=>lu,QuestionAnsweringPipeline:()=>Sh,Qwen2ForCausalLM:()=>wa,Qwen2Model:()=>ga,Qwen2PreTrainedModel:()=>ma,Qwen2Tokenizer:()=>is,RawImage:()=>bu,ResNetForImageClassification:()=>cl,ResNetModel:()=>ll,ResNetPreTrainedModel:()=>al,RoFormerForMaskedLM:()=>Ar,RoFormerForQuestionAnswering:()=>zr,RoFormerForSequenceClassification:()=>Sr,RoFormerForTokenClassification:()=>Fr,RoFormerModel:()=>Cr,RoFormerPreTrainedModel:()=>Er,RoFormerTokenizer:()=>Dn,RobertaForMaskedLM:()=>_i,RobertaForQuestionAnswering:()=>mi,RobertaForSequenceClassification:()=>pi,RobertaForTokenClassification:()=>fi,RobertaModel:()=>hi,RobertaPreTrainedModel:()=>ui,RobertaTokenizer:()=>Kn,SamImageProcessor:()=>ah,SamImageSegmentationOutput:()=>Rl,SamModel:()=>Dl,SamPreTrainedModel:()=>jl,SamProcessor:()=>gh,SeamlessM4TFeatureExtractor:()=>hh,SegformerFeatureExtractor:()=>ju,SegformerForImageClassification:()=>Wc,SegformerForSemanticSegmentation:()=>Xc,SegformerModel:()=>Gc,SegformerPreTrainedModel:()=>Vc,Seq2SeqLMOutput:()=>su,SequenceClassifierOutput:()=>ru,SiglipImageProcessor:()=>Wu,SiglipModel:()=>Ri,SiglipPreTrainedModel:()=>Di,SiglipTextModel:()=>qi,SiglipTokenizer:()=>ws,SiglipVisionModel:()=>Ui,SpeechT5FeatureExtractor:()=>fh,SpeechT5ForSpeechToText:()=>Mc,SpeechT5ForTextToSpeech:()=>Tc,SpeechT5HifiGan:()=>Ec,SpeechT5Model:()=>kc,SpeechT5PreTrainedModel:()=>vc,SpeechT5Processor:()=>xh,SpeechT5Tokenizer:()=>ks,SqueezeBertForMaskedLM:()=>Io,SqueezeBertForQuestionAnswering:()=>Bo,SqueezeBertForSequenceClassification:()=>Lo,SqueezeBertModel:()=>Po,SqueezeBertPreTrainedModel:()=>zo,SqueezeBertTokenizer:()=>Bn,StableLmForCausalLM:()=>Kc,StableLmModel:()=>Qc,StableLmPreTrainedModel:()=>Hc,Starcoder2ForCausalLM:()=>Lc,Starcoder2Model:()=>Ic,Starcoder2PreTrainedModel:()=>Pc,SummarizationPipeline:()=>Ph,Swin2SRForImageSuperResolution:()=>fl,Swin2SRImageProcessor:()=>lh,Swin2SRModel:()=>pl,Swin2SRPreTrainedModel:()=>_l,SwinForImageClassification:()=>hl,SwinModel:()=>ul,SwinPreTrainedModel:()=>dl,T5ForConditionalGeneration:()=>Uo,T5Model:()=>qo,T5PreTrainedModel:()=>Ro,T5Tokenizer:()=>Gn,TableTransformerForObjectDetection:()=>nl,TableTransformerModel:()=>tl,TableTransformerObjectDetectionOutput:()=>sl,TableTransformerPreTrainedModel:()=>el,Tensor:()=>de,Text2TextGenerationPipeline:()=>zh,TextClassificationPipeline:()=>Ch,TextGenerationPipeline:()=>Bh,TextToAudioPipeline:()=>Qh,TokenClassificationPipeline:()=>Ah,TokenClassifierOutput:()=>iu,TokenizerModel:()=>It,TrOCRForCausalLM:()=>Ac,TrOCRPreTrainedModel:()=>Cc,TranslationPipeline:()=>Ih,UniSpeechForCTC:()=>tc,UniSpeechForSequenceClassification:()=>nc,UniSpeechModel:()=>ec,UniSpeechPreTrainedModel:()=>Zl,UniSpeechSatForAudioFrameClassification:()=>ac,UniSpeechSatForCTC:()=>oc,UniSpeechSatForSequenceClassification:()=>ic,UniSpeechSatModel:()=>rc,UniSpeechSatPreTrainedModel:()=>sc,ViTFeatureExtractor:()=>Qu,ViTForImageClassification:()=>Ia,ViTImageProcessor:()=>Ku,ViTModel:()=>Pa,ViTPreTrainedModel:()=>za,VisionEncoderDecoderModel:()=>Bi,VitMatteForImageMatting:()=>Ba,VitMatteImageProcessor:()=>ch,VitMattePreTrainedModel:()=>La,VitsModel:()=>Uc,VitsModelOutput:()=>hu,VitsPreTrainedModel:()=>qc,VitsTokenizer:()=>Ts,Wav2Vec2BertForCTC:()=>dc,Wav2Vec2BertForSequenceClassification:()=>uc,Wav2Vec2BertModel:()=>cc,Wav2Vec2BertPreTrainedModel:()=>lc,Wav2Vec2CTCTokenizer:()=>xs,Wav2Vec2FeatureExtractor:()=>uh,Wav2Vec2ForAudioFrameClassification:()=>Jl,Wav2Vec2ForCTC:()=>Kl,Wav2Vec2ForSequenceClassification:()=>Yl,Wav2Vec2Model:()=>Ql,Wav2Vec2PreTrainedModel:()=>Hl,Wav2Vec2ProcessorWithLM:()=>yh,WavLMForAudioFrameClassification:()=>bc,WavLMForCTC:()=>wc,WavLMForSequenceClassification:()=>yc,WavLMForXVector:()=>xc,WavLMModel:()=>gc,WavLMPreTrainedModel:()=>mc,WhisperFeatureExtractor:()=>dh,WhisperForConditionalGeneration:()=>Li,WhisperModel:()=>Ii,WhisperPreTrainedModel:()=>Pi,WhisperProcessor:()=>wh,WhisperTokenizer:()=>fs,XLMForQuestionAnswering:()=>vi,XLMForSequenceClassification:()=>xi,XLMForTokenClassification:()=>bi,XLMModel:()=>wi,XLMPreTrainedModel:()=>gi,XLMRobertaForMaskedLM:()=>Ti,XLMRobertaForQuestionAnswering:()=>Ai,XLMRobertaForSequenceClassification:()=>Ei,XLMRobertaForTokenClassification:()=>Ci,XLMRobertaModel:()=>Mi,XLMRobertaPreTrainedModel:()=>ki,XLMRobertaTokenizer:()=>ts,XLMTokenizer:()=>Un,XLMWithLMHeadModel:()=>yi,XVectorOutput:()=>ou,YolosFeatureExtractor:()=>ih,YolosForObjectDetection:()=>Nl,YolosModel:()=>Ol,YolosObjectDetectionOutput:()=>$l,YolosPreTrainedModel:()=>Bl,ZeroShotAudioClassificationPipeline:()=>Dh,ZeroShotClassificationPipeline:()=>Oh,ZeroShotImageClassificationPipeline:()=>Gh,ZeroShotObjectDetectionPipeline:()=>Xh,bankers_round:()=>ae,cat:()=>we,cos_sim:()=>Y,dot:()=>Q,dynamicTimeWarping:()=>ve,env:()=>B,getTopItems:()=>K,hanning:()=>ku,interpolate:()=>he,interpolate_data:()=>G,layer_norm:()=>pe,log_softmax:()=>H,magnitude:()=>J,max:()=>ee,mean:()=>be,mean_pooling:()=>_e,medianFilter:()=>oe,mel_filter_bank:()=>Au,min:()=>Z,ones:()=>ke,ones_like:()=>Me,permute:()=>ue,permute_data:()=>W,pipeline:()=>e_,quantize_embeddings:()=>Te,read_audio:()=>vu,round:()=>ie,softmax:()=>X,spectrogram:()=>Fu,stack:()=>ye,std_mean:()=>xe,window_function:()=>zu})
|
|
299
|
+
function t(e,t){e&&e(t)}function s(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}const r=class{constructor(){let e=function(...t){return e._call(...t)}
|
|
300
|
+
return Object.setPrototypeOf(e,new.target.prototype)}_call(...e){throw Error("Must implement _call method in subclass")}}
|
|
301
|
+
function i(e){return Number.isInteger(e)||"bigint"==typeof e}function a(e){const t=[]
|
|
302
|
+
let n=e
|
|
303
|
+
for(;Array.isArray(n);){t.push(n.length)
|
|
304
|
+
n=n[0]}return t}function l(e,t,n=void 0){const s=e[t]
|
|
305
|
+
if(void 0!==s){delete e[t]
|
|
306
|
+
return s}if(void 0===n)throw Error(`Key ${t} does not exist in object.`)
|
|
307
|
+
return n}function c(...e){return Array.prototype.concat.apply([],e)}function d(...e){return e.reduce(((e,t)=>e.flatMap((e=>t.map((t=>[e,t]))))))}function u(e,t){return Math.abs((e+t)%(2*t)-t)}const h=require("fs")
|
|
308
|
+
var _=n(928)
|
|
309
|
+
const p=require("stream/web")
|
|
310
|
+
const f=require("url")
|
|
311
|
+
var m=n(536)
|
|
312
|
+
var g=n.t(m,2)
|
|
313
|
+
var w=n(71)
|
|
314
|
+
var y=n.t(w,2)
|
|
315
|
+
var x
|
|
316
|
+
let b
|
|
317
|
+
const v=["wasm"]
|
|
318
|
+
if("undefined"!=typeof process&&"node"===(null==(x=process)||null==(x=x.release)?void 0:x.name)){var k
|
|
319
|
+
b=null!=(k=m)?k:g
|
|
320
|
+
v.unshift("cpu")}else{var M
|
|
321
|
+
b=null!=(M=w)?M:y
|
|
322
|
+
"undefined"!=typeof navigator&&/iP(hone|od|ad).+16_4.+AppleWebKit/.test(navigator.userAgent)&&(b.env.wasm.simd=!1)}const{env:T}=b
|
|
323
|
+
const E="2.17.1"
|
|
324
|
+
const C="undefined"!=typeof self&&"caches"in self
|
|
325
|
+
const A=!O(h)
|
|
326
|
+
const S=!O(_)
|
|
327
|
+
const F=A&&S
|
|
328
|
+
const z=F?_.dirname(_.dirname(f.fileURLToPath("file:///home/dr/Git/min-pack/min-pack-tfjs-node/node_modules/@xenova/transformers/src/env.js"))):"./"
|
|
329
|
+
const P=F?_.join(z,"/.cache/"):null
|
|
330
|
+
const I="/models/"
|
|
331
|
+
const L=F?_.join(z,I):I
|
|
332
|
+
null!=T&&T.wasm&&(T.wasm.wasmPaths=F?_.join(z,"/dist/"):`https://cdn.jsdelivr.net/npm/@xenova/transformers@${E}/dist/`)
|
|
333
|
+
const B={backends:{onnx:T,tfjs:{}},__dirname:z,version:E,allowRemoteModels:!0,remoteHost:"https://huggingface.co/",remotePathTemplate:"{model}/resolve/{revision}/",allowLocalModels:!0,localModelPath:L,useFS:A,useBrowserCache:C,useFSCache:A,cacheDir:P,useCustomCache:!1,customCache:null}
|
|
334
|
+
function O(e){return 0===Object.keys(e).length}globalThis.ReadableStream||(globalThis.ReadableStream=p.ReadableStream)
|
|
335
|
+
class N{_CONTENT_TYPE_MAP={txt:"text/plain",html:"text/html",css:"text/css",js:"text/javascript",json:"application/json",png:"image/png",jpg:"image/jpeg",jpeg:"image/jpeg",gif:"image/gif"}
|
|
336
|
+
constructor(e){this.filePath=e
|
|
337
|
+
this.headers=new Headers
|
|
338
|
+
this.exists=h.existsSync(e)
|
|
339
|
+
if(this.exists){this.status=200
|
|
340
|
+
this.statusText="OK"
|
|
341
|
+
let t=h.statSync(e)
|
|
342
|
+
this.headers.set("content-length",t.size.toString())
|
|
343
|
+
this.updateContentType()
|
|
344
|
+
let n=this
|
|
345
|
+
this.body=new ReadableStream({start(e){n.arrayBuffer().then((t=>{e.enqueue(new Uint8Array(t))
|
|
346
|
+
e.close()}))}})}else{this.status=404
|
|
347
|
+
this.statusText="Not Found"
|
|
348
|
+
this.body=null}}updateContentType(){var e
|
|
349
|
+
const t=this.filePath.toString().split(".").pop().toLowerCase()
|
|
350
|
+
this.headers.set("content-type",null!=(e=this._CONTENT_TYPE_MAP[t])?e:"application/octet-stream")}clone(){let e=new N(this.filePath)
|
|
351
|
+
e.exists=this.exists
|
|
352
|
+
e.status=this.status
|
|
353
|
+
e.statusText=this.statusText
|
|
354
|
+
e.headers=new Headers(this.headers)
|
|
355
|
+
return e}async arrayBuffer(){return(await h.promises.readFile(this.filePath)).buffer}async blob(){const e=await h.promises.readFile(this.filePath)
|
|
356
|
+
return new Blob([e],{type:this.headers.get("content-type")})}async text(){return await h.promises.readFile(this.filePath,"utf8")}async json(){return JSON.parse(await this.text())}}function $(e,t=null){let n
|
|
357
|
+
try{n=new URL(e)}catch(e){return!1}return!(t&&!t.includes(n.hostname)||"http:"!==n.protocol&&"https:"!==n.protocol)}async function j(e){var t
|
|
358
|
+
if(B.useFS&&!$(e))return new N(e)
|
|
359
|
+
if("undefined"!=typeof process&&"node"===(null==(t=process)||null==(t=t.release)?void 0:t.name)){var n
|
|
360
|
+
const t=!(null==(n=process.env)||!n.TESTING_REMOTELY)
|
|
361
|
+
const i=B.version
|
|
362
|
+
const a=new Headers
|
|
363
|
+
a.set("User-Agent",`transformers.js/${i}; is_ci/${t};`)
|
|
364
|
+
if($(e,["huggingface.co","hf.co"])){var s,r,o
|
|
365
|
+
const e=null!=(s=null==(r=process.env)?void 0:r.HF_TOKEN)?s:null==(o=process.env)?void 0:o.HF_ACCESS_TOKEN
|
|
366
|
+
e&&a.set("Authorization",`Bearer ${e}`)}return fetch(e,{headers:a})}return fetch(e)}const D={400:"Bad request error occurred while trying to load file",401:"Unauthorized access to file",403:"Forbidden access to file",404:"Could not locate file",408:"Request timeout error occurred while trying to load file",500:"Internal server error error occurred while trying to load file",502:"Bad gateway error occurred while trying to load file",503:"Service unavailable error occurred while trying to load file",504:"Gateway timeout error occurred while trying to load file"}
|
|
367
|
+
class R{constructor(e){this.path=e}async match(e){let t=_.join(this.path,e)
|
|
368
|
+
let n=new N(t)
|
|
369
|
+
return n.exists?n:void 0}async put(e,t){const n=Buffer.from(await t.arrayBuffer())
|
|
370
|
+
let s=_.join(this.path,e)
|
|
371
|
+
try{await h.promises.mkdir(_.dirname(s),{recursive:!0})
|
|
372
|
+
await h.promises.writeFile(s,n)}catch(e){console.warn("An error occurred while writing the file to cache:",e)}}}async function q(e,n,s=!0,r={}){var o
|
|
373
|
+
if(!B.allowLocalModels){if(r.local_files_only)throw Error("Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`).")
|
|
374
|
+
if(!B.allowRemoteModels)throw Error("Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`.")}t(r.progress_callback,{status:"initiate",name:e,file:n})
|
|
375
|
+
let i
|
|
376
|
+
if(!i&&B.useBrowserCache){if("undefined"==typeof caches)throw Error("Browser cache is not available in this environment.")
|
|
377
|
+
try{i=await caches.open("transformers-cache")}catch(e){console.warn("An error occurred while opening the browser cache:",e)}}if(!i&&B.useFSCache){var a
|
|
378
|
+
i=new R(null!=(a=r.cache_dir)?a:B.cacheDir)}if(!i&&B.useCustomCache){if(!B.customCache)throw Error("`env.useCustomCache=true`, but `env.customCache` is not defined.")
|
|
379
|
+
if(!B.customCache.match||!B.customCache.put)throw new Error("`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache")
|
|
380
|
+
i=B.customCache}const l=null!=(o=r.revision)?o:"main"
|
|
381
|
+
let c=V(e,n)
|
|
382
|
+
let d=V(B.localModelPath,c)
|
|
383
|
+
let u=V(B.remoteHost,B.remotePathTemplate.replaceAll("{model}",e).replaceAll("{revision}",encodeURIComponent(l)),n)
|
|
384
|
+
let h="main"===l?c:V(e,l,n)
|
|
385
|
+
let _
|
|
386
|
+
let p=i instanceof R?h:u
|
|
387
|
+
let f=!1
|
|
388
|
+
let m
|
|
389
|
+
i&&(m=await async function(e,...t){for(let n of t)try{let t=await e.match(n)
|
|
390
|
+
if(t)return t}catch(e){continue}}(i,d,p))
|
|
391
|
+
const g=void 0!==m
|
|
392
|
+
if(void 0===m){if(B.allowLocalModels)if($(c)){if(r.local_files_only)throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${c}.`)
|
|
393
|
+
if(!B.allowRemoteModels)throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${c}.`)}else try{m=await j(d)
|
|
394
|
+
_=d}catch(e){console.warn(`Unable to load from local path "${d}": "${e}"`)}if(void 0===m||404===m.status){if(r.local_files_only||!B.allowRemoteModels){if(s)throw Error(`\`local_files_only=true\` or \`env.allowRemoteModels=false\` and file was not found locally at "${d}".`)
|
|
395
|
+
return null}m=await j(u)
|
|
396
|
+
if(200!==m.status)return function(e,t,n){var s
|
|
397
|
+
if(!n)return null
|
|
398
|
+
const r=null!=(s=D[e])?s:`Error (${e}) occurred while trying to load file`
|
|
399
|
+
throw Error(`${r}: "${t}".`)}(m.status,u,s)
|
|
400
|
+
_=p}f=i&&"undefined"!=typeof Response&&m instanceof Response&&200===m.status}t(r.progress_callback,{status:"download",name:e,file:n})
|
|
401
|
+
const w={status:"progress",name:e,file:n}
|
|
402
|
+
let y
|
|
403
|
+
if(r.progress_callback)if(g&&"undefined"!=typeof navigator&&/firefox/i.test(navigator.userAgent)){y=new Uint8Array(await m.arrayBuffer())
|
|
404
|
+
t(r.progress_callback,{...w,progress:100,loaded:y.length,total:y.length})}else y=await async function(e,n){const s=e.headers.get("Content-Length")
|
|
405
|
+
null===s&&console.warn("Unable to determine content-length from response headers. Will expand buffer when needed.")
|
|
406
|
+
let o=parseInt(null!=s?s:"0")
|
|
407
|
+
let i=new Uint8Array(o)
|
|
408
|
+
let a=0
|
|
409
|
+
const l=e.body.getReader()
|
|
410
|
+
await async function e(){const{done:n,value:s}=await l.read()
|
|
411
|
+
if(n)return
|
|
412
|
+
let c=a+s.length
|
|
413
|
+
if(c>o){o=c
|
|
414
|
+
let e=new Uint8Array(o)
|
|
415
|
+
e.set(i)
|
|
416
|
+
i=e}i.set(s,a)
|
|
417
|
+
a=c;(e=>{t(r.progress_callback,{...w,...e})})({progress:a/o*100,loaded:a,total:o})
|
|
418
|
+
return e()}()
|
|
419
|
+
return i}(m)
|
|
420
|
+
else y=new Uint8Array(await m.arrayBuffer())
|
|
421
|
+
f&&_&&void 0===await i.match(_)&&await i.put(_,new Response(y,{headers:m.headers})).catch((e=>{console.warn(`Unable to add response to browser cache: ${e}.`)}))
|
|
422
|
+
t(r.progress_callback,{status:"done",name:e,file:n})
|
|
423
|
+
return y}async function U(e,t,n=!0,s={}){let r=await q(e,t,n,s)
|
|
424
|
+
if(null===r)return{}
|
|
425
|
+
let o=new TextDecoder("utf-8").decode(r)
|
|
426
|
+
return JSON.parse(o)}function V(...e){return(e=e.map(((t,n)=>{n&&(t=t.replace(new RegExp("^/"),""))
|
|
427
|
+
n!==e.length-1&&(t=t.replace(new RegExp("/$"),""))
|
|
428
|
+
return t}))).join("/")}function G(e,[t,n,s],[r,o],i="bilinear",a=!1){const l=o/s
|
|
429
|
+
const c=r/n
|
|
430
|
+
const d=new e.constructor(r*o*t)
|
|
431
|
+
const u=n*s
|
|
432
|
+
const h=r*o
|
|
433
|
+
for(let i=0;i<r;++i)for(let r=0;r<o;++r){const a=i*o+r
|
|
434
|
+
const _=(r+.5)/l-.5
|
|
435
|
+
const p=(i+.5)/c-.5
|
|
436
|
+
let f=Math.floor(_)
|
|
437
|
+
let m=Math.floor(p)
|
|
438
|
+
const g=Math.min(f+1,s-1)
|
|
439
|
+
const w=Math.min(m+1,n-1)
|
|
440
|
+
f=Math.max(f,0)
|
|
441
|
+
m=Math.max(m,0)
|
|
442
|
+
const y=_-f
|
|
443
|
+
const x=p-m
|
|
444
|
+
const b=(1-y)*(1-x)
|
|
445
|
+
const v=y*(1-x)
|
|
446
|
+
const k=(1-y)*x
|
|
447
|
+
const M=y*x
|
|
448
|
+
const T=m*s
|
|
449
|
+
const E=w*s
|
|
450
|
+
const C=T+f
|
|
451
|
+
const A=T+g
|
|
452
|
+
const S=E+f
|
|
453
|
+
const F=E+g
|
|
454
|
+
for(let n=0;n<t;++n){const t=n*u
|
|
455
|
+
d[n*h+a]=b*e[t+C]+v*e[t+A]+k*e[t+S]+M*e[t+F]}}return d}function W(e,t,n){const s=new Array(n.length)
|
|
456
|
+
const r=new Array(n.length)
|
|
457
|
+
for(let e=n.length-1,o=1;e>=0;--e){r[e]=o
|
|
458
|
+
s[e]=t[n[e]]
|
|
459
|
+
o*=s[e]}const o=n.map(((e,t)=>r[n.indexOf(t)]))
|
|
460
|
+
const i=new e.constructor(e.length)
|
|
461
|
+
for(let n=0;n<e.length;++n){let s=0
|
|
462
|
+
for(let e=t.length-1,r=n;e>=0;--e){s+=r%t[e]*o[e]
|
|
463
|
+
r=Math.floor(r/t[e])}i[s]=e[n]}return[i,s]}function X(e){const t=ee(e)[0]
|
|
464
|
+
const n=e.map((e=>Math.exp(e-t)))
|
|
465
|
+
const s=n.reduce(((e,t)=>e+t),0)
|
|
466
|
+
return n.map((e=>e/s))}function H(e){return X(e).map((e=>Math.log(e)))}function Q(e,t){let n=0
|
|
467
|
+
for(let s=0;s<e.length;++s)n+=e[s]*t[s]
|
|
468
|
+
return n}function K(e,t=0){e=Array.from(e).map(((e,t)=>[t,e])).sort(((e,t)=>t[1]-e[1]))
|
|
469
|
+
null!==t&&t>0&&(e=e.slice(0,t))
|
|
470
|
+
return e}function Y(e,t){return Q(e,t)/(J(e)*J(t))}function J(e){return Math.sqrt(e.reduce(((e,t)=>e+t*t),0))}function Z(e){if(0===e.length)throw Error("Array must not be empty")
|
|
471
|
+
let t=e[0]
|
|
472
|
+
let n=0
|
|
473
|
+
for(let s=1;s<e.length;++s)if(e[s]<t){t=e[s]
|
|
474
|
+
n=s}return[t,n]}function ee(e){if(0===e.length)throw Error("Array must not be empty")
|
|
475
|
+
let t=e[0]
|
|
476
|
+
let n=0
|
|
477
|
+
for(let s=1;s<e.length;++s)if(e[s]>t){t=e[s]
|
|
478
|
+
n=s}return[Number(t),n]}function te(e){return e>0&&!(e&e-1)}class ne{constructor(e){this.size=0|e
|
|
479
|
+
if(this.size<=1||!te(this.size))throw new Error("FFT size must be a power of two larger than 1")
|
|
480
|
+
this._csize=e<<1
|
|
481
|
+
this.table=new Float64Array(2*this.size)
|
|
482
|
+
for(let e=0;e<this.table.length;e+=2){const t=Math.PI*e/this.size
|
|
483
|
+
this.table[e]=Math.cos(t)
|
|
484
|
+
this.table[e+1]=-Math.sin(t)}let t=0
|
|
485
|
+
for(let e=1;this.size>e;e<<=1)++t
|
|
486
|
+
this._width=t%2==0?t-1:t
|
|
487
|
+
this._bitrev=new Int32Array(1<<this._width)
|
|
488
|
+
for(let e=0;e<this._bitrev.length;++e){this._bitrev[e]=0
|
|
489
|
+
for(let t=0;t<this._width;t+=2){const n=this._width-t-2
|
|
490
|
+
this._bitrev[e]|=(e>>>t&3)<<n}}}createComplexArray(){return new Float64Array(this._csize)}fromComplexArray(e,t){const n=t||new Array(e.length>>>1)
|
|
491
|
+
for(let t=0;t<e.length;t+=2)n[t>>>1]=e[t]
|
|
492
|
+
return n}toComplexArray(e,t){const n=t||this.createComplexArray()
|
|
493
|
+
for(let t=0;t<n.length;t+=2){n[t]=e[t>>>1]
|
|
494
|
+
n[t+1]=0}return n}completeSpectrum(e){const t=this._csize
|
|
495
|
+
const n=t>>>1
|
|
496
|
+
for(let s=2;s<n;s+=2){e[t-s]=e[s]
|
|
497
|
+
e[t-s+1]=-e[s+1]}}transform(e,t){if(e===t)throw new Error("Input and output buffers must be different")
|
|
498
|
+
this._transform4(e,t,1)}realTransform(e,t){if(e===t)throw new Error("Input and output buffers must be different")
|
|
499
|
+
this._realTransform4(e,t,1)}inverseTransform(e,t){if(e===t)throw new Error("Input and output buffers must be different")
|
|
500
|
+
this._transform4(e,t,-1)
|
|
501
|
+
for(let t=0;t<e.length;++t)e[t]/=this.size}_transform4(e,t,n){const s=this._csize
|
|
502
|
+
let r=1<<this._width
|
|
503
|
+
let o=s/r<<1
|
|
504
|
+
let i
|
|
505
|
+
let a
|
|
506
|
+
const l=this._bitrev
|
|
507
|
+
if(4===o)for(i=0,a=0;i<s;i+=o,++a){const n=l[a]
|
|
508
|
+
this._singleTransform2(t,e,i,n,r)}else for(i=0,a=0;i<s;i+=o,++a){const s=l[a]
|
|
509
|
+
this._singleTransform4(t,e,i,s,r,n)}for(r>>=2;r>=2;r>>=2){o=s/r<<1
|
|
510
|
+
const t=o>>>2
|
|
511
|
+
for(i=0;i<s;i+=o){const s=i+t-1
|
|
512
|
+
for(let o=i,a=0;o<s;o+=2,a+=r){const s=o
|
|
513
|
+
const r=s+t
|
|
514
|
+
const i=r+t
|
|
515
|
+
const l=i+t
|
|
516
|
+
const c=e[s]
|
|
517
|
+
const d=e[s+1]
|
|
518
|
+
const u=e[r]
|
|
519
|
+
const h=e[r+1]
|
|
520
|
+
const _=e[i]
|
|
521
|
+
const p=e[i+1]
|
|
522
|
+
const f=e[l]
|
|
523
|
+
const m=e[l+1]
|
|
524
|
+
const g=this.table[a]
|
|
525
|
+
const w=n*this.table[a+1]
|
|
526
|
+
const y=u*g-h*w
|
|
527
|
+
const x=u*w+h*g
|
|
528
|
+
const b=this.table[2*a]
|
|
529
|
+
const v=n*this.table[2*a+1]
|
|
530
|
+
const k=_*b-p*v
|
|
531
|
+
const M=_*v+p*b
|
|
532
|
+
const T=this.table[3*a]
|
|
533
|
+
const E=n*this.table[3*a+1]
|
|
534
|
+
const C=f*T-m*E
|
|
535
|
+
const A=f*E+m*T
|
|
536
|
+
const S=c+k
|
|
537
|
+
const F=d+M
|
|
538
|
+
const z=c-k
|
|
539
|
+
const P=d-M
|
|
540
|
+
const I=y+C
|
|
541
|
+
const L=x+A
|
|
542
|
+
const B=n*(y-C)
|
|
543
|
+
const O=n*(x-A)
|
|
544
|
+
e[s]=S+I
|
|
545
|
+
e[s+1]=F+L
|
|
546
|
+
e[r]=z+O
|
|
547
|
+
e[r+1]=P-B
|
|
548
|
+
e[i]=S-I
|
|
549
|
+
e[i+1]=F-L
|
|
550
|
+
e[l]=z-O
|
|
551
|
+
e[l+1]=P+B}}}}_singleTransform2(e,t,n,s,r){const o=e[s]
|
|
552
|
+
const i=e[s+1]
|
|
553
|
+
const a=e[s+r]
|
|
554
|
+
const l=e[s+r+1]
|
|
555
|
+
t[n]=o+a
|
|
556
|
+
t[n+1]=i+l
|
|
557
|
+
t[n+2]=o-a
|
|
558
|
+
t[n+3]=i-l}_singleTransform4(e,t,n,s,r,o){const i=2*r
|
|
559
|
+
const a=3*r
|
|
560
|
+
const l=e[s]
|
|
561
|
+
const c=e[s+1]
|
|
562
|
+
const d=e[s+r]
|
|
563
|
+
const u=e[s+r+1]
|
|
564
|
+
const h=e[s+i]
|
|
565
|
+
const _=e[s+i+1]
|
|
566
|
+
const p=e[s+a]
|
|
567
|
+
const f=e[s+a+1]
|
|
568
|
+
const m=l+h
|
|
569
|
+
const g=c+_
|
|
570
|
+
const w=l-h
|
|
571
|
+
const y=c-_
|
|
572
|
+
const x=d+p
|
|
573
|
+
const b=u+f
|
|
574
|
+
const v=o*(d-p)
|
|
575
|
+
const k=o*(u-f)
|
|
576
|
+
t[n]=m+x
|
|
577
|
+
t[n+1]=g+b
|
|
578
|
+
t[n+2]=w+k
|
|
579
|
+
t[n+3]=y-v
|
|
580
|
+
t[n+4]=m-x
|
|
581
|
+
t[n+5]=g-b
|
|
582
|
+
t[n+6]=w-k
|
|
583
|
+
t[n+7]=y+v}_realTransform4(e,t,n){const s=this._csize
|
|
584
|
+
let r=1<<this._width
|
|
585
|
+
let o=s/r<<1
|
|
586
|
+
let i
|
|
587
|
+
let a
|
|
588
|
+
const l=this._bitrev
|
|
589
|
+
if(4===o)for(i=0,a=0;i<s;i+=o,++a){const n=l[a]
|
|
590
|
+
this._singleRealTransform2(t,e,i,n>>>1,r>>>1)}else for(i=0,a=0;i<s;i+=o,++a){const s=l[a]
|
|
591
|
+
this._singleRealTransform4(t,e,i,s>>>1,r>>>1,n)}for(r>>=2;r>=2;r>>=2){o=s/r<<1
|
|
592
|
+
const t=o>>>2
|
|
593
|
+
for(i=0;i<s;i+=o){const s=i+t-1
|
|
594
|
+
for(let o=i,a=0;o<s;o+=2,a+=r){const s=o
|
|
595
|
+
const r=s+t
|
|
596
|
+
const i=r+t
|
|
597
|
+
const l=i+t
|
|
598
|
+
const c=e[s]
|
|
599
|
+
const d=e[s+1]
|
|
600
|
+
const u=e[r]
|
|
601
|
+
const h=e[r+1]
|
|
602
|
+
const _=e[i]
|
|
603
|
+
const p=e[i+1]
|
|
604
|
+
const f=e[l]
|
|
605
|
+
const m=e[l+1]
|
|
606
|
+
const g=this.table[a]
|
|
607
|
+
const w=n*this.table[a+1]
|
|
608
|
+
const y=u*g-h*w
|
|
609
|
+
const x=u*w+h*g
|
|
610
|
+
const b=this.table[2*a]
|
|
611
|
+
const v=n*this.table[2*a+1]
|
|
612
|
+
const k=_*b-p*v
|
|
613
|
+
const M=_*v+p*b
|
|
614
|
+
const T=this.table[3*a]
|
|
615
|
+
const E=n*this.table[3*a+1]
|
|
616
|
+
const C=f*T-m*E
|
|
617
|
+
const A=f*E+m*T
|
|
618
|
+
const S=c+k
|
|
619
|
+
const F=d+M
|
|
620
|
+
const z=c-k
|
|
621
|
+
const P=d-M
|
|
622
|
+
const I=y+C
|
|
623
|
+
const L=x+A
|
|
624
|
+
const B=n*(y-C)
|
|
625
|
+
const O=n*(x-A)
|
|
626
|
+
e[s]=S+I
|
|
627
|
+
e[s+1]=F+L
|
|
628
|
+
e[r]=z+O
|
|
629
|
+
e[r+1]=P-B
|
|
630
|
+
e[i]=S-I
|
|
631
|
+
e[i+1]=F-L
|
|
632
|
+
e[l]=z-O
|
|
633
|
+
e[l+1]=P+B}}}}_singleRealTransform2(e,t,n,s,r){const o=e[s]
|
|
634
|
+
const i=e[s+r]
|
|
635
|
+
t[n]=o+i
|
|
636
|
+
t[n+1]=0
|
|
637
|
+
t[n+2]=o-i
|
|
638
|
+
t[n+3]=0}_singleRealTransform4(e,t,n,s,r,o){const i=2*r
|
|
639
|
+
const a=3*r
|
|
640
|
+
const l=e[s]
|
|
641
|
+
const c=e[s+r]
|
|
642
|
+
const d=e[s+i]
|
|
643
|
+
const u=e[s+a]
|
|
644
|
+
const h=l+d
|
|
645
|
+
const _=l-d
|
|
646
|
+
const p=c+u
|
|
647
|
+
const f=o*(c-u)
|
|
648
|
+
t[n]=h+p
|
|
649
|
+
t[n+1]=0
|
|
650
|
+
t[n+2]=_
|
|
651
|
+
t[n+3]=-f
|
|
652
|
+
t[n+4]=h-p
|
|
653
|
+
t[n+5]=0
|
|
654
|
+
t[n+6]=_
|
|
655
|
+
t[n+7]=f}}class se{constructor(e){const t=2*(e-1)
|
|
656
|
+
const n=2*(2*e-1)
|
|
657
|
+
const s=2**Math.ceil(Math.log2(n))
|
|
658
|
+
this.bufferSize=s
|
|
659
|
+
this._a=t
|
|
660
|
+
const r=new Float64Array(n)
|
|
661
|
+
const o=new Float64Array(s)
|
|
662
|
+
this._chirpBuffer=new Float64Array(s)
|
|
663
|
+
this._buffer1=new Float64Array(s)
|
|
664
|
+
this._buffer2=new Float64Array(s)
|
|
665
|
+
this._outBuffer1=new Float64Array(s)
|
|
666
|
+
this._outBuffer2=new Float64Array(s)
|
|
667
|
+
const i=-2*Math.PI/e
|
|
668
|
+
const a=Math.cos(i)
|
|
669
|
+
const l=Math.sin(i)
|
|
670
|
+
for(let t=0;t<n>>1;++t){const n=(t+1-e)**2/2
|
|
671
|
+
const s=Math.sqrt(a**2+l**2)**n
|
|
672
|
+
const i=n*Math.atan2(l,a)
|
|
673
|
+
const c=2*t
|
|
674
|
+
r[c]=s*Math.cos(i)
|
|
675
|
+
r[c+1]=s*Math.sin(i)
|
|
676
|
+
o[c]=r[c]
|
|
677
|
+
o[c+1]=-r[c+1]}this._slicedChirpBuffer=r.subarray(t,n)
|
|
678
|
+
this._f=new ne(s>>1)
|
|
679
|
+
this._f.transform(this._chirpBuffer,o)}_transform(e,t,n){const s=this._buffer1
|
|
680
|
+
const r=this._buffer2
|
|
681
|
+
const o=this._outBuffer1
|
|
682
|
+
const i=this._outBuffer2
|
|
683
|
+
const a=this._chirpBuffer
|
|
684
|
+
const l=this._slicedChirpBuffer
|
|
685
|
+
const c=this._a
|
|
686
|
+
if(n)for(let e=0;e<l.length;e+=2){const n=e+1
|
|
687
|
+
const r=t[e>>1]
|
|
688
|
+
s[e]=r*l[e]
|
|
689
|
+
s[n]=r*l[n]}else for(let e=0;e<l.length;e+=2){const n=e+1
|
|
690
|
+
s[e]=t[e]*l[e]-t[n]*l[n]
|
|
691
|
+
s[n]=t[e]*l[n]+t[n]*l[e]}this._f.transform(o,s)
|
|
692
|
+
for(let e=0;e<a.length;e+=2){const t=e+1
|
|
693
|
+
r[e]=o[e]*a[e]-o[t]*a[t]
|
|
694
|
+
r[t]=o[e]*a[t]+o[t]*a[e]}this._f.inverseTransform(i,r)
|
|
695
|
+
for(let t=0;t<i.length;t+=2){const n=i[t+c]
|
|
696
|
+
const s=i[t+c+1]
|
|
697
|
+
const r=l[t]
|
|
698
|
+
const o=l[t+1]
|
|
699
|
+
e[t]=n*r-s*o
|
|
700
|
+
e[t+1]=n*o+s*r}}transform(e,t){this._transform(e,t,!1)}realTransform(e,t){this._transform(e,t,!0)}}class re{constructor(e){this.fft_length=e
|
|
701
|
+
this.isPowerOfTwo=te(e)
|
|
702
|
+
if(this.isPowerOfTwo){this.fft=new ne(e)
|
|
703
|
+
this.outputBufferSize=2*e}else{this.fft=new se(e)
|
|
704
|
+
this.outputBufferSize=this.fft.bufferSize}}realTransform(e,t){this.fft.realTransform(e,t)}transform(e,t){this.fft.transform(e,t)}}function oe(e,t){if(t%2==0||t<=0)throw new Error("Window size must be a positive odd number")
|
|
705
|
+
const n=new e.constructor(e.length)
|
|
706
|
+
const s=new e.constructor(t)
|
|
707
|
+
const r=Math.floor(t/2)
|
|
708
|
+
for(let t=0;t<e.length;++t){let o=0
|
|
709
|
+
for(let n=-r;n<=r;++n){let r=t+n
|
|
710
|
+
r<0?r=Math.abs(r):r>=e.length&&(r=2*(e.length-1)-r)
|
|
711
|
+
s[o++]=e[r]}s.sort()
|
|
712
|
+
n[t]=s[r]}return n}function ie(e,t){const n=Math.pow(10,t)
|
|
713
|
+
return Math.round(e*n)/n}function ae(e){const t=Math.round(e)
|
|
714
|
+
return Math.abs(e)%1==.5?t%2==0?t:t-1:t}const le=Object.freeze({float32:Float32Array,float64:Float64Array,string:Array,int8:Int8Array,uint8:Uint8Array,int16:Int16Array,uint16:Uint16Array,int32:Int32Array,uint32:Uint32Array,int64:BigInt64Array,uint64:BigUint64Array,bool:Uint8Array})
|
|
715
|
+
const ce=b.Tensor
|
|
716
|
+
class de{dims
|
|
717
|
+
type
|
|
718
|
+
data
|
|
719
|
+
size
|
|
720
|
+
constructor(...e){e[0]instanceof ce?Object.assign(this,e[0]):Object.assign(this,new ce(e[0],e[1],e[2]))
|
|
721
|
+
return new Proxy(this,{get:(e,t)=>{if("string"==typeof t){let n=Number(t)
|
|
722
|
+
if(Number.isInteger(n))return e._getitem(n)}return e[t]},set:(e,t,n)=>e[t]=n})}*[Symbol.iterator](){const[e,...t]=this.dims
|
|
723
|
+
if(t.length>0){const n=t.reduce(((e,t)=>e*t))
|
|
724
|
+
for(let s=0;s<e;++s)yield this._subarray(s,n,t)}else yield*this.data}_getitem(e){const[t,...n]=this.dims
|
|
725
|
+
e=ge(e,t)
|
|
726
|
+
if(n.length>0){const t=n.reduce(((e,t)=>e*t))
|
|
727
|
+
return this._subarray(e,t,n)}return new de(this.type,[this.data[e]],n)}indexOf(e){for(let t=0;t<this.data.length;++t)if(this.data[t]==e)return t
|
|
728
|
+
return-1}_subarray(e,t,n){const s=e*t
|
|
729
|
+
const r=(e+1)*t
|
|
730
|
+
const o="subarray"in this.data?this.data.subarray(s,r):this.data.slice(s,r)
|
|
731
|
+
return new de(this.type,o,n)}item(){if(1!==this.data.length)throw new Error(`a Tensor with ${this.data.length} elements cannot be converted to Scalar`)
|
|
732
|
+
return this.data[0]}tolist(){return function(e,t){const n=e.length
|
|
733
|
+
if(n!==t.reduce(((e,t)=>e*t)))throw Error(`cannot reshape array of size ${n} into shape (${t})`)
|
|
734
|
+
let s=e
|
|
735
|
+
for(let e=t.length-1;e>=0;e--)s=s.reduce(((n,s)=>{let r=n[n.length-1]
|
|
736
|
+
r.length<t[e]?r.push(s):n.push([s])
|
|
737
|
+
return n}),[[]])
|
|
738
|
+
return s[0]}(this.data,this.dims)}sigmoid(){return this.clone().sigmoid_()}sigmoid_(){for(let e=0;e<this.data.length;++e)this.data[e]=1/(1+Math.exp(-this.data[e]))
|
|
739
|
+
return this}mul(e){return this.clone().mul_(e)}mul_(e){for(let t=0;t<this.data.length;++t)this.data[t]*=e
|
|
740
|
+
return this}add(e){return this.clone().add_(e)}add_(e){for(let t=0;t<this.data.length;++t)this.data[t]+=e
|
|
741
|
+
return this}clone(){return new de(this.type,this.data.slice(),this.dims.slice())}slice(...e){let t=[]
|
|
742
|
+
let n=[]
|
|
743
|
+
for(let s=0;s<this.dims.length;++s){let r=e[s]
|
|
744
|
+
if(null==r){n.push([0,this.dims[s]])
|
|
745
|
+
t.push(this.dims[s])}else if("number"==typeof r){r=ge(r,this.dims[s],s)
|
|
746
|
+
n.push([r,r+1])}else{if(!Array.isArray(r)||2!==r.length)throw new Error(`Invalid slice: ${r}`)
|
|
747
|
+
{if(r[0]>r[1])throw new Error(`Invalid slice: ${r}`)
|
|
748
|
+
let e=[Math.max(r[0],0),Math.min(r[1],this.dims[s])]
|
|
749
|
+
n.push(e)
|
|
750
|
+
t.push(e[1]-e[0])}}}let s=n.map((([e,t])=>t-e))
|
|
751
|
+
let r=s.reduce(((e,t)=>e*t))
|
|
752
|
+
let o=new this.data.constructor(r)
|
|
753
|
+
const i=this.stride()
|
|
754
|
+
for(let e=0;e<r;++e){let t=0
|
|
755
|
+
for(let r=s.length-1,o=e;r>=0;--r){const e=s[r]
|
|
756
|
+
t+=(o%e+n[r][0])*i[r]
|
|
757
|
+
o=Math.floor(o/e)}o[e]=this.data[t]}return new de(this.type,o,t)}permute(...e){return ue(this,e)}transpose(...e){return this.permute(...e)}sum(e=null,t=!1){return this.norm(1,e,t)}norm(e="fro",t=null,n=!1){if("fro"===e)e=2
|
|
758
|
+
else if("string"==typeof e)throw Error(`Unsupported norm: ${e}`)
|
|
759
|
+
if(null===t){let t=this.data.reduce(((t,n)=>t+n**e),0)**(1/e)
|
|
760
|
+
return new de(this.type,[t],[])}t=ge(t,this.dims.length)
|
|
761
|
+
const s=this.dims.slice()
|
|
762
|
+
s[t]=1
|
|
763
|
+
const r=new this.data.constructor(this.data.length/this.dims[t])
|
|
764
|
+
for(let n=0;n<this.data.length;++n){let o=0
|
|
765
|
+
for(let e=this.dims.length-1,r=n,i=1;e>=0;--e){const n=this.dims[e]
|
|
766
|
+
if(e!==t){o+=r%n*i
|
|
767
|
+
i*=s[e]}r=Math.floor(r/n)}r[o]+=this.data[n]**e}if(1!==e)for(let t=0;t<r.length;++t)r[t]=r[t]**(1/e)
|
|
768
|
+
n||s.splice(t,1)
|
|
769
|
+
return new de(this.type,r,s)}normalize_(e=2,t=1){t=ge(t,this.dims.length)
|
|
770
|
+
const n=this.norm(e,t,!0)
|
|
771
|
+
for(let e=0;e<this.data.length;++e){let s=0
|
|
772
|
+
for(let n=this.dims.length-1,r=e,o=1;n>=0;--n){const e=this.dims[n]
|
|
773
|
+
if(n!==t){s+=r%e*o
|
|
774
|
+
o*=this.dims[n]}r=Math.floor(r/e)}this.data[e]/=n.data[s]}return this}normalize(e=2,t=1){return this.clone().normalize_(e,t)}stride(){return function(e){const t=new Array(e.length)
|
|
775
|
+
for(let n=e.length-1,s=1;n>=0;--n){t[n]=s
|
|
776
|
+
s*=e[n]}return t}(this.dims)}squeeze(e=null){return new de(this.type,this.data,fe(this.dims,e))}squeeze_(e=null){this.dims=fe(this.dims,e)
|
|
777
|
+
return this}unsqueeze(e=null){return new de(this.type,this.data,me(this.dims,e))}unsqueeze_(e=null){this.dims=me(this.dims,e)
|
|
778
|
+
return this}flatten_(e=0,t=-1){t=(t+this.dims.length)%this.dims.length
|
|
779
|
+
let n=this.dims.slice(0,e)
|
|
780
|
+
let s=this.dims.slice(e,t+1)
|
|
781
|
+
let r=this.dims.slice(t+1)
|
|
782
|
+
this.dims=[...n,s.reduce(((e,t)=>e*t),1),...r]
|
|
783
|
+
return this}flatten(e=0,t=-1){return this.clone().flatten_(e,t)}view(...e){let t=-1
|
|
784
|
+
for(let n=0;n<e.length;++n)if(-1===e[n]){if(-1!==t)throw new Error("Only one dimension can be inferred")
|
|
785
|
+
t=n}if(-1!==t){const n=e.reduce(((e,n,s)=>s!==t?e*n:e),1)
|
|
786
|
+
e[t]=this.data.length/n}return new de(this.type,this.data,e)}neg_(){for(let e=0;e<this.data.length;++e)this.data[e]=-this.data[e]
|
|
787
|
+
return this}neg(){return this.clone().neg_()}clamp_(e,t){for(let n=0;n<this.data.length;++n)this.data[n]=Math.min(Math.max(this.data[n],e),t)
|
|
788
|
+
return this}clamp(e,t){return this.clone().clamp_(e,t)}round_(){for(let e=0;e<this.data.length;++e)this.data[e]=Math.round(this.data[e])
|
|
789
|
+
return this}round(){return this.clone().round_()}to(e){if(this.type===e)return this
|
|
790
|
+
if(!le.hasOwnProperty(e))throw new Error(`Unsupported type: ${e}`)
|
|
791
|
+
return new de(e,le[e].from(this.data),this.dims)}}function ue(e,t){const[n,s]=W(e.data,e.dims,t)
|
|
792
|
+
return new de(e.type,n,s)}function he(e,[t,n],s="bilinear",r=!1){var o
|
|
793
|
+
const i=null!=(o=e.dims.at(-3))?o:1
|
|
794
|
+
const a=e.dims.at(-2)
|
|
795
|
+
const l=e.dims.at(-1)
|
|
796
|
+
let c=G(e.data,[i,a,l],[t,n],s,r)
|
|
797
|
+
return new de(e.type,c,[i,t,n])}function _e(e,t){let n=[e.dims[0],e.dims[2]]
|
|
798
|
+
let s=new e.data.constructor(n[0]*n[1])
|
|
799
|
+
let[r,o,i]=e.dims
|
|
800
|
+
let a=0
|
|
801
|
+
for(let n=0;n<r;++n){let r=n*i*o
|
|
802
|
+
for(let l=0;l<i;++l){let c=0
|
|
803
|
+
let d=0
|
|
804
|
+
let u=n*o
|
|
805
|
+
let h=r+l
|
|
806
|
+
for(let n=0;n<o;++n){let s=Number(t.data[u+n])
|
|
807
|
+
d+=s
|
|
808
|
+
c+=e.data[h+n*i]*s}let _=c/d
|
|
809
|
+
s[a++]=_}}return new de(e.type,s,n)}function pe(e,t,{eps:n=1e-5}={}){if(2!==e.dims.length)throw new Error("`layer_norm` currently only supports 2D input.")
|
|
810
|
+
const[s,r]=e.dims
|
|
811
|
+
if(1!==t.length&&t[0]!==r)throw new Error("`normalized_shape` must be a 1D array with shape `[input.dims[1]]`.")
|
|
812
|
+
const[o,i]=xe(e,1,0,!0)
|
|
813
|
+
const a=new e.data.constructor(e.data.length)
|
|
814
|
+
for(let t=0;t<s;++t){const s=t*r
|
|
815
|
+
for(let l=0;l<r;++l){const r=s+l
|
|
816
|
+
a[r]=(e.data[r]-i.data[t])/(o.data[t]+n)}}return new de(e.type,a,e.dims)}function fe(e,t){e=e.slice()
|
|
817
|
+
null===t?e=e.filter((e=>1!==e)):"number"==typeof t?1===e[t]&&e.splice(t,1):Array.isArray(t)&&(e=e.filter(((e,n)=>1!==e||!t.includes(n))))
|
|
818
|
+
return e}function me(e,t){t=ge(t,e.length+1);(e=e.slice()).splice(t,0,1)
|
|
819
|
+
return e}function ge(e,t,n=null){if(e<-t||e>=t)throw new Error(`IndexError: index ${e} is out of bounds for dimension${null===n?"":" "+n} with size ${t}`)
|
|
820
|
+
e<0&&(e=(e%t+t)%t)
|
|
821
|
+
return e}function we(e,t=0){t=ge(t,e[0].dims.length)
|
|
822
|
+
const n=e[0].dims.slice()
|
|
823
|
+
n[t]=e.reduce(((e,n)=>e+n.dims[t]),0)
|
|
824
|
+
const s=n.reduce(((e,t)=>e*t),1)
|
|
825
|
+
const r=new e[0].data.constructor(s)
|
|
826
|
+
const o=e[0].type
|
|
827
|
+
if(0===t){let t=0
|
|
828
|
+
for(let n of e){r.set(n.data,t)
|
|
829
|
+
t+=n.data.length}}else{let s=0
|
|
830
|
+
for(let o=0;o<e.length;++o){let i=e[o]
|
|
831
|
+
for(let e=0;e<i.data.length;++e){let o=0
|
|
832
|
+
for(let r=i.dims.length-1,a=e,l=1;r>=0;--r){const e=i.dims[r]
|
|
833
|
+
let c=a%e
|
|
834
|
+
r===t&&(c+=s)
|
|
835
|
+
o+=c*l
|
|
836
|
+
l*=n[r]
|
|
837
|
+
a=Math.floor(a/e)}r[o]=i.data[e]}s+=i.dims[t]}}return new de(o,r,n)}function ye(e,t=0){return we(e.map((e=>e.unsqueeze(t))),t)}function xe(e,t=null,n=1,s=!1){if(null===t){const t=e.data.reduce(((e,t)=>e+t),0)/e.data.length
|
|
838
|
+
const s=Math.sqrt(e.data.reduce(((e,n)=>e+(n-t)**2),0)/(e.data.length-n))
|
|
839
|
+
const r=new de(e.type,[t],[])
|
|
840
|
+
return[new de(e.type,[s],[]),r]}const r=be(e,t=ge(t,e.dims.length),s)
|
|
841
|
+
const o=e.dims.slice()
|
|
842
|
+
o[t]=1
|
|
843
|
+
const i=new e.data.constructor(e.data.length/e.dims[t])
|
|
844
|
+
for(let n=0;n<e.data.length;++n){let s=0
|
|
845
|
+
for(let r=e.dims.length-1,i=n,a=1;r>=0;--r){const n=e.dims[r]
|
|
846
|
+
if(r!==t){s+=i%n*a
|
|
847
|
+
a*=o[r]}i=Math.floor(i/n)}i[s]+=(e.data[n]-r.data[s])**2}for(let s=0;s<i.length;++s)i[s]=Math.sqrt(i[s]/(e.dims[t]-n))
|
|
848
|
+
s||o.splice(t,1)
|
|
849
|
+
return[new de(e.type,i,o),r]}function be(e,t=null,n=!1){if(null===t){let t=e.data.reduce(((e,t)=>e+t),0)
|
|
850
|
+
return new de(e.type,[t/e.data.length],[])}t=ge(t,e.dims.length)
|
|
851
|
+
const s=e.dims.slice()
|
|
852
|
+
s[t]=1
|
|
853
|
+
const r=new e.data.constructor(e.data.length/e.dims[t])
|
|
854
|
+
for(let n=0;n<e.data.length;++n){let o=0
|
|
855
|
+
for(let r=e.dims.length-1,i=n,a=1;r>=0;--r){const n=e.dims[r]
|
|
856
|
+
if(r!==t){o+=i%n*a
|
|
857
|
+
a*=s[r]}i=Math.floor(i/n)}r[o]+=e.data[n]}if(1!==e.dims[t])for(let n=0;n<r.length;++n)r[n]=r[n]/e.dims[t]
|
|
858
|
+
n||s.splice(t,1)
|
|
859
|
+
return new de(e.type,r,s)}function ve(e){const[t,n]=e.dims
|
|
860
|
+
const s=[t+1,n+1]
|
|
861
|
+
const r=new de("float32",new Float32Array(s[0]*s[1]).fill(1/0),s)
|
|
862
|
+
const o=new de("float32",new Float32Array(s[0]*s[1]).fill(-1),s)
|
|
863
|
+
r[0].data[0]=0
|
|
864
|
+
for(let s=1;s<n+1;++s)for(let n=1;n<t+1;++n){const t=r[n-1][s-1].item()
|
|
865
|
+
const i=r[n-1][s].item()
|
|
866
|
+
const a=r[n][s-1].item()
|
|
867
|
+
let l,c
|
|
868
|
+
if(t<i&&t<a){l=t
|
|
869
|
+
c=0}else if(i<t&&i<a){l=i
|
|
870
|
+
c=1}else{l=a
|
|
871
|
+
c=2}r[n].data[s]=e[n-1][s-1].item()+l
|
|
872
|
+
o[n].data[s]=c}let i=t
|
|
873
|
+
let a=n
|
|
874
|
+
o.data.fill(2,0,s[1])
|
|
875
|
+
for(let e=0;e<s[0];++e)o[e].data[0]=1
|
|
876
|
+
let l=[]
|
|
877
|
+
let c=[]
|
|
878
|
+
for(;i>0||a>0;){l.push(i-1)
|
|
879
|
+
c.push(a-1)
|
|
880
|
+
switch(o[i][a].item()){case 0:--i;--a
|
|
881
|
+
break
|
|
882
|
+
case 1:--i
|
|
883
|
+
break
|
|
884
|
+
case 2:--a
|
|
885
|
+
break
|
|
886
|
+
default:throw new Error(`Internal error in dynamic time warping. Unexpected trace[${i}, ${a}]. Please file a bug report.`)}}l.reverse()
|
|
887
|
+
c.reverse()
|
|
888
|
+
return[l,c]}function ke(e){const t=e.reduce(((e,t)=>e*t),1)
|
|
889
|
+
return new de("int64",new BigInt64Array(t).fill(1n),e)}function Me(e){return ke(e.dims)}function Te(e,t){if(2!==e.dims.length)throw new Error("The tensor must have 2 dimensions")
|
|
890
|
+
if(e.dims.at(-1)%8!=0)throw new Error("The last dimension of the tensor must be a multiple of 8")
|
|
891
|
+
if(!["binary","ubinary"].includes(t))throw new Error("The precision must be either 'binary' or 'ubinary'")
|
|
892
|
+
const n="binary"===t
|
|
893
|
+
const s=n?"int8":"uint8"
|
|
894
|
+
const r=n?Int8Array:Uint8Array
|
|
895
|
+
const o=e.data
|
|
896
|
+
const i=new r(o.length/8)
|
|
897
|
+
for(let e=0;e<o.length;++e){const t=o[e]>0?1:0
|
|
898
|
+
const s=Math.floor(e/8)
|
|
899
|
+
const r=e%8
|
|
900
|
+
i[s]|=t<<7-r
|
|
901
|
+
n&&0===r&&(i[s]-=128)}return new de(s,i,[e.dims[0],e.dims[1]/8])}class Ee{constructor(e=((e,t)=>e>t)){this._heap=[]
|
|
902
|
+
this._comparator=e}get size(){return this._heap.length}isEmpty(){return 0===this.size}peek(){return this._heap[0]}push(...e){return this.extend(e)}extend(e){for(const t of e){this._heap.push(t)
|
|
903
|
+
this._siftUp()}return this.size}pop(){const e=this.peek()
|
|
904
|
+
const t=this.size-1
|
|
905
|
+
t>0&&this._swap(0,t)
|
|
906
|
+
this._heap.pop()
|
|
907
|
+
this._siftDown()
|
|
908
|
+
return e}replace(e){const t=this.peek()
|
|
909
|
+
this._heap[0]=e
|
|
910
|
+
this._siftDown()
|
|
911
|
+
return t}_parent(e){return(e+1>>>1)-1}_left(e){return 1+(e<<1)}_right(e){return e+1<<1}_greater(e,t){return this._comparator(this._heap[e],this._heap[t])}_swap(e,t){const n=this._heap[e]
|
|
912
|
+
this._heap[e]=this._heap[t]
|
|
913
|
+
this._heap[t]=n}_siftUp(){let e=this.size-1
|
|
914
|
+
for(;e>0&&this._greater(e,this._parent(e));){this._swap(e,this._parent(e))
|
|
915
|
+
e=this._parent(e)}}_siftDown(){let e=0
|
|
916
|
+
for(;this._left(e)<this.size&&this._greater(this._left(e),e)||this._right(e)<this.size&&this._greater(this._right(e),e);){const t=this._right(e)<this.size&&this._greater(this._right(e),this._left(e))?this._right(e):this._left(e)
|
|
917
|
+
this._swap(e,t)
|
|
918
|
+
e=t}}}class Ce{constructor(){this.root=Ae.default()}extend(e){for(let t of e)this.push(t)}push(e){let t=this.root
|
|
919
|
+
for(let n of e){let e=t.children.get(n)
|
|
920
|
+
if(void 0===e){e=Ae.default()
|
|
921
|
+
t.children.set(n,e)}t=e}t.isLeaf=!0}*commonPrefixSearch(e){let t=this.root
|
|
922
|
+
let n=""
|
|
923
|
+
for(let s=0;s<e.length&&void 0!==t;++s){const r=e[s]
|
|
924
|
+
n+=r
|
|
925
|
+
t=t.children.get(r)
|
|
926
|
+
void 0!==t&&t.isLeaf&&(yield n)}}}class Ae{constructor(e,t){this.isLeaf=e
|
|
927
|
+
this.children=t}static default(){return new Ae(!1,new Map)}}class Se{constructor(e,t,n){this.sentence=e
|
|
928
|
+
this.len=e.length
|
|
929
|
+
this.bosTokenId=t
|
|
930
|
+
this.eosTokenId=n
|
|
931
|
+
this.nodes=[]
|
|
932
|
+
this.beginNodes=Array.from({length:this.len+1},(()=>[]))
|
|
933
|
+
this.endNodes=Array.from({length:this.len+1},(()=>[]))
|
|
934
|
+
const s=new Fe(this.bosTokenId,0,0,0,0)
|
|
935
|
+
const r=new Fe(this.eosTokenId,1,this.len,0,0)
|
|
936
|
+
this.nodes.push(s.clone())
|
|
937
|
+
this.nodes.push(r.clone())
|
|
938
|
+
this.beginNodes[this.len].push(r)
|
|
939
|
+
this.endNodes[0].push(s)}insert(e,t,n,s){const r=this.nodes.length
|
|
940
|
+
const o=new Fe(s,r,e,t,n)
|
|
941
|
+
this.beginNodes[e].push(o)
|
|
942
|
+
this.endNodes[e+t].push(o)
|
|
943
|
+
this.nodes.push(o)}viterbi(){const e=this.len
|
|
944
|
+
let t=0
|
|
945
|
+
for(;t<=e;){if(0==this.beginNodes[t].length)return[]
|
|
946
|
+
for(let e of this.beginNodes[t]){e.prev=null
|
|
947
|
+
let n=0
|
|
948
|
+
let s=null
|
|
949
|
+
for(let r of this.endNodes[t]){const t=r.backtraceScore+e.score
|
|
950
|
+
if(null===s||t>n){s=r.clone()
|
|
951
|
+
n=t}}if(null===s)return[]
|
|
952
|
+
e.prev=s
|
|
953
|
+
e.backtraceScore=n}++t}const n=[]
|
|
954
|
+
const s=this.beginNodes[e][0].prev
|
|
955
|
+
if(null===s)return[]
|
|
956
|
+
let r=s.clone()
|
|
957
|
+
for(;null!==r.prev;){n.push(r.clone())
|
|
958
|
+
const e=r.clone()
|
|
959
|
+
r=e.prev.clone()}n.reverse()
|
|
960
|
+
return n}piece(e){return this.sentence.slice(e.pos,e.pos+e.length)}tokens(){return this.viterbi().map((e=>this.piece(e)))}tokenIds(){return this.viterbi().map((e=>e.tokenId))}}class Fe{constructor(e,t,n,s,r){this.tokenId=e
|
|
961
|
+
this.nodeId=t
|
|
962
|
+
this.pos=n
|
|
963
|
+
this.length=s
|
|
964
|
+
this.score=r
|
|
965
|
+
this.prev=null
|
|
966
|
+
this.backtraceScore=0}clone(){const e=new Fe(this.tokenId,this.nodeId,this.pos,this.length,this.score)
|
|
967
|
+
e.prev=this.prev
|
|
968
|
+
e.backtraceScore=this.backtraceScore
|
|
969
|
+
return e}}var ze=Object.freeze({Text:"Text",NumericLiteral:"NumericLiteral",BooleanLiteral:"BooleanLiteral",StringLiteral:"StringLiteral",Identifier:"Identifier",Equals:"Equals",OpenParen:"OpenParen",CloseParen:"CloseParen",OpenStatement:"OpenStatement",CloseStatement:"CloseStatement",OpenExpression:"OpenExpression",CloseExpression:"CloseExpression",OpenSquareBracket:"OpenSquareBracket",CloseSquareBracket:"CloseSquareBracket",OpenCurlyBracket:"OpenCurlyBracket",CloseCurlyBracket:"CloseCurlyBracket",Comma:"Comma",Dot:"Dot",Colon:"Colon",Pipe:"Pipe",CallOperator:"CallOperator",AdditiveBinaryOperator:"AdditiveBinaryOperator",MultiplicativeBinaryOperator:"MultiplicativeBinaryOperator",ComparisonBinaryOperator:"ComparisonBinaryOperator",UnaryOperator:"UnaryOperator",Set:"Set",If:"If",For:"For",In:"In",Is:"Is",NotIn:"NotIn",Else:"Else",EndIf:"EndIf",ElseIf:"ElseIf",EndFor:"EndFor",And:"And",Or:"Or",Not:"UnaryOperator"})
|
|
970
|
+
var Pe=Object.freeze({set:ze.Set,for:ze.For,in:ze.In,is:ze.Is,if:ze.If,else:ze.Else,endif:ze.EndIf,elif:ze.ElseIf,endfor:ze.EndFor,and:ze.And,or:ze.Or,not:ze.Not,"not in":ze.NotIn,true:ze.BooleanLiteral,false:ze.BooleanLiteral})
|
|
971
|
+
var Ie=class{constructor(e,t){this.value=e
|
|
972
|
+
this.type=t}}
|
|
973
|
+
function Le(e){return/\w/.test(e)}function Be(e){return/[0-9]/.test(e)}var Oe=[["{%",ze.OpenStatement],["%}",ze.CloseStatement],["{{",ze.OpenExpression],["}}",ze.CloseExpression],["(",ze.OpenParen],[")",ze.CloseParen],["{",ze.OpenCurlyBracket],["}",ze.CloseCurlyBracket],["[",ze.OpenSquareBracket],["]",ze.CloseSquareBracket],[",",ze.Comma],[".",ze.Dot],[":",ze.Colon],["|",ze.Pipe],["<=",ze.ComparisonBinaryOperator],[">=",ze.ComparisonBinaryOperator],["==",ze.ComparisonBinaryOperator],["!=",ze.ComparisonBinaryOperator],["<",ze.ComparisonBinaryOperator],[">",ze.ComparisonBinaryOperator],["+",ze.AdditiveBinaryOperator],["-",ze.AdditiveBinaryOperator],["*",ze.MultiplicativeBinaryOperator],["/",ze.MultiplicativeBinaryOperator],["%",ze.MultiplicativeBinaryOperator],["=",ze.Equals]]
|
|
974
|
+
var Ne=new Map([["n","\n"],["t","\t"],["r","\r"],["b","\b"],["f","\f"],["v","\v"],["'","'"],['"','"'],["\\","\\"]])
|
|
975
|
+
var $e=class{type="Statement"}
|
|
976
|
+
var je=class extends $e{constructor(e){super()
|
|
977
|
+
this.body=e}type="Program"}
|
|
978
|
+
var De=class extends $e{constructor(e,t,n){super()
|
|
979
|
+
this.test=e
|
|
980
|
+
this.body=t
|
|
981
|
+
this.alternate=n}type="If"}
|
|
982
|
+
var Re=class extends $e{constructor(e,t,n){super()
|
|
983
|
+
this.loopvar=e
|
|
984
|
+
this.iterable=t
|
|
985
|
+
this.body=n}type="For"}
|
|
986
|
+
var qe=class extends $e{constructor(e,t){super()
|
|
987
|
+
this.assignee=e
|
|
988
|
+
this.value=t}type="Set"}
|
|
989
|
+
var Ue=class extends $e{type="Expression"}
|
|
990
|
+
var Ve=class extends Ue{constructor(e,t,n){super()
|
|
991
|
+
this.object=e
|
|
992
|
+
this.property=t
|
|
993
|
+
this.computed=n}type="MemberExpression"}
|
|
994
|
+
var Ge=class extends Ue{constructor(e,t){super()
|
|
995
|
+
this.callee=e
|
|
996
|
+
this.args=t}type="CallExpression"}
|
|
997
|
+
var We=class extends Ue{constructor(e){super()
|
|
998
|
+
this.value=e}type="Identifier"}
|
|
999
|
+
var Xe=class extends Ue{constructor(e){super()
|
|
1000
|
+
this.value=e}type="Literal"}
|
|
1001
|
+
var He=class extends Xe{type="NumericLiteral"}
|
|
1002
|
+
var Qe=class extends Xe{type="StringLiteral"}
|
|
1003
|
+
var Ke=class extends Xe{type="BooleanLiteral"}
|
|
1004
|
+
var Ye=class extends Xe{type="ArrayLiteral"}
|
|
1005
|
+
var Je=class extends Xe{type="TupleLiteral"}
|
|
1006
|
+
var Ze=class extends Xe{type="ObjectLiteral"}
|
|
1007
|
+
var et=class extends Ue{constructor(e,t,n){super()
|
|
1008
|
+
this.operator=e
|
|
1009
|
+
this.left=t
|
|
1010
|
+
this.right=n}type="BinaryExpression"}
|
|
1011
|
+
var tt=class extends Ue{constructor(e,t){super()
|
|
1012
|
+
this.operand=e
|
|
1013
|
+
this.filter=t}type="FilterExpression"}
|
|
1014
|
+
var nt=class extends Ue{constructor(e,t,n){super()
|
|
1015
|
+
this.operand=e
|
|
1016
|
+
this.negate=t
|
|
1017
|
+
this.test=n}type="TestExpression"}
|
|
1018
|
+
var st=class extends Ue{constructor(e,t){super()
|
|
1019
|
+
this.operator=e
|
|
1020
|
+
this.argument=t}type="UnaryExpression"}
|
|
1021
|
+
var rt=class extends Ue{constructor(e=void 0,t=void 0,n=void 0){super()
|
|
1022
|
+
this.start=e
|
|
1023
|
+
this.stop=t
|
|
1024
|
+
this.step=n}type="SliceExpression"}
|
|
1025
|
+
var ot=class extends Ue{constructor(e,t){super()
|
|
1026
|
+
this.key=e
|
|
1027
|
+
this.value=t}type="KeywordArgumentExpression"}
|
|
1028
|
+
function it(e){const t=new je([])
|
|
1029
|
+
let n=0
|
|
1030
|
+
function s(t,s){const r=e[n++]
|
|
1031
|
+
if(!r||r.type!==t)throw new Error(`Parser Error: ${s}. ${r.type} !== ${t}.`)
|
|
1032
|
+
return r}function r(){switch(e[n].type){case ze.Text:return new Qe(s(ze.Text,"Expected text token").value)
|
|
1033
|
+
case ze.OpenStatement:return function(){s(ze.OpenStatement,"Expected opening statement token")
|
|
1034
|
+
let t
|
|
1035
|
+
switch(e[n].type){case ze.Set:++n
|
|
1036
|
+
t=a()
|
|
1037
|
+
s(ze.CloseStatement,"Expected closing statement token")
|
|
1038
|
+
break
|
|
1039
|
+
case ze.If:++n
|
|
1040
|
+
t=l()
|
|
1041
|
+
s(ze.OpenStatement,"Expected {% token")
|
|
1042
|
+
s(ze.EndIf,"Expected endif token")
|
|
1043
|
+
s(ze.CloseStatement,"Expected %} token")
|
|
1044
|
+
break
|
|
1045
|
+
case ze.For:++n
|
|
1046
|
+
t=function(){const e=c(!0)
|
|
1047
|
+
if(!(e instanceof We||e instanceof Je))throw new SyntaxError(`Expected identifier/tuple for the loop variable, got ${e.type} instead`)
|
|
1048
|
+
s(ze.In,"Expected `in` keyword following loop variable")
|
|
1049
|
+
const t=d()
|
|
1050
|
+
s(ze.CloseStatement,"Expected closing statement token")
|
|
1051
|
+
const n=[]
|
|
1052
|
+
for(;o(ze.OpenStatement,ze.EndFor);)n.push(r())
|
|
1053
|
+
return new Re(e,t,n)}()
|
|
1054
|
+
s(ze.OpenStatement,"Expected {% token")
|
|
1055
|
+
s(ze.EndFor,"Expected endfor token")
|
|
1056
|
+
s(ze.CloseStatement,"Expected %} token")
|
|
1057
|
+
break
|
|
1058
|
+
default:throw new SyntaxError(`Unknown statement type: ${e[n].type}`)}return t}()
|
|
1059
|
+
case ze.OpenExpression:return function(){s(ze.OpenExpression,"Expected opening expression token")
|
|
1060
|
+
const e=d()
|
|
1061
|
+
s(ze.CloseExpression,"Expected closing expression token")
|
|
1062
|
+
return e}()
|
|
1063
|
+
default:throw new SyntaxError(`Unexpected token type: ${e[n].type}`)}}function o(...t){return n+t.length<=e.length&&t.some(((t,s)=>t!==e[n+s].type))}function i(...t){return n+t.length<=e.length&&t.every(((t,s)=>t===e[n+s].type))}function a(){const e=d()
|
|
1064
|
+
if(i(ze.Equals)){++n
|
|
1065
|
+
const t=a()
|
|
1066
|
+
return new qe(e,t)}return e}function l(){var t,o
|
|
1067
|
+
const a=d()
|
|
1068
|
+
s(ze.CloseStatement,"Expected closing statement token")
|
|
1069
|
+
const c=[]
|
|
1070
|
+
const u=[]
|
|
1071
|
+
for(;(null==(h=e[n])?void 0:h.type)!==ze.OpenStatement||(null==(_=e[n+1])?void 0:_.type)!==ze.ElseIf&&(null==(p=e[n+1])?void 0:p.type)!==ze.Else&&(null==(f=e[n+1])?void 0:f.type)!==ze.EndIf;){var h,_,p,f
|
|
1072
|
+
c.push(r())}if((null==(t=e[n])?void 0:t.type)===ze.OpenStatement&&(null==(o=e[n+1])?void 0:o.type)!==ze.EndIf){++n
|
|
1073
|
+
if(i(ze.ElseIf)){s(ze.ElseIf,"Expected elseif token")
|
|
1074
|
+
u.push(l())}else{s(ze.Else,"Expected else token")
|
|
1075
|
+
s(ze.CloseStatement,"Expected closing statement token")
|
|
1076
|
+
for(;(null==(m=e[n])?void 0:m.type)!==ze.OpenStatement||(null==(g=e[n+1])?void 0:g.type)!==ze.EndIf;){var m,g
|
|
1077
|
+
u.push(r())}}}return new De(a,c,u)}function c(e=!1){const t=e?y:d
|
|
1078
|
+
const s=[t()]
|
|
1079
|
+
const r=i(ze.Comma)
|
|
1080
|
+
for(;r;){++n
|
|
1081
|
+
s.push(t())
|
|
1082
|
+
if(!i(ze.Comma))break}return r?new Je(s):s[0]}function d(){return function(){const e=u()
|
|
1083
|
+
if(i(ze.If)){++n
|
|
1084
|
+
const t=u()
|
|
1085
|
+
s(ze.Else,"Expected else token")
|
|
1086
|
+
const r=u()
|
|
1087
|
+
return new De(t,[e],[r])}return e}()}function u(){let t=h()
|
|
1088
|
+
for(;i(ze.Or);){const s=e[n];++n
|
|
1089
|
+
const r=h()
|
|
1090
|
+
t=new et(s,t,r)}return t}function h(){let t=_()
|
|
1091
|
+
for(;i(ze.And);){const s=e[n];++n
|
|
1092
|
+
const r=_()
|
|
1093
|
+
t=new et(s,t,r)}return t}function _(){var t
|
|
1094
|
+
let s
|
|
1095
|
+
for(;i(ze.Not);){const t=e[n];++n
|
|
1096
|
+
const r=_()
|
|
1097
|
+
s=new st(t,r)}return null!=(t=s)?t:function(){let t=p()
|
|
1098
|
+
for(;i(ze.ComparisonBinaryOperator)||i(ze.In)||i(ze.NotIn);){const s=e[n];++n
|
|
1099
|
+
const r=p()
|
|
1100
|
+
t=new et(s,t,r)}return t}()}function p(){let t=g()
|
|
1101
|
+
for(;i(ze.AdditiveBinaryOperator);){const s=e[n];++n
|
|
1102
|
+
const r=g()
|
|
1103
|
+
t=new et(s,t,r)}return t}function f(e){let t=new Ge(e,function(){s(ze.OpenParen,"Expected opening parenthesis for arguments list")
|
|
1104
|
+
const e=function(){const e=[]
|
|
1105
|
+
for(;!i(ze.CloseParen);){let t=d()
|
|
1106
|
+
if(i(ze.Equals)){++n
|
|
1107
|
+
if(!(t instanceof We))throw new SyntaxError("Expected identifier for keyword argument")
|
|
1108
|
+
const e=d()
|
|
1109
|
+
t=new ot(t,e)}e.push(t)
|
|
1110
|
+
i(ze.Comma)&&++n}return e}()
|
|
1111
|
+
s(ze.CloseParen,"Expected closing parenthesis for arguments list")
|
|
1112
|
+
return e}())
|
|
1113
|
+
i(ze.OpenParen)&&(t=f(t))
|
|
1114
|
+
return t}function m(){const e=[]
|
|
1115
|
+
let t=!1
|
|
1116
|
+
for(;!i(ze.CloseSquareBracket);)if(i(ze.Colon)){e.push(void 0);++n
|
|
1117
|
+
t=!0}else{e.push(d())
|
|
1118
|
+
if(i(ze.Colon)){++n
|
|
1119
|
+
t=!0}}if(0===e.length)throw new SyntaxError("Expected at least one argument for member/slice expression")
|
|
1120
|
+
if(t){if(e.length>3)throw new SyntaxError("Expected 0-3 arguments for slice expression")
|
|
1121
|
+
return new rt(...e)}return e[0]}function g(){let t=w()
|
|
1122
|
+
for(;i(ze.MultiplicativeBinaryOperator);){const s=e[n];++n
|
|
1123
|
+
const r=w()
|
|
1124
|
+
t=new et(s,t,r)}return t}function w(){let t=function(){let t=function(){const t=function(){let t=y()
|
|
1125
|
+
for(;i(ze.Dot)||i(ze.OpenSquareBracket);){const r=e[n];++n
|
|
1126
|
+
let o
|
|
1127
|
+
const i=r.type!==ze.Dot
|
|
1128
|
+
if(i){o=m()
|
|
1129
|
+
s(ze.CloseSquareBracket,"Expected closing square bracket")}else{o=y()
|
|
1130
|
+
if("Identifier"!==o.type)throw new SyntaxError("Expected identifier following dot operator")}t=new Ve(t,o,i)}return t}()
|
|
1131
|
+
return i(ze.OpenParen)?f(t):t}()
|
|
1132
|
+
for(;i(ze.Pipe);){++n
|
|
1133
|
+
let e=y()
|
|
1134
|
+
if(!(e instanceof We))throw new SyntaxError("Expected identifier for the filter")
|
|
1135
|
+
i(ze.OpenParen)&&(e=f(e))
|
|
1136
|
+
t=new tt(t,e)}return t}()
|
|
1137
|
+
for(;i(ze.Is);){++n
|
|
1138
|
+
const e=i(ze.Not)
|
|
1139
|
+
e&&++n
|
|
1140
|
+
let s=y()
|
|
1141
|
+
s instanceof Ke&&(s=new We(s.value.toString()))
|
|
1142
|
+
if(!(s instanceof We))throw new SyntaxError("Expected identifier for the test")
|
|
1143
|
+
t=new nt(t,e,s)}return t}function y(){const t=e[n]
|
|
1144
|
+
switch(t.type){case ze.NumericLiteral:++n
|
|
1145
|
+
return new He(Number(t.value))
|
|
1146
|
+
case ze.StringLiteral:++n
|
|
1147
|
+
return new Qe(t.value)
|
|
1148
|
+
case ze.BooleanLiteral:++n
|
|
1149
|
+
return new Ke("true"===t.value)
|
|
1150
|
+
case ze.Identifier:++n
|
|
1151
|
+
return new We(t.value)
|
|
1152
|
+
case ze.OpenParen:{++n
|
|
1153
|
+
const t=c()
|
|
1154
|
+
if(e[n].type!==ze.CloseParen)throw new SyntaxError(`Expected closing parenthesis, got ${e[n].type} instead`);++n
|
|
1155
|
+
return t}case ze.OpenSquareBracket:{++n
|
|
1156
|
+
const e=[]
|
|
1157
|
+
for(;!i(ze.CloseSquareBracket);){e.push(d())
|
|
1158
|
+
i(ze.Comma)&&++n}++n
|
|
1159
|
+
return new Ye(e)}case ze.OpenCurlyBracket:{++n
|
|
1160
|
+
const e=new Map
|
|
1161
|
+
for(;!i(ze.CloseCurlyBracket);){const t=d()
|
|
1162
|
+
s(ze.Colon,"Expected colon between key and value in object literal")
|
|
1163
|
+
const r=d()
|
|
1164
|
+
e.set(t,r)
|
|
1165
|
+
i(ze.Comma)&&++n}++n
|
|
1166
|
+
return new Ze(e)}default:throw new SyntaxError(`Unexpected token: ${t.type}`)}}for(;n<e.length;)t.body.push(r())
|
|
1167
|
+
return t}function at(e,t,n=1){if(void 0===t){t=e
|
|
1168
|
+
e=0}const s=[]
|
|
1169
|
+
for(let r=e;r<t;r+=n)s.push(r)
|
|
1170
|
+
return s}function lt(e,t,n,s=1){const r=Math.sign(s)
|
|
1171
|
+
if(r>=0){var o,i
|
|
1172
|
+
t=(null!=(o=t)?o:t=0)<0?Math.max(e.length+t,0):Math.min(t,e.length)
|
|
1173
|
+
n=(null!=(i=n)?i:n=e.length)<0?Math.max(e.length+n,0):Math.min(n,e.length)}else{var a,l
|
|
1174
|
+
t=(null!=(a=t)?a:t=e.length-1)<0?Math.max(e.length+t,-1):Math.min(t,e.length-1)
|
|
1175
|
+
n=(null!=(l=n)?l:n=-1)<-1?Math.max(e.length+n,-1):Math.min(n,e.length-1)}const c=[]
|
|
1176
|
+
for(let o=t;r*o<r*n;o+=s)c.push(e[o])
|
|
1177
|
+
return c}function ct(e){return e.replace(/\b\w/g,(e=>e.toUpperCase()))}var dt=class{type="RuntimeValue"
|
|
1178
|
+
value
|
|
1179
|
+
builtins=new Map
|
|
1180
|
+
constructor(e=void 0){this.value=e}__bool__(){return new _t(!!this.value)}}
|
|
1181
|
+
var ut=class extends dt{type="NumericValue"}
|
|
1182
|
+
var ht=class extends dt{type="StringValue"
|
|
1183
|
+
builtins=new Map([["upper",new gt((()=>new ht(this.value.toUpperCase())))],["lower",new gt((()=>new ht(this.value.toLowerCase())))],["strip",new gt((()=>new ht(this.value.trim())))],["title",new gt((()=>new ht(ct(this.value))))],["length",new ut(this.value.length)]])}
|
|
1184
|
+
var _t=class extends dt{type="BooleanValue"}
|
|
1185
|
+
var pt=class extends dt{type="ObjectValue"
|
|
1186
|
+
__bool__(){return new _t(this.value.size>0)}builtins=new Map([["get",new gt((([e,t])=>{var n,s
|
|
1187
|
+
if(!(e instanceof ht))throw new Error(`Object key must be a string: got ${e.type}`)
|
|
1188
|
+
return null!=(n=null!=(s=this.value.get(e.value))?s:t)?n:new wt}))],["items",new gt((()=>new ft(Array.from(this.value.entries()).map((([e,t])=>new ft([new ht(e),t]))))))]])}
|
|
1189
|
+
var ft=class extends dt{type="ArrayValue"
|
|
1190
|
+
builtins=new Map([["length",new ut(this.value.length)]])
|
|
1191
|
+
__bool__(){return new _t(this.value.length>0)}}
|
|
1192
|
+
var mt=class extends ft{type="TupleValue"}
|
|
1193
|
+
var gt=class extends dt{type="FunctionValue"}
|
|
1194
|
+
var wt=class extends dt{type="NullValue"}
|
|
1195
|
+
var yt=class extends dt{type="UndefinedValue"}
|
|
1196
|
+
var xt=class{constructor(e){this.parent=e}variables=new Map([["namespace",new gt((e=>{if(0===e.length)return new pt(new Map)
|
|
1197
|
+
if(1!==e.length||!(e[0]instanceof pt))throw new Error("`namespace` expects either zero arguments or a single object argument")
|
|
1198
|
+
return e[0]}))]])
|
|
1199
|
+
tests=new Map([["boolean",e=>"BooleanValue"===e.type],["callable",e=>e instanceof gt],["odd",e=>{if("NumericValue"!==e.type)throw new Error(`Cannot apply test "odd" to type: ${e.type}`)
|
|
1200
|
+
return e.value%2!=0}],["even",e=>{if("NumericValue"!==e.type)throw new Error(`Cannot apply test "even" to type: ${e.type}`)
|
|
1201
|
+
return e.value%2==0}],["false",e=>"BooleanValue"===e.type&&!e.value],["true",e=>"BooleanValue"===e.type&&e.value],["number",e=>"NumericValue"===e.type],["integer",e=>"NumericValue"===e.type&&Number.isInteger(e.value)],["iterable",e=>e instanceof ft||e instanceof ht],["lower",e=>{const t=e.value
|
|
1202
|
+
return"StringValue"===e.type&&t===t.toLowerCase()}],["upper",e=>{const t=e.value
|
|
1203
|
+
return"StringValue"===e.type&&t===t.toUpperCase()}],["none",e=>"NullValue"===e.type],["defined",e=>"UndefinedValue"!==e.type],["undefined",e=>"UndefinedValue"===e.type],["equalto",(e,t)=>e.value===t.value]])
|
|
1204
|
+
set(e,t){return this.declareVariable(e,vt(t))}declareVariable(e,t){if(this.variables.has(e))throw new SyntaxError(`Variable already declared: ${e}`)
|
|
1205
|
+
this.variables.set(e,t)
|
|
1206
|
+
return t}setVariable(e,t){this.variables.set(e,t)
|
|
1207
|
+
return t}resolve(e){if(this.variables.has(e))return this
|
|
1208
|
+
if(this.parent)return this.parent.resolve(e)
|
|
1209
|
+
throw new Error(`Unknown variable: ${e}`)}lookupVariable(e){try{var t
|
|
1210
|
+
return null!=(t=this.resolve(e).variables.get(e))?t:new yt}catch{return new yt}}}
|
|
1211
|
+
var bt=class{global
|
|
1212
|
+
constructor(e){this.global=null!=e?e:new xt}run(e){return this.evaluate(e,this.global)}evaluateBinaryExpression(e,t){const n=this.evaluate(e.left,t)
|
|
1213
|
+
switch(e.operator.value){case"and":return n.__bool__().value?this.evaluate(e.right,t):n
|
|
1214
|
+
case"or":return n.__bool__().value?n:this.evaluate(e.right,t)}const s=this.evaluate(e.right,t)
|
|
1215
|
+
switch(e.operator.value){case"==":return new _t(n.value==s.value)
|
|
1216
|
+
case"!=":return new _t(n.value!=s.value)}if(n instanceof yt||s instanceof yt)throw new Error("Cannot perform operation on undefined values")
|
|
1217
|
+
if(n instanceof wt||s instanceof wt)throw new Error("Cannot perform operation on null values")
|
|
1218
|
+
if(n instanceof ut&&s instanceof ut)switch(e.operator.value){case"+":return new ut(n.value+s.value)
|
|
1219
|
+
case"-":return new ut(n.value-s.value)
|
|
1220
|
+
case"*":return new ut(n.value*s.value)
|
|
1221
|
+
case"/":return new ut(n.value/s.value)
|
|
1222
|
+
case"%":return new ut(n.value%s.value)
|
|
1223
|
+
case"<":return new _t(n.value<s.value)
|
|
1224
|
+
case">":return new _t(n.value>s.value)
|
|
1225
|
+
case">=":return new _t(n.value>=s.value)
|
|
1226
|
+
case"<=":return new _t(n.value<=s.value)}else if(n instanceof ft&&s instanceof ft){if("+"===e.operator.value)return new ft(n.value.concat(s.value))}else if(s instanceof ft){const t=void 0!==s.value.find((e=>e.value===n.value))
|
|
1227
|
+
switch(e.operator.value){case"in":return new _t(t)
|
|
1228
|
+
case"not in":return new _t(!t)}}if((n instanceof ht||s instanceof ht)&&"+"===e.operator.value)return new ht(n.value.toString()+s.value.toString())
|
|
1229
|
+
if(n instanceof ht&&s instanceof ht)switch(e.operator.value){case"in":return new _t(s.value.includes(n.value))
|
|
1230
|
+
case"not in":return new _t(!s.value.includes(n.value))}if(n instanceof ht&&s instanceof pt)switch(e.operator.value){case"in":return new _t(s.value.has(n.value))
|
|
1231
|
+
case"not in":return new _t(!s.value.has(n.value))}throw new SyntaxError(`Unknown operator "${e.operator.value}" between ${n.type} and ${s.type}`)}evaluateFilterExpression(e,t){const n=this.evaluate(e.operand,t)
|
|
1232
|
+
if("Identifier"===e.filter.type){const t=e.filter
|
|
1233
|
+
if(n instanceof ft)switch(t.value){case"list":return n
|
|
1234
|
+
case"first":return n.value[0]
|
|
1235
|
+
case"last":return n.value[n.value.length-1]
|
|
1236
|
+
case"length":return new ut(n.value.length)
|
|
1237
|
+
case"reverse":return new ft(n.value.reverse())
|
|
1238
|
+
case"sort":return new ft(n.value.sort(((e,t)=>{if(e.type!==t.type)throw new Error(`Cannot compare different types: ${e.type} and ${t.type}`)
|
|
1239
|
+
switch(e.type){case"NumericValue":return e.value-t.value
|
|
1240
|
+
case"StringValue":return e.value.localeCompare(t.value)
|
|
1241
|
+
default:throw new Error(`Cannot compare type: ${e.type}`)}})))
|
|
1242
|
+
default:throw new Error(`Unknown ArrayValue filter: ${t.value}`)}else if(n instanceof ht)switch(t.value){case"length":return new ut(n.value.length)
|
|
1243
|
+
case"upper":return new ht(n.value.toUpperCase())
|
|
1244
|
+
case"lower":return new ht(n.value.toLowerCase())
|
|
1245
|
+
case"title":return new ht(ct(n.value))
|
|
1246
|
+
case"capitalize":return new ht(n.value.charAt(0).toUpperCase()+n.value.slice(1))
|
|
1247
|
+
case"trim":return new ht(n.value.trim())
|
|
1248
|
+
default:throw new Error(`Unknown StringValue filter: ${t.value}`)}else{if(n instanceof ut){if("abs"===t.value)return new ut(Math.abs(n.value))
|
|
1249
|
+
throw new Error(`Unknown NumericValue filter: ${t.value}`)}if(n instanceof pt)switch(t.value){case"items":return new ft(Array.from(n.value.entries()).map((([e,t])=>new ft([new ht(e),t]))))
|
|
1250
|
+
case"length":return new ut(n.value.size)
|
|
1251
|
+
default:throw new Error(`Unknown ObjectValue filter: ${t.value}`)}}throw new Error(`Cannot apply filter "${t.value}" to type: ${n.type}`)}if("CallExpression"===e.filter.type){const s=e.filter
|
|
1252
|
+
if("Identifier"!==s.callee.type)throw new Error(`Unknown filter: ${s.callee.type}`)
|
|
1253
|
+
const r=s.callee.value
|
|
1254
|
+
if(n instanceof ft){if("selectattr"===r){if(n.value.some((e=>!(e instanceof pt))))throw new Error("`selectattr` can only be applied to array of objects")
|
|
1255
|
+
if(s.args.some((e=>"StringLiteral"!==e.type)))throw new Error("arguments of `selectattr` must be strings")
|
|
1256
|
+
const[e,r,o]=s.args.map((e=>this.evaluate(e,t)))
|
|
1257
|
+
let i
|
|
1258
|
+
if(r){const e=t.tests.get(r.value)
|
|
1259
|
+
if(!e)throw new Error(`Unknown test: ${r.value}`)
|
|
1260
|
+
i=e}else i=(...e)=>e[0].__bool__().value
|
|
1261
|
+
const a=n.value.filter((t=>{const n=t.value.get(e.value)
|
|
1262
|
+
return!!n&&i(n,o)}))
|
|
1263
|
+
return new ft(a)}throw new Error(`Unknown ArrayValue filter: ${r}`)}throw new Error(`Cannot apply filter "${r}" to type: ${n.type}`)}throw new Error(`Unknown filter: ${e.filter.type}`)}evaluateTestExpression(e,t){const n=this.evaluate(e.operand,t)
|
|
1264
|
+
const s=t.tests.get(e.test.value)
|
|
1265
|
+
if(!s)throw new Error(`Unknown test: ${e.test.value}`)
|
|
1266
|
+
const r=s(n)
|
|
1267
|
+
return new _t(e.negate?!r:r)}evaluateUnaryExpression(e,t){const n=this.evaluate(e.argument,t)
|
|
1268
|
+
if("not"===e.operator.value)return new _t(!n.value)
|
|
1269
|
+
throw new SyntaxError(`Unknown operator: ${e.operator.value}`)}evalProgram(e,t){return this.evaluateBlock(e.body,t)}evaluateBlock(e,t){let n=""
|
|
1270
|
+
for(const s of e){const e=this.evaluate(s,t)
|
|
1271
|
+
"NullValue"!==e.type&&"UndefinedValue"!==e.type&&(n+=e.value)}return new ht(n)}evaluateIdentifier(e,t){return t.lookupVariable(e.value)}evaluateCallExpression(e,t){const n=[]
|
|
1272
|
+
const s=new Map
|
|
1273
|
+
for(const r of e.args)if("KeywordArgumentExpression"===r.type){const e=r
|
|
1274
|
+
s.set(e.key.value,this.evaluate(e.value,t))}else n.push(this.evaluate(r,t))
|
|
1275
|
+
s.size>0&&n.push(new pt(s))
|
|
1276
|
+
const r=this.evaluate(e.callee,t)
|
|
1277
|
+
if("FunctionValue"!==r.type)throw new Error(`Cannot call something that is not a function: got ${r.type}`)
|
|
1278
|
+
return r.value(n,t)}evaluateSliceExpression(e,t,n){if(!(e instanceof ft||e instanceof ht))throw new Error("Slice object must be an array or string")
|
|
1279
|
+
const s=this.evaluate(t.start,n)
|
|
1280
|
+
const r=this.evaluate(t.stop,n)
|
|
1281
|
+
const o=this.evaluate(t.step,n)
|
|
1282
|
+
if(!(s instanceof ut||s instanceof yt))throw new Error("Slice start must be numeric or undefined")
|
|
1283
|
+
if(!(r instanceof ut||r instanceof yt))throw new Error("Slice stop must be numeric or undefined")
|
|
1284
|
+
if(!(o instanceof ut||o instanceof yt))throw new Error("Slice step must be numeric or undefined")
|
|
1285
|
+
return e instanceof ft?new ft(lt(e.value,s.value,r.value,o.value)):new ht(lt(Array.from(e.value),s.value,r.value,o.value).join(""))}evaluateMemberExpression(e,t){const n=this.evaluate(e.object,t)
|
|
1286
|
+
let s
|
|
1287
|
+
if(e.computed){if("SliceExpression"===e.property.type)return this.evaluateSliceExpression(n,e.property,t)
|
|
1288
|
+
s=this.evaluate(e.property,t)}else s=new ht(e.property.value)
|
|
1289
|
+
let r
|
|
1290
|
+
if(n instanceof pt){var o
|
|
1291
|
+
if(!(s instanceof ht))throw new Error(`Cannot access property with non-string: got ${s.type}`)
|
|
1292
|
+
r=null!=(o=n.value.get(s.value))?o:n.builtins.get(s.value)}else if(n instanceof ft||n instanceof ht)if(s instanceof ut){r=n.value.at(s.value)
|
|
1293
|
+
n instanceof ht&&(r=new ht(n.value.at(s.value)))}else{if(!(s instanceof ht))throw new Error(`Cannot access property with non-string/non-number: got ${s.type}`)
|
|
1294
|
+
r=n.builtins.get(s.value)}else{if(!(s instanceof ht))throw new Error(`Cannot access property with non-string: got ${s.type}`)
|
|
1295
|
+
r=n.builtins.get(s.value)}return r instanceof dt?r:new yt}evaluateSet(e,t){const n=this.evaluate(e.value,t)
|
|
1296
|
+
if("Identifier"===e.assignee.type){const s=e.assignee.value
|
|
1297
|
+
t.setVariable(s,n)}else{if("MemberExpression"!==e.assignee.type)throw new Error(`Invalid LHS inside assignment expression: ${JSON.stringify(e.assignee)}`)
|
|
1298
|
+
{const s=e.assignee
|
|
1299
|
+
const r=this.evaluate(s.object,t)
|
|
1300
|
+
if(!(r instanceof pt))throw new Error("Cannot assign to member of non-object")
|
|
1301
|
+
if("Identifier"!==s.property.type)throw new Error("Cannot assign to member with non-identifier property")
|
|
1302
|
+
r.value.set(s.property.value,n)}}return new wt}evaluateIf(e,t){const n=this.evaluate(e.test,t)
|
|
1303
|
+
return this.evaluateBlock(n.__bool__().value?e.body:e.alternate,t)}evaluateFor(e,t){const n=new xt(t)
|
|
1304
|
+
const s=this.evaluate(e.iterable,n)
|
|
1305
|
+
if(!(s instanceof ft))throw new Error(`Expected iterable type in for loop: got ${s.type}`)
|
|
1306
|
+
let r=""
|
|
1307
|
+
for(let t=0;t<s.value.length;++t){const o=new Map([["index",new ut(t+1)],["index0",new ut(t)],["revindex",new ut(s.value.length-t)],["revindex0",new ut(s.value.length-t-1)],["first",new _t(0===t)],["last",new _t(t===s.value.length-1)],["length",new ut(s.value.length)],["previtem",t>0?s.value[t-1]:new yt],["nextitem",t<s.value.length-1?s.value[t+1]:new yt]])
|
|
1308
|
+
n.setVariable("loop",new pt(o))
|
|
1309
|
+
const i=s.value[t]
|
|
1310
|
+
if("Identifier"===e.loopvar.type)n.setVariable(e.loopvar.value,i)
|
|
1311
|
+
else if("TupleLiteral"===e.loopvar.type){const t=e.loopvar
|
|
1312
|
+
if("ArrayValue"!==i.type)throw new Error(`Cannot unpack non-iterable type: ${i.type}`)
|
|
1313
|
+
const s=i
|
|
1314
|
+
if(t.value.length!==s.value.length)throw new Error(`Too ${t.value.length>s.value.length?"few":"many"} items to unpack`)
|
|
1315
|
+
for(let e=0;e<t.value.length;++e){if("Identifier"!==t.value[e].type)throw new Error(`Cannot unpack non-identifier type: ${t.value[e].type}`)
|
|
1316
|
+
n.setVariable(t.value[e].value,s.value[e])}}r+=this.evaluateBlock(e.body,n).value}return new ht(r)}evaluate(e,t){if(void 0===e)return new yt
|
|
1317
|
+
switch(e.type){case"Program":return this.evalProgram(e,t)
|
|
1318
|
+
case"Set":return this.evaluateSet(e,t)
|
|
1319
|
+
case"If":return this.evaluateIf(e,t)
|
|
1320
|
+
case"For":return this.evaluateFor(e,t)
|
|
1321
|
+
case"NumericLiteral":return new ut(Number(e.value))
|
|
1322
|
+
case"StringLiteral":return new ht(e.value)
|
|
1323
|
+
case"BooleanLiteral":return new _t(e.value)
|
|
1324
|
+
case"ArrayLiteral":return new ft(e.value.map((e=>this.evaluate(e,t))))
|
|
1325
|
+
case"TupleLiteral":return new mt(e.value.map((e=>this.evaluate(e,t))))
|
|
1326
|
+
case"ObjectLiteral":{const n=new Map
|
|
1327
|
+
for(const[s,r]of e.value){const e=this.evaluate(s,t)
|
|
1328
|
+
if(!(e instanceof ht))throw new Error(`Object keys must be strings: got ${e.type}`)
|
|
1329
|
+
n.set(e.value,this.evaluate(r,t))}return new pt(n)}case"Identifier":return this.evaluateIdentifier(e,t)
|
|
1330
|
+
case"CallExpression":return this.evaluateCallExpression(e,t)
|
|
1331
|
+
case"MemberExpression":return this.evaluateMemberExpression(e,t)
|
|
1332
|
+
case"UnaryExpression":return this.evaluateUnaryExpression(e,t)
|
|
1333
|
+
case"BinaryExpression":return this.evaluateBinaryExpression(e,t)
|
|
1334
|
+
case"FilterExpression":return this.evaluateFilterExpression(e,t)
|
|
1335
|
+
case"TestExpression":return this.evaluateTestExpression(e,t)
|
|
1336
|
+
default:throw new SyntaxError(`Unknown node type: ${e.type}`)}}}
|
|
1337
|
+
function vt(e){switch(typeof e){case"number":return new ut(e)
|
|
1338
|
+
case"string":return new ht(e)
|
|
1339
|
+
case"boolean":return new _t(e)
|
|
1340
|
+
case"object":return null===e?new wt:Array.isArray(e)?new ft(e.map(vt)):new pt(new Map(Object.entries(e).map((([e,t])=>[e,vt(t)]))))
|
|
1341
|
+
case"function":return new gt(((t,n)=>{var s
|
|
1342
|
+
return vt(null!=(s=e(...t.map((e=>e.value))))?s:null)}))
|
|
1343
|
+
default:throw new Error(`Cannot convert to runtime value: ${e}`)}}var kt=class{parsed
|
|
1344
|
+
constructor(e){const t=function(e,t={}){const n=[]
|
|
1345
|
+
const s=function(e,t={}){e.endsWith("\n")&&(e=e.slice(0,-1))
|
|
1346
|
+
e=e.replace(/{#.*?#}/gs,"{##}")
|
|
1347
|
+
t.lstrip_blocks&&(e=e.replace(/^[ \t]*({[#%])/gm,"$1"))
|
|
1348
|
+
t.trim_blocks&&(e=e.replace(/([#%]})\n/g,"$1"))
|
|
1349
|
+
return e.replace(/{##}/g,"").replace(/-%}\s*/g,"%}").replace(/\s*{%-/g,"{%").replace(/-}}\s*/g,"}}").replace(/\s*{{-/g,"{{")}(e,t)
|
|
1350
|
+
let r=0
|
|
1351
|
+
const o=e=>{let t=""
|
|
1352
|
+
for(;e(s[r]);)if("\\"!==s[r]){t+=s[r++]
|
|
1353
|
+
if(r>=s.length)throw new SyntaxError("Unexpected end of input")}else{++r
|
|
1354
|
+
if(r>=s.length)throw new SyntaxError("Unexpected end of input")
|
|
1355
|
+
const e=s[r++]
|
|
1356
|
+
const n=Ne.get(e)
|
|
1357
|
+
if(void 0===n)throw new SyntaxError(`Unexpected escaped character: ${e}`)
|
|
1358
|
+
t+=n}return t}
|
|
1359
|
+
e:for(;r<s.length;){var i
|
|
1360
|
+
const e=null==(i=n.at(-1))?void 0:i.type
|
|
1361
|
+
if(void 0===e||e===ze.CloseStatement||e===ze.CloseExpression){let e=""
|
|
1362
|
+
for(;r<s.length&&("{"!==s[r]||"%"!==s[r+1]&&"{"!==s[r+1]);)e+=s[r++]
|
|
1363
|
+
if(e.length>0){n.push(new Ie(e,ze.Text))
|
|
1364
|
+
continue}}o((e=>/\s/.test(e)))
|
|
1365
|
+
const t=s[r]
|
|
1366
|
+
if("-"===t||"+"===t){var a
|
|
1367
|
+
const e=null==(a=n.at(-1))?void 0:a.type
|
|
1368
|
+
if(e===ze.Text||void 0===e)throw new SyntaxError(`Unexpected character: ${t}`)
|
|
1369
|
+
switch(e){case ze.Identifier:case ze.NumericLiteral:case ze.BooleanLiteral:case ze.StringLiteral:case ze.CloseParen:case ze.CloseSquareBracket:break
|
|
1370
|
+
default:{++r
|
|
1371
|
+
const e=o(Be)
|
|
1372
|
+
n.push(new Ie(`${t}${e}`,e.length>0?ze.NumericLiteral:ze.UnaryOperator))
|
|
1373
|
+
continue}}}for(const[e,t]of Oe)if(s.slice(r,r+e.length)===e){n.push(new Ie(e,t))
|
|
1374
|
+
r+=e.length
|
|
1375
|
+
continue e}if("'"!==t&&'"'!==t)if(Be(t)){const e=o(Be)
|
|
1376
|
+
n.push(new Ie(e,ze.NumericLiteral))}else{if(!Le(t))throw new SyntaxError(`Unexpected character: ${t}`)
|
|
1377
|
+
{var l
|
|
1378
|
+
const e=o(Le)
|
|
1379
|
+
const t=Object.hasOwn(Pe,e)?Pe[e]:ze.Identifier
|
|
1380
|
+
if(t===ze.In&&(null==(l=n.at(-1))?void 0:l.type)===ze.Not){n.pop()
|
|
1381
|
+
n.push(new Ie("not in",ze.NotIn))}else n.push(new Ie(e,t))}}else{++r
|
|
1382
|
+
const e=o((e=>e!==t))
|
|
1383
|
+
n.push(new Ie(e,ze.StringLiteral));++r}}return n}(e,{lstrip_blocks:!0,trim_blocks:!0})
|
|
1384
|
+
this.parsed=it(t)}render(e){const t=new xt
|
|
1385
|
+
t.set("false",!1)
|
|
1386
|
+
t.set("true",!0)
|
|
1387
|
+
t.set("raise_exception",(e=>{throw new Error(e)}))
|
|
1388
|
+
t.set("range",at)
|
|
1389
|
+
for(const[n,s]of Object.entries(e))t.set(n,s)
|
|
1390
|
+
return new bt(t).run(this.parsed).value}}
|
|
1391
|
+
async function Mt(e,t){const n=await Promise.all([U(e,"tokenizer.json",!0,t),U(e,"tokenizer_config.json",!0,t)])
|
|
1392
|
+
null!==t.legacy&&(n[1].legacy=t.legacy)
|
|
1393
|
+
return n}function Tt(e,t=!0){if(void 0!==e.Regex){let t=e.Regex.replace(/\\([#&~])/g,"$1")
|
|
1394
|
+
for(const[e,n]of zt)t=t.replaceAll(e,n)
|
|
1395
|
+
return new RegExp(t,"gu")}if(void 0!==e.String){const n=s(e.String)
|
|
1396
|
+
return new RegExp(t?n:`(${n})`,"gu")}console.warn("Unknown pattern type:",e)
|
|
1397
|
+
return null}function Et(e){return new Map(Object.entries(e))}function Ct(e){const t=e.dims
|
|
1398
|
+
switch(t.length){case 1:return e.tolist()
|
|
1399
|
+
case 2:if(1!==t[0])throw new Error("Unable to decode tensor with `batch size !== 1`. Use `tokenizer.batch_decode(...)` for batched inputs.")
|
|
1400
|
+
return e.tolist()[0]
|
|
1401
|
+
default:throw new Error(`Expected tensor to have 1-2 dimensions, got ${t.length}.`)}}function At(e){return e.replace(/ \./g,".").replace(/ \?/g,"?").replace(/ \!/g,"!").replace(/ ,/g,",").replace(/ \' /g,"'").replace(/ n\'t/g,"n't").replace(/ \'m/g,"'m").replace(/ \'s/g,"'s").replace(/ \'ve/g,"'ve").replace(/ \'re/g,"'re")}function St(e){return e.replace(/[\u0300-\u036f]/g,"")}const Ft="\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E"
|
|
1402
|
+
const zt=new Map([["(?i:'s|'t|'re|'ve|'m|'ll|'d)","(?:'([sS]|[tT]|[rR][eE]|[vV][eE]|[mM]|[lL][lL]|[dD]))"]])
|
|
1403
|
+
class Pt{constructor(e){var t,n,s,r,o
|
|
1404
|
+
this.content=e.content
|
|
1405
|
+
this.id=e.id
|
|
1406
|
+
this.single_word=null!=(t=e.single_word)&&t
|
|
1407
|
+
this.lstrip=null!=(n=e.lstrip)&&n
|
|
1408
|
+
this.rstrip=null!=(s=e.rstrip)&&s
|
|
1409
|
+
this.special=null!=(r=e.special)&&r
|
|
1410
|
+
this.normalized=null!=(o=e.normalized)?o:null}}class It extends r{constructor(e){var t
|
|
1411
|
+
super()
|
|
1412
|
+
this.config=e
|
|
1413
|
+
this.vocab=[]
|
|
1414
|
+
this.tokens_to_ids=new Map
|
|
1415
|
+
this.unk_token_id=void 0
|
|
1416
|
+
this.unk_token=void 0
|
|
1417
|
+
this.end_of_word_suffix=void 0
|
|
1418
|
+
this.fuse_unk=null!=(t=this.config.fuse_unk)&&t}static fromConfig(e,...t){switch(e.type){case"WordPiece":return new Lt(e)
|
|
1419
|
+
case"Unigram":return new Bt(e,...t)
|
|
1420
|
+
case"BPE":return new jt(e)
|
|
1421
|
+
default:if(e.vocab)return new Dt(e,...t)
|
|
1422
|
+
throw new Error(`Unknown TokenizerModel type: ${e.type}`)}}_call(e){let t=this.encode(e)
|
|
1423
|
+
this.fuse_unk&&(t=function(e,t,n){const s=[]
|
|
1424
|
+
let r=0
|
|
1425
|
+
for(;r<e.length;){var o
|
|
1426
|
+
s.push(e[r])
|
|
1427
|
+
if((null!=(o=n.get(e[r]))?o:t)===t)for(;r<e.length&&(null!=(i=n.get(e[r]))?i:t)===t;){var i;++r}else++r}return s}(t,this.unk_token_id,this.tokens_to_ids))
|
|
1428
|
+
return t}encode(e){throw Error("encode should be implemented in subclass.")}convert_tokens_to_ids(e){return e.map((e=>{var t
|
|
1429
|
+
return null!=(t=this.tokens_to_ids.get(e))?t:this.unk_token_id}))}convert_ids_to_tokens(e){return e.map((e=>{var t
|
|
1430
|
+
return null!=(t=this.vocab[e])?t:this.unk_token}))}}class Lt extends It{constructor(e){var t
|
|
1431
|
+
super(e)
|
|
1432
|
+
this.tokens_to_ids=Et(e.vocab)
|
|
1433
|
+
this.unk_token_id=this.tokens_to_ids.get(e.unk_token)
|
|
1434
|
+
this.unk_token=e.unk_token
|
|
1435
|
+
this.max_input_chars_per_word=null!=(t=e.max_input_chars_per_word)?t:100
|
|
1436
|
+
this.vocab=new Array(this.tokens_to_ids.size)
|
|
1437
|
+
for(const[e,t]of this.tokens_to_ids)this.vocab[t]=e}encode(e){const t=[]
|
|
1438
|
+
for(const n of e){const e=[...n]
|
|
1439
|
+
if(e.length>this.max_input_chars_per_word){t.push(this.unk_token)
|
|
1440
|
+
continue}let s=!1
|
|
1441
|
+
let r=0
|
|
1442
|
+
const o=[]
|
|
1443
|
+
for(;r<e.length;){let t=e.length
|
|
1444
|
+
let n=null
|
|
1445
|
+
for(;r<t;){let s=e.slice(r,t).join("")
|
|
1446
|
+
r>0&&(s=this.config.continuing_subword_prefix+s)
|
|
1447
|
+
if(this.tokens_to_ids.has(s)){n=s
|
|
1448
|
+
break}--t}if(null===n){s=!0
|
|
1449
|
+
break}o.push(n)
|
|
1450
|
+
r=t}s?t.push(this.unk_token):t.push(...o)}return t}}class Bt extends It{constructor(e,t){super(e)
|
|
1451
|
+
const n=e.vocab.length
|
|
1452
|
+
this.vocab=new Array(n)
|
|
1453
|
+
this.scores=new Array(n)
|
|
1454
|
+
for(let t=0;t<n;++t){const n=e.vocab[t]
|
|
1455
|
+
this.vocab[t]=n[0]
|
|
1456
|
+
this.scores[t]=n[1]}this.unk_token_id=e.unk_id
|
|
1457
|
+
this.unk_token=this.vocab[e.unk_id]
|
|
1458
|
+
this.tokens_to_ids=new Map(this.vocab.map(((e,t)=>[e,t])))
|
|
1459
|
+
this.bosToken=" "
|
|
1460
|
+
this.bosTokenId=this.tokens_to_ids.get(this.bosToken)
|
|
1461
|
+
this.eosToken=t.eos_token
|
|
1462
|
+
this.eosTokenId=this.tokens_to_ids.get(this.eosToken)
|
|
1463
|
+
this.unkToken=this.vocab[this.unk_token_id]
|
|
1464
|
+
this.minScore=Z(this.scores)[0]
|
|
1465
|
+
this.unkScore=this.minScore-10
|
|
1466
|
+
this.scores[this.unk_token_id]=this.unkScore
|
|
1467
|
+
this.trie=new Ce
|
|
1468
|
+
this.trie.extend(this.vocab)
|
|
1469
|
+
this.fuse_unk=!0}populateNodes(e){const t=e.sentence
|
|
1470
|
+
const n=t.length
|
|
1471
|
+
let s=0
|
|
1472
|
+
for(;s<n;){const n=1
|
|
1473
|
+
let r=!1
|
|
1474
|
+
const o=[]
|
|
1475
|
+
for(let i of this.trie.commonPrefixSearch(t.slice(s))){o.push(i)
|
|
1476
|
+
const t=this.tokens_to_ids.get(i)
|
|
1477
|
+
const a=this.scores[t]
|
|
1478
|
+
const l=i.length
|
|
1479
|
+
e.insert(s,l,a,t)
|
|
1480
|
+
r||l!==n||(r=!0)}r||e.insert(s,n,this.unkScore,this.unk_token_id)
|
|
1481
|
+
s+=n}}tokenize(e){const t=new Se(e,this.bosTokenId,this.eosTokenId)
|
|
1482
|
+
this.populateNodes(t)
|
|
1483
|
+
return t.tokens()}encode(e){const t=[]
|
|
1484
|
+
for(const n of e){const e=this.tokenize(n)
|
|
1485
|
+
t.push(...e)}return t}}const Ot=(()=>{const e=[...Array.from({length:"~".charCodeAt(0)-"!".charCodeAt(0)+1},((e,t)=>t+"!".charCodeAt(0))),...Array.from({length:"¬".charCodeAt(0)-"¡".charCodeAt(0)+1},((e,t)=>t+"¡".charCodeAt(0))),...Array.from({length:"ÿ".charCodeAt(0)-"®".charCodeAt(0)+1},((e,t)=>t+"®".charCodeAt(0)))]
|
|
1486
|
+
const t=e.slice()
|
|
1487
|
+
let n=0
|
|
1488
|
+
for(let s=0;s<256;++s)if(!e.includes(s)){e.push(s)
|
|
1489
|
+
t.push(256+n)
|
|
1490
|
+
n+=1}const s=t.map((e=>String.fromCharCode(e)))
|
|
1491
|
+
return Object.fromEntries(e.map(((e,t)=>[e,s[t]])))})()
|
|
1492
|
+
const Nt=($t=Ot,Object.fromEntries(Object.entries($t).map((([e,t])=>[t,e]))))
|
|
1493
|
+
var $t
|
|
1494
|
+
class jt extends It{constructor(e){var t,n,s
|
|
1495
|
+
super(e)
|
|
1496
|
+
this.BPE_SPLIT_TOKEN=" "
|
|
1497
|
+
this.tokens_to_ids=Et(e.vocab)
|
|
1498
|
+
this.unk_token_id=this.tokens_to_ids.get(e.unk_token)
|
|
1499
|
+
this.unk_token=e.unk_token
|
|
1500
|
+
this.vocab=new Array(this.tokens_to_ids.size)
|
|
1501
|
+
for(const[e,t]of this.tokens_to_ids)this.vocab[t]=e
|
|
1502
|
+
this.bpe_ranks=new Map(e.merges.map(((e,t)=>[e,t])))
|
|
1503
|
+
this.merges=e.merges.map((e=>e.split(this.BPE_SPLIT_TOKEN)))
|
|
1504
|
+
this.end_of_word_suffix=e.end_of_word_suffix
|
|
1505
|
+
this.continuing_subword_suffix=null!=(t=e.continuing_subword_suffix)?t:null
|
|
1506
|
+
this.byte_fallback=null!=(n=this.config.byte_fallback)&&n
|
|
1507
|
+
this.byte_fallback&&(this.text_encoder=new TextEncoder)
|
|
1508
|
+
this.ignore_merges=null!=(s=this.config.ignore_merges)&&s
|
|
1509
|
+
this.cache=new Map}bpe(e){if(0===e.length)return[]
|
|
1510
|
+
const t=this.cache.get(e)
|
|
1511
|
+
if(void 0!==t)return t
|
|
1512
|
+
const n=Array.from(e)
|
|
1513
|
+
this.end_of_word_suffix&&(n[n.length-1]+=this.end_of_word_suffix)
|
|
1514
|
+
let s=[]
|
|
1515
|
+
if(n.length>1){const e=new Ee(((e,t)=>e.score<t.score))
|
|
1516
|
+
let t={token:n[0],bias:0,prev:null,next:null}
|
|
1517
|
+
let r=t
|
|
1518
|
+
for(let t=1;t<n.length;++t){const s={bias:t/n.length,token:n[t],prev:r,next:null}
|
|
1519
|
+
r.next=s
|
|
1520
|
+
this._add_node(e,r)
|
|
1521
|
+
r=s}for(;!e.isEmpty();){const n=e.pop()
|
|
1522
|
+
if(n.deleted||!n.next||n.next.deleted)continue
|
|
1523
|
+
n.deleted=!0
|
|
1524
|
+
n.next.deleted=!0
|
|
1525
|
+
if(n.prev){const e={...n.prev}
|
|
1526
|
+
n.prev.deleted=!0
|
|
1527
|
+
n.prev=e
|
|
1528
|
+
e.prev?e.prev.next=e:t=e}const s={token:n.token+n.next.token,bias:n.bias,prev:n.prev,next:n.next.next}
|
|
1529
|
+
if(s.prev){s.prev.next=s
|
|
1530
|
+
this._add_node(e,s.prev)}else t=s
|
|
1531
|
+
if(s.next){s.next.prev=s
|
|
1532
|
+
this._add_node(e,s)}}for(let e=t;null!==e;e=e.next)s.push(e.token)}else s=n
|
|
1533
|
+
if(this.continuing_subword_suffix)for(let e=0;e<s.length-1;++e)s[e]+=this.continuing_subword_suffix
|
|
1534
|
+
this.cache.set(e,s)
|
|
1535
|
+
return s}_add_node(e,t){const n=this.bpe_ranks.get(t.token+this.BPE_SPLIT_TOKEN+t.next.token)
|
|
1536
|
+
if(void 0!==n){t.score=n+t.bias
|
|
1537
|
+
e.push(t)}}encode(e){const t=[]
|
|
1538
|
+
for(const n of e){if(this.ignore_merges&&this.tokens_to_ids.has(n)){t.push(n)
|
|
1539
|
+
continue}const e=this.bpe(n)
|
|
1540
|
+
for(const n of e)this.tokens_to_ids.has(n)?t.push(n):this.byte_fallback?t.push(...Array.from(this.text_encoder.encode(n)).map((e=>`<0x${e.toString(16).toUpperCase().padStart(2,"0")}>`))):t.push(this.unk_token)}return t}}class Dt extends It{constructor(e,t){super(e)
|
|
1541
|
+
this.tokens_to_ids=Et(t.target_lang?e.vocab[t.target_lang]:e.vocab)
|
|
1542
|
+
this.bos_token=t.bos_token
|
|
1543
|
+
this.bos_token_id=this.tokens_to_ids.get(this.bos_token)
|
|
1544
|
+
this.eos_token=t.eos_token
|
|
1545
|
+
this.eos_token_id=this.tokens_to_ids.get(this.eos_token)
|
|
1546
|
+
this.pad_token=t.pad_token
|
|
1547
|
+
this.pad_token_id=this.tokens_to_ids.get(this.pad_token)
|
|
1548
|
+
this.unk_token=t.unk_token
|
|
1549
|
+
this.unk_token_id=this.tokens_to_ids.get(this.unk_token)
|
|
1550
|
+
this.vocab=new Array(this.tokens_to_ids.size)
|
|
1551
|
+
for(const[e,t]of this.tokens_to_ids)this.vocab[t]=e}encode(e){return e}}class Rt extends r{constructor(e){super()
|
|
1552
|
+
this.config=e}static fromConfig(e){if(null===e)return null
|
|
1553
|
+
switch(e.type){case"BertNormalizer":return new Yt(e)
|
|
1554
|
+
case"Precompiled":return new kn(e)
|
|
1555
|
+
case"Sequence":return new Kt(e)
|
|
1556
|
+
case"Replace":return new qt(e)
|
|
1557
|
+
case"NFC":return new Ut(e)
|
|
1558
|
+
case"NFKC":return new Vt(e)
|
|
1559
|
+
case"NFKD":return new Gt(e)
|
|
1560
|
+
case"Strip":return new Wt(e)
|
|
1561
|
+
case"StripAccents":return new Xt(e)
|
|
1562
|
+
case"Lowercase":return new Ht(e)
|
|
1563
|
+
case"Prepend":return new Qt(e)
|
|
1564
|
+
default:throw new Error(`Unknown Normalizer type: ${e.type}`)}}normalize(e){throw Error("normalize should be implemented in subclass.")}_call(e){return this.normalize(e)}}class qt extends Rt{normalize(e){const t=Tt(this.config.pattern)
|
|
1565
|
+
return null===t?e:e.replaceAll(t,this.config.content)}}class Ut extends Rt{normalize(e){return e.normalize("NFC")}}class Vt extends Rt{normalize(e){return e.normalize("NFKC")}}class Gt extends Rt{normalize(e){return e.normalize("NFKD")}}class Wt extends Rt{normalize(e){if(this.config.strip_left&&this.config.strip_right)e=e.trim()
|
|
1566
|
+
else{this.config.strip_left&&(e=e.trimStart())
|
|
1567
|
+
this.config.strip_right&&(e=e.trimEnd())}return e}}class Xt extends Rt{normalize(e){return St(e)}}class Ht extends Rt{normalize(e){return e.toLowerCase()}}class Qt extends Rt{normalize(e){return this.config.prepend+e}}class Kt extends Rt{constructor(e){super(e)
|
|
1568
|
+
this.normalizers=e.normalizers.map((e=>Rt.fromConfig(e)))}normalize(e){return this.normalizers.reduce(((e,t)=>t.normalize(e)),e)}}class Yt extends Rt{_tokenize_chinese_chars(e){const t=[]
|
|
1569
|
+
for(let n=0;n<e.length;++n){const s=e[n]
|
|
1570
|
+
const r=s.charCodeAt(0)
|
|
1571
|
+
if(this._is_chinese_char(r)){t.push(" ")
|
|
1572
|
+
t.push(s)
|
|
1573
|
+
t.push(" ")}else t.push(s)}return t.join("")}_is_chinese_char(e){return e>=19968&&e<=40959||e>=13312&&e<=19903||e>=131072&&e<=173791||e>=173824&&e<=177983||e>=177984&&e<=178207||e>=178208&&e<=183983||e>=63744&&e<=64255||e>=194560&&e<=195103}stripAccents(e){return e.normalize("NFD").replace(/[\u0300-\u036f]/g,"")}_is_control(e){switch(e){case"\t":case"\n":case"\r":return!1
|
|
1574
|
+
default:return/^\p{Cc}|\p{Cf}|\p{Co}|\p{Cs}$/u.test(e)}}_clean_text(e){const t=[]
|
|
1575
|
+
for(const n of e){const e=n.charCodeAt(0)
|
|
1576
|
+
0===e||65533===e||this._is_control(n)||(/^\s$/.test(n)?t.push(" "):t.push(n))}return t.join("")}normalize(e){this.config.clean_text&&(e=this._clean_text(e))
|
|
1577
|
+
this.config.handle_chinese_chars&&(e=this._tokenize_chinese_chars(e))
|
|
1578
|
+
if(this.config.lowercase){e=e.toLowerCase()
|
|
1579
|
+
!1!==this.config.strip_accents&&(e=this.stripAccents(e))}else this.config.strip_accents&&(e=this.stripAccents(e))
|
|
1580
|
+
return e}}class Jt extends r{static fromConfig(e){if(null===e)return null
|
|
1581
|
+
switch(e.type){case"BertPreTokenizer":return new Zt(e)
|
|
1582
|
+
case"Sequence":return new Mn(e)
|
|
1583
|
+
case"Whitespace":return new Tn(e)
|
|
1584
|
+
case"WhitespaceSplit":return new En(e)
|
|
1585
|
+
case"Metaspace":return new bn(e)
|
|
1586
|
+
case"ByteLevel":return new en(e)
|
|
1587
|
+
case"Split":return new tn(e)
|
|
1588
|
+
case"Punctuation":return new nn(e)
|
|
1589
|
+
case"Digits":return new sn(e)
|
|
1590
|
+
case"Replace":return new Cn(e)
|
|
1591
|
+
default:throw new Error(`Unknown PreTokenizer type: ${e.type}`)}}pre_tokenize_text(e,t){throw Error("pre_tokenize_text should be implemented in subclass.")}pre_tokenize(e,t){return(Array.isArray(e)?e.map((e=>this.pre_tokenize_text(e,t))):this.pre_tokenize_text(e,t)).flat()}_call(e,t){return this.pre_tokenize(e,t)}}class Zt extends Jt{constructor(e){super()
|
|
1592
|
+
this.pattern=new RegExp(`[^\\s${Ft}]+|[${Ft}]`,"gu")}pre_tokenize_text(e,t){return e.trim().match(this.pattern)||[]}}class en extends Jt{constructor(e){var t
|
|
1593
|
+
super()
|
|
1594
|
+
this.config=e
|
|
1595
|
+
this.add_prefix_space=this.config.add_prefix_space
|
|
1596
|
+
this.trim_offsets=this.config.trim_offsets
|
|
1597
|
+
this.use_regex=null==(t=this.config.use_regex)||t
|
|
1598
|
+
this.pattern=/'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu
|
|
1599
|
+
this.byte_encoder=Ot
|
|
1600
|
+
this.text_encoder=new TextEncoder}pre_tokenize_text(e,t){this.add_prefix_space&&!e.startsWith(" ")&&(e=" "+e)
|
|
1601
|
+
return(this.use_regex?e.match(this.pattern)||[]:[e]).map((e=>Array.from(this.text_encoder.encode(e),(e=>this.byte_encoder[e])).join("")))}}class tn extends Jt{constructor(e){super()
|
|
1602
|
+
this.config=e
|
|
1603
|
+
this.pattern=Tt(this.config.pattern,this.config.invert)}pre_tokenize_text(e,t){return null===this.pattern?[]:this.config.invert?e.match(this.pattern)||[]:function(e,t){const n=[]
|
|
1604
|
+
let s=0
|
|
1605
|
+
for(const r of e.matchAll(t)){const t=r[0]
|
|
1606
|
+
s<r.index&&n.push(e.slice(s,r.index))
|
|
1607
|
+
t.length>0&&n.push(t)
|
|
1608
|
+
s=r.index+t.length}s<e.length&&n.push(e.slice(s))
|
|
1609
|
+
return n}(e,this.pattern)}}class nn extends Jt{constructor(e){super()
|
|
1610
|
+
this.config=e
|
|
1611
|
+
this.pattern=new RegExp(`[^${Ft}]+|[${Ft}]+`,"gu")}pre_tokenize_text(e,t){return e.match(this.pattern)||[]}}class sn extends Jt{constructor(e){super()
|
|
1612
|
+
this.config=e
|
|
1613
|
+
const t="[^\\d]+|\\d"+(this.config.individual_digits?"":"+")
|
|
1614
|
+
this.pattern=new RegExp(t,"gu")}pre_tokenize_text(e,t){return e.match(this.pattern)||[]}}class rn extends r{constructor(e){super()
|
|
1615
|
+
this.config=e}static fromConfig(e){if(null===e)return null
|
|
1616
|
+
switch(e.type){case"TemplateProcessing":return new ln(e)
|
|
1617
|
+
case"ByteLevel":return new cn(e)
|
|
1618
|
+
case"RobertaProcessing":return new an(e)
|
|
1619
|
+
case"BertProcessing":return new on(e)
|
|
1620
|
+
default:throw new Error(`Unknown PostProcessor type: ${e.type}`)}}post_process(e,...t){throw Error("post_process should be implemented in subclass.")}_call(e,...t){return this.post_process(e,...t)}}class on extends rn{constructor(e){super(e)
|
|
1621
|
+
this.cls=e.cls[0]
|
|
1622
|
+
this.sep=e.sep[0]}post_process(e,t=null,{add_special_tokens:n=!0}={}){n&&(e=c([this.cls],e,[this.sep]))
|
|
1623
|
+
let s=new Array(e.length).fill(0)
|
|
1624
|
+
if(null!==t){const r=n&&this instanceof an?[this.sep]:[]
|
|
1625
|
+
const o=n?[this.sep]:[]
|
|
1626
|
+
e=c(e,r,t,o)
|
|
1627
|
+
s=c(s,new Array(t.length+r.length+o.length).fill(1))}return{tokens:e,token_type_ids:s}}}class an extends on{}class ln extends rn{constructor(e){super(e)
|
|
1628
|
+
this.single=e.single
|
|
1629
|
+
this.pair=e.pair}post_process(e,t=null,{add_special_tokens:n=!0}={}){const s=null===t?this.single:this.pair
|
|
1630
|
+
let r=[]
|
|
1631
|
+
let o=[]
|
|
1632
|
+
for(const i of s)if("SpecialToken"in i){if(n){r.push(i.SpecialToken.id)
|
|
1633
|
+
o.push(i.SpecialToken.type_id)}}else if("Sequence"in i)if("A"===i.Sequence.id){r=c(r,e)
|
|
1634
|
+
o=c(o,new Array(e.length).fill(i.Sequence.type_id))}else if("B"===i.Sequence.id){r=c(r,t)
|
|
1635
|
+
o=c(o,new Array(t.length).fill(i.Sequence.type_id))}return{tokens:r,token_type_ids:o}}}class cn extends rn{post_process(e,t=null){t&&(e=c(e,t))
|
|
1636
|
+
return{tokens:e}}}class dn extends r{constructor(e){super()
|
|
1637
|
+
this.config=e
|
|
1638
|
+
this.added_tokens=[]
|
|
1639
|
+
this.end_of_word_suffix=null
|
|
1640
|
+
this.trim_offsets=e.trim_offsets}static fromConfig(e){if(null===e)return null
|
|
1641
|
+
switch(e.type){case"WordPiece":return new fn(e)
|
|
1642
|
+
case"Metaspace":return new vn(e)
|
|
1643
|
+
case"ByteLevel":return new mn(e)
|
|
1644
|
+
case"Replace":return new un(e)
|
|
1645
|
+
case"ByteFallback":return new hn(e)
|
|
1646
|
+
case"Fuse":return new _n(e)
|
|
1647
|
+
case"Strip":return new pn(e)
|
|
1648
|
+
case"Sequence":return new wn(e)
|
|
1649
|
+
case"CTC":return new gn(e)
|
|
1650
|
+
case"BPEDecoder":return new yn(e)
|
|
1651
|
+
default:throw new Error(`Unknown Decoder type: ${e.type}`)}}_call(e){return this.decode(e)}decode(e){return this.decode_chain(e).join("")}decode_chain(e){throw Error("`decode_chain` should be implemented in subclass.")}}class un extends dn{decode_chain(e){const t=Tt(this.config.pattern)
|
|
1652
|
+
return null===t?e:e.map((e=>e.replaceAll(t,this.config.content)))}}class hn extends dn{constructor(e){super(e)
|
|
1653
|
+
this.text_decoder=new TextDecoder}decode_chain(e){const t=[]
|
|
1654
|
+
let n=[]
|
|
1655
|
+
for(const s of e){let e=null
|
|
1656
|
+
if(6===s.length&&s.startsWith("<0x")&&s.endsWith(">")){const t=parseInt(s.slice(3,5),16)
|
|
1657
|
+
isNaN(t)||(e=t)}if(null!==e)n.push(e)
|
|
1658
|
+
else{if(n.length>0){const e=this.text_decoder.decode(Uint8Array.from(n))
|
|
1659
|
+
t.push(e)
|
|
1660
|
+
n=[]}t.push(s)}}if(n.length>0){const e=this.text_decoder.decode(Uint8Array.from(n))
|
|
1661
|
+
t.push(e)
|
|
1662
|
+
n=[]}return t}}class _n extends dn{decode_chain(e){return[e.join("")]}}class pn extends dn{constructor(e){super(e)
|
|
1663
|
+
this.content=this.config.content
|
|
1664
|
+
this.start=this.config.start
|
|
1665
|
+
this.stop=this.config.stop}decode_chain(e){return e.map((e=>{let t=0
|
|
1666
|
+
for(let n=0;n<this.start&&e[n]===this.content;++n)t=n+1
|
|
1667
|
+
let n=e.length
|
|
1668
|
+
for(let t=0;t<this.stop;++t){const s=e.length-t-1
|
|
1669
|
+
if(e[s]!==this.content)break
|
|
1670
|
+
n=s}return e.slice(t,n)}))}}class fn extends dn{constructor(e){super(e)
|
|
1671
|
+
this.cleanup=e.cleanup}decode_chain(e){return e.map(((e,t)=>{0!==t&&(e=e.startsWith(this.config.prefix)?e.replace(this.config.prefix,""):" "+e)
|
|
1672
|
+
this.cleanup&&(e=At(e))
|
|
1673
|
+
return e}))}}class mn extends dn{constructor(e){super(e)
|
|
1674
|
+
this.byte_decoder=Nt
|
|
1675
|
+
this.text_decoder=new TextDecoder("utf-8",{fatal:!1,ignoreBOM:!0})
|
|
1676
|
+
this.end_of_word_suffix=null}convert_tokens_to_string(e){const t=e.join("")
|
|
1677
|
+
const n=new Uint8Array([...t].map((e=>this.byte_decoder[e])))
|
|
1678
|
+
return this.text_decoder.decode(n)}decode_chain(e){const t=[]
|
|
1679
|
+
let n=[]
|
|
1680
|
+
for(const s of e)if(void 0!==this.added_tokens.find((e=>e.content===s))){if(n.length>0){t.push(this.convert_tokens_to_string(n))
|
|
1681
|
+
n=[]}t.push(s)}else n.push(s)
|
|
1682
|
+
n.length>0&&t.push(this.convert_tokens_to_string(n))
|
|
1683
|
+
return t}}class gn extends dn{constructor(e){super(e)
|
|
1684
|
+
this.pad_token=this.config.pad_token
|
|
1685
|
+
this.word_delimiter_token=this.config.word_delimiter_token
|
|
1686
|
+
this.cleanup=this.config.cleanup}convert_tokens_to_string(e){if(0===e.length)return""
|
|
1687
|
+
const t=[e[0]]
|
|
1688
|
+
for(let n=1;n<e.length;++n)e[n]!==t.at(-1)&&t.push(e[n])
|
|
1689
|
+
let n=t.filter((e=>e!==this.pad_token)).join("")
|
|
1690
|
+
this.cleanup&&(n=At(n).replaceAll(this.word_delimiter_token," ").trim())
|
|
1691
|
+
return n}decode_chain(e){return[this.convert_tokens_to_string(e)]}}class wn extends dn{constructor(e){super(e)
|
|
1692
|
+
this.decoders=e.decoders.map((e=>dn.fromConfig(e)))}decode_chain(e){return this.decoders.reduce(((e,t)=>t.decode_chain(e)),e)}}class yn extends dn{constructor(e){super(e)
|
|
1693
|
+
this.suffix=this.config.suffix}decode_chain(e){return e.map(((t,n)=>t.replaceAll(this.suffix,n===e.length-1?"":" ")))}}class xn extends dn{decode_chain(e){let t=""
|
|
1694
|
+
for(let n=1;n<e.length;n+=2)t+=e[n]
|
|
1695
|
+
return[t]}}class bn extends Jt{constructor(e){var t
|
|
1696
|
+
super()
|
|
1697
|
+
this.addPrefixSpace=e.add_prefix_space
|
|
1698
|
+
this.replacement=e.replacement
|
|
1699
|
+
this.strRep=e.str_rep||this.replacement
|
|
1700
|
+
this.prepend_scheme=null!=(t=e.prepend_scheme)?t:"always"}pre_tokenize_text(e,{section_index:t}={}){let n=e.replaceAll(" ",this.strRep)
|
|
1701
|
+
this.addPrefixSpace&&!n.startsWith(this.replacement)&&("always"===this.prepend_scheme||"first"===this.prepend_scheme&&0===t)&&(n=this.strRep+n)
|
|
1702
|
+
return[n]}}class vn extends dn{constructor(e){super(e)
|
|
1703
|
+
this.addPrefixSpace=e.add_prefix_space
|
|
1704
|
+
this.replacement=e.replacement}decode_chain(e){const t=[]
|
|
1705
|
+
for(let n=0;n<e.length;++n){let s=e[n].replaceAll(this.replacement," ")
|
|
1706
|
+
this.addPrefixSpace&&0==n&&s.startsWith(" ")&&(s=s.substring(1))
|
|
1707
|
+
t.push(s)}return t}}class kn extends Rt{constructor(e){super(e)
|
|
1708
|
+
this.charsmap=e.precompiled_charsmap}normalize(e){if((e=(e=e.replace(/[\u0001-\u0008\u000B\u000E-\u001F\u007F\u008F\u009F]/gm,"")).replace(/[\u0009\u000A\u000C\u000D\u1680\u200B\u200C\u200E\u200F\u2028\u2029\u2581\uFEFF\uFFFD]/gm," ")).includes("~")){const t=e.split("~")
|
|
1709
|
+
e=t.map((e=>e.normalize("NFKC"))).join("~")}else e=e.normalize("NFKC")
|
|
1710
|
+
return e}}class Mn extends Jt{constructor(e){super()
|
|
1711
|
+
this.tokenizers=e.pretokenizers.map((e=>Jt.fromConfig(e)))}pre_tokenize_text(e,t){return this.tokenizers.reduce(((e,n)=>n.pre_tokenize(e,t)),[e])}}class Tn extends Jt{constructor(e){super()}pre_tokenize_text(e,t){return e.match(/\w+|[^\w\s]+/g)||[]}}class En extends Jt{constructor(e){super()}pre_tokenize_text(e,t){return function(e){return e.match(/\S+/g)||[]}(e)}}class Cn extends Jt{constructor(e){super()
|
|
1712
|
+
this.config=e
|
|
1713
|
+
this.pattern=Tt(this.config.pattern)
|
|
1714
|
+
this.content=this.config.content}pre_tokenize_text(e,t){return null===this.pattern?[e]:[e.replaceAll(this.pattern,this.config.content)]}}const An=["bos_token","eos_token","unk_token","sep_token","pad_token","cls_token","mask_token"]
|
|
1715
|
+
function Sn(e,t,n,s){for(const r of Object.keys(e)){const o=t-e[r].length
|
|
1716
|
+
const i=n(r)
|
|
1717
|
+
const a=new Array(o).fill(i)
|
|
1718
|
+
e[r]="right"===s?c(e[r],a):c(a,e[r])}}function Fn(e,t){for(const n of Object.keys(e))e[n].length=t}class zn extends r{return_token_type_ids=!1
|
|
1719
|
+
_default_chat_template="{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"
|
|
1720
|
+
constructor(e,t){var n,r,o,i
|
|
1721
|
+
super()
|
|
1722
|
+
this._tokenizer_config=t
|
|
1723
|
+
this.normalizer=Rt.fromConfig(e.normalizer)
|
|
1724
|
+
this.pre_tokenizer=Jt.fromConfig(e.pre_tokenizer)
|
|
1725
|
+
this.model=It.fromConfig(e.model,t)
|
|
1726
|
+
this.post_processor=rn.fromConfig(e.post_processor)
|
|
1727
|
+
this.decoder=dn.fromConfig(e.decoder)
|
|
1728
|
+
this.special_tokens=[]
|
|
1729
|
+
this.all_special_ids=[]
|
|
1730
|
+
this.added_tokens=[]
|
|
1731
|
+
for(const t of e.added_tokens){const e=new Pt(t)
|
|
1732
|
+
this.added_tokens.push(e)
|
|
1733
|
+
this.model.tokens_to_ids.set(e.content,e.id)
|
|
1734
|
+
this.model.vocab[e.id]=e.content
|
|
1735
|
+
if(e.special){this.special_tokens.push(e.content)
|
|
1736
|
+
this.all_special_ids.push(e.id)}}this.additional_special_tokens=null!=(n=t.additional_special_tokens)?n:[]
|
|
1737
|
+
this.special_tokens.push(...this.additional_special_tokens)
|
|
1738
|
+
this.special_tokens=[...new Set(this.special_tokens)]
|
|
1739
|
+
if(this.decoder){this.decoder.added_tokens=this.added_tokens
|
|
1740
|
+
this.decoder.end_of_word_suffix=this.model.end_of_word_suffix}this.added_tokens_regex=this.added_tokens.length>0?new RegExp(this.added_tokens.map((e=>`${e.lstrip?"\\s*":""}(${s(e.content)})${e.rstrip?"\\s*":""}`)).join("|")):null
|
|
1741
|
+
this.mask_token=this.getToken("mask_token")
|
|
1742
|
+
this.mask_token_id=this.model.tokens_to_ids.get(this.mask_token)
|
|
1743
|
+
this.pad_token=this.getToken("pad_token","eos_token")
|
|
1744
|
+
this.pad_token_id=this.model.tokens_to_ids.get(this.pad_token)
|
|
1745
|
+
this.sep_token=this.getToken("sep_token")
|
|
1746
|
+
this.sep_token_id=this.model.tokens_to_ids.get(this.sep_token)
|
|
1747
|
+
this.unk_token=this.getToken("unk_token")
|
|
1748
|
+
this.unk_token_id=this.model.tokens_to_ids.get(this.unk_token)
|
|
1749
|
+
this.model_max_length=t.model_max_length
|
|
1750
|
+
this.remove_space=t.remove_space
|
|
1751
|
+
this.clean_up_tokenization_spaces=null==(r=t.clean_up_tokenization_spaces)||r
|
|
1752
|
+
this.do_lowercase_and_remove_accent=null!=(o=t.do_lowercase_and_remove_accent)&&o
|
|
1753
|
+
this.padding_side="right"
|
|
1754
|
+
this.legacy=!1
|
|
1755
|
+
this.chat_template=null!=(i=t.chat_template)?i:null
|
|
1756
|
+
if(Array.isArray(this.chat_template)){const e=Object.create(null)
|
|
1757
|
+
for(const{name:t,template:n}of this.chat_template){if("string"!=typeof t||"string"!=typeof n)throw new Error('Chat template must be a list of objects with "name" and "template" properties')
|
|
1758
|
+
e[t]=n}this.chat_template=e}this._compiled_template_cache=new Map}getToken(...e){for(const t of e){const e=this._tokenizer_config[t]
|
|
1759
|
+
if(e){if("object"==typeof e){if("AddedToken"===e.__type)return e.content
|
|
1760
|
+
throw Error(`Unknown token: ${e}`)}return e}}return null}static async from_pretrained(e,{progress_callback:t=null,config:n=null,cache_dir:s=null,local_files_only:r=!1,revision:o="main",legacy:i=null}={}){return new this(...await Mt(e,{progress_callback:t,config:n,cache_dir:s,local_files_only:r,revision:o,legacy:i}))}_call(e,{text_pair:t=null,add_special_tokens:n=!0,padding:s=!1,truncation:r=null,max_length:o=null,return_tensor:i=!0}={}){const a=Array.isArray(e)
|
|
1761
|
+
let l
|
|
1762
|
+
if(a){if(0===e.length)throw Error("text array must be non-empty")
|
|
1763
|
+
if(null!==t){if(!Array.isArray(t))throw Error("text_pair must also be an array")
|
|
1764
|
+
if(e.length!==t.length)throw Error("text and text_pair must have the same length")
|
|
1765
|
+
l=e.map(((e,s)=>this._encode_plus(e,t[s],{add_special_tokens:n})))}else l=e.map((e=>this._encode_plus(e,null,{add_special_tokens:n})))}else{if(null==e)throw Error("text may not be null or undefined")
|
|
1766
|
+
if(Array.isArray(t))throw Error("When specifying `text_pair`, since `text` is a string, `text_pair` must also be a string (i.e., not an array).")
|
|
1767
|
+
l=[this._encode_plus(e,t,{add_special_tokens:n})]}null===o?o="max_length"===s?this.model_max_length:ee(l.map((e=>e.input_ids.length)))[0]:r||console.warn("Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation=true` to explicitly truncate examples to max length.")
|
|
1768
|
+
o=Math.min(o,this.model_max_length)
|
|
1769
|
+
if(s||r)for(let e=0;e<l.length;++e)l[e].input_ids.length!==o&&(l[e].input_ids.length>o?r&&Fn(l[e],o):s&&Sn(l[e],o,(e=>"input_ids"===e?this.pad_token_id:0),this.padding_side))
|
|
1770
|
+
const c={}
|
|
1771
|
+
if(i){if((!s||!r)&&l.some((e=>{for(const n of Object.keys(e)){var t
|
|
1772
|
+
if(e[n].length!==(null==(t=l[0][n])?void 0:t.length))return!0}return!1})))throw Error("Unable to create tensor, you should probably activate truncation and/or padding with 'padding=true' and 'truncation=true' to have batched tensors with the same length.")
|
|
1773
|
+
const e=[l.length,l[0].input_ids.length]
|
|
1774
|
+
for(const t of Object.keys(l[0]))c[t]=new de("int64",BigInt64Array.from(l.flatMap((e=>e[t])).map(BigInt)),e)}else{for(const e of Object.keys(l[0]))c[e]=l.map((t=>t[e]))
|
|
1775
|
+
if(!a)for(const e of Object.keys(c))c[e]=c[e][0]}return c}_encode_text(e){if(null===e)return null
|
|
1776
|
+
const t=(this.added_tokens_regex?e.split(this.added_tokens_regex).filter((e=>e)):[e]).map(((e,t)=>{if(void 0!==this.added_tokens.find((t=>t.content===e)))return e
|
|
1777
|
+
{!0===this.remove_space&&(e=e.trim().split(/\s+/).join(" "))
|
|
1778
|
+
this.do_lowercase_and_remove_accent&&(e=function(e){return St(e.toLowerCase())}(e))
|
|
1779
|
+
null!==this.normalizer&&(e=this.normalizer(e))
|
|
1780
|
+
if(0===e.length)return[]
|
|
1781
|
+
const n=null!==this.pre_tokenizer?this.pre_tokenizer(e,{section_index:t}):[e]
|
|
1782
|
+
return this.model(n)}})).flat()
|
|
1783
|
+
return t}_encode_plus(e,t=null,{add_special_tokens:n=!0}={}){const s=this._encode_text(e)
|
|
1784
|
+
const r=this._encode_text(t)
|
|
1785
|
+
const o=this.post_processor?this.post_processor(s,r,{add_special_tokens:n}):{tokens:c(null!=s?s:[],null!=r?r:[])}
|
|
1786
|
+
const i=this.model.convert_tokens_to_ids(o.tokens)
|
|
1787
|
+
const a={input_ids:i,attention_mask:new Array(i.length).fill(1)}
|
|
1788
|
+
this.return_token_type_ids&&o.token_type_ids&&(a.token_type_ids=o.token_type_ids)
|
|
1789
|
+
return a}encode(e,t=null,{add_special_tokens:n=!0}={}){const{input_ids:s}=this._encode_plus(e,t,{add_special_tokens:n})
|
|
1790
|
+
return s}batch_decode(e,t={}){e instanceof de&&(e=e.tolist())
|
|
1791
|
+
return e.map((e=>this.decode(e,t)))}decode(e,t={}){e instanceof de&&(e=Ct(e))
|
|
1792
|
+
if(!Array.isArray(e)||0===e.length||!i(e[0]))throw Error("token_ids must be a non-empty array of integers.")
|
|
1793
|
+
return this.decode_single(e,t)}decode_single(e,{skip_special_tokens:t=!1,clean_up_tokenization_spaces:n=null}){let s=this.model.convert_ids_to_tokens(e)
|
|
1794
|
+
t&&(s=s.filter((e=>!this.special_tokens.includes(e))))
|
|
1795
|
+
let r=this.decoder?this.decoder(s):s.join(" ")
|
|
1796
|
+
if(this.decoder&&this.decoder.end_of_word_suffix){r=r.replaceAll(this.decoder.end_of_word_suffix," ")
|
|
1797
|
+
t&&(r=r.trim())}(null!=n?n:this.clean_up_tokenization_spaces)&&(r=At(r))
|
|
1798
|
+
return r}get default_chat_template(){if(!this._warned_about_chat_template){console.warn("No chat template is defined for this tokenizer - using a default chat template that implements the ChatML format. If the default is not appropriate for your model, please set `tokenizer.chat_template` to an appropriate template. See https://huggingface.co/docs/transformers/main/chat_templating for more information.")
|
|
1799
|
+
this._warned_about_chat_template=!0}return this._default_chat_template}apply_chat_template(e,{chat_template:t=null,add_generation_prompt:n=!1,tokenize:s=!0,padding:r=!1,truncation:o=!1,max_length:i=null,return_tensor:a=!0,tokenizer_kwargs:l={},...c}={}){if(this.chat_template&&"object"==typeof this.chat_template||null===this.chat_template&&this.default_chat_template&&"object"==typeof this.default_chat_template){var d
|
|
1800
|
+
const e=null!=(d=this.chat_template)?d:this.default_chat_template
|
|
1801
|
+
if(null!==t&&Object.hasOwn(e,t))t=e[t]
|
|
1802
|
+
else if(null===t&&"default"in e)t=e.default
|
|
1803
|
+
else if(null===t)throw Error(`This model has multiple chat templates with no default specified! Please either pass a chat template or the name of the template you wish to use to the 'chat_template' argument. Available template names are ${Object.keys(e).sort()}.`)}else{var u
|
|
1804
|
+
null!=t||(t=null!=(u=this.chat_template)?u:this.default_chat_template)}if("string"!=typeof t)throw Error("chat_template must be a string, but got "+typeof t)
|
|
1805
|
+
let h=this._compiled_template_cache.get(t)
|
|
1806
|
+
if(void 0===h){h=new kt(t)
|
|
1807
|
+
this._compiled_template_cache.set(t,h)}const _=Object.create(null)
|
|
1808
|
+
for(const e of An){const t=this.getToken(e)
|
|
1809
|
+
t&&(_[e]=t)}const p=h.render({messages:e,add_generation_prompt:n,..._,...c})
|
|
1810
|
+
return s?this._call(p,{add_special_tokens:!1,padding:r,truncation:o,max_length:i,return_tensor:a,...l}).input_ids:p}}class Pn extends zn{return_token_type_ids=!0}class In extends zn{return_token_type_ids=!0}class Ln extends zn{return_token_type_ids=!0}class Bn extends zn{return_token_type_ids=!0}class On extends zn{return_token_type_ids=!0}class Nn extends zn{return_token_type_ids=!0}class $n extends zn{return_token_type_ids=!0}class jn extends zn{return_token_type_ids=!0}class Dn extends zn{return_token_type_ids=!0}class Rn extends zn{}class qn extends zn{}class Un extends zn{return_token_type_ids=!0
|
|
1811
|
+
constructor(e,t){super(e,t)
|
|
1812
|
+
console.warn('WARNING: `XLMTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.')}}class Vn extends zn{return_token_type_ids=!0}class Gn extends zn{}class Wn extends zn{_default_chat_template='{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}'}class Xn extends zn{}class Hn extends zn{constructor(e,t){super(e,t)
|
|
1813
|
+
this.languageRegex=/^[a-z]{2}_[A-Z]{2}$/
|
|
1814
|
+
this.language_codes=this.special_tokens.filter((e=>this.languageRegex.test(e)))
|
|
1815
|
+
this.lang_to_token=e=>e}_build_translation_inputs(e,t,n){return cs(this,e,t,n)}}class Qn extends Hn{}class Kn extends zn{}class Yn extends Wn{constructor(e,t){var n
|
|
1816
|
+
const s=".,!?…。,、।۔،"
|
|
1817
|
+
const r=null==(n=e.pre_tokenizer)||null==(n=n.pretokenizers[0])?void 0:n.pattern
|
|
1818
|
+
r&&r.Regex===` ?[^(\\s|[${s}])]+`&&(r.Regex=` ?[^\\s${s}]+`)
|
|
1819
|
+
super(e,t)}}const Jn="▁"
|
|
1820
|
+
class Zn extends zn{_default_chat_template="{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = messages[0]['content'] %}{% elif USE_DEFAULT_PROMPT == true and not '<<SYS>>' in messages[0]['content'] %}{% set loop_messages = messages %}{% set system_message = 'DEFAULT_SYSTEM_MESSAGE' %}{% else %}{% set loop_messages = messages %}{% set system_message = false %}{% endif %}{% for message in loop_messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if loop.index0 == 0 and system_message != false %}{% set content = '<<SYS>>\n' + system_message + '\n<</SYS>>\n\n' + message['content'] %}{% else %}{% set content = message['content'] %}{% endif %}{% if message['role'] == 'user' %}{{ bos_token + '[INST] ' + content.strip() + ' [/INST]' }}{% elif message['role'] == 'system' %}{{ '<<SYS>>\n' + content.strip() + '\n<</SYS>>\n\n' }}{% elif message['role'] == 'assistant' %}{{ ' ' + content.strip() + ' ' + eos_token }}{% endif %}{% endfor %}"
|
|
1821
|
+
DEFAULT_SYSTEM_PROMPT="You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
|
|
1822
|
+
constructor(e,t){var n,s
|
|
1823
|
+
super(e,t)
|
|
1824
|
+
this.use_default_system_prompt=null!=(n=t.use_default_system_prompt)&&n
|
|
1825
|
+
this.legacy=null==(s=t.legacy)||s
|
|
1826
|
+
if(!this.legacy){this.normalizer=null
|
|
1827
|
+
this.pre_tokenizer=new bn({replacement:Jn,add_prefix_space:!0,prepend_scheme:"first"})}}_encode_text(e){if(null===e)return null
|
|
1828
|
+
if(this.legacy||0===e.length)return super._encode_text(e)
|
|
1829
|
+
let t=super._encode_text(Jn+e.replaceAll(Jn," "))
|
|
1830
|
+
t.length>1&&t[0]===Jn&&this.special_tokens.includes(t[1])&&(t=t.slice(1))
|
|
1831
|
+
return t}get default_chat_template(){return super.default_chat_template.replaceAll("USE_DEFAULT_PROMPT",this.use_default_system_prompt?"true":"false").replaceAll("DEFAULT_SYSTEM_MESSAGE",this.DEFAULT_SYSTEM_PROMPT.replaceAll("\n","\\n").replaceAll("'","\\'"))}}class es extends Zn{}class ts extends zn{}class ns extends zn{}class ss extends zn{}class rs extends zn{}class os extends zn{}class is extends zn{}class as extends zn{_default_chat_template="{% if messages[0]['role'] == 'system' %}{{ raise_exception('System role not supported') }}{% endif %}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if (message['role'] == 'assistant') %}{% set role = 'model' %}{% else %}{% set role = message['role'] %}{% endif %}{{ '<start_of_turn>' + role + '\n' + message['content'] | trim + '<end_of_turn>\n' }}{% endfor %}{% if add_generation_prompt %}{{'<start_of_turn>model\n'}}{% endif %}"}class ls extends zn{}function cs(e,t,n,s){if(!("language_codes"in e)||!Array.isArray(e.language_codes))throw new Error("Tokenizer must have `language_codes` attribute set and it should be an array of language ids.")
|
|
1832
|
+
if(!("languageRegex"in e&&e.languageRegex instanceof RegExp))throw new Error("Tokenizer must have `languageRegex` attribute set and it should be a regular expression.")
|
|
1833
|
+
if(!("lang_to_token"in e)||"function"!=typeof e.lang_to_token)throw new Error("Tokenizer must have `lang_to_token` attribute set and it should be a function.")
|
|
1834
|
+
const r=s.src_lang
|
|
1835
|
+
const o=s.tgt_lang
|
|
1836
|
+
if(!e.language_codes.includes(o))throw new Error(`Target language code "${o}" is not valid. Must be one of: {${e.language_codes.join(", ")}}`)
|
|
1837
|
+
if(void 0!==r){if(!e.language_codes.includes(r))throw new Error(`Source language code "${r}" is not valid. Must be one of: {${e.language_codes.join(", ")}}`)
|
|
1838
|
+
for(const t of e.post_processor.config.single)if("SpecialToken"in t&&e.languageRegex.test(t.SpecialToken.id)){t.SpecialToken.id=e.lang_to_token(r)
|
|
1839
|
+
break}}s.forced_bos_token_id=e.model.convert_tokens_to_ids([e.lang_to_token(o)])[0]
|
|
1840
|
+
return e._call(t,n)}class ds extends zn{constructor(e,t){super(e,t)
|
|
1841
|
+
this.languageRegex=/^[a-z]{3}_[A-Z][a-z]{3}$/
|
|
1842
|
+
this.language_codes=this.special_tokens.filter((e=>this.languageRegex.test(e)))
|
|
1843
|
+
this.lang_to_token=e=>e}_build_translation_inputs(e,t,n){return cs(this,e,t,n)}}class us extends zn{constructor(e,t){super(e,t)
|
|
1844
|
+
this.languageRegex=/^__[a-z]{2,3}__$/
|
|
1845
|
+
this.language_codes=this.special_tokens.filter((e=>this.languageRegex.test(e))).map((e=>e.slice(2,-2)))
|
|
1846
|
+
this.lang_to_token=e=>`__${e}__`}_build_translation_inputs(e,t,n){return cs(this,e,t,n)}}const hs=[["en","english"],["zh","chinese"],["de","german"],["es","spanish"],["ru","russian"],["ko","korean"],["fr","french"],["ja","japanese"],["pt","portuguese"],["tr","turkish"],["pl","polish"],["ca","catalan"],["nl","dutch"],["ar","arabic"],["sv","swedish"],["it","italian"],["id","indonesian"],["hi","hindi"],["fi","finnish"],["vi","vietnamese"],["he","hebrew"],["uk","ukrainian"],["el","greek"],["ms","malay"],["cs","czech"],["ro","romanian"],["da","danish"],["hu","hungarian"],["ta","tamil"],["no","norwegian"],["th","thai"],["ur","urdu"],["hr","croatian"],["bg","bulgarian"],["lt","lithuanian"],["la","latin"],["mi","maori"],["ml","malayalam"],["cy","welsh"],["sk","slovak"],["te","telugu"],["fa","persian"],["lv","latvian"],["bn","bengali"],["sr","serbian"],["az","azerbaijani"],["sl","slovenian"],["kn","kannada"],["et","estonian"],["mk","macedonian"],["br","breton"],["eu","basque"],["is","icelandic"],["hy","armenian"],["ne","nepali"],["mn","mongolian"],["bs","bosnian"],["kk","kazakh"],["sq","albanian"],["sw","swahili"],["gl","galician"],["mr","marathi"],["pa","punjabi"],["si","sinhala"],["km","khmer"],["sn","shona"],["yo","yoruba"],["so","somali"],["af","afrikaans"],["oc","occitan"],["ka","georgian"],["be","belarusian"],["tg","tajik"],["sd","sindhi"],["gu","gujarati"],["am","amharic"],["yi","yiddish"],["lo","lao"],["uz","uzbek"],["fo","faroese"],["ht","haitian creole"],["ps","pashto"],["tk","turkmen"],["nn","nynorsk"],["mt","maltese"],["sa","sanskrit"],["lb","luxembourgish"],["my","myanmar"],["bo","tibetan"],["tl","tagalog"],["mg","malagasy"],["as","assamese"],["tt","tatar"],["haw","hawaiian"],["ln","lingala"],["ha","hausa"],["ba","bashkir"],["jw","javanese"],["su","sundanese"]]
|
|
1847
|
+
const _s=new Map(hs)
|
|
1848
|
+
const ps=new Map([...hs.map((([e,t])=>[t,e])),["burmese","my"],["valencian","ca"],["flemish","nl"],["haitian","ht"],["letzeburgesch","lb"],["pushto","ps"],["panjabi","pa"],["moldavian","ro"],["moldovan","ro"],["sinhalese","si"],["castilian","es"]])
|
|
1849
|
+
class fs extends zn{_default_chat_template='{% for message in messages %}" "{{ message.content }}{{ eos_token }}" "{% endfor %}'
|
|
1850
|
+
_decode_asr(e,{return_timestamps:t=!1,return_language:n=!1,time_precision:s=null,force_full_sequences:r=!0}={}){if(null===s)throw Error("Must specify time_precision")
|
|
1851
|
+
let o=null
|
|
1852
|
+
const i="word"===t
|
|
1853
|
+
function a(){return{language:o,timestamp:[null,null],text:""}}const l=[]
|
|
1854
|
+
let c=a()
|
|
1855
|
+
let d=0
|
|
1856
|
+
const u=this.model.convert_tokens_to_ids(["<|notimestamps|>"])[0]+1
|
|
1857
|
+
let h=[]
|
|
1858
|
+
let _=[]
|
|
1859
|
+
let p=!1
|
|
1860
|
+
let f=null
|
|
1861
|
+
const m=new Set(this.all_special_ids)
|
|
1862
|
+
for(const n of e){const e=n.tokens
|
|
1863
|
+
const r=i?n.token_timestamps:null
|
|
1864
|
+
let g=null
|
|
1865
|
+
let w=u
|
|
1866
|
+
if("stride"in n){const[t,r,o]=n.stride
|
|
1867
|
+
d-=r
|
|
1868
|
+
f=t-o
|
|
1869
|
+
r&&(w=r/s+u)
|
|
1870
|
+
if(o)for(let t=e.length-1;t>=0;--t){const n=e[t]
|
|
1871
|
+
if(n>=u){if(null!==g&&(n-u)*s<f)break
|
|
1872
|
+
g=n}}}let y=[]
|
|
1873
|
+
let x=[]
|
|
1874
|
+
for(let n=0;n<e.length;++n){const f=e[n]
|
|
1875
|
+
if(m.has(f)){const e=this.decode([f])
|
|
1876
|
+
const n=_s.get(e.slice(2,-2))
|
|
1877
|
+
if(void 0!==n){if(null!==o&&n!==o&&!t){h.push(y)
|
|
1878
|
+
const e=this.findLongestCommonSequence(h)[0]
|
|
1879
|
+
const t=this.decode(e)
|
|
1880
|
+
c.text=t
|
|
1881
|
+
l.push(c)
|
|
1882
|
+
h=[]
|
|
1883
|
+
y=[]
|
|
1884
|
+
c=a()}o=c.language=n}}else if(f>=u){const e=ie((f-u)*s+d,2)
|
|
1885
|
+
if(null!==g&&f>=g)p=!0
|
|
1886
|
+
else if(p||h.length>0&&f<w)p=!1
|
|
1887
|
+
else if(null===c.timestamp[0])c.timestamp[0]=e
|
|
1888
|
+
else if(e===c.timestamp[0]);else{c.timestamp[1]=e
|
|
1889
|
+
h.push(y)
|
|
1890
|
+
i&&_.push(x)
|
|
1891
|
+
const[t,n]=this.findLongestCommonSequence(h,_)
|
|
1892
|
+
const s=this.decode(t)
|
|
1893
|
+
c.text=s
|
|
1894
|
+
i&&(c.words=this.collateWordTimestamps(t,n,o))
|
|
1895
|
+
l.push(c)
|
|
1896
|
+
h=[]
|
|
1897
|
+
y=[]
|
|
1898
|
+
_=[]
|
|
1899
|
+
x=[]
|
|
1900
|
+
c=a()}}else{y.push(f)
|
|
1901
|
+
if(i){let e=ie(r[n]+d,2)
|
|
1902
|
+
let t
|
|
1903
|
+
t=n+1<r.length?ie(r[n+1]+d,2):null
|
|
1904
|
+
x.push([e,t])}}}if("stride"in n){const[e,t,s]=n.stride
|
|
1905
|
+
d+=e-s}if(y.length>0){h.push(y)
|
|
1906
|
+
i&&_.push(x)}else if(h.every((e=>0===e.length))){c=a()
|
|
1907
|
+
h=[]
|
|
1908
|
+
y=[]
|
|
1909
|
+
_=[]
|
|
1910
|
+
x=[]}}if(h.length>0){if(r&&t)throw new Error("Whisper did not predict an ending timestamp, which can happen if audio is cut off in the middle of a word. Also make sure WhisperTimeStampLogitsProcessor was used during generation.")
|
|
1911
|
+
const[e,n]=this.findLongestCommonSequence(h,_)
|
|
1912
|
+
const s=this.decode(e)
|
|
1913
|
+
c.text=s
|
|
1914
|
+
i&&(c.words=this.collateWordTimestamps(e,n,o))
|
|
1915
|
+
l.push(c)}let g=Object.create(null)
|
|
1916
|
+
const w=l.map((e=>e.text)).join("")
|
|
1917
|
+
if(t||n){for(let e=0;e<l.length;++e){const s=l[e]
|
|
1918
|
+
t||delete s.timestamp
|
|
1919
|
+
n||delete s.language}if(i){const e=[]
|
|
1920
|
+
for(const t of l)for(const n of t.words)e.push(n)
|
|
1921
|
+
g={chunks:e}}else g={chunks:l}}return[w,g]}findLongestCommonSequence(e,t=null){let n=e[0]
|
|
1922
|
+
let s=n.length
|
|
1923
|
+
let r=[]
|
|
1924
|
+
const o=Array.isArray(t)&&t.length>0
|
|
1925
|
+
let i=o?[]:null
|
|
1926
|
+
let a=o?t[0]:null
|
|
1927
|
+
for(let l=1;l<e.length;++l){const c=e[l]
|
|
1928
|
+
let d=0
|
|
1929
|
+
let u=[s,s,0,0]
|
|
1930
|
+
const h=c.length
|
|
1931
|
+
for(let e=1;e<s+h;++e){const t=e/1e4
|
|
1932
|
+
const r=Math.max(0,s-e)
|
|
1933
|
+
const o=Math.min(s,s+h-e)
|
|
1934
|
+
const i=n.slice(r,o)
|
|
1935
|
+
const a=Math.max(0,e-s)
|
|
1936
|
+
const l=Math.min(h,e)
|
|
1937
|
+
const _=c.slice(a,l)
|
|
1938
|
+
if(i.length!==_.length)throw new Error("There is a bug within whisper `decode_asr` function, please report it. Dropping to prevent bad inference.")
|
|
1939
|
+
const p=i.filter(((e,t)=>e===_[t])).length
|
|
1940
|
+
const f=p/e+t
|
|
1941
|
+
if(p>1&&f>d){d=f
|
|
1942
|
+
u=[r,o,a,l]}}const[_,p,f,m]=u
|
|
1943
|
+
const g=Math.floor((p+_)/2)
|
|
1944
|
+
const w=Math.floor((m+f)/2)
|
|
1945
|
+
r.push(...n.slice(0,g))
|
|
1946
|
+
n=c.slice(w)
|
|
1947
|
+
s=n.length
|
|
1948
|
+
if(o){i.push(...a.slice(0,g))
|
|
1949
|
+
a=t[l].slice(w)}}r.push(...n)
|
|
1950
|
+
if(o){i.push(...a)
|
|
1951
|
+
return[r,i]}return[r,[]]}collateWordTimestamps(e,t,n){const[s,r,o]=this.combineTokensIntoWords(e,n)
|
|
1952
|
+
const i=[]
|
|
1953
|
+
for(let e=0;e<s.length;++e){const n=o[e]
|
|
1954
|
+
i.push({text:s[e],timestamp:[t[n.at(0)][0],t[n.at(-1)][1]]})}return i}combineTokensIntoWords(e,t,n="\"'“¡¿([{-",s="\"'.。,,!!??::”)]}、"){var r
|
|
1955
|
+
t=null!=(r=t)?r:"english"
|
|
1956
|
+
let o,i,a;["chinese","japanese","thai","lao","myanmar"].includes(t)?[o,i,a]=this.splitTokensOnUnicode(e):[o,i,a]=this.splitTokensOnSpaces(e)
|
|
1957
|
+
return this.mergePunctuations(o,i,a,n,s)}decode(e,t){let n
|
|
1958
|
+
if(t&&t.decode_with_timestamps){e instanceof de&&(e=Ct(e))
|
|
1959
|
+
n=this.decodeWithTimestamps(e,t)}else n=super.decode(e,t)
|
|
1960
|
+
return n}decodeWithTimestamps(e,t){var n
|
|
1961
|
+
const s=null!=(n=null==t?void 0:t.time_precision)?n:.02
|
|
1962
|
+
const r=Array.from(this.all_special_ids).at(-1)+1
|
|
1963
|
+
let o=[[]]
|
|
1964
|
+
for(const t of e)if(t>=r){const e=ie((t-r)*s,2)
|
|
1965
|
+
o.push(`<|${e}|>`)
|
|
1966
|
+
o.push([])}else o[o.length-1].push(t)
|
|
1967
|
+
o=o.map((e=>"string"==typeof e?e:super.decode(e,t)))
|
|
1968
|
+
return o.join("")}splitTokensOnUnicode(e){const t=this.decode(e,{decode_with_timestamps:!0})
|
|
1969
|
+
const n=[]
|
|
1970
|
+
const s=[]
|
|
1971
|
+
const r=[]
|
|
1972
|
+
let o=[]
|
|
1973
|
+
let i=[]
|
|
1974
|
+
let a=0
|
|
1975
|
+
for(let l=0;l<e.length;++l){const c=e[l]
|
|
1976
|
+
o.push(c)
|
|
1977
|
+
i.push(l)
|
|
1978
|
+
const d=this.decode(o,{decode_with_timestamps:!0})
|
|
1979
|
+
if(!d.includes("�")||"�"===t[a+d.indexOf("�")]){n.push(d)
|
|
1980
|
+
s.push(o)
|
|
1981
|
+
r.push(i)
|
|
1982
|
+
o=[]
|
|
1983
|
+
i=[]
|
|
1984
|
+
a+=d.length}}return[n,s,r]}splitTokensOnSpaces(e){const[t,n,s]=this.splitTokensOnUnicode(e)
|
|
1985
|
+
const r=[]
|
|
1986
|
+
const o=[]
|
|
1987
|
+
const i=[]
|
|
1988
|
+
const a=new RegExp(`^[${Ft}]$`,"gu")
|
|
1989
|
+
for(let e=0;e<t.length;++e){const l=t[e]
|
|
1990
|
+
const c=n[e]
|
|
1991
|
+
const d=s[e]
|
|
1992
|
+
const u=c[0]>=this.model.tokens_to_ids.get("<|endoftext|>")
|
|
1993
|
+
const h=l.startsWith(" ")
|
|
1994
|
+
const _=l.trim()
|
|
1995
|
+
const p=a.test(_)
|
|
1996
|
+
if(u||h||p||0===r.length){r.push(l)
|
|
1997
|
+
o.push(c)
|
|
1998
|
+
i.push(d)}else{const e=r.length-1
|
|
1999
|
+
r[e]+=l
|
|
2000
|
+
o[e].push(...c)
|
|
2001
|
+
i[e].push(...d)}}return[r,o,i]}mergePunctuations(e,t,n,s,r){const o=structuredClone(e)
|
|
2002
|
+
const i=structuredClone(t)
|
|
2003
|
+
const a=structuredClone(n)
|
|
2004
|
+
let l=o.length-2
|
|
2005
|
+
let d=o.length-1
|
|
2006
|
+
for(;l>=0;){if(o[l].startsWith(" ")&&s.includes(o[l].trim())){o[d]=o[l]+o[d]
|
|
2007
|
+
i[d]=c(i[l],i[d])
|
|
2008
|
+
a[d]=c(a[l],a[d])
|
|
2009
|
+
o[l]=""
|
|
2010
|
+
i[l]=[]
|
|
2011
|
+
a[l]=[]}else d=l;--l}l=0
|
|
2012
|
+
d=1
|
|
2013
|
+
for(;d<o.length;){if(!o[l].endsWith(" ")&&r.includes(o[d])){o[l]+=o[d]
|
|
2014
|
+
i[l]=c(i[l],i[d])
|
|
2015
|
+
a[l]=c(a[l],a[d])
|
|
2016
|
+
o[d]=""
|
|
2017
|
+
i[d]=[]
|
|
2018
|
+
a[d]=[]}else l=d;++d}return[o.filter((e=>e)),i.filter((e=>e.length>0)),a.filter((e=>e.length>0))]}get_decoder_prompt_ids({language:e=null,task:t=null,no_timestamps:n=!0}={}){const s=[]
|
|
2019
|
+
if(e){e=e.toLowerCase()
|
|
2020
|
+
let t=ps.get(e)
|
|
2021
|
+
if(void 0===t){if(!_s.has(e)){const t=2===e.length?_s.keys():_s.values()
|
|
2022
|
+
throw new Error(`Language "${e}" is not supported. Must be one of: ${JSON.stringify(t)}`)}t=e}const n=this.model.tokens_to_ids.get(`<|${t}|>`)
|
|
2023
|
+
if(void 0===n)throw new Error(`Unable to find language "${t}" in model vocabulary. Please report this issue at https://github.com/xenova/transformers.js/issues/new/choose.`)
|
|
2024
|
+
s.push(n)}else s.push(null)
|
|
2025
|
+
if(t){if("transcribe"!==(t=t.toLowerCase())&&"translate"!==t)throw new Error(`Task "${t}" is not supported. Must be one of: ["transcribe", "translate"]`)
|
|
2026
|
+
const e=this.model.tokens_to_ids.get(`<|${t}|>`)
|
|
2027
|
+
if(void 0===e)throw new Error(`Unable to find task "${t}" in model vocabulary. Please report this issue at https://github.com/xenova/transformers.js/issues/new/choose.`)
|
|
2028
|
+
s.push(e)}else s.push(null)
|
|
2029
|
+
if(n){const e=this.model.tokens_to_ids.get("<|notimestamps|>")
|
|
2030
|
+
if(void 0===e)throw new Error('Unable to find "<|notimestamps|>" in model vocabulary. Please report this issue at https://github.com/xenova/transformers.js/issues/new/choose.')
|
|
2031
|
+
s.push(e)}return s.map(((e,t)=>[t+1,e])).filter((e=>null!==e[1]))}}class ms extends zn{}class gs extends zn{}class ws extends zn{}class ys extends zn{constructor(e,t){super(e,t)
|
|
2032
|
+
this.languageRegex=/^(>>\w+<<)\s*/g
|
|
2033
|
+
this.supported_language_codes=this.model.vocab.filter((e=>this.languageRegex.test(e)))
|
|
2034
|
+
console.warn('WARNING: `MarianTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.')}_encode_text(e){if(null===e)return null
|
|
2035
|
+
const[t,...n]=e.trim().split(this.languageRegex)
|
|
2036
|
+
if(0===n.length)return super._encode_text(t)
|
|
2037
|
+
if(2===n.length){const[e,t]=n
|
|
2038
|
+
this.supported_language_codes.includes(e)||console.warn(`Unsupported language code "${e}" detected, which may lead to unexpected behavior. Should be one of: ${JSON.stringify(this.supported_language_codes)}`)
|
|
2039
|
+
return c([e],super._encode_text(t))}}}class xs extends zn{}class bs extends zn{_default_chat_template="{% for message in messages %}{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}{{ message['content'] }}{% if not loop.last %}{{ ' ' }}{% endif %}{% endfor %}{{ eos_token }}"}class vs extends bs{}class ks extends zn{}class Ms extends zn{}class Ts extends zn{constructor(e,t){super(e,t)
|
|
2040
|
+
this.decoder=new xn({})}}class Es extends zn{}class Cs{static TOKENIZER_CLASS_MAPPING={T5Tokenizer:Gn,DistilBertTokenizer:Rn,CamembertTokenizer:qn,DebertaTokenizer:On,DebertaV2Tokenizer:Nn,BertTokenizer:Pn,HerbertTokenizer:$n,ConvBertTokenizer:jn,RoFormerTokenizer:Dn,XLMTokenizer:Un,ElectraTokenizer:Vn,MobileBertTokenizer:Ln,SqueezeBertTokenizer:Bn,AlbertTokenizer:In,GPT2Tokenizer:Wn,BartTokenizer:Xn,MBartTokenizer:Hn,MBart50Tokenizer:Qn,RobertaTokenizer:Kn,WhisperTokenizer:fs,CodeGenTokenizer:ms,CLIPTokenizer:gs,SiglipTokenizer:ws,MarianTokenizer:ys,BloomTokenizer:Yn,NllbTokenizer:ds,M2M100Tokenizer:us,LlamaTokenizer:Zn,CodeLlamaTokenizer:es,XLMRobertaTokenizer:ts,MPNetTokenizer:ns,FalconTokenizer:ss,GPTNeoXTokenizer:rs,EsmTokenizer:os,Wav2Vec2CTCTokenizer:xs,BlenderbotTokenizer:bs,BlenderbotSmallTokenizer:vs,SpeechT5Tokenizer:ks,NougatTokenizer:Ms,VitsTokenizer:Ts,Qwen2Tokenizer:is,GemmaTokenizer:as,Grok1Tokenizer:ls,CohereTokenizer:Es,PreTrainedTokenizer:zn}
|
|
2041
|
+
static async from_pretrained(e,{quantized:t=!0,progress_callback:n=null,config:s=null,cache_dir:r=null,local_files_only:o=!1,revision:i="main",legacy:a=null}={}){var l,c
|
|
2042
|
+
const[d,u]=await Mt(e,{quantized:t,progress_callback:n,config:s,cache_dir:r,local_files_only:o,revision:i,legacy:a})
|
|
2043
|
+
const h=null!=(l=null==(c=u.tokenizer_class)?void 0:c.replace(/Fast$/,""))?l:"PreTrainedTokenizer"
|
|
2044
|
+
let _=this.TOKENIZER_CLASS_MAPPING[h]
|
|
2045
|
+
if(!_){console.warn(`Unknown tokenizer class "${h}", attempting to construct from base class.`)
|
|
2046
|
+
_=zn}return new _(d,u)}}class As{constructor(e){this.model_type=null
|
|
2047
|
+
this.is_encoder_decoder=!1
|
|
2048
|
+
Object.assign(this,e)}static async from_pretrained(e,{progress_callback:t=null,config:n=null,cache_dir:s=null,local_files_only:r=!1,revision:o="main"}={}){let i=null!=n?n:await async function(e,t){return await U(e,"config.json",!0,t)}(e,{progress_callback:t,config:n,cache_dir:s,local_files_only:r,revision:o})
|
|
2049
|
+
return new this(i)}}class Ss{static async from_pretrained(...e){return As.from_pretrained(...e)}}class Fs extends r{constructor(){super()
|
|
2050
|
+
this.processors=[]}push(e){this.processors.push(e)}extend(e){this.processors.push(...e)}_call(e,t){for(let n of t)this.processors.forEach((t=>t(e,n)))}[Symbol.iterator](){return this.processors.values()}}class zs extends r{_call(e,t){throw Error("`_call` should be implemented in a subclass")}}class Ps extends zs{constructor(e){super()
|
|
2051
|
+
this.force_token_map=Object.fromEntries(null!=e?e:[])}_call(e,t){let n=this.force_token_map[e.length]
|
|
2052
|
+
if(null!=n){t.data.fill(-1/0)
|
|
2053
|
+
t.data[n]=0}return t}}class Is extends zs{constructor(e){super()
|
|
2054
|
+
this.bos_token_id=e}_call(e,t){if(1===e.length){t.data.fill(-1/0)
|
|
2055
|
+
t.data[this.bos_token_id]=0}return t}}class Ls extends zs{constructor(e,t){super()
|
|
2056
|
+
this.max_length=e
|
|
2057
|
+
this.forced_eos_token_id=t}_call(e,t){}}class Bs extends zs{constructor(e,t){super()
|
|
2058
|
+
this.begin_suppress_tokens=e
|
|
2059
|
+
this.begin_index=t}_call(e,t){if(e.length===this.begin_index)for(let e of this.begin_suppress_tokens)t.data[e]=-1/0
|
|
2060
|
+
return t}}class Os extends zs{constructor(e){super()
|
|
2061
|
+
this.eos_token_id=e.eos_token_id
|
|
2062
|
+
this.no_timestamps_token_id=e.no_timestamps_token_id
|
|
2063
|
+
this.timestamp_begin=this.no_timestamps_token_id+1
|
|
2064
|
+
this.begin_index=(e.forced_decoder_ids||[]).length+2
|
|
2065
|
+
e.forced_decoder_ids.slice(-1)[0][1]===this.no_timestamps_token_id&&(this.begin_index-=1)
|
|
2066
|
+
this.max_initial_timestamp_index=e.max_initial_timestamp_index}_call(e,t){const n=t.data
|
|
2067
|
+
n[this.no_timestamps_token_id]=-1/0
|
|
2068
|
+
if(e.length===this.begin_index-1){n.fill(-1/0)
|
|
2069
|
+
n[this.timestamp_begin]=0
|
|
2070
|
+
return t}const s=e.slice(this.begin_index)
|
|
2071
|
+
const r=s.length>=1&&s[s.length-1]>=this.timestamp_begin
|
|
2072
|
+
const o=s.length<2||s[s.length-2]>=this.timestamp_begin
|
|
2073
|
+
r&&(o?n.subarray(this.timestamp_begin).fill(-1/0):n.subarray(0,this.eos_token_id).fill(-1/0))
|
|
2074
|
+
if(e.length===this.begin_index&&null!==this.max_initial_timestamp_index){const e=this.timestamp_begin+this.max_initial_timestamp_index
|
|
2075
|
+
n.subarray(e+1).fill(-1/0)}const i=H(n)
|
|
2076
|
+
Math.log(i.subarray(this.timestamp_begin).map(Math.exp).reduce(((e,t)=>e+t)))>ee(i.subarray(0,this.timestamp_begin))[0]&&n.subarray(0,this.timestamp_begin).fill(-1/0)
|
|
2077
|
+
return t}}class Ns extends zs{constructor(e){super()
|
|
2078
|
+
this.no_repeat_ngram_size=e}getNgrams(e){const t=e.length
|
|
2079
|
+
const n=[]
|
|
2080
|
+
for(let s=0;s<t+1-this.no_repeat_ngram_size;++s){const t=[]
|
|
2081
|
+
for(let n=0;n<this.no_repeat_ngram_size;++n)t.push(e[s+n])
|
|
2082
|
+
n.push(t)}const s=new Map
|
|
2083
|
+
for(const e of n){var r
|
|
2084
|
+
const t=e.slice(0,e.length-1)
|
|
2085
|
+
const n=JSON.stringify(t)
|
|
2086
|
+
const o=null!=(r=s.get(n))?r:[]
|
|
2087
|
+
o.push(e[e.length-1])
|
|
2088
|
+
s.set(n,o)}return s}getGeneratedNgrams(e,t){var n
|
|
2089
|
+
const s=t.slice(t.length+1-this.no_repeat_ngram_size,t.length)
|
|
2090
|
+
return null!=(n=e.get(JSON.stringify(s)))?n:[]}calcBannedNgramTokens(e){if(e.length+1<this.no_repeat_ngram_size)return[]
|
|
2091
|
+
{const t=this.getNgrams(e)
|
|
2092
|
+
return this.getGeneratedNgrams(t,e)}}_call(e,t){const n=this.calcBannedNgramTokens(e)
|
|
2093
|
+
for(const e of n)t.data[e]=-1/0
|
|
2094
|
+
return t}}class $s extends zs{constructor(e){super()
|
|
2095
|
+
this.penalty=e}_call(e,t){for(const n of e)t.data[n]<0?t.data[n]*=this.penalty:t.data[n]/=this.penalty
|
|
2096
|
+
return t}}class js extends zs{constructor(e,t){super()
|
|
2097
|
+
this.min_length=e
|
|
2098
|
+
this.eos_token_id=Array.isArray(t)?t:[t]}_call(e,t){if(e.length<this.min_length)for(const e of this.eos_token_id)t.data[e]=-1/0
|
|
2099
|
+
return t}}class Ds extends zs{constructor(e,t,n){super()
|
|
2100
|
+
this.prompt_length_to_skip=e
|
|
2101
|
+
this.min_new_tokens=t
|
|
2102
|
+
this.eos_token_id=Array.isArray(n)?n:[n]}_call(e,t){if(e.length-this.prompt_length_to_skip<this.min_new_tokens)for(const e of this.eos_token_id)t.data[e]=-1/0
|
|
2103
|
+
return t}}class Rs extends zs{constructor(e,t){super()
|
|
2104
|
+
this.bad_words_ids=e
|
|
2105
|
+
this.eos_token_id=Array.isArray(t)?t:[t]}_call(e,t){for(const n of this.bad_words_ids){let s=!0
|
|
2106
|
+
for(let t=1;t<=n.length-1&&n.length<e.length;++t)if(n.at(-t-1)!==e.at(-t)){s=!1
|
|
2107
|
+
break}s&&(t.data[n.at(-1)]=-1/0)}return t}}const qs=class{constructor(e={}){var t,n,s,r,o,i,a,l,c,d,u,h,_,p,f,m,g,w,y,x,b,v,k,M,T,E,C,A,S,F,z,P,I,L,B,O,N,$,j,D,R,q,U,V
|
|
2108
|
+
this.max_length=null!=(t=e.max_length)?t:20
|
|
2109
|
+
this.max_new_tokens=null!=(n=e.max_new_tokens)?n:null
|
|
2110
|
+
this.min_length=null!=(s=e.min_length)?s:0
|
|
2111
|
+
this.min_new_tokens=null!=(r=e.min_new_tokens)?r:null
|
|
2112
|
+
this.early_stopping=null!=(o=e.early_stopping)&&o
|
|
2113
|
+
this.max_time=null!=(i=e.max_time)?i:null
|
|
2114
|
+
this.do_sample=null!=(a=e.do_sample)&&a
|
|
2115
|
+
this.num_beams=null!=(l=e.num_beams)?l:1
|
|
2116
|
+
this.num_beam_groups=null!=(c=e.num_beam_groups)?c:1
|
|
2117
|
+
this.penalty_alpha=null!=(d=e.penalty_alpha)?d:null
|
|
2118
|
+
this.use_cache=null==(u=e.use_cache)||u
|
|
2119
|
+
this.temperature=null!=(h=e.temperature)?h:1
|
|
2120
|
+
this.top_k=null!=(_=e.top_k)?_:50
|
|
2121
|
+
this.top_p=null!=(p=e.top_p)?p:1
|
|
2122
|
+
this.typical_p=null!=(f=e.typical_p)?f:1
|
|
2123
|
+
this.epsilon_cutoff=null!=(m=e.epsilon_cutoff)?m:0
|
|
2124
|
+
this.eta_cutoff=null!=(g=e.eta_cutoff)?g:0
|
|
2125
|
+
this.diversity_penalty=null!=(w=e.diversity_penalty)?w:0
|
|
2126
|
+
this.repetition_penalty=null!=(y=e.repetition_penalty)?y:1
|
|
2127
|
+
this.encoder_repetition_penalty=null!=(x=e.encoder_repetition_penalty)?x:1
|
|
2128
|
+
this.length_penalty=null!=(b=e.length_penalty)?b:1
|
|
2129
|
+
this.no_repeat_ngram_size=null!=(v=e.no_repeat_ngram_size)?v:0
|
|
2130
|
+
this.bad_words_ids=null!=(k=e.bad_words_ids)?k:null
|
|
2131
|
+
this.force_words_ids=null!=(M=e.force_words_ids)?M:null
|
|
2132
|
+
this.renormalize_logits=null!=(T=e.renormalize_logits)&&T
|
|
2133
|
+
this.constraints=null!=(E=e.constraints)?E:null
|
|
2134
|
+
this.forced_bos_token_id=null!=(C=e.forced_bos_token_id)?C:null
|
|
2135
|
+
this.forced_eos_token_id=null!=(A=e.forced_eos_token_id)?A:null
|
|
2136
|
+
this.remove_invalid_values=null!=(S=e.remove_invalid_values)&&S
|
|
2137
|
+
this.exponential_decay_length_penalty=null!=(F=e.exponential_decay_length_penalty)?F:null
|
|
2138
|
+
this.suppress_tokens=null!=(z=e.suppress_tokens)?z:null
|
|
2139
|
+
this.begin_suppress_tokens=null!=(P=e.begin_suppress_tokens)?P:null
|
|
2140
|
+
this.forced_decoder_ids=null!=(I=e.forced_decoder_ids)?I:null
|
|
2141
|
+
this.num_return_sequences=null!=(L=e.num_return_sequences)?L:1
|
|
2142
|
+
this.output_attentions=null!=(B=e.output_attentions)&&B
|
|
2143
|
+
this.output_hidden_states=null!=(O=e.output_hidden_states)&&O
|
|
2144
|
+
this.output_scores=null!=(N=e.output_scores)&&N
|
|
2145
|
+
this.return_dict_in_generate=null!=($=e.return_dict_in_generate)&&$
|
|
2146
|
+
this.pad_token_id=null!=(j=e.pad_token_id)?j:null
|
|
2147
|
+
this.bos_token_id=null!=(D=e.bos_token_id)?D:null
|
|
2148
|
+
this.eos_token_id=null!=(R=e.eos_token_id)?R:null
|
|
2149
|
+
this.encoder_no_repeat_ngram_size=null!=(q=e.encoder_no_repeat_ngram_size)?q:0
|
|
2150
|
+
this.decoder_start_token_id=null!=(U=e.decoder_start_token_id)?U:null
|
|
2151
|
+
this.generation_kwargs=null!=(V=e.generation_kwargs)?V:{}}}
|
|
2152
|
+
class Us extends r{constructor(e){super()
|
|
2153
|
+
this.generation_config=e}_call(e,t=-1){return this.sample(e,t)}sample(e,t){throw Error("sample should be implemented in subclasses.")}getLogits(e,t){let n=e.dims.at(-1)
|
|
2154
|
+
let s=e.data
|
|
2155
|
+
if(-1===t)s=s.slice(-n)
|
|
2156
|
+
else{let e=t*n
|
|
2157
|
+
s=s.slice(e,e+n)}this.generation_config.temperature>0&&(s=s.map((e=>e/this.generation_config.temperature)))
|
|
2158
|
+
return s}randomSelect(e){let t=e.reduce(((e,t)=>e+t),0)
|
|
2159
|
+
let n=Math.random()*t
|
|
2160
|
+
for(let t=0;t<e.length;++t){n-=e[t]
|
|
2161
|
+
if(n<=0)return t}return 0}static getSampler(e){if(e.do_sample)return new Gs(e)
|
|
2162
|
+
if(e.num_beams>1)return new Ws(e)
|
|
2163
|
+
if(e.num_return_sequences>1)throw Error(`num_return_sequences has to be 1 when doing greedy search, but is ${e.num_return_sequences}.`)
|
|
2164
|
+
return new Vs(e)}}class Vs extends Us{sample(e,t=-1){return[[ee(this.getLogits(e,t))[1],0]]}}class Gs extends Us{sample(e,t=-1){let n=e.dims.at(-1)
|
|
2165
|
+
this.generation_config.top_k>0&&(n=Math.min(this.generation_config.top_k,n))
|
|
2166
|
+
const s=K(this.getLogits(e,t),n)
|
|
2167
|
+
const r=X(s.map((e=>e[1])))
|
|
2168
|
+
return Array.from({length:this.generation_config.num_beams},(()=>{const e=this.randomSelect(r)
|
|
2169
|
+
return[s[e][0],Math.log(r[e])]}))}}class Ws extends Us{sample(e,t=-1){let n=e.dims.at(-1)
|
|
2170
|
+
this.generation_config.top_k>0&&(n=Math.min(this.generation_config.top_k,n))
|
|
2171
|
+
const s=K(this.getLogits(e,t),n)
|
|
2172
|
+
const r=X(s.map((e=>e[1])))
|
|
2173
|
+
return Array.from({length:this.generation_config.num_beams},((e,t)=>[s[t][0],Math.log(r[t])]))}}const{InferenceSession:Xs,Tensor:Hs,env:Qs}=b
|
|
2174
|
+
const Ks=new Map
|
|
2175
|
+
const Ys=new Map
|
|
2176
|
+
const Js=new Map
|
|
2177
|
+
async function Zs(e,t,n){let s=`onnx/${t}${n.quantized?"_quantized":""}.onnx`
|
|
2178
|
+
let r=await q(e,s,!0,n)
|
|
2179
|
+
try{return await Xs.create(r,{executionProviders:v})}catch(e){if(1===v.length&&"wasm"===v[0])throw e
|
|
2180
|
+
console.warn(e)
|
|
2181
|
+
console.warn("Something went wrong during model construction (most likely a missing operation). Using `wasm` as a fallback. ")
|
|
2182
|
+
return await Xs.create(r,{executionProviders:["wasm"]})}}async function er(e,t){const n=function(e,t){const n=Object.create(null)
|
|
2183
|
+
const s=[]
|
|
2184
|
+
for(const r of e.inputNames){const e=t[r]
|
|
2185
|
+
e instanceof de?n[r]=Qs.wasm.proxy?e.clone():e:s.push(r)}if(s.length>0)throw new Error(`An error occurred during model execution: "Missing the following inputs: ${s.join(", ")}.`)
|
|
2186
|
+
const r=Object.keys(t).length
|
|
2187
|
+
const o=e.inputNames.length
|
|
2188
|
+
if(r>o){let n=Object.keys(t).filter((t=>!e.inputNames.includes(t)))
|
|
2189
|
+
console.warn(`WARNING: Too many inputs were provided (${r} > ${o}). The following inputs will be ignored: "${n.join(", ")}".`)}return n}(e,t)
|
|
2190
|
+
try{let t=await e.run(n)
|
|
2191
|
+
t=tr(t)
|
|
2192
|
+
return t}catch(e){console.error(`An error occurred during model execution: "${e}".`)
|
|
2193
|
+
console.error("Inputs given to model:",n)
|
|
2194
|
+
throw e}}function tr(e){for(let t in e)e[t]instanceof Hs?e[t]=new de(e[t]):"object"==typeof e[t]&&tr(e[t])
|
|
2195
|
+
return e}function nr(e){if(e instanceof de)return e
|
|
2196
|
+
if(0===e.length)throw Error("items must be non-empty")
|
|
2197
|
+
if(Array.isArray(e[0])){if(e.some((t=>t.length!==e[0].length)))throw Error("Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' and/or 'truncation=True' to have batched tensors with the same length.")
|
|
2198
|
+
return new de("int64",BigInt64Array.from(e.flat().map((e=>BigInt(e)))),[e.length,e[0].length])}return new de("int64",BigInt64Array.from(e.map((e=>BigInt(e)))),[1,e.length])}function sr(e,t){var n,s
|
|
2199
|
+
let r=null!=(n=e.config.pad_token_id)?n:null
|
|
2200
|
+
let o=null!=(s=e.config.eos_token_id)?s:null
|
|
2201
|
+
i(o)&&(o=[o])
|
|
2202
|
+
let a=-1!==t.indexOf(r)
|
|
2203
|
+
let l=null===o||!o.includes(r)
|
|
2204
|
+
if(a&&l){let e=BigInt64Array.from(t.data.map((e=>e!=r)))
|
|
2205
|
+
return new de("int64",e,t.dims)}return Me(t)}function rr(e,t,n){if(!e.inputNames.includes("position_ids"))return
|
|
2206
|
+
const s=new BigInt64Array(t.attention_mask.data.length)
|
|
2207
|
+
for(let e=0;e<t.attention_mask.dims[0];++e){let n=e*t.attention_mask.dims[1]
|
|
2208
|
+
let r=BigInt(0)
|
|
2209
|
+
for(let e=0;e<t.attention_mask.dims[1];++e){const o=n+e
|
|
2210
|
+
if(0n===t.attention_mask.data[o])s[o]=BigInt(1)
|
|
2211
|
+
else{s[o]=r
|
|
2212
|
+
r+=t.attention_mask.data[o]}}}t.position_ids=new de("int64",s,t.attention_mask.dims)
|
|
2213
|
+
n&&(t.position_ids=t.position_ids.slice(null,-1).unsqueeze_(-1))}function or(e){return new de("bool",[e],[1])}async function ir(e,t){let{encoder_outputs:n,past_key_values:s}=t
|
|
2214
|
+
n||(n=(await dr(e,t)).last_hidden_state)
|
|
2215
|
+
let r={input_ids:t.decoder_input_ids,encoder_hidden_states:n}
|
|
2216
|
+
const o=!!s
|
|
2217
|
+
e.decoder_merged_session.inputNames.includes("use_cache_branch")&&(r.use_cache_branch=or(o))
|
|
2218
|
+
e.decoder_merged_session.inputNames.includes("encoder_attention_mask")&&(r.encoder_attention_mask=t.attention_mask)
|
|
2219
|
+
rr(e.decoder_merged_session,r,o)
|
|
2220
|
+
e.addPastKeyValues(r,s)
|
|
2221
|
+
const i=await er(e.decoder_merged_session,r)
|
|
2222
|
+
let a=i.logits
|
|
2223
|
+
s=e.getPastKeyValues(i,s)
|
|
2224
|
+
const l=e.getAttentions(i)
|
|
2225
|
+
return new su({logits:a,past_key_values:s,encoder_outputs:n,...l})}function ar(e,t,n,s){var r,o,i,a
|
|
2226
|
+
let l=[]
|
|
2227
|
+
let c=0
|
|
2228
|
+
const d=null==(r=e.requires_attention_mask)||r
|
|
2229
|
+
let u=null!=(o=null!=(i=null!=(a=n.decoder_input_ids)?a:n.decoder_start_token_id)?i:n.bos_token_id)?o:n.eos_token_id
|
|
2230
|
+
u instanceof de?u=u.tolist().flat():Array.isArray(u)||(u=[u])
|
|
2231
|
+
for(let n of t){n.dims=[1,...n.dims]
|
|
2232
|
+
let t={inputs:n,encoder_outputs:null,prev_model_outputs:null,output_token_ids:u,done:!1,score:0,id:c++}
|
|
2233
|
+
d&&(t.attention_mask=sr(e,n))
|
|
2234
|
+
l.push(t)}return l}async function lr(e,t){var n
|
|
2235
|
+
const s=e.main_input_name
|
|
2236
|
+
let r=t.output_token_ids
|
|
2237
|
+
t.prev_model_outputs&&(r=r.slice(-1))
|
|
2238
|
+
let o={[s]:t.inputs,decoder_input_ids:nr(r),encoder_outputs:t.encoder_outputs,past_key_values:null==(n=t.prev_model_outputs)?void 0:n.past_key_values}
|
|
2239
|
+
t.attention_mask&&(o.attention_mask=t.attention_mask)
|
|
2240
|
+
let i=await e.forward(o)
|
|
2241
|
+
t.prev_model_outputs=i
|
|
2242
|
+
t.encoder_outputs=i.encoder_outputs
|
|
2243
|
+
return i}function cr(e,t){e.output_token_ids=[...e.output_token_ids,t]}async function dr(e,t){const n=Object.create(null)
|
|
2244
|
+
for(const s of e.session.inputNames)n[s]=t[s]
|
|
2245
|
+
e.session.inputNames.includes("token_type_ids")&&!n.token_type_ids&&(n.token_type_ids=new de("int64",new BigInt64Array(n.input_ids.data.length),n.input_ids.dims))
|
|
2246
|
+
return await er(e.session,n)}async function ur(e,t){let{input_ids:n,past_key_values:s,attention_mask:r}=t
|
|
2247
|
+
let o={input_ids:n,attention_mask:null!=r?r:sr(e,n)}
|
|
2248
|
+
const i=!!s
|
|
2249
|
+
e.session.inputNames.includes("use_cache_branch")&&(o.use_cache_branch=or(i))
|
|
2250
|
+
rr(e.session,o,i)
|
|
2251
|
+
e.addPastKeyValues(o,s)
|
|
2252
|
+
let a=await er(e.session,o)
|
|
2253
|
+
let l=a.logits
|
|
2254
|
+
s=e.getPastKeyValues(a,s)
|
|
2255
|
+
return{logits:l,past_key_values:s}}function hr(e,t,n,s,r){let o=[]
|
|
2256
|
+
let i=0
|
|
2257
|
+
for(let n of t){let t=n.tolist().map(Number)
|
|
2258
|
+
n.dims=[1,...n.dims]
|
|
2259
|
+
let a
|
|
2260
|
+
if(r){a=r[i]
|
|
2261
|
+
a.dims=[1,...a.dims]}else a=sr(e,n)
|
|
2262
|
+
let l={input:n,model_input_ids:n,attention_mask:a,prev_model_outputs:null,output_token_ids:t,num_output_tokens:s,done:!1,score:0,id:i++}
|
|
2263
|
+
o.push(l)}return o}async function _r(e,t){var n
|
|
2264
|
+
let s=new BigInt64Array(t.output_token_ids.length).fill(1n)
|
|
2265
|
+
let r={input_ids:t.model_input_ids,attention_mask:new de("int64",s,[1,s.length]),past_key_values:null==(n=t.prev_model_outputs)?void 0:n.past_key_values}
|
|
2266
|
+
let o=await e.forward(r)
|
|
2267
|
+
t.prev_model_outputs=o
|
|
2268
|
+
return o}function pr(e,t){e.output_token_ids=[...e.output_token_ids,t]
|
|
2269
|
+
e.model_input_ids=new de("int64",[BigInt(t)],[1,1])}class fr extends r{main_input_name="input_ids"
|
|
2270
|
+
constructor(e,t){super()
|
|
2271
|
+
this.config=e
|
|
2272
|
+
this.session=t
|
|
2273
|
+
const n=Js.get(this.constructor)
|
|
2274
|
+
const s=Ks.get(n)
|
|
2275
|
+
this.can_generate=!1
|
|
2276
|
+
this._runBeam=null
|
|
2277
|
+
this._getStartBeams=null
|
|
2278
|
+
this._updateBeam=null
|
|
2279
|
+
this._forward=null
|
|
2280
|
+
if(4===s){this.can_generate=!0
|
|
2281
|
+
this._runBeam=_r
|
|
2282
|
+
this._getStartBeams=hr
|
|
2283
|
+
this._updateBeam=pr
|
|
2284
|
+
this._forward=ur}else if(2===s||3===s){this.can_generate=!0
|
|
2285
|
+
this._runBeam=lr
|
|
2286
|
+
this._getStartBeams=ar
|
|
2287
|
+
this._updateBeam=cr
|
|
2288
|
+
this._forward=ir}else this._forward=dr}async dispose(){const e=[]
|
|
2289
|
+
for(let t of Object.keys(this)){const n=this[t]
|
|
2290
|
+
n instanceof Xs&&e.push(n.handler.dispose())}return await Promise.all(e)}static async from_pretrained(e,{quantized:t=!0,progress_callback:n=null,config:s=null,cache_dir:r=null,local_files_only:o=!1,revision:i="main",model_file_name:a=null}={}){let l={quantized:t,progress_callback:n,config:s,cache_dir:r,local_files_only:o,revision:i,model_file_name:a}
|
|
2291
|
+
const c=Js.get(this)
|
|
2292
|
+
const d=Ks.get(c)
|
|
2293
|
+
let u
|
|
2294
|
+
if(4===d){var h
|
|
2295
|
+
u=await Promise.all([Ss.from_pretrained(e,l),Zs(e,null!=(h=l.model_file_name)?h:"decoder_model_merged",l),U(e,"generation_config.json",!1,l)])}else if(2===d||3===d)u=await Promise.all([Ss.from_pretrained(e,l),Zs(e,"encoder_model",l),Zs(e,"decoder_model_merged",l),U(e,"generation_config.json",!1,l)])
|
|
2296
|
+
else if(5===d)u=await Promise.all([Ss.from_pretrained(e,l),Zs(e,"vision_encoder",l),Zs(e,"prompt_encoder_mask_decoder",l)])
|
|
2297
|
+
else if(1===d)u=await Promise.all([Ss.from_pretrained(e,l),Zs(e,"encoder_model",l),Zs(e,"decoder_model_merged",l)])
|
|
2298
|
+
else{var _
|
|
2299
|
+
0!==d&&console.warn(`Model type for '${null!=c?c:null==s?void 0:s.model_type}' not found, assuming encoder-only architecture. Please report this at https://github.com/xenova/transformers.js/issues/new/choose.`)
|
|
2300
|
+
u=await Promise.all([Ss.from_pretrained(e,l),Zs(e,null!=(_=l.model_file_name)?_:"model",l)])}return new this(...u)}async _call(e){return await this.forward(e)}async forward(e){return await this._forward(this,e)}_get_logits_processor(e,t,n=null){const s=new Fs
|
|
2301
|
+
null!==e.repetition_penalty&&1!==e.repetition_penalty&&s.push(new $s(e.repetition_penalty))
|
|
2302
|
+
null!==e.no_repeat_ngram_size&&e.no_repeat_ngram_size>0&&s.push(new Ns(e.no_repeat_ngram_size))
|
|
2303
|
+
null!==e.bad_words_ids&&s.push(new Rs(e.bad_words_ids,e.eos_token_id))
|
|
2304
|
+
null!==e.min_length&&null!==e.eos_token_id&&e.min_length>0&&s.push(new js(e.min_length,e.eos_token_id))
|
|
2305
|
+
null!==e.min_new_tokens&&null!==e.eos_token_id&&e.min_new_tokens>0&&s.push(new Ds(t,e.min_new_tokens,e.eos_token_id))
|
|
2306
|
+
null!==e.forced_bos_token_id&&s.push(new Is(e.forced_bos_token_id))
|
|
2307
|
+
null!==e.forced_eos_token_id&&s.push(new Ls(e.max_length,e.forced_eos_token_id))
|
|
2308
|
+
if(null!==e.begin_suppress_tokens){let n=t>1||null===e.forced_bos_token_id?t:t+1
|
|
2309
|
+
null!==e.forced_decoder_ids&&(n+=e.forced_decoder_ids[e.forced_decoder_ids.length-1][0])
|
|
2310
|
+
s.push(new Bs(e.begin_suppress_tokens,n))}null!==e.forced_decoder_ids&&s.push(new Ps(e.forced_decoder_ids))
|
|
2311
|
+
null!==n&&s.extend(n)
|
|
2312
|
+
return s}_get_generation_config(e){let t=new qs(this.config)
|
|
2313
|
+
"generation_config"in this&&Object.assign(t,this.generation_config)
|
|
2314
|
+
null!==e&&Object.assign(t,e)
|
|
2315
|
+
return t}async generate(e,t=null,n=null,{inputs_attention_mask:s=null}={}){var r,o,i
|
|
2316
|
+
if(!this.can_generate){var a,l,c
|
|
2317
|
+
let e=`The current model class (${Js.get(this.constructor)}) is not compatible with \`.generate()\`, as it doesn't have a language model head.`
|
|
2318
|
+
const t=this.config.model_type
|
|
2319
|
+
const n=null!=(a=null!=(l=null!=(c=dd.get(t))?c:cd.get(t))?l:rd.get(t))?a:_d.get(t)
|
|
2320
|
+
n&&(e+=` Please use the following class instead: '${n[0]}'`)
|
|
2321
|
+
throw Error(e)}if(!(e instanceof de||(d=e,"TypedArray"===(null==d||null==(u=d.prototype)||null==(u=u.__proto__)||null==(u=u.constructor)?void 0:u.name))||Array.isArray(e)))throw Error(`\`inputs\` must be a Tensor, TypedArray, or Array, but is "${e.constructor.name}".`)
|
|
2322
|
+
var d,u
|
|
2323
|
+
let h
|
|
2324
|
+
if(this.config.is_encoder_decoder)h=0
|
|
2325
|
+
else{h=e instanceof de?e.dims.at(-1):e.length
|
|
2326
|
+
if(0===h)throw Error("Must supply a non-empty array of input token ids.")}t=this._get_generation_config(t)
|
|
2327
|
+
n=null!=(r=n)?r:new Fs
|
|
2328
|
+
n=this._get_logits_processor(t,h,n)
|
|
2329
|
+
let _=t.eos_token_id
|
|
2330
|
+
null===_||Array.isArray(_)||(_=[_])
|
|
2331
|
+
let p=1
|
|
2332
|
+
const f=p+(null!=(o=t.max_new_tokens)?o:1/0)
|
|
2333
|
+
const m=Number.isInteger(t.max_length)&&null===(null!=(i=t.max_new_tokens)?i:null)
|
|
2334
|
+
let g=Us.getSampler(t)
|
|
2335
|
+
let w=this.getStartBeams(e,t,p,s)
|
|
2336
|
+
for(;w.some((e=>!e.done))&&p<f;){let e=[]
|
|
2337
|
+
for(let s of w){if(s.done){e.push(s)
|
|
2338
|
+
continue}if(m&&s.output_token_ids.length>=t.max_length){s.done=!0
|
|
2339
|
+
e.push(s)
|
|
2340
|
+
continue}let r=await this.runBeam(s)
|
|
2341
|
+
t.output_attentions&&this.addAttentionsToBeam(s,r)
|
|
2342
|
+
t.output_scores
|
|
2343
|
+
let o=r.logits.slice(null,-1,null)
|
|
2344
|
+
n(s.output_token_ids,o)
|
|
2345
|
+
let i=g(o)
|
|
2346
|
+
for(let[t,n]of i){let r={...s}
|
|
2347
|
+
this.updateBeam(r,t)
|
|
2348
|
+
r.score+=n
|
|
2349
|
+
_&&_.includes(t)&&(r.done=!0)
|
|
2350
|
+
e.push(r)}}++p
|
|
2351
|
+
e=this.groupBeams(e).map((e=>e.sort(((e,t)=>t.score-e.score)).slice(0,t.num_beams)))
|
|
2352
|
+
w=e.flat()
|
|
2353
|
+
t.callback_function&&t.callback_function(w)}const y=this.groupBeams(w)
|
|
2354
|
+
const x=e=>y.map((n=>t.num_return_sequences>1?n.slice(0,t.num_return_sequences).map((t=>t[e])):[n[0][e]])).flat()
|
|
2355
|
+
const b=x("output_token_ids")
|
|
2356
|
+
return t.return_dict_in_generate?{sequences:b,decoder_attentions:x("decoder_attentions"),cross_attentions:x("cross_attentions")}:b}addAttentionsToBeam(e,t){if(this.config.is_encoder_decoder){if(!t.cross_attentions||0===t.cross_attentions.length)throw Error("`output_attentions` is true, but the model did not produce cross-attentions. This is most likely because the model was not exported with `output_attentions=True`.")
|
|
2357
|
+
e.cross_attentions||(e.cross_attentions=[])
|
|
2358
|
+
e.cross_attentions.push(t.cross_attentions)}if(!t.decoder_attentions||0===t.decoder_attentions.length)throw Error("`output_attentions` is true, but the model did not produce decoder-attentions. This is most likely because the model was not exported with `output_attentions=True`.")
|
|
2359
|
+
e.decoder_attentions||(e.decoder_attentions=[])
|
|
2360
|
+
e.decoder_attentions.push(t.decoder_attentions)}groupBeams(e){const t=Object.create(null)
|
|
2361
|
+
for(const n of e)void 0===t[n.id]?t[n.id]=[n]:t[n.id].push(n)
|
|
2362
|
+
return Object.values(t)}getPastKeyValues(e,t){const n=Object.create(null)
|
|
2363
|
+
for(const s in e)if(s.startsWith("present")){let r=s.replace("present","past_key_values")
|
|
2364
|
+
t&&s.includes("encoder")?n[r]=t[r]:n[r]=e[s]}return n}getAttentions(e){const t=Object.create(null)
|
|
2365
|
+
for(const n of["cross_attentions","decoder_attentions"]){const s=[]
|
|
2366
|
+
for(const t in e)t.startsWith(n)&&(s[t.split(".").pop()]=e[t])
|
|
2367
|
+
t[n]=s}return t}addPastKeyValues(e,t){if(t)Object.assign(e,t)
|
|
2368
|
+
else{var n
|
|
2369
|
+
const t=1
|
|
2370
|
+
if(this.config.is_encoder_decoder&&(null==(n=this.add_encoder_pkv)||n)){let n=[t,this.num_encoder_heads,0,this.encoder_dim_kv]
|
|
2371
|
+
let s=[t,this.num_decoder_heads,0,this.decoder_dim_kv]
|
|
2372
|
+
for(let t=0;t<this.num_decoder_layers;++t){e[`past_key_values.${t}.encoder.key`]=new de("float32",[],n)
|
|
2373
|
+
e[`past_key_values.${t}.encoder.value`]=new de("float32",[],n)
|
|
2374
|
+
e[`past_key_values.${t}.decoder.key`]=new de("float32",[],s)
|
|
2375
|
+
e[`past_key_values.${t}.decoder.value`]=new de("float32",[],s)}}else if("falcon"===this.config.model_type){let n=[t*this.num_heads,0,this.dim_kv]
|
|
2376
|
+
for(let t=0;t<this.num_layers;++t){e[`past_key_values.${t}.key`]=new de("float32",[],n)
|
|
2377
|
+
e[`past_key_values.${t}.value`]=new de("float32",[],n)}}else if(this.config.multi_query){let n=[t*this.num_heads,0,2*this.dim_kv]
|
|
2378
|
+
for(let t=0;t<this.num_layers;++t)e[`past_key_values.${t}.key_value`]=new de("float32",[],n)}else if("bloom"===this.config.model_type){let n=[t*this.num_heads,this.dim_kv,0]
|
|
2379
|
+
let s=[t*this.num_heads,0,this.dim_kv]
|
|
2380
|
+
for(let t=0;t<this.num_layers;++t){e[`past_key_values.${t}.key`]=new de("float32",[],n)
|
|
2381
|
+
e[`past_key_values.${t}.value`]=new de("float32",[],s)}}else{let n=[t,this.num_heads,0,this.dim_kv]
|
|
2382
|
+
for(let t=0;t<this.num_layers;++t){e[`past_key_values.${t}.key`]=new de("float32",[],n)
|
|
2383
|
+
e[`past_key_values.${t}.value`]=new de("float32",[],n)}}}}getStartBeams(e,t,n,s){return this._getStartBeams(this,e,t,n,s)}async runBeam(e){return await this._runBeam(this,e)}updateBeam(e,t){return this._updateBeam(e,t)}}class mr{}class gr extends mr{constructor({last_hidden_state:e,hidden_states:t=null,attentions:n=null}){super()
|
|
2384
|
+
this.last_hidden_state=e
|
|
2385
|
+
this.hidden_states=t
|
|
2386
|
+
this.attentions=n}}class wr extends fr{}class yr extends wr{}class xr extends wr{async _call(e){return new au(await super._call(e))}}class br extends wr{async _call(e){return new ru(await super._call(e))}}class vr extends wr{async _call(e){return new iu(await super._call(e))}}class kr extends wr{async _call(e){return new lu(await super._call(e))}}class Mr extends fr{}class Tr extends Mr{}class Er extends fr{}class Cr extends Er{}class Ar extends Er{async _call(e){return new au(await super._call(e))}}class Sr extends Er{async _call(e){return new ru(await super._call(e))}}class Fr extends Er{async _call(e){return new iu(await super._call(e))}}class zr extends Er{async _call(e){return new lu(await super._call(e))}}class Pr extends fr{}class Ir extends Pr{}class Lr extends Pr{async _call(e){return new au(await super._call(e))}}class Br extends Pr{async _call(e){return new ru(await super._call(e))}}class Or extends Pr{async _call(e){return new iu(await super._call(e))}}class Nr extends Pr{async _call(e){return new lu(await super._call(e))}}class $r extends fr{}class jr extends $r{}class Dr extends $r{async _call(e){return new au(await super._call(e))}}class Rr extends $r{async _call(e){return new ru(await super._call(e))}}class qr extends $r{async _call(e){return new iu(await super._call(e))}}class Ur extends $r{async _call(e){return new lu(await super._call(e))}}class Vr extends fr{}class Gr extends Vr{}class Wr extends Vr{async _call(e){return new au(await super._call(e))}}class Xr extends Vr{async _call(e){return new ru(await super._call(e))}}class Hr extends Vr{async _call(e){return new iu(await super._call(e))}}class Qr extends Vr{async _call(e){return new lu(await super._call(e))}}class Kr extends fr{}class Yr extends Kr{}class Jr extends Kr{async _call(e){return new au(await super._call(e))}}class Zr extends Kr{async _call(e){return new ru(await super._call(e))}}class eo extends Kr{async _call(e){return new iu(await super._call(e))}}class to extends Kr{async _call(e){return new lu(await super._call(e))}}class no extends fr{}class so extends no{}class ro extends no{async _call(e){return new au(await super._call(e))}}class oo extends no{async _call(e){return new ru(await super._call(e))}}class io extends no{async _call(e){return new iu(await super._call(e))}}class ao extends no{async _call(e){return new lu(await super._call(e))}}class lo extends fr{}class co extends lo{}class uo extends lo{async _call(e){return new ru(await super._call(e))}}class ho extends lo{async _call(e){return new iu(await super._call(e))}}class _o extends lo{async _call(e){return new lu(await super._call(e))}}class po extends lo{async _call(e){return new au(await super._call(e))}}class fo extends fr{}class mo extends fo{}class go extends fo{async _call(e){return new au(await super._call(e))}}class wo extends fo{async _call(e){return new ru(await super._call(e))}}class yo extends fo{async _call(e){return new iu(await super._call(e))}}class xo extends fr{}class bo extends xo{}class vo extends xo{async _call(e){return new au(await super._call(e))}}class ko extends xo{async _call(e){return new ru(await super._call(e))}}class Mo extends xo{async _call(e){return new lu(await super._call(e))}}class To extends fr{}class Eo extends To{}class Co extends To{async _call(e){return new au(await super._call(e))}}class Ao extends To{async _call(e){return new ru(await super._call(e))}}class So extends To{async _call(e){return new iu(await super._call(e))}}class Fo extends To{async _call(e){return new lu(await super._call(e))}}class zo extends fr{}class Po extends zo{}class Io extends zo{async _call(e){return new au(await super._call(e))}}class Lo extends zo{async _call(e){return new ru(await super._call(e))}}class Bo extends zo{async _call(e){return new lu(await super._call(e))}}class Oo extends fr{}class No extends Oo{}class $o extends Oo{async _call(e){return new ru(await super._call(e))}}class jo extends Oo{async _call(e){return new lu(await super._call(e))}}class Do extends Oo{async _call(e){return new au(await super._call(e))}}class Ro extends fr{}class qo extends Ro{}class Uo extends Ro{constructor(e,t,n,s){super(e,t)
|
|
2387
|
+
this.decoder_merged_session=n
|
|
2388
|
+
this.generation_config=s
|
|
2389
|
+
this.num_decoder_layers=this.config.num_decoder_layers
|
|
2390
|
+
this.num_decoder_heads=this.config.num_heads
|
|
2391
|
+
this.decoder_dim_kv=this.config.d_kv
|
|
2392
|
+
this.num_encoder_layers=this.config.num_layers
|
|
2393
|
+
this.num_encoder_heads=this.config.num_heads
|
|
2394
|
+
this.encoder_dim_kv=this.config.d_kv}}class Vo extends fr{}class Go extends Vo{}class Wo extends Vo{constructor(e,t,n,s){super(e,t)
|
|
2395
|
+
this.decoder_merged_session=n
|
|
2396
|
+
this.generation_config=s
|
|
2397
|
+
this.num_decoder_layers=this.config.num_decoder_layers
|
|
2398
|
+
this.num_decoder_heads=this.config.num_heads
|
|
2399
|
+
this.decoder_dim_kv=this.config.d_kv
|
|
2400
|
+
this.num_encoder_layers=this.config.num_layers
|
|
2401
|
+
this.num_encoder_heads=this.config.num_heads
|
|
2402
|
+
this.encoder_dim_kv=this.config.d_kv}}class Xo extends fr{}class Ho extends Xo{}class Qo extends Xo{constructor(e,t,n,s){super(e,t)
|
|
2403
|
+
this.decoder_merged_session=n
|
|
2404
|
+
this.generation_config=s
|
|
2405
|
+
this.num_decoder_layers=this.config.num_decoder_layers
|
|
2406
|
+
this.num_decoder_heads=this.config.num_heads
|
|
2407
|
+
this.decoder_dim_kv=this.config.d_kv
|
|
2408
|
+
this.num_encoder_layers=this.config.num_layers
|
|
2409
|
+
this.num_encoder_heads=this.config.num_heads
|
|
2410
|
+
this.encoder_dim_kv=this.config.d_kv}}class Ko extends fr{}class Yo extends Ko{}class Jo extends Ko{constructor(e,t,n,s){super(e,t)
|
|
2411
|
+
this.decoder_merged_session=n
|
|
2412
|
+
this.generation_config=s
|
|
2413
|
+
this.num_decoder_layers=this.config.decoder_layers
|
|
2414
|
+
this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2415
|
+
this.decoder_dim_kv=this.config.d_model/this.num_decoder_heads
|
|
2416
|
+
this.num_encoder_layers=this.config.encoder_layers
|
|
2417
|
+
this.num_encoder_heads=this.config.encoder_attention_heads
|
|
2418
|
+
this.encoder_dim_kv=this.config.d_model/this.num_encoder_heads}}class Zo extends Ko{async _call(e){return new ru(await super._call(e))}}class ei extends fr{}class ti extends ei{}class ni extends ei{constructor(e,t,n,s){super(e,t)
|
|
2419
|
+
this.decoder_merged_session=n
|
|
2420
|
+
this.generation_config=s
|
|
2421
|
+
this.num_decoder_layers=this.config.decoder_layers
|
|
2422
|
+
this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2423
|
+
this.decoder_dim_kv=this.config.d_model/this.num_decoder_heads
|
|
2424
|
+
this.num_encoder_layers=this.config.encoder_layers
|
|
2425
|
+
this.num_encoder_heads=this.config.encoder_attention_heads
|
|
2426
|
+
this.encoder_dim_kv=this.config.d_model/this.num_encoder_heads}}class si extends ei{async _call(e){return new ru(await super._call(e))}}class ri extends ei{constructor(e,t,n){super(e,t)
|
|
2427
|
+
this.generation_config=n
|
|
2428
|
+
this.num_decoder_layers=this.config.decoder_layers
|
|
2429
|
+
this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2430
|
+
this.decoder_dim_kv=this.config.d_model/this.num_decoder_heads
|
|
2431
|
+
this.num_encoder_layers=this.config.encoder_layers
|
|
2432
|
+
this.num_encoder_heads=this.config.encoder_attention_heads
|
|
2433
|
+
this.encoder_dim_kv=this.config.d_model/this.num_encoder_heads}}class oi extends fr{}class ii extends oi{}class ai extends oi{constructor(e,t,n,s){super(e,t)
|
|
2434
|
+
this.decoder_merged_session=n
|
|
2435
|
+
this.generation_config=s
|
|
2436
|
+
this.num_decoder_layers=this.config.decoder_layers
|
|
2437
|
+
this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2438
|
+
this.decoder_dim_kv=this.config.d_model/this.num_decoder_heads
|
|
2439
|
+
this.num_encoder_layers=this.config.encoder_layers
|
|
2440
|
+
this.num_encoder_heads=this.config.encoder_attention_heads
|
|
2441
|
+
this.encoder_dim_kv=this.config.d_model/this.num_encoder_heads}}class li extends fr{}class ci extends li{}class di extends li{constructor(e,t,n,s){super(e,t)
|
|
2442
|
+
this.decoder_merged_session=n
|
|
2443
|
+
this.generation_config=s
|
|
2444
|
+
this.num_decoder_layers=this.config.decoder_layers
|
|
2445
|
+
this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2446
|
+
this.decoder_dim_kv=this.config.d_model/this.num_decoder_heads
|
|
2447
|
+
this.num_encoder_layers=this.config.encoder_layers
|
|
2448
|
+
this.num_encoder_heads=this.config.encoder_attention_heads
|
|
2449
|
+
this.encoder_dim_kv=this.config.d_model/this.num_encoder_heads}}class ui extends fr{}class hi extends ui{}class _i extends ui{async _call(e){return new au(await super._call(e))}}class pi extends ui{async _call(e){return new ru(await super._call(e))}}class fi extends ui{async _call(e){return new iu(await super._call(e))}}class mi extends ui{async _call(e){return new lu(await super._call(e))}}class gi extends fr{}class wi extends gi{}class yi extends gi{async _call(e){return new au(await super._call(e))}}class xi extends gi{async _call(e){return new ru(await super._call(e))}}class bi extends gi{async _call(e){return new iu(await super._call(e))}}class vi extends gi{async _call(e){return new lu(await super._call(e))}}class ki extends fr{}class Mi extends ki{}class Ti extends ki{async _call(e){return new au(await super._call(e))}}class Ei extends ki{async _call(e){return new ru(await super._call(e))}}class Ci extends ki{async _call(e){return new iu(await super._call(e))}}class Ai extends ki{async _call(e){return new lu(await super._call(e))}}class Si extends fr{}class Fi extends Si{}class zi extends Si{}class Pi extends fr{}class Ii extends Pi{}class Li extends Pi{requires_attention_mask=!1
|
|
2450
|
+
main_input_name="input_features"
|
|
2451
|
+
constructor(e,t,n,s){super(e,t)
|
|
2452
|
+
this.decoder_merged_session=n
|
|
2453
|
+
this.generation_config=s
|
|
2454
|
+
this.num_decoder_layers=this.config.decoder_layers
|
|
2455
|
+
this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2456
|
+
this.decoder_dim_kv=this.config.d_model/this.num_decoder_heads
|
|
2457
|
+
this.num_encoder_layers=this.config.encoder_layers
|
|
2458
|
+
this.num_encoder_heads=this.config.encoder_attention_heads
|
|
2459
|
+
this.encoder_dim_kv=this.config.d_model/this.num_encoder_heads}async generate(e,t=null,n=null){var s
|
|
2460
|
+
null!=(s=t=this._get_generation_config(t)).return_timestamps||(s.return_timestamps=!1)
|
|
2461
|
+
t.return_timestamps&&(n=[new Os(t)])
|
|
2462
|
+
if(t.return_token_timestamps){t.output_attentions=!0
|
|
2463
|
+
t.return_dict_in_generate=!0
|
|
2464
|
+
"translate"===t.task&&console.warn("Token-level timestamps may not be reliable for task 'translate'.")
|
|
2465
|
+
if(!t.alignment_heads)throw new Error("Model generation config has no `alignment_heads`, token-level timestamps not available. See https://gist.github.com/hollance/42e32852f24243b748ae6bc1f985b13a on how to add this property to the generation config.")}const r=await super.generate(e,t,n)
|
|
2466
|
+
t.return_token_timestamps&&t.alignment_heads&&(r.token_timestamps=this._extract_token_timestamps(r,t.alignment_heads,t.num_frames))
|
|
2467
|
+
return r}_extract_token_timestamps(e,t,n=null,s=.02){if(!e.cross_attentions)throw new Error("Model outputs must contain cross attentions to extract timestamps. This is most likely because the model was not exported with `output_attentions=True`.")
|
|
2468
|
+
let r=this.config.median_filter_width
|
|
2469
|
+
if(void 0===r){console.warn("Model config has no `median_filter_width`, using default value of 7.")
|
|
2470
|
+
r=7}const o=e.cross_attentions.map((e=>{let s=Array.from({length:this.config.decoder_layers},((t,n)=>we(e.map((e=>e[n])),2)))
|
|
2471
|
+
let o=ye(t.map((([e,t])=>n?s[e].slice(null,t,null,[0,n]):s[e].slice(null,t))))
|
|
2472
|
+
o=o.transpose(1,0,2,3)
|
|
2473
|
+
let[i,a]=xe(o,-2,0,!0)
|
|
2474
|
+
let l=o.clone()
|
|
2475
|
+
for(let e=0;e<l.dims[0];++e){let t=l[e]
|
|
2476
|
+
for(let n=0;n<t.dims[0];++n){let s=t[n]
|
|
2477
|
+
const o=i[e][n][0]
|
|
2478
|
+
const l=a[e][n][0]
|
|
2479
|
+
for(let e=0;e<s.dims[0];++e){let t=s[e]
|
|
2480
|
+
for(let e=0;e<t.data.length;++e)t.data[e]=(t.data[e]-l.data[e])/o.data[e]
|
|
2481
|
+
t.data.set(oe(t.data,r))}}}return be(l,1)}))
|
|
2482
|
+
const i=[e.sequences.length,e.sequences[0].length]
|
|
2483
|
+
const a=new de("float32",new Float32Array(i[0]*i[1]),i)
|
|
2484
|
+
for(let e=0;e<i[0];++e){const t=o[e].neg().squeeze_(0)
|
|
2485
|
+
let[n,r]=ve(t)
|
|
2486
|
+
let i=c([1],Array.from({length:n.length-1},((e,t)=>n[t+1]-n[t]))).map((e=>!!e))
|
|
2487
|
+
let l=[]
|
|
2488
|
+
for(let e=0;e<i.length;++e)i[e]&&l.push(r[e]*s)
|
|
2489
|
+
a[e].data.set(l,1)}return a}}class Bi extends fr{main_input_name="pixel_values"
|
|
2490
|
+
constructor(e,t,n,s){var r
|
|
2491
|
+
super(e,t)
|
|
2492
|
+
this.decoder_merged_session=n
|
|
2493
|
+
this.generation_config=s
|
|
2494
|
+
const o=this.config.encoder
|
|
2495
|
+
const i=this.config.decoder
|
|
2496
|
+
const a=o.model_type;(null!=(r=td.get(a))?r:nd.get(a))||console.warn(`Model type for encoder '${a}' not found, assuming encoder-only architecture. Please report this at https://github.com/xenova/transformers.js/issues/new/choose.`)
|
|
2497
|
+
const l=dd.get(i.model_type)
|
|
2498
|
+
if(!l)throw new Error(`Unable to construct \`VisionEncoderDecoder\` due to unsupported decoder: "${this.config.decoder.model_type}"`)
|
|
2499
|
+
const c=new(0,l[1])(i,n,s)
|
|
2500
|
+
this.add_encoder_pkv="num_decoder_layers"in c
|
|
2501
|
+
if(this.add_encoder_pkv){this.num_decoder_layers=c.num_decoder_layers
|
|
2502
|
+
this.num_decoder_heads=c.num_decoder_heads
|
|
2503
|
+
this.decoder_dim_kv=c.decoder_dim_kv
|
|
2504
|
+
this.num_encoder_layers=c.num_encoder_layers
|
|
2505
|
+
this.num_encoder_heads=c.num_encoder_heads
|
|
2506
|
+
this.encoder_dim_kv=c.encoder_dim_kv}else{this.num_layers=c.num_layers
|
|
2507
|
+
this.num_heads=c.num_heads
|
|
2508
|
+
this.dim_kv=c.dim_kv}}}class Oi extends fr{}class Ni extends Oi{}class $i extends Oi{static async from_pretrained(e,t={}){null!=t.model_file_name||(t.model_file_name="text_model")
|
|
2509
|
+
return super.from_pretrained(e,t)}}class ji extends Oi{static async from_pretrained(e,t={}){null!=t.model_file_name||(t.model_file_name="vision_model")
|
|
2510
|
+
return super.from_pretrained(e,t)}}class Di extends fr{}class Ri extends Di{}class qi extends Di{static async from_pretrained(e,t={}){null!=t.model_file_name||(t.model_file_name="text_model")
|
|
2511
|
+
return super.from_pretrained(e,t)}}class Ui extends Oi{static async from_pretrained(e,t={}){null!=t.model_file_name||(t.model_file_name="vision_model")
|
|
2512
|
+
return super.from_pretrained(e,t)}}class Vi extends fr{}class Gi extends Vi{}class Wi extends fr{}class Xi extends Wi{}class Hi extends Wi{}class Qi extends fr{constructor(e,t,n){super(e,t)
|
|
2513
|
+
this.generation_config=n
|
|
2514
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2515
|
+
this.num_heads=this.config.n_head
|
|
2516
|
+
this.num_layers=this.config.n_layer
|
|
2517
|
+
this.dim_kv=this.config.n_embd/this.num_heads}}class Ki extends Qi{}class Yi extends Qi{}class Ji extends fr{constructor(e,t,n){super(e,t)
|
|
2518
|
+
this.generation_config=n
|
|
2519
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2520
|
+
this.num_heads=this.config.num_heads
|
|
2521
|
+
this.num_layers=this.config.num_layers
|
|
2522
|
+
this.dim_kv=this.config.hidden_size/this.num_heads}}class Zi extends Ji{}class ea extends Ji{}class ta extends fr{constructor(e,t,n){super(e,t)
|
|
2523
|
+
this.generation_config=n
|
|
2524
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2525
|
+
this.num_heads=this.config.num_attention_heads
|
|
2526
|
+
this.num_layers=this.config.num_hidden_layers
|
|
2527
|
+
this.dim_kv=this.config.hidden_size/this.num_heads}}class na extends ta{}class sa extends ta{}class ra extends fr{constructor(e,t,n){super(e,t)
|
|
2528
|
+
this.generation_config=n
|
|
2529
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2530
|
+
this.num_heads=this.config.n_head
|
|
2531
|
+
this.num_layers=this.config.n_layer
|
|
2532
|
+
this.dim_kv=this.config.n_embd/this.num_heads}}class oa extends ra{}class ia extends ra{}class aa extends fr{constructor(e,t,n){super(e,t)
|
|
2533
|
+
this.generation_config=n
|
|
2534
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2535
|
+
this.num_heads=this.config.n_head
|
|
2536
|
+
this.num_layers=this.config.n_layer
|
|
2537
|
+
this.dim_kv=this.config.n_embd/this.num_heads}}class la extends aa{}class ca extends aa{}class da extends fr{constructor(e,t,n){super(e,t)
|
|
2538
|
+
this.generation_config=n
|
|
2539
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2540
|
+
this.num_heads=this.config.n_head
|
|
2541
|
+
this.num_layers=this.config.n_layer
|
|
2542
|
+
this.dim_kv=this.config.n_embd/this.num_heads}}class ua extends da{}class ha extends da{}class _a extends fr{constructor(e,t,n){var s
|
|
2543
|
+
super(e,t)
|
|
2544
|
+
this.generation_config=n
|
|
2545
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2546
|
+
this.num_heads=null!=(s=this.config.num_key_value_heads)?s:this.config.num_attention_heads
|
|
2547
|
+
this.num_layers=this.config.num_hidden_layers
|
|
2548
|
+
this.dim_kv=this.config.hidden_size/this.config.num_attention_heads}}class pa extends _a{}class fa extends _a{}class ma extends fr{constructor(e,t,n){var s
|
|
2549
|
+
super(e,t)
|
|
2550
|
+
this.generation_config=n
|
|
2551
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2552
|
+
this.num_heads=null!=(s=this.config.num_key_value_heads)?s:this.config.num_attention_heads
|
|
2553
|
+
this.num_layers=this.config.num_hidden_layers
|
|
2554
|
+
this.dim_kv=this.config.hidden_size/this.config.num_attention_heads}}class ga extends ma{}class wa extends ma{}class ya extends fr{constructor(e,t,n){super(e,t)
|
|
2555
|
+
this.generation_config=n
|
|
2556
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2557
|
+
this.num_heads=this.config.num_attention_heads
|
|
2558
|
+
this.num_layers=this.config.num_hidden_layers
|
|
2559
|
+
this.dim_kv=this.config.hidden_size/this.num_heads}}class xa extends ya{}class ba extends ya{}class va extends fr{constructor(e,t,n){super(e,t)
|
|
2560
|
+
this.generation_config=n
|
|
2561
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2562
|
+
this.num_heads=this.config.n_head
|
|
2563
|
+
this.num_layers=this.config.n_layer
|
|
2564
|
+
this.dim_kv=this.config.hidden_size/this.num_heads}}class ka extends va{}class Ma extends va{}class Ta extends fr{constructor(e,t,n){super(e,t)
|
|
2565
|
+
this.generation_config=n
|
|
2566
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2567
|
+
this.num_heads=this.config.n_heads
|
|
2568
|
+
this.num_layers=this.config.n_layers
|
|
2569
|
+
this.dim_kv=this.config.d_model/this.num_heads}}class Ea extends Ta{}class Ca extends Ta{}class Aa extends fr{constructor(e,t,n){super(e,t)
|
|
2570
|
+
this.generation_config=n
|
|
2571
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2572
|
+
this.num_heads=this.config.num_attention_heads
|
|
2573
|
+
this.num_layers=this.config.num_hidden_layers
|
|
2574
|
+
this.dim_kv=this.config.hidden_size/this.num_heads}}class Sa extends Aa{}class Fa extends Aa{}class za extends fr{}class Pa extends za{}class Ia extends za{async _call(e){return new ru(await super._call(e))}}class La extends fr{}class Ba extends La{async _call(e){return new uu(await super._call(e))}}class Oa extends fr{}class Na extends Oa{}class $a extends Oa{async _call(e){return new ru(await super._call(e))}}class ja extends fr{}class Da extends ja{}class Ra extends ja{}class qa extends fr{}class Ua extends qa{}class Va extends qa{}class Ga extends fr{}class Wa extends Ga{}class Xa extends Ga{async _call(e){return new ru(await super._call(e))}}class Ha extends fr{}class Qa extends Ha{}class Ka extends Ha{async _call(e){return new Ja(await super._call(e))}}class Ya extends Ha{async _call(e){return new Za(await super._call(e))}}class Ja extends mr{constructor({logits:e,pred_boxes:t}){super()
|
|
2575
|
+
this.logits=e
|
|
2576
|
+
this.pred_boxes=t}}class Za extends mr{constructor({logits:e,pred_boxes:t,pred_masks:n}){super()
|
|
2577
|
+
this.logits=e
|
|
2578
|
+
this.pred_boxes=t
|
|
2579
|
+
this.pred_masks=n}}class el extends fr{}class tl extends el{}class nl extends el{async _call(e){return new sl(await super._call(e))}}class sl extends Ja{}class rl extends fr{}class ol extends rl{}class il extends rl{async _call(e){return new ru(await super._call(e))}}class al extends fr{}class ll extends al{}class cl extends al{async _call(e){return new ru(await super._call(e))}}class dl extends fr{}class ul extends dl{}class hl extends dl{async _call(e){return new ru(await super._call(e))}}class _l extends fr{}class pl extends _l{}class fl extends _l{}class ml extends fr{}class gl extends ml{}class wl extends ml{}class yl extends fr{}class xl extends yl{}class bl extends fr{}class vl extends bl{}class kl extends bl{}class Ml extends fr{}class Tl extends Ml{}class El extends fr{}class Cl extends El{}class Al extends El{async _call(e){return new ru(await super._call(e))}}class Sl extends fr{}class Fl extends Sl{}class zl extends Sl{async _call(e){return new ru(await super._call(e))}}class Pl extends fr{}class Il extends Pl{}class Ll extends Pl{async _call(e){return new ru(await super._call(e))}}class Bl extends fr{}class Ol extends Bl{}class Nl extends Bl{async _call(e){return new $l(await super._call(e))}}class $l extends mr{constructor({logits:e,pred_boxes:t}){super()
|
|
2580
|
+
this.logits=e
|
|
2581
|
+
this.pred_boxes=t}}class jl extends fr{}class Dl extends jl{constructor(e,t,n){super(e,t)
|
|
2582
|
+
this.prompt_encoder_mask_decoder=n}async get_image_embeddings({pixel_values:e}){return await dr(this,{pixel_values:e})}async forward(e){e.image_embeddings&&e.image_positional_embeddings||(e={...e,...await this.get_image_embeddings(e)})
|
|
2583
|
+
if(!e.input_labels){const t=e.input_points.dims.slice(0,-1)
|
|
2584
|
+
const n=t.reduce(((e,t)=>e*t),1)
|
|
2585
|
+
e.input_labels=new de("int64",new BigInt64Array(n).fill(1n),t)}return await er(this.prompt_encoder_mask_decoder,{input_points:e.input_points,input_labels:e.input_labels,image_embeddings:e.image_embeddings,image_positional_embeddings:e.image_positional_embeddings})}async _call(e){return new Rl(await super._call(e))}}class Rl extends mr{constructor({iou_scores:e,pred_masks:t}){super()
|
|
2586
|
+
this.iou_scores=e
|
|
2587
|
+
this.pred_masks=t}}class ql extends fr{}class Ul extends ql{}class Vl extends ql{constructor(e,t,n,s){super(e,t)
|
|
2588
|
+
this.decoder_merged_session=n
|
|
2589
|
+
this.generation_config=s
|
|
2590
|
+
this.num_decoder_layers=this.config.decoder_layers
|
|
2591
|
+
this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2592
|
+
this.decoder_dim_kv=this.config.d_model/this.num_decoder_heads
|
|
2593
|
+
this.num_encoder_layers=this.config.encoder_layers
|
|
2594
|
+
this.num_encoder_heads=this.config.encoder_attention_heads
|
|
2595
|
+
this.encoder_dim_kv=this.config.d_model/this.num_encoder_heads}}class Gl extends fr{}class Wl extends Gl{}class Xl extends Gl{constructor(e,t,n,s){super(e,t)
|
|
2596
|
+
this.decoder_merged_session=n
|
|
2597
|
+
this.generation_config=s
|
|
2598
|
+
this.num_decoder_layers=this.config.decoder_layers
|
|
2599
|
+
this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2600
|
+
this.decoder_dim_kv=this.config.d_model/this.num_decoder_heads
|
|
2601
|
+
this.num_encoder_layers=this.config.encoder_layers
|
|
2602
|
+
this.num_encoder_heads=this.config.encoder_attention_heads
|
|
2603
|
+
this.encoder_dim_kv=this.config.d_model/this.num_encoder_heads}}class Hl extends fr{}class Ql extends Hl{}class Kl extends Hl{async _call(e){return new cu(await super._call(e))}}class Yl extends Hl{async _call(e){return new ru(await super._call(e))}}class Jl extends Hl{async _call(e){return new iu(await super._call(e))}}class Zl extends fr{}class ec extends Zl{}class tc extends Zl{async _call(e){return new cu(await super._call(e))}}class nc extends Zl{async _call(e){return new ru(await super._call(e))}}class sc extends fr{}class rc extends sc{}class oc extends sc{async _call(e){return new cu(await super._call(e))}}class ic extends sc{async _call(e){return new ru(await super._call(e))}}class ac extends sc{async _call(e){return new iu(await super._call(e))}}class lc extends fr{}class cc extends lc{}class dc extends lc{async _call(e){return new cu(await super._call(e))}}class uc extends lc{async _call(e){return new ru(await super._call(e))}}class hc extends fr{}class _c extends Hl{}class pc extends Hl{async _call(e){return new cu(await super._call(e))}}class fc extends Hl{async _call(e){return new ru(await super._call(e))}}class mc extends fr{}class gc extends mc{}class wc extends mc{async _call(e){return new cu(await super._call(e))}}class yc extends mc{async _call(e){return new ru(await super._call(e))}}class xc extends mc{async _call(e){return new ou(await super._call(e))}}class bc extends mc{async _call(e){return new iu(await super._call(e))}}class vc extends fr{}class kc extends vc{}class Mc extends vc{}class Tc extends vc{constructor(e,t,n,s){super(e,t)
|
|
2604
|
+
this.decoder_merged_session=n
|
|
2605
|
+
this.generation_config=s
|
|
2606
|
+
this.num_decoder_layers=this.config.decoder_layers
|
|
2607
|
+
this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2608
|
+
this.decoder_dim_kv=this.config.hidden_size/this.num_decoder_heads
|
|
2609
|
+
this.num_encoder_layers=this.config.encoder_layers
|
|
2610
|
+
this.num_encoder_heads=this.config.encoder_attention_heads
|
|
2611
|
+
this.encoder_dim_kv=this.config.hidden_size/this.num_encoder_heads}async generate_speech(e,t,{threshold:n=.5,minlenratio:s=0,maxlenratio:r=20,vocoder:o=null}={}){const i={input_ids:e}
|
|
2612
|
+
const{encoder_outputs:a,encoder_attention_mask:l}=await dr(this,i)
|
|
2613
|
+
const c=a.dims[1]/this.config.reduction_factor
|
|
2614
|
+
const d=Math.floor(c*r)
|
|
2615
|
+
const u=Math.floor(c*s)
|
|
2616
|
+
const h=this.config.num_mel_bins
|
|
2617
|
+
let _=[]
|
|
2618
|
+
let p=null
|
|
2619
|
+
let f=null
|
|
2620
|
+
let m=0
|
|
2621
|
+
for(;;){++m
|
|
2622
|
+
const e=or(!!f)
|
|
2623
|
+
let s
|
|
2624
|
+
s=f?f.output_sequence_out:new de("float32",new Float32Array(h),[1,1,h])
|
|
2625
|
+
let r={use_cache_branch:e,output_sequence:s,encoder_attention_mask:l,speaker_embeddings:t,encoder_hidden_states:a}
|
|
2626
|
+
this.addPastKeyValues(r,p)
|
|
2627
|
+
f=await er(this.decoder_merged_session,r)
|
|
2628
|
+
p=this.getPastKeyValues(f,p)
|
|
2629
|
+
const{prob:o,spectrum:i}=f
|
|
2630
|
+
_.push(i)
|
|
2631
|
+
if(m>=u&&(Array.from(o.data).filter((e=>e>=n)).length>0||m>=d))break}const g=we(_)
|
|
2632
|
+
const{waveform:w}=await er(o.session,{spectrogram:g})
|
|
2633
|
+
return{spectrogram:g,waveform:w}}}class Ec extends fr{main_input_name="spectrogram"}class Cc extends fr{constructor(e,t,n){super(e,t)
|
|
2634
|
+
this.generation_config=n
|
|
2635
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2636
|
+
this.num_encoder_layers=this.num_decoder_layers=this.config.decoder_layers
|
|
2637
|
+
this.num_encoder_heads=this.num_decoder_heads=this.config.decoder_attention_heads
|
|
2638
|
+
this.encoder_dim_kv=this.decoder_dim_kv=this.config.d_model/this.num_decoder_heads}}class Ac extends Cc{}class Sc extends fr{constructor(e,t,n){super(e,t)
|
|
2639
|
+
this.generation_config=n
|
|
2640
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2641
|
+
this.num_heads=this.config.num_key_value_heads
|
|
2642
|
+
this.num_layers=this.config.num_hidden_layers
|
|
2643
|
+
this.dim_kv=this.config.hidden_size/this.config.num_attention_heads}}class Fc extends Sc{}class zc extends Sc{}class Pc extends fr{constructor(e,t,n){super(e,t)
|
|
2644
|
+
this.generation_config=n
|
|
2645
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2646
|
+
this.num_heads=this.config.num_key_value_heads
|
|
2647
|
+
this.num_layers=this.config.num_hidden_layers
|
|
2648
|
+
this.dim_kv=this.config.hidden_size/this.config.num_attention_heads}}class Ic extends Pc{}class Lc extends Pc{}class Bc extends fr{constructor(e,t,n){super(e,t)
|
|
2649
|
+
this.generation_config=n
|
|
2650
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2651
|
+
this.num_heads=this.config.num_attention_heads
|
|
2652
|
+
this.num_layers=this.config.num_hidden_layers
|
|
2653
|
+
this.dim_kv=this.config.hidden_size/this.config.num_attention_heads}}class Oc extends Bc{}class Nc extends Bc{}class $c extends fr{}class jc extends $c{}class Dc extends $c{static async from_pretrained(e,t={}){null!=t.model_file_name||(t.model_file_name="text_model")
|
|
2654
|
+
return super.from_pretrained(e,t)}}class Rc extends $c{static async from_pretrained(e,t={}){null!=t.model_file_name||(t.model_file_name="audio_model")
|
|
2655
|
+
return super.from_pretrained(e,t)}}class qc extends fr{}class Uc extends qc{async _call(e){return new hu(await super._call(e))}}class Vc extends fr{}class Gc extends Vc{}class Wc extends Vc{}class Xc extends Vc{}class Hc extends fr{constructor(e,t,n){super(e,t)
|
|
2656
|
+
this.generation_config=n
|
|
2657
|
+
this.config.pad_token_id=this.config.eos_token_id
|
|
2658
|
+
this.num_heads=this.config.num_attention_heads
|
|
2659
|
+
this.num_layers=this.config.num_hidden_layers
|
|
2660
|
+
this.dim_kv=this.config.hidden_size/this.num_heads}}class Qc extends Hc{}class Kc extends Hc{}class Yc extends fr{}class Jc extends Yc{}class Zc extends Yc{async _call(e){return new ru(await super._call(e))}}class ed{static MODEL_CLASS_MAPPINGS=null
|
|
2661
|
+
static BASE_IF_FAIL=!1
|
|
2662
|
+
static async from_pretrained(e,{quantized:t=!0,progress_callback:n=null,config:s=null,cache_dir:r=null,local_files_only:o=!1,revision:i="main",model_file_name:a=null}={}){let l={quantized:t,progress_callback:n,config:s,cache_dir:r,local_files_only:o,revision:i,model_file_name:a}
|
|
2663
|
+
s=await Ss.from_pretrained(e,l)
|
|
2664
|
+
l.config||(l.config=s)
|
|
2665
|
+
if(!this.MODEL_CLASS_MAPPINGS)throw new Error("`MODEL_CLASS_MAPPINGS` not implemented for this type of `AutoClass`: "+this.name)
|
|
2666
|
+
for(let t of this.MODEL_CLASS_MAPPINGS){const n=t.get(s.model_type)
|
|
2667
|
+
if(n)return await n[1].from_pretrained(e,l)}if(this.BASE_IF_FAIL){console.warn(`Unknown model class "${s.model_type}", attempting to construct from base class.`)
|
|
2668
|
+
return await fr.from_pretrained(e,l)}throw Error(`Unsupported model type: ${s.model_type}`)}}const td=new Map([["bert",["BertModel",yr]],["nomic_bert",["NomicBertModel",Tr]],["roformer",["RoFormerModel",Cr]],["electra",["ElectraModel",jr]],["esm",["EsmModel",mo]],["convbert",["ConvBertModel",Ir]],["camembert",["CamembertModel",Gr]],["deberta",["DebertaModel",Yr]],["deberta-v2",["DebertaV2Model",so]],["mpnet",["MPNetModel",Eo]],["albert",["AlbertModel",No]],["distilbert",["DistilBertModel",co]],["roberta",["RobertaModel",hi]],["xlm",["XLMModel",wi]],["xlm-roberta",["XLMRobertaModel",Mi]],["clap",["ClapModel",jc]],["clip",["CLIPModel",Ni]],["clipseg",["CLIPSegModel",Xi]],["chinese_clip",["ChineseCLIPModel",Gi]],["siglip",["SiglipModel",Ri]],["mobilebert",["MobileBertModel",bo]],["squeezebert",["SqueezeBertModel",Po]],["wav2vec2",["Wav2Vec2Model",Ql]],["wav2vec2-bert",["Wav2Vec2BertModel",cc]],["unispeech",["UniSpeechModel",ec]],["unispeech-sat",["UniSpeechSatModel",rc]],["hubert",["HubertModel",_c]],["wavlm",["WavLMModel",gc]],["audio-spectrogram-transformer",["ASTModel",Fi]],["vits",["VitsModel",Uc]],["detr",["DetrModel",Qa]],["table-transformer",["TableTransformerModel",tl]],["vit",["ViTModel",Pa]],["mobilevit",["MobileViTModel",Na]],["owlvit",["OwlViTModel",Da]],["owlv2",["Owlv2Model",Ua]],["beit",["BeitModel",Wa]],["deit",["DeiTModel",ol]],["convnext",["ConvNextModel",Cl]],["convnextv2",["ConvNextV2Model",Fl]],["dinov2",["Dinov2Model",Il]],["resnet",["ResNetModel",ll]],["swin",["SwinModel",ul]],["swin2sr",["Swin2SRModel",pl]],["donut-swin",["DonutSwinModel",Tl]],["yolos",["YolosModel",Ol]],["dpt",["DPTModel",gl]],["glpn",["GLPNModel",vl]],["hifigan",["SpeechT5HifiGan",Ec]],["efficientnet",["EfficientNetModel",Jc]]])
|
|
2669
|
+
const nd=new Map([["t5",["T5Model",qo]],["longt5",["LongT5Model",Go]],["mt5",["MT5Model",Ho]],["bart",["BartModel",Yo]],["mbart",["MBartModel",ti]],["marian",["MarianModel",Ul]],["whisper",["WhisperModel",Ii]],["m2m_100",["M2M100Model",Wl]],["blenderbot",["BlenderbotModel",ii]],["blenderbot-small",["BlenderbotSmallModel",ci]]])
|
|
2670
|
+
const sd=new Map([["bloom",["BloomModel",ka]],["gpt2",["GPT2Model",Ki]],["gptj",["GPTJModel",oa]],["gpt_bigcode",["GPTBigCodeModel",la]],["gpt_neo",["GPTNeoModel",Zi]],["gpt_neox",["GPTNeoXModel",na]],["codegen",["CodeGenModel",ua]],["llama",["LlamaModel",pa]],["qwen2",["Qwen2Model",ga]],["phi",["PhiModel",xa]],["mpt",["MptModel",Ea]],["opt",["OPTModel",Sa]],["mistral",["MistralModel",Fc]],["starcoder2",["Starcoder2Model",Ic]],["falcon",["FalconModel",Oc]]])
|
|
2671
|
+
const rd=new Map([["speecht5",["SpeechT5ForSpeechToText",Mc]],["whisper",["WhisperForConditionalGeneration",Li]]])
|
|
2672
|
+
const od=new Map([["speecht5",["SpeechT5ForTextToSpeech",Tc]]])
|
|
2673
|
+
const id=new Map([["vits",["VitsModel",Uc]]])
|
|
2674
|
+
const ad=new Map([["bert",["BertForSequenceClassification",br]],["roformer",["RoFormerForSequenceClassification",Sr]],["electra",["ElectraForSequenceClassification",Rr]],["esm",["EsmForSequenceClassification",wo]],["convbert",["ConvBertForSequenceClassification",Br]],["camembert",["CamembertForSequenceClassification",Xr]],["deberta",["DebertaForSequenceClassification",Zr]],["deberta-v2",["DebertaV2ForSequenceClassification",oo]],["mpnet",["MPNetForSequenceClassification",Ao]],["albert",["AlbertForSequenceClassification",$o]],["distilbert",["DistilBertForSequenceClassification",uo]],["roberta",["RobertaForSequenceClassification",pi]],["xlm",["XLMForSequenceClassification",xi]],["xlm-roberta",["XLMRobertaForSequenceClassification",Ei]],["bart",["BartForSequenceClassification",Zo]],["mbart",["MBartForSequenceClassification",si]],["mobilebert",["MobileBertForSequenceClassification",ko]],["squeezebert",["SqueezeBertForSequenceClassification",Lo]]])
|
|
2675
|
+
const ld=new Map([["bert",["BertForTokenClassification",vr]],["roformer",["RoFormerForTokenClassification",Fr]],["electra",["ElectraForTokenClassification",qr]],["esm",["EsmForTokenClassification",yo]],["convbert",["ConvBertForTokenClassification",Or]],["camembert",["CamembertForTokenClassification",Hr]],["deberta",["DebertaForTokenClassification",eo]],["deberta-v2",["DebertaV2ForTokenClassification",io]],["mpnet",["MPNetForTokenClassification",So]],["distilbert",["DistilBertForTokenClassification",ho]],["roberta",["RobertaForTokenClassification",fi]],["xlm",["XLMForTokenClassification",bi]],["xlm-roberta",["XLMRobertaForTokenClassification",Ci]]])
|
|
2676
|
+
const cd=new Map([["t5",["T5ForConditionalGeneration",Uo]],["longt5",["LongT5ForConditionalGeneration",Wo]],["mt5",["MT5ForConditionalGeneration",Qo]],["bart",["BartForConditionalGeneration",Jo]],["mbart",["MBartForConditionalGeneration",ni]],["marian",["MarianMTModel",Vl]],["m2m_100",["M2M100ForConditionalGeneration",Xl]],["blenderbot",["BlenderbotForConditionalGeneration",ai]],["blenderbot-small",["BlenderbotSmallForConditionalGeneration",di]]])
|
|
2677
|
+
const dd=new Map([["bloom",["BloomForCausalLM",Ma]],["gpt2",["GPT2LMHeadModel",Yi]],["gptj",["GPTJForCausalLM",ia]],["gpt_bigcode",["GPTBigCodeForCausalLM",ca]],["gpt_neo",["GPTNeoForCausalLM",ea]],["gpt_neox",["GPTNeoXForCausalLM",sa]],["codegen",["CodeGenForCausalLM",ha]],["llama",["LlamaForCausalLM",fa]],["qwen2",["Qwen2ForCausalLM",wa]],["phi",["PhiForCausalLM",ba]],["mpt",["MptForCausalLM",Ca]],["opt",["OPTForCausalLM",Fa]],["mbart",["MBartForCausalLM",ri]],["mistral",["MistralForCausalLM",zc]],["starcoder2",["Starcoder2ForCausalLM",Lc]],["falcon",["FalconForCausalLM",Nc]],["trocr",["TrOCRForCausalLM",Ac]],["stablelm",["StableLmForCausalLM",Kc]]])
|
|
2678
|
+
const ud=new Map([["bert",["BertForMaskedLM",xr]],["roformer",["RoFormerForMaskedLM",Ar]],["electra",["ElectraForMaskedLM",Dr]],["esm",["EsmForMaskedLM",go]],["convbert",["ConvBertForMaskedLM",Lr]],["camembert",["CamembertForMaskedLM",Wr]],["deberta",["DebertaForMaskedLM",Jr]],["deberta-v2",["DebertaV2ForMaskedLM",ro]],["mpnet",["MPNetForMaskedLM",Co]],["albert",["AlbertForMaskedLM",Do]],["distilbert",["DistilBertForMaskedLM",po]],["roberta",["RobertaForMaskedLM",_i]],["xlm",["XLMWithLMHeadModel",yi]],["xlm-roberta",["XLMRobertaForMaskedLM",Ti]],["mobilebert",["MobileBertForMaskedLM",vo]],["squeezebert",["SqueezeBertForMaskedLM",Io]]])
|
|
2679
|
+
const hd=new Map([["bert",["BertForQuestionAnswering",kr]],["roformer",["RoFormerForQuestionAnswering",zr]],["electra",["ElectraForQuestionAnswering",Ur]],["convbert",["ConvBertForQuestionAnswering",Nr]],["camembert",["CamembertForQuestionAnswering",Qr]],["deberta",["DebertaForQuestionAnswering",to]],["deberta-v2",["DebertaV2ForQuestionAnswering",ao]],["mpnet",["MPNetForQuestionAnswering",Fo]],["albert",["AlbertForQuestionAnswering",jo]],["distilbert",["DistilBertForQuestionAnswering",_o]],["roberta",["RobertaForQuestionAnswering",mi]],["xlm",["XLMForQuestionAnswering",vi]],["xlm-roberta",["XLMRobertaForQuestionAnswering",Ai]],["mobilebert",["MobileBertForQuestionAnswering",Mo]],["squeezebert",["SqueezeBertForQuestionAnswering",Bo]]])
|
|
2680
|
+
const _d=new Map([["vision-encoder-decoder",["VisionEncoderDecoderModel",Bi]]])
|
|
2681
|
+
const pd=new Map([["vision-encoder-decoder",["VisionEncoderDecoderModel",Bi]]])
|
|
2682
|
+
const fd=new Map([["vit",["ViTForImageClassification",Ia]],["mobilevit",["MobileViTForImageClassification",$a]],["beit",["BeitForImageClassification",Xa]],["deit",["DeiTForImageClassification",il]],["convnext",["ConvNextForImageClassification",Al]],["convnextv2",["ConvNextV2ForImageClassification",zl]],["dinov2",["Dinov2ForImageClassification",Ll]],["resnet",["ResNetForImageClassification",cl]],["swin",["SwinForImageClassification",hl]],["segformer",["SegformerForImageClassification",Wc]],["efficientnet",["EfficientNetForImageClassification",Zc]]])
|
|
2683
|
+
const md=new Map([["detr",["DetrForObjectDetection",Ka]],["table-transformer",["TableTransformerForObjectDetection",nl]],["yolos",["YolosForObjectDetection",Nl]]])
|
|
2684
|
+
const gd=new Map([["owlvit",["OwlViTForObjectDetection",Ra]],["owlv2",["Owlv2ForObjectDetection",Va]]])
|
|
2685
|
+
const wd=new Map([["detr",["DetrForSegmentation",Ya]],["clipseg",["CLIPSegForImageSegmentation",Hi]]])
|
|
2686
|
+
const yd=new Map([["segformer",["SegformerForSemanticSegmentation",Xc]]])
|
|
2687
|
+
const xd=new Map([["sam",["SamModel",Dl]]])
|
|
2688
|
+
const bd=new Map([["wav2vec2",["Wav2Vec2ForCTC",Kl]],["wav2vec2-bert",["Wav2Vec2BertForCTC",dc]],["unispeech",["UniSpeechForCTC",tc]],["unispeech-sat",["UniSpeechSatForCTC",oc]],["wavlm",["WavLMForCTC",wc]],["hubert",["HubertForCTC",pc]]])
|
|
2689
|
+
const vd=new Map([["wav2vec2",["Wav2Vec2ForSequenceClassification",Yl]],["wav2vec2-bert",["Wav2Vec2BertForSequenceClassification",uc]],["unispeech",["UniSpeechForSequenceClassification",nc]],["unispeech-sat",["UniSpeechSatForSequenceClassification",ic]],["wavlm",["WavLMForSequenceClassification",yc]],["hubert",["HubertForSequenceClassification",fc]],["audio-spectrogram-transformer",["ASTForAudioClassification",zi]]])
|
|
2690
|
+
const kd=new Map([["wavlm",["WavLMForXVector",xc]]])
|
|
2691
|
+
const Md=new Map([["unispeech-sat",["UniSpeechSatForAudioFrameClassification",ac]],["wavlm",["WavLMForAudioFrameClassification",bc]],["wav2vec2",["Wav2Vec2ForAudioFrameClassification",Jl]]])
|
|
2692
|
+
const Td=new Map([["vitmatte",["VitMatteForImageMatting",Ba]]])
|
|
2693
|
+
const Ed=new Map([["swin2sr",["Swin2SRForImageSuperResolution",fl]]])
|
|
2694
|
+
const Cd=new Map([["dpt",["DPTForDepthEstimation",wl]],["depth_anything",["DepthAnythingForDepthEstimation",xl]],["glpn",["GLPNForDepthEstimation",kl]]])
|
|
2695
|
+
const Ad=new Map([["clip",["CLIPVisionModelWithProjection",ji]],["siglip",["SiglipVisionModel",Ui]]])
|
|
2696
|
+
const Sd=[[td,0],[nd,1],[sd,4],[ad,0],[ld,0],[cd,2],[rd,2],[dd,4],[ud,0],[hd,0],[_d,3],[fd,0],[wd,0],[yd,0],[Td,0],[Ed,0],[Cd,0],[md,0],[gd,0],[xd,5],[bd,0],[vd,0],[od,2],[id,0],[kd,0],[Md,0],[Ad,0]]
|
|
2697
|
+
for(const[e,t]of Sd)for(const[n,s]of e.values()){Ks.set(n,t)
|
|
2698
|
+
Js.set(s,n)
|
|
2699
|
+
Ys.set(n,s)}const Fd=[["CLIPTextModelWithProjection",$i,0],["SiglipTextModel",qi,0],["ClapTextModelWithProjection",Dc,0],["ClapAudioModelWithProjection",Rc,0]]
|
|
2700
|
+
for(const[e,t,n]of Fd){Ks.set(e,n)
|
|
2701
|
+
Js.set(t,e)
|
|
2702
|
+
Ys.set(e,t)}class zd extends ed{static MODEL_CLASS_MAPPINGS=Sd.map((e=>e[0]))
|
|
2703
|
+
static BASE_IF_FAIL=!0}class Pd extends ed{static MODEL_CLASS_MAPPINGS=[ad]}class Id extends ed{static MODEL_CLASS_MAPPINGS=[ld]}class Ld extends ed{static MODEL_CLASS_MAPPINGS=[cd]}class Bd extends ed{static MODEL_CLASS_MAPPINGS=[rd]}class Od extends ed{static MODEL_CLASS_MAPPINGS=[od]}class Nd extends ed{static MODEL_CLASS_MAPPINGS=[id]}class $d extends ed{static MODEL_CLASS_MAPPINGS=[dd]}class jd extends ed{static MODEL_CLASS_MAPPINGS=[ud]}class Dd extends ed{static MODEL_CLASS_MAPPINGS=[hd]}class Rd extends ed{static MODEL_CLASS_MAPPINGS=[_d]}class qd extends ed{static MODEL_CLASS_MAPPINGS=[fd]}class Ud extends ed{static MODEL_CLASS_MAPPINGS=[wd]}class Vd extends ed{static MODEL_CLASS_MAPPINGS=[yd]}class Gd extends ed{static MODEL_CLASS_MAPPINGS=[md]}class Wd extends ed{static MODEL_CLASS_MAPPINGS=[gd]}class Xd extends ed{static MODEL_CLASS_MAPPINGS=[xd]}class Hd extends ed{static MODEL_CLASS_MAPPINGS=[bd]}class Qd extends ed{static MODEL_CLASS_MAPPINGS=[vd]}class Kd extends ed{static MODEL_CLASS_MAPPINGS=[kd]}class Yd extends ed{static MODEL_CLASS_MAPPINGS=[Md]}class Jd extends ed{static MODEL_CLASS_MAPPINGS=[pd]}class Zd extends ed{static MODEL_CLASS_MAPPINGS=[Td]}class eu extends ed{static MODEL_CLASS_MAPPINGS=[Ed]}class tu extends ed{static MODEL_CLASS_MAPPINGS=[Cd]}class nu extends ed{static MODEL_CLASS_MAPPINGS=[Ad]}class su extends mr{constructor({logits:e,past_key_values:t,encoder_outputs:n,decoder_attentions:s=null,cross_attentions:r=null}){super()
|
|
2704
|
+
this.logits=e
|
|
2705
|
+
this.past_key_values=t
|
|
2706
|
+
this.encoder_outputs=n
|
|
2707
|
+
this.decoder_attentions=s
|
|
2708
|
+
this.cross_attentions=r}}class ru extends mr{constructor({logits:e}){super()
|
|
2709
|
+
this.logits=e}}class ou extends mr{constructor({logits:e,embeddings:t}){super()
|
|
2710
|
+
this.logits=e
|
|
2711
|
+
this.embeddings=t}}class iu extends mr{constructor({logits:e}){super()
|
|
2712
|
+
this.logits=e}}class au extends mr{constructor({logits:e}){super()
|
|
2713
|
+
this.logits=e}}class lu extends mr{constructor({start_logits:e,end_logits:t}){super()
|
|
2714
|
+
this.start_logits=e
|
|
2715
|
+
this.end_logits=t}}class cu extends mr{constructor({logits:e}){super()
|
|
2716
|
+
this.logits=e}}class du extends mr{constructor({logits:e,past_key_values:t}){super()
|
|
2717
|
+
this.logits=e
|
|
2718
|
+
this.past_key_values=t}}class uu extends mr{constructor({alphas:e}){super()
|
|
2719
|
+
this.alphas=e}}class hu extends mr{constructor({waveform:e,spectrogram:t}){super()
|
|
2720
|
+
this.waveform=e
|
|
2721
|
+
this.spectrogram=t}}const _u=require("sharp")
|
|
2722
|
+
const pu="undefined"!=typeof self
|
|
2723
|
+
const fu=pu&&"DedicatedWorkerGlobalScope"===self.constructor.name
|
|
2724
|
+
let mu
|
|
2725
|
+
let gu
|
|
2726
|
+
let wu
|
|
2727
|
+
if(pu){mu=(e,t)=>{if(!self.OffscreenCanvas)throw new Error("OffscreenCanvas not supported by this browser.")
|
|
2728
|
+
return new self.OffscreenCanvas(e,t)}
|
|
2729
|
+
wu=self.createImageBitmap
|
|
2730
|
+
gu=self.ImageData}else{if(!_u)throw new Error("Unable to load image processing library.")
|
|
2731
|
+
wu=async e=>{const t=(await e.metadata()).channels
|
|
2732
|
+
let{data:n,info:s}=await e.raw().toBuffer({resolveWithObject:!0})
|
|
2733
|
+
const r=new bu(new Uint8ClampedArray(n),s.width,s.height,s.channels)
|
|
2734
|
+
void 0!==t&&t!==s.channels&&r.convert(t)
|
|
2735
|
+
return r}}const yu={0:"nearest",1:"lanczos",2:"bilinear",3:"bicubic",4:"box",5:"hamming"}
|
|
2736
|
+
const xu=new Map([["png","image/png"],["jpg","image/jpeg"],["jpeg","image/jpeg"],["gif","image/gif"]])
|
|
2737
|
+
class bu{constructor(e,t,n,s){this.data=e
|
|
2738
|
+
this.width=t
|
|
2739
|
+
this.height=n
|
|
2740
|
+
this.channels=s}get size(){return[this.width,this.height]}static async read(e){if(e instanceof bu)return e
|
|
2741
|
+
if("string"==typeof e||e instanceof URL)return await this.fromURL(e)
|
|
2742
|
+
throw new Error("Unsupported input type: "+typeof e)}static async fromURL(e){let t=await j(e)
|
|
2743
|
+
if(200!==t.status)throw new Error(`Unable to read image from "${e}" (${t.status} ${t.statusText})`)
|
|
2744
|
+
let n=await t.blob()
|
|
2745
|
+
return this.fromBlob(n)}static async fromBlob(e){if(pu){let t=await wu(e)
|
|
2746
|
+
const n=mu(t.width,t.height).getContext("2d")
|
|
2747
|
+
n.drawImage(t,0,0)
|
|
2748
|
+
return new this(n.getImageData(0,0,t.width,t.height).data,t.width,t.height,4)}{let t=_u(await e.arrayBuffer())
|
|
2749
|
+
return await wu(t)}}static fromTensor(e,t="CHW"){if(3!==e.dims.length)throw new Error(`Tensor should have 3 dimensions, but has ${e.dims.length} dimensions.`)
|
|
2750
|
+
if("CHW"===t)e=e.transpose(1,2,0)
|
|
2751
|
+
else if("HWC"!==t)throw new Error(`Unsupported channel format: ${t}`)
|
|
2752
|
+
if(!(e.data instanceof Uint8ClampedArray||e.data instanceof Uint8Array))throw new Error(`Unsupported tensor type: ${e.type}`)
|
|
2753
|
+
switch(e.dims[2]){case 1:case 2:case 3:case 4:return new bu(e.data,e.dims[1],e.dims[0],e.dims[2])
|
|
2754
|
+
default:throw new Error(`Unsupported number of channels: ${e.dims[2]}`)}}grayscale(){if(1===this.channels)return this
|
|
2755
|
+
let e=new Uint8ClampedArray(this.width*this.height*1)
|
|
2756
|
+
switch(this.channels){case 3:case 4:for(let t=0,n=0;t<this.data.length;t+=this.channels){const s=this.data[t]
|
|
2757
|
+
const r=this.data[t+1]
|
|
2758
|
+
const o=this.data[t+2]
|
|
2759
|
+
e[n++]=Math.round(.2989*s+.587*r+.114*o)}break
|
|
2760
|
+
default:throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`)}return this._update(e,this.width,this.height,1)}rgb(){if(3===this.channels)return this
|
|
2761
|
+
let e=new Uint8ClampedArray(this.width*this.height*3)
|
|
2762
|
+
switch(this.channels){case 1:for(let t=0,n=0;t<this.data.length;++t){e[n++]=this.data[t]
|
|
2763
|
+
e[n++]=this.data[t]
|
|
2764
|
+
e[n++]=this.data[t]}break
|
|
2765
|
+
case 4:for(let t=0,n=0;t<this.data.length;t+=4){e[n++]=this.data[t]
|
|
2766
|
+
e[n++]=this.data[t+1]
|
|
2767
|
+
e[n++]=this.data[t+2]}break
|
|
2768
|
+
default:throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`)}return this._update(e,this.width,this.height,3)}rgba(){if(4===this.channels)return this
|
|
2769
|
+
let e=new Uint8ClampedArray(this.width*this.height*4)
|
|
2770
|
+
switch(this.channels){case 1:for(let t=0,n=0;t<this.data.length;++t){e[n++]=this.data[t]
|
|
2771
|
+
e[n++]=this.data[t]
|
|
2772
|
+
e[n++]=this.data[t]
|
|
2773
|
+
e[n++]=255}break
|
|
2774
|
+
case 3:for(let t=0,n=0;t<this.data.length;t+=3){e[n++]=this.data[t]
|
|
2775
|
+
e[n++]=this.data[t+1]
|
|
2776
|
+
e[n++]=this.data[t+2]
|
|
2777
|
+
e[n++]=255}break
|
|
2778
|
+
default:throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`)}return this._update(e,this.width,this.height,4)}async resize(e,t,{resample:n=2}={}){var s
|
|
2779
|
+
let r=null!=(s=yu[n])?s:n
|
|
2780
|
+
if(pu){let n=this.channels
|
|
2781
|
+
let s=this.toCanvas()
|
|
2782
|
+
const r=mu(e,t).getContext("2d")
|
|
2783
|
+
r.drawImage(s,0,0,e,t)
|
|
2784
|
+
return new bu(r.getImageData(0,0,e,t).data,e,t,4).convert(n)}{let n=this.toSharp()
|
|
2785
|
+
switch(r){case"box":case"hamming":if("box"===r||"hamming"===r){console.warn(`Resampling method ${r} is not yet supported. Using bilinear instead.`)
|
|
2786
|
+
r="bilinear"}case"nearest":case"bilinear":case"bicubic":n=n.affine([e/this.width,0,0,t/this.height],{interpolator:r})
|
|
2787
|
+
break
|
|
2788
|
+
case"lanczos":n=n.resize({width:e,height:t,fit:"fill",kernel:"lanczos3"})
|
|
2789
|
+
break
|
|
2790
|
+
default:throw new Error(`Resampling method ${r} is not supported.`)}return await wu(n)}}async pad([e,t,n,s]){e=Math.max(e,0)
|
|
2791
|
+
t=Math.max(t,0)
|
|
2792
|
+
n=Math.max(n,0)
|
|
2793
|
+
s=Math.max(s,0)
|
|
2794
|
+
if(0===e&&0===t&&0===n&&0===s)return this
|
|
2795
|
+
if(pu){let r=this.channels
|
|
2796
|
+
let o=this.toCanvas()
|
|
2797
|
+
let i=this.width+e+t
|
|
2798
|
+
let a=this.height+n+s
|
|
2799
|
+
const l=mu(i,a).getContext("2d")
|
|
2800
|
+
l.drawImage(o,0,0,this.width,this.height,e,n,i,a)
|
|
2801
|
+
return new bu(l.getImageData(0,0,i,a).data,i,a,4).convert(r)}{let r=this.toSharp().extend({left:e,right:t,top:n,bottom:s})
|
|
2802
|
+
return await wu(r)}}async crop([e,t,n,s]){e=Math.max(e,0)
|
|
2803
|
+
t=Math.max(t,0)
|
|
2804
|
+
n=Math.min(n,this.width-1)
|
|
2805
|
+
s=Math.min(s,this.height-1)
|
|
2806
|
+
if(0===e&&0===t&&n===this.width-1&&s===this.height-1)return this
|
|
2807
|
+
const r=n-e+1
|
|
2808
|
+
const o=s-t+1
|
|
2809
|
+
if(pu){const n=this.channels
|
|
2810
|
+
const s=this.toCanvas()
|
|
2811
|
+
const i=mu(r,o).getContext("2d")
|
|
2812
|
+
i.drawImage(s,e,t,r,o,0,0,r,o)
|
|
2813
|
+
return new bu(i.getImageData(0,0,r,o).data,r,o,4).convert(n)}{const n=this.toSharp().extract({left:e,top:t,width:r,height:o})
|
|
2814
|
+
return await wu(n)}}async center_crop(e,t){if(this.width===e&&this.height===t)return this
|
|
2815
|
+
let n=(this.width-e)/2
|
|
2816
|
+
let s=(this.height-t)/2
|
|
2817
|
+
if(pu){let r=this.channels
|
|
2818
|
+
let o=this.toCanvas()
|
|
2819
|
+
const i=mu(e,t).getContext("2d")
|
|
2820
|
+
let a=0
|
|
2821
|
+
let l=0
|
|
2822
|
+
let c=0
|
|
2823
|
+
let d=0
|
|
2824
|
+
n>=0?a=n:c=-n
|
|
2825
|
+
s>=0?l=s:d=-s
|
|
2826
|
+
i.drawImage(o,a,l,e,t,c,d,e,t)
|
|
2827
|
+
return new bu(i.getImageData(0,0,e,t).data,e,t,4).convert(r)}{let r=this.toSharp()
|
|
2828
|
+
if(n>=0&&s>=0)r=r.extract({left:Math.floor(n),top:Math.floor(s),width:e,height:t})
|
|
2829
|
+
else if(n<=0&&s<=0){let o=Math.floor(-s)
|
|
2830
|
+
let i=Math.floor(-n)
|
|
2831
|
+
r=r.extend({top:o,left:i,right:e-this.width-i,bottom:t-this.height-o})}else{let o=[0,0]
|
|
2832
|
+
let i=0
|
|
2833
|
+
if(s<0){o[0]=Math.floor(-s)
|
|
2834
|
+
o[1]=t-this.height-o[0]}else i=Math.floor(s)
|
|
2835
|
+
let a=[0,0]
|
|
2836
|
+
let l=0
|
|
2837
|
+
if(n<0){a[0]=Math.floor(-n)
|
|
2838
|
+
a[1]=e-this.width-a[0]}else l=Math.floor(n)
|
|
2839
|
+
r=r.extend({top:o[0],bottom:o[1],left:a[0],right:a[1]}).extract({left:l,top:i,width:e,height:t})}return await wu(r)}}async toBlob(e="image/png",t=1){if(!pu)throw new Error("toBlob() is only supported in browser environments.")
|
|
2840
|
+
const n=this.toCanvas()
|
|
2841
|
+
return await n.convertToBlob({type:e,quality:t})}toTensor(e="CHW"){let t=new de("uint8",new Uint8Array(this.data),[this.height,this.width,this.channels])
|
|
2842
|
+
if("HWC"===e);else{if("CHW"!==e)throw new Error(`Unsupported channel format: ${e}`)
|
|
2843
|
+
t=t.permute(2,0,1)}return t}toCanvas(){if(!pu)throw new Error("toCanvas() is only supported in browser environments.")
|
|
2844
|
+
let e=this.clone().rgba()
|
|
2845
|
+
let t=mu(e.width,e.height)
|
|
2846
|
+
let n=new gu(e.data,e.width,e.height)
|
|
2847
|
+
t.getContext("2d").putImageData(n,0,0)
|
|
2848
|
+
return t}_update(e,t,n,s=null){this.data=e
|
|
2849
|
+
this.width=t
|
|
2850
|
+
this.height=n
|
|
2851
|
+
null!==s&&(this.channels=s)
|
|
2852
|
+
return this}clone(){return new bu(this.data.slice(),this.width,this.height,this.channels)}convert(e){if(this.channels===e)return this
|
|
2853
|
+
switch(e){case 1:this.grayscale()
|
|
2854
|
+
break
|
|
2855
|
+
case 3:this.rgb()
|
|
2856
|
+
break
|
|
2857
|
+
case 4:this.rgba()
|
|
2858
|
+
break
|
|
2859
|
+
default:throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`)}return this}async save(e){if(!pu){if(B.useFS){const t=this.toSharp()
|
|
2860
|
+
return await t.toFile(e)}throw new Error("Unable to save the image because filesystem is disabled in this environment.")}{var t
|
|
2861
|
+
if(fu)throw new Error("Unable to save an image from a Web Worker.")
|
|
2862
|
+
const n=e.split(".").pop().toLowerCase()
|
|
2863
|
+
const s=null!=(t=xu.get(n))?t:"image/png"
|
|
2864
|
+
const r=await this.toBlob(s)
|
|
2865
|
+
const o=URL.createObjectURL(r)
|
|
2866
|
+
const i=document.createElement("a")
|
|
2867
|
+
i.href=o
|
|
2868
|
+
i.download=e
|
|
2869
|
+
i.click()
|
|
2870
|
+
i.remove()}}toSharp(){if(pu)throw new Error("toSharp() is only supported in server-side environments.")
|
|
2871
|
+
return _u(this.data,{raw:{width:this.width,height:this.height,channels:this.channels}})}}async function vu(e,t){if("undefined"==typeof AudioContext)throw Error("Unable to load audio from path/URL since `AudioContext` is not available in your environment. Instead, audio data should be passed directly to the pipeline/processor. For more information and some example code, see https://huggingface.co/docs/transformers.js/guides/node-audio-processing.")
|
|
2872
|
+
const n=await(await j(e)).arrayBuffer()
|
|
2873
|
+
const s=new AudioContext({sampleRate:t})
|
|
2874
|
+
void 0===t&&console.warn(`No sampling rate provided, using default of ${s.sampleRate}Hz.`)
|
|
2875
|
+
const r=await s.decodeAudioData(n)
|
|
2876
|
+
let o
|
|
2877
|
+
if(2===r.numberOfChannels){const e=Math.sqrt(2)
|
|
2878
|
+
const t=r.getChannelData(0)
|
|
2879
|
+
const n=r.getChannelData(1)
|
|
2880
|
+
o=new Float32Array(t.length)
|
|
2881
|
+
for(let s=0;s<r.length;++s)o[s]=e*(t[s]+n[s])/2}else o=r.getChannelData(0)
|
|
2882
|
+
return o}function ku(e){if(e<1)return new Float64Array
|
|
2883
|
+
if(1===e)return new Float64Array([1])
|
|
2884
|
+
const t=e-1
|
|
2885
|
+
const n=Math.PI/t
|
|
2886
|
+
const s=new Float64Array(e)
|
|
2887
|
+
for(let r=0;r<e;++r){const e=2*r-t
|
|
2888
|
+
s[r]=.5+.5*Math.cos(n*e)}return s}const Mu={htk:e=>2595*Math.log10(1+e/700),kaldi:e=>1127*Math.log(1+e/700),slaney:(e,t=1e3,n=15,s=27/Math.log(6.4))=>e>=t?n+Math.log(e/t)*s:3*e/200}
|
|
2889
|
+
function Tu(e,t="htk"){const n=Mu[t]
|
|
2890
|
+
if(!n)throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".')
|
|
2891
|
+
return"number"==typeof e?n(e):e.map((e=>n(e)))}const Eu={htk:e=>700*(10**(e/2595)-1),kaldi:e=>700*(Math.exp(e/1127)-1),slaney:(e,t=1e3,n=15,s=Math.log(6.4)/27)=>e>=n?t*Math.exp(s*(e-n)):200*e/3}
|
|
2892
|
+
function Cu(e,t,n){const s=(t-e)/(n-1)
|
|
2893
|
+
return Float64Array.from({length:n},((t,n)=>e+s*n))}function Au(e,t,n,s,r,o=null,i="htk",a=!1){if(null!==o&&"slaney"!==o)throw new Error('norm must be one of null or "slaney"')
|
|
2894
|
+
const l=Cu(Tu(n,i),Tu(s,i),t+2)
|
|
2895
|
+
let c=function(e,t="htk"){const n=Eu[t]
|
|
2896
|
+
if(!n)throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".')
|
|
2897
|
+
return"number"==typeof e?n(e):e.map((e=>n(e)))}(l,i)
|
|
2898
|
+
let d
|
|
2899
|
+
if(a){const t=r/(2*e)
|
|
2900
|
+
d=Tu(Float64Array.from({length:e},((e,n)=>n*t)),i)
|
|
2901
|
+
c=l}else d=Cu(0,Math.floor(r/2),e)
|
|
2902
|
+
const u=function(e,t){const n=Float64Array.from({length:t.length-1},((e,n)=>t[n+1]-t[n]))
|
|
2903
|
+
const s=Array.from({length:e.length},(()=>new Array(t.length)))
|
|
2904
|
+
for(let n=0;n<e.length;++n){const r=s[n]
|
|
2905
|
+
for(let s=0;s<t.length;++s)r[s]=t[s]-e[n]}const r=t.length-2
|
|
2906
|
+
const o=Array.from({length:r},(()=>new Array(e.length)))
|
|
2907
|
+
for(let t=0;t<e.length;++t){const e=s[t]
|
|
2908
|
+
for(let s=0;s<r;++s){const r=-e[s]/n[s]
|
|
2909
|
+
const i=e[s+2]/n[s+1]
|
|
2910
|
+
o[s][t]=Math.max(0,Math.min(r,i))}}return o}(d,c)
|
|
2911
|
+
if(null!==o&&"slaney"===o)for(let n=0;n<t;++n){const t=u[n]
|
|
2912
|
+
const s=2/(c[n+2]-c[n])
|
|
2913
|
+
for(let n=0;n<e;++n)t[n]*=s}return u}function Su(e,t,n,s,r){if(n<=0)throw new Error("reference must be greater than zero")
|
|
2914
|
+
if(s<=0)throw new Error("min_value must be greater than zero")
|
|
2915
|
+
n=Math.max(s,n)
|
|
2916
|
+
const o=Math.log10(n)
|
|
2917
|
+
for(let n=0;n<e.length;++n)e[n]=t*Math.log10(Math.max(s,e[n])-o)
|
|
2918
|
+
if(null!==r){if(r<=0)throw new Error("db_range must be greater than zero")
|
|
2919
|
+
const t=ee(e)[0]-r
|
|
2920
|
+
for(let n=0;n<e.length;++n)e[n]=Math.max(e[n],t)}return e}function Fu(e,t,n,s,{fft_length:r=null,power:o=1,center:i=!0,pad_mode:a="reflect",onesided:l=!0,preemphasis:c=null,mel_filters:d=null,mel_floor:h=1e-10,log_mel:_=null,reference:p=1,min_value:f=1e-10,db_range:m=null,remove_dc_offset:g=null,max_num_frames:w=null,do_pad:y=!0,transpose:x=!1}={}){const b=t.length
|
|
2921
|
+
null===r&&(r=n)
|
|
2922
|
+
if(n>r)throw Error(`frame_length (${n}) may not be larger than fft_length (${r})`)
|
|
2923
|
+
if(b!==n)throw new Error(`Length of the window (${b}) must equal frame_length (${n})`)
|
|
2924
|
+
if(s<=0)throw new Error("hop_length must be greater than zero")
|
|
2925
|
+
if(i){if("reflect"!==a)throw new Error(`pad_mode="${a}" not implemented yet.`)
|
|
2926
|
+
const t=Math.floor((r-1)/2)+1
|
|
2927
|
+
e=function(e,t,n){const s=new e.constructor(e.length+t+n)
|
|
2928
|
+
const r=e.length-1
|
|
2929
|
+
for(let n=0;n<e.length;++n)s[t+n]=e[n]
|
|
2930
|
+
for(let n=1;n<=t;++n)s[t-n]=e[u(n,r)]
|
|
2931
|
+
for(let o=1;o<=n;++o)s[r+t+o]=e[u(r-o,r)]
|
|
2932
|
+
return s}(e,t,t)}const v=Math.floor(1+Math.floor((e.length-n)/s))
|
|
2933
|
+
const k=l?Math.floor(r/2)+1:r
|
|
2934
|
+
let M=v
|
|
2935
|
+
let T=v
|
|
2936
|
+
null!==w&&(w>v?y&&(T=w):T=M=w)
|
|
2937
|
+
const E=new re(r)
|
|
2938
|
+
const C=new Float64Array(r)
|
|
2939
|
+
const A=new Float64Array(E.outputBufferSize)
|
|
2940
|
+
const S=new Array(M)
|
|
2941
|
+
for(let r=0;r<M;++r){const o=r*s
|
|
2942
|
+
for(let t=0;t<n;++t)C[t]=e[o+t]
|
|
2943
|
+
if(g){let e=0
|
|
2944
|
+
for(let t=0;t<n;++t)e+=C[t]
|
|
2945
|
+
const t=e/n
|
|
2946
|
+
for(let e=0;e<n;++e)C[e]-=t}if(null!==c){for(let e=n-1;e>=1;--e)C[e]-=c*C[e-1]
|
|
2947
|
+
C[0]*=1-c}for(let e=0;e<t.length;++e)C[e]*=t[e]
|
|
2948
|
+
E.realTransform(A,C)
|
|
2949
|
+
const i=new Array(k)
|
|
2950
|
+
for(let e=0;e<i.length;++e){const t=e<<1
|
|
2951
|
+
i[e]=A[t]**2+A[t+1]**2}S[r]=i}if(null!==o&&2!==o){const e=2/o
|
|
2952
|
+
for(let t=0;t<S.length;++t){const n=S[t]
|
|
2953
|
+
for(let t=0;t<n.length;++t)n[t]**=e}}const F=d.length
|
|
2954
|
+
const z=new Float32Array(F*T)
|
|
2955
|
+
const P=x?[T,F]:[F,T]
|
|
2956
|
+
for(let e=0;e<F;++e){const t=d[e]
|
|
2957
|
+
for(let n=0;n<M;++n){const s=S[n]
|
|
2958
|
+
let r=0
|
|
2959
|
+
for(let e=0;e<k;++e)r+=t[e]*s[e]
|
|
2960
|
+
z[x?n*F+e:e*M+n]=Math.max(h,r)}}if(null!==o&&null!==_){const e=Math.min(z.length,M*F)
|
|
2961
|
+
switch(_){case"log":for(let t=0;t<e;++t)z[t]=Math.log(z[t])
|
|
2962
|
+
break
|
|
2963
|
+
case"log10":for(let t=0;t<e;++t)z[t]=Math.log10(z[t])
|
|
2964
|
+
break
|
|
2965
|
+
case"dB":if(1===o)!function(e,t=1,n=1e-5,s=null){Su(e,20,t,n,s)}(z,p,f,m)
|
|
2966
|
+
else{if(2!==o)throw new Error(`Cannot use log_mel option '${_}' with power ${o}`)
|
|
2967
|
+
!function(e,t=1,n=1e-10,s=null){Su(e,10,t,n,s)}(z,p,f,m)}break
|
|
2968
|
+
default:throw new Error(`log_mel must be one of null, 'log', 'log10' or 'dB'. Got '${_}'`)}}return{data:z,dims:P}}function zu(e,t,{periodic:n=!0,frame_length:s=null,center:r=!0}={}){const o=n?e+1:e
|
|
2969
|
+
let i
|
|
2970
|
+
switch(t){case"boxcar":i=new Float64Array(o).fill(1)
|
|
2971
|
+
break
|
|
2972
|
+
case"hann":case"hann_window":i=ku(o)
|
|
2973
|
+
break
|
|
2974
|
+
case"povey":i=ku(o).map((e=>Math.pow(e,.85)))
|
|
2975
|
+
break
|
|
2976
|
+
default:throw new Error(`Unknown window type ${t}.`)}n&&(i=i.subarray(0,e))
|
|
2977
|
+
if(null===s)return i
|
|
2978
|
+
if(e>s)throw new Error(`Length of the window (${e}) may not be larger than frame_length (${s})`)
|
|
2979
|
+
return i}function Pu([e,t,n,s]){return[e-n/2,t-s/2,e+n/2,t+s/2]}function Iu(e,t=.5,n=null,s=!1){const r=e.logits
|
|
2980
|
+
const o=e.pred_boxes
|
|
2981
|
+
const[i,a,l]=r.dims
|
|
2982
|
+
if(null!==n&&n.length!==i)throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits")
|
|
2983
|
+
let c=[]
|
|
2984
|
+
for(let e=0;e<i;++e){let i=null!==n?n[e]:null
|
|
2985
|
+
let d={boxes:[],classes:[],scores:[]}
|
|
2986
|
+
let u=r[e]
|
|
2987
|
+
let h=o[e]
|
|
2988
|
+
for(let e=0;e<a;++e){let n=u[e]
|
|
2989
|
+
let r=[]
|
|
2990
|
+
let o
|
|
2991
|
+
if(s){o=n.sigmoid().data
|
|
2992
|
+
for(let e=0;e<o.length;++e)o[e]>t&&r.push(e)}else{let e=ee(n.data)[1]
|
|
2993
|
+
if(e===l-1)continue
|
|
2994
|
+
r.push(e)
|
|
2995
|
+
o=X(n.data)}for(const t of r){let n=h[e].data
|
|
2996
|
+
n=Pu(n)
|
|
2997
|
+
null!==i&&(n=n.map(((e,t)=>e*i[(t+1)%2])))
|
|
2998
|
+
d.boxes.push(n)
|
|
2999
|
+
d.classes.push(t)
|
|
3000
|
+
d.scores.push(o[t])}}c.push(d)}return c}function Lu(e,t){if(!(e instanceof Float32Array||e instanceof Float64Array)){var n,s
|
|
3001
|
+
throw new Error(`${t} expects input to be a Float32Array or a Float64Array, but got ${null!=(n=null==e||null==(s=e.constructor)?void 0:s.name)?n:typeof e} instead. If using the feature extractor directly, remember to use \`read_audio(url, sampling_rate)\` to obtain the raw audio data of the file/url.`)}}function Bu(e,t,n=0,s=null){const r=e/t
|
|
3002
|
+
let o=ae(r)*t
|
|
3003
|
+
null!==s&&o>s&&(o=Math.floor(r)*t)
|
|
3004
|
+
o<n&&(o=Math.ceil(r)*t)
|
|
3005
|
+
return o}function Ou([e,t],n){return[Math.max(Math.floor(e/n),1)*n,Math.max(Math.floor(t/n),1)*n]}class Nu extends r{constructor(e){super()
|
|
3006
|
+
this.config=e}}class $u extends Nu{constructor(e){var t,n,s,r,o,i,a
|
|
3007
|
+
super(e)
|
|
3008
|
+
this.image_mean=null!=(t=this.config.image_mean)?t:this.config.mean
|
|
3009
|
+
this.image_std=null!=(n=this.config.image_std)?n:this.config.std
|
|
3010
|
+
this.resample=null!=(s=this.config.resample)?s:2
|
|
3011
|
+
this.do_rescale=null==(r=this.config.do_rescale)||r
|
|
3012
|
+
this.rescale_factor=null!=(o=this.config.rescale_factor)?o:1/255
|
|
3013
|
+
this.do_normalize=this.config.do_normalize
|
|
3014
|
+
this.do_resize=this.config.do_resize
|
|
3015
|
+
this.do_thumbnail=this.config.do_thumbnail
|
|
3016
|
+
this.size=this.config.size
|
|
3017
|
+
this.size_divisibility=null!=(i=this.config.size_divisibility)?i:this.config.size_divisor
|
|
3018
|
+
this.do_center_crop=this.config.do_center_crop
|
|
3019
|
+
this.crop_size=this.config.crop_size
|
|
3020
|
+
this.do_convert_rgb=null==(a=this.config.do_convert_rgb)||a
|
|
3021
|
+
this.do_crop_margin=this.config.do_crop_margin
|
|
3022
|
+
this.pad_size=this.config.pad_size
|
|
3023
|
+
this.do_pad=this.config.do_pad
|
|
3024
|
+
this.do_pad&&!this.pad_size&&this.size&&void 0!==this.size.width&&void 0!==this.size.height&&(this.pad_size=this.size)}async thumbnail(e,t,n=2){const s=e.height
|
|
3025
|
+
const r=e.width
|
|
3026
|
+
const o=t.height
|
|
3027
|
+
const i=t.width
|
|
3028
|
+
let a=Math.min(s,o)
|
|
3029
|
+
let l=Math.min(r,i)
|
|
3030
|
+
if(a===s&&l===r)return e
|
|
3031
|
+
s>r?l=Math.floor(r*a/s):r>s&&(a=Math.floor(s*l/r))
|
|
3032
|
+
return await e.resize(l,a,{resample:n})}async crop_margin(e,t=200){const n=e.clone().grayscale()
|
|
3033
|
+
const s=Z(n.data)[0]
|
|
3034
|
+
const r=ee(n.data)[0]-s
|
|
3035
|
+
if(0===r)return e
|
|
3036
|
+
const o=t/255
|
|
3037
|
+
let i=n.width,a=n.height,l=0,c=0
|
|
3038
|
+
for(let e=0;e<n.height;++e){const t=e*n.width
|
|
3039
|
+
for(let d=0;d<n.width;++d)if((n.data[t+d]-s)/r<o){i=Math.min(i,d)
|
|
3040
|
+
a=Math.min(a,e)
|
|
3041
|
+
l=Math.max(l,d)
|
|
3042
|
+
c=Math.max(c,e)}}return await e.crop([i,a,l,c])}pad_image(e,t,n,{mode:s="constant",center:r=!1,constant_values:o=0}={}){const[i,a,l]=t
|
|
3043
|
+
let c,d
|
|
3044
|
+
if("number"==typeof n){c=n
|
|
3045
|
+
d=n}else{c=n.width
|
|
3046
|
+
d=n.height}if(c!==a||d!==i){const n=new Float32Array(c*d*l)
|
|
3047
|
+
if(Array.isArray(o))for(let e=0;e<n.length;++e)n[e]=o[e%l]
|
|
3048
|
+
else 0!==o&&n.fill(o)
|
|
3049
|
+
const[h,_]=r?[Math.floor((c-a)/2),Math.floor((d-i)/2)]:[0,0]
|
|
3050
|
+
for(let t=0;t<i;++t){const s=(t+_)*c
|
|
3051
|
+
const r=t*a
|
|
3052
|
+
for(let t=0;t<a;++t){const o=(s+t+h)*l
|
|
3053
|
+
const i=(r+t)*l
|
|
3054
|
+
for(let t=0;t<l;++t)n[o+t]=e[i+t]}}if("symmetric"===s){if(r)throw new Error("`center` padding is not supported when `mode` is set to `symmetric`.")
|
|
3055
|
+
const t=i-1
|
|
3056
|
+
const s=a-1
|
|
3057
|
+
for(let r=0;r<d;++r){const o=r*c
|
|
3058
|
+
const d=u(r,t)*a
|
|
3059
|
+
for(let t=0;t<c;++t){if(r<i&&t<a)continue
|
|
3060
|
+
const c=(o+t)*l
|
|
3061
|
+
const h=(d+u(t,s))*l
|
|
3062
|
+
for(let t=0;t<l;++t)n[c+t]=e[h+t]}}}e=n
|
|
3063
|
+
t=[d,c,l]}return[e,t]}rescale(e){for(let t=0;t<e.length;++t)e[t]=this.rescale_factor*e[t]}get_resize_output_image_size(e,t){const[n,s]=e.size
|
|
3064
|
+
let r
|
|
3065
|
+
let o
|
|
3066
|
+
if(this.do_thumbnail){const{height:e,width:n}=t
|
|
3067
|
+
r=Math.min(e,n)}else if(Number.isInteger(t)){var i
|
|
3068
|
+
r=t
|
|
3069
|
+
o=null!=(i=this.config.max_size)?i:r}else if(void 0!==t){r=t.shortest_edge
|
|
3070
|
+
o=t.longest_edge}if(void 0!==r||void 0!==o){const e=void 0===r?1:Math.max(r/n,r/s)
|
|
3071
|
+
const t=n*e
|
|
3072
|
+
const i=s*e
|
|
3073
|
+
const a=void 0===o?1:Math.min(o/t,o/i)
|
|
3074
|
+
let l=Math.floor(Number((t*a).toFixed(2)))
|
|
3075
|
+
let c=Math.floor(Number((i*a).toFixed(2)))
|
|
3076
|
+
void 0!==this.size_divisibility&&([l,c]=Ou([l,c],this.size_divisibility))
|
|
3077
|
+
return[l,c]}if(void 0!==t&&void 0!==t.width&&void 0!==t.height){let e=t.width
|
|
3078
|
+
let r=t.height
|
|
3079
|
+
if(this.config.keep_aspect_ratio&&this.config.ensure_multiple_of){let t=r/s
|
|
3080
|
+
let o=e/n
|
|
3081
|
+
Math.abs(1-o)<Math.abs(1-t)?t=o:o=t
|
|
3082
|
+
r=Bu(t*s,this.config.ensure_multiple_of)
|
|
3083
|
+
e=Bu(o*n,this.config.ensure_multiple_of)}return[e,r]}if(void 0!==this.size_divisibility)return Ou([n,s],this.size_divisibility)
|
|
3084
|
+
throw new Error(`Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(t)}`)}async resize(e){const[t,n]=this.get_resize_output_image_size(e,this.size)
|
|
3085
|
+
return await e.resize(t,n,{resample:this.resample})}async preprocess(e,{do_normalize:t=null,do_pad:n=null,do_convert_rgb:s=null,do_convert_grayscale:r=null}={}){this.do_crop_margin&&(e=await this.crop_margin(e))
|
|
3086
|
+
const[o,i]=e.size;(null!=s?s:this.do_convert_rgb)?e=e.rgb():r&&(e=e.grayscale())
|
|
3087
|
+
this.do_resize&&(e=await this.resize(e))
|
|
3088
|
+
this.do_thumbnail&&(e=await this.thumbnail(e,this.size,this.resample))
|
|
3089
|
+
if(this.do_center_crop){let t
|
|
3090
|
+
let n
|
|
3091
|
+
if(Number.isInteger(this.crop_size)){t=this.crop_size
|
|
3092
|
+
n=this.crop_size}else{t=this.crop_size.width
|
|
3093
|
+
n=this.crop_size.height}e=await e.center_crop(t,n)}const a=[e.height,e.width]
|
|
3094
|
+
let l=Float32Array.from(e.data)
|
|
3095
|
+
let c=[e.height,e.width,e.channels]
|
|
3096
|
+
this.do_rescale&&this.rescale(l)
|
|
3097
|
+
if(null!=t?t:this.do_normalize){let t=this.image_mean
|
|
3098
|
+
Array.isArray(this.image_mean)||(t=new Array(e.channels).fill(t))
|
|
3099
|
+
let n=this.image_std
|
|
3100
|
+
Array.isArray(this.image_std)||(n=new Array(e.channels).fill(t))
|
|
3101
|
+
if(t.length!==e.channels||n.length!==e.channels)throw new Error(`When set to arrays, the length of \`image_mean\` (${t.length}) and \`image_std\` (${n.length}) must match the number of channels in the image (${e.channels}).`)
|
|
3102
|
+
for(let s=0;s<l.length;s+=e.channels)for(let r=0;r<e.channels;++r)l[s+r]=(l[s+r]-t[r])/n[r]}if(null!=n?n:this.do_pad)if(this.pad_size){const t=this.pad_image(l,[e.height,e.width,e.channels],this.pad_size);[l,c]=t}else if(this.size_divisibility){const[e,t]=Ou([c[1],c[0]],this.size_divisibility);[l,c]=this.pad_image(l,c,{width:e,height:t})}return{original_size:[i,o],reshaped_input_size:a,pixel_values:new de("float32",l,c).permute(2,0,1)}}async _call(e,...t){Array.isArray(e)||(e=[e])
|
|
3103
|
+
const n=await Promise.all(e.map((e=>this.preprocess(e))))
|
|
3104
|
+
return{pixel_values:ye(n.map((e=>e.pixel_values)),0),original_sizes:n.map((e=>e.original_size)),reshaped_input_sizes:n.map((e=>e.reshaped_input_size))}}}class ju extends $u{post_process_semantic_segmentation(e,t=null){const n=e.logits
|
|
3105
|
+
const s=n.dims[0]
|
|
3106
|
+
if(null!==t&&t.length!==s)throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits")
|
|
3107
|
+
const r=[]
|
|
3108
|
+
for(let e=0;e<s;++e){const s=null!==t?t[e]:null
|
|
3109
|
+
let o=n[e]
|
|
3110
|
+
null!==s&&(o=he(o,s,"bilinear",!1))
|
|
3111
|
+
const[i,a]=null!=s?s:o.dims.slice(-2)
|
|
3112
|
+
const l=new de("int32",new Int32Array(i*a),[i,a])
|
|
3113
|
+
const c=o[0].data
|
|
3114
|
+
for(let e=1;e<o.dims[0];++e){const t=o[e].data
|
|
3115
|
+
for(let n=0;n<t.length;++n)if(t[n]>c[n]){c[n]=t[n]
|
|
3116
|
+
l.data[n]=e}}const d=new Array(o.dims[0])
|
|
3117
|
+
const u=l.data
|
|
3118
|
+
for(let e=0;e<u.length;++e){const t=u[e]
|
|
3119
|
+
d[t]=t}const h=d.filter((e=>void 0!==e))
|
|
3120
|
+
r.push({segmentation:l,labels:h})}return r}}class Du extends $u{}class Ru extends Du{}class qu extends $u{}class Uu extends $u{}class Vu extends $u{}class Gu extends $u{}class Wu extends $u{}class Xu extends $u{constructor(e){var t
|
|
3121
|
+
super(e)
|
|
3122
|
+
this.crop_pct=null!=(t=this.config.crop_pct)?t:.875}async resize(e){var t
|
|
3123
|
+
const n=null==(t=this.size)?void 0:t.shortest_edge
|
|
3124
|
+
if(void 0===n)throw new Error("Size dictionary must contain 'shortest_edge' key.")
|
|
3125
|
+
if(n<384){const t=Math.floor(n/this.crop_pct)
|
|
3126
|
+
const[s,r]=this.get_resize_output_image_size(e,{shortest_edge:t})
|
|
3127
|
+
e=await e.resize(s,r,{resample:this.resample})
|
|
3128
|
+
e=await e.center_crop(n,n)}else e=await e.resize(n,n,{resample:this.resample})
|
|
3129
|
+
return e}}class Hu extends Xu{}class Qu extends $u{}class Ku extends $u{}class Yu extends $u{constructor(e){var t
|
|
3130
|
+
super(e)
|
|
3131
|
+
this.include_top=null==(t=this.config.include_top)||t
|
|
3132
|
+
this.include_top&&(this.image_std=this.image_std.map((e=>e*e)))}}class Ju extends $u{}class Zu extends $u{post_process_object_detection(...e){return Iu(...e)}}class eh extends Zu{}class th extends $u{}class nh extends $u{}class sh extends $u{pad_image(e,t,n,s={}){const[r,o,i]=t
|
|
3133
|
+
let a=this.image_mean
|
|
3134
|
+
Array.isArray(this.image_mean)||(a=new Array(i).fill(a))
|
|
3135
|
+
let l=this.image_std
|
|
3136
|
+
Array.isArray(l)||(l=new Array(i).fill(a))
|
|
3137
|
+
const c=a.map(((e,t)=>-e/l[t]))
|
|
3138
|
+
return super.pad_image(e,t,n,{center:!0,constant_values:c,...s})}}class rh extends sh{}class oh extends $u{async _call(e){const t=await super._call(e)
|
|
3139
|
+
const n=[t.pixel_values.dims[0],64,64]
|
|
3140
|
+
const s=new de("int64",new BigInt64Array(n.reduce(((e,t)=>e*t))).fill(1n),n)
|
|
3141
|
+
return{...t,pixel_mask:s}}post_process_object_detection(...e){return Iu(...e)}remove_low_and_no_objects(e,t,n,s){let r=[]
|
|
3142
|
+
let o=[]
|
|
3143
|
+
let i=[]
|
|
3144
|
+
for(let a=0;a<e.dims[0];++a){let l=e[a]
|
|
3145
|
+
let c=t[a]
|
|
3146
|
+
let d=ee(l.data)[1]
|
|
3147
|
+
if(d===s)continue
|
|
3148
|
+
let u=X(l.data)[d]
|
|
3149
|
+
if(u>n){r.push(c)
|
|
3150
|
+
o.push(u)
|
|
3151
|
+
i.push(d)}}return[r,o,i]}check_segment_validity(e,t,n,s=.5,r=.8){let o=[]
|
|
3152
|
+
let i=0
|
|
3153
|
+
let a=0
|
|
3154
|
+
for(let r=0;r<e.length;++r){if(e[r]===n){o.push(r);++i}t[n].data[r]>=s&&++a}let l=i>0&&a>0
|
|
3155
|
+
l&&(l=i/a>r)
|
|
3156
|
+
return[l,o]}compute_segments(e,t,n,s,r,o=null,i=null){let[a,l]=null!=i?i:e[0].dims
|
|
3157
|
+
let c=new de("int32",new Int32Array(a*l),[a,l])
|
|
3158
|
+
let d=[]
|
|
3159
|
+
if(null!==i)for(let t=0;t<e.length;++t)e[t]=he(e[t],i,"bilinear",!1)
|
|
3160
|
+
let u=new Int32Array(e[0].data.length)
|
|
3161
|
+
let h=new Float32Array(e[0].data.length)
|
|
3162
|
+
for(let n=0;n<e.length;++n){let s=t[n]
|
|
3163
|
+
for(let t=0;t<e[n].data.length;++t){e[n].data[t]*=s
|
|
3164
|
+
if(e[n].data[t]>h[t]){u[t]=n
|
|
3165
|
+
h[t]=e[n].data[t]}}}let _=0
|
|
3166
|
+
for(let o=0;o<n.length;++o){let i=n[o]
|
|
3167
|
+
let[a,l]=this.check_segment_validity(u,e,o,s,r)
|
|
3168
|
+
if(a){++_
|
|
3169
|
+
for(let e of l)c.data[e]=_
|
|
3170
|
+
d.push({id:_,label_id:i,score:t[o]})}}return[c,d]}post_process_panoptic_segmentation(e,t=.5,n=.5,s=.8,r=null,o=null){if(null===r){console.warn("`label_ids_to_fuse` unset. No instance will be fused.")
|
|
3171
|
+
r=new Set}const i=e.logits
|
|
3172
|
+
const a=e.pred_masks.sigmoid()
|
|
3173
|
+
let[l,c,d]=i.dims
|
|
3174
|
+
d-=1
|
|
3175
|
+
if(null!==o&&o.length!==l)throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits")
|
|
3176
|
+
let u=[]
|
|
3177
|
+
for(let e=0;e<l;++e){let l=null!==o?o[e]:null
|
|
3178
|
+
let c=i[e]
|
|
3179
|
+
let h=a[e]
|
|
3180
|
+
let[_,p,f]=this.remove_low_and_no_objects(c,h,t,d)
|
|
3181
|
+
if(0===f.length){let[e,t]=null!=l?l:h.dims.slice(-2)
|
|
3182
|
+
let n=new de("int32",new Int32Array(e*t).fill(-1),[e,t])
|
|
3183
|
+
u.push({segmentation:n,segments_info:[]})
|
|
3184
|
+
continue}let[m,g]=this.compute_segments(_,p,f,n,s,r,l)
|
|
3185
|
+
u.push({segmentation:m,segments_info:g})}return u}post_process_instance_segmentation(){throw Error("Not implemented yet")}}class ih extends $u{post_process_object_detection(...e){return Iu(...e)}}class ah extends $u{reshape_input_points(e,t,n){let s=a(e=structuredClone(e))
|
|
3186
|
+
if(3===s.length){s=[1,...s]
|
|
3187
|
+
e=[e]}else if(4!==s.length)throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.")
|
|
3188
|
+
for(let s=0;s<e.length;++s){let r=t[s]
|
|
3189
|
+
let o=n[s]
|
|
3190
|
+
let i=[o[0]/r[0],o[1]/r[1]]
|
|
3191
|
+
for(let t=0;t<e[s].length;++t)for(let n=0;n<e[s][t].length;++n)for(let r=0;r<e[s][t][n].length;++r)e[s][t][n][r]*=i[r]}return new de("float32",Float32Array.from(e.flat(1/0)),s)}add_input_labels(e,t){let n=a(e)
|
|
3192
|
+
if(2===n.length){n=[1,...n]
|
|
3193
|
+
e=[e]}else if(3!==n.length)throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.")
|
|
3194
|
+
if(n.some(((e,n)=>e!==t.dims[n])))throw Error(`The first ${n.length} dimensions of 'input_points' and 'input_labels' must be the same.`)
|
|
3195
|
+
return new de("int64",e.flat(1/0).map(BigInt),n)}async _call(e,t=null,n=null){const s=await super._call(e)
|
|
3196
|
+
t&&(s.input_points=this.reshape_input_points(t,s.original_sizes,s.reshaped_input_sizes))
|
|
3197
|
+
if(n){if(!s.input_points)throw Error("`input_points` must be provided if `input_labels` are provided.")
|
|
3198
|
+
s.input_labels=this.add_input_labels(n,s.input_points)}return s}post_process_masks(e,t,n,{mask_threshold:s=0,binarize:r=!0,pad_size:o=null}={}){var i
|
|
3199
|
+
const a=[]
|
|
3200
|
+
const l=[(o=null!=(i=o)?i:this.pad_size).height,o.width]
|
|
3201
|
+
for(let o=0;o<t.length;++o){const i=t[o]
|
|
3202
|
+
const c=n[o]
|
|
3203
|
+
const d=e[o]
|
|
3204
|
+
const u=[]
|
|
3205
|
+
for(let e=0;e<d.dims[0];++e){let t=he(d[e],l,"bilinear",!1)
|
|
3206
|
+
t=t.slice(null,[0,c[0]],[0,c[1]])
|
|
3207
|
+
t=he(t,i,"bilinear",!1)
|
|
3208
|
+
if(r){const e=new Uint8Array(t.data.length)
|
|
3209
|
+
for(let n=0;n<t.data.length;++n)t.data[n]>s&&(e[n]=1)
|
|
3210
|
+
t=new de("bool",e,t.dims)}u.push(t)}a.push(ye(u))}return a}}class lh extends $u{pad_image(e,t,n,s={}){const[r,o,i]=t
|
|
3211
|
+
return super.pad_image(e,t,{width:o+(n-o%n)%n,height:r+(n-r%n)%n},{mode:"symmetric",center:!1,constant_values:-1,...s})}}class ch extends $u{async _call(e,t){Array.isArray(e)||(e=[e])
|
|
3212
|
+
Array.isArray(t)||(t=[t])
|
|
3213
|
+
const n=await Promise.all(e.map((e=>this.preprocess(e))))
|
|
3214
|
+
const s=await Promise.all(t.map((e=>this.preprocess(e,{do_normalize:!1,do_convert_rgb:!1,do_convert_grayscale:!0}))))
|
|
3215
|
+
return{pixel_values:ye(n.map(((e,t)=>we([e.pixel_values,s[t].pixel_values],0))),0),original_sizes:n.map((e=>e.original_size)),reshaped_input_sizes:n.map((e=>e.reshaped_input_size))}}}class dh extends Nu{constructor(e){var t
|
|
3216
|
+
super(e)
|
|
3217
|
+
null!=(t=this.config).mel_filters||(t.mel_filters=Au(Math.floor(1+this.config.n_fft/2),this.config.feature_size,0,8e3,this.config.sampling_rate,"slaney","slaney"))
|
|
3218
|
+
this.window=zu(this.config.n_fft,"hann")}_extract_fbank_features(e){const{data:t,dims:n}=Fu(e,this.window,this.config.n_fft,this.config.hop_length,{power:2,mel_filters:this.config.mel_filters,log_mel:"log10",max_num_frames:this.config.nb_max_frames})
|
|
3219
|
+
const s=ee(t)[0]
|
|
3220
|
+
for(let e=0;e<t.length;++e)t[e]=(Math.max(t[e],s-8)+4)/4
|
|
3221
|
+
return{data:t,dims:n}}async _call(e){Lu(e,"WhisperFeatureExtractor")
|
|
3222
|
+
let t
|
|
3223
|
+
if(e.length>this.config.n_samples){console.warn("Attempting to extract features for audio longer than 30 seconds. If using a pipeline to extract transcript from a long audio clip, remember to specify `chunk_length_s` and/or `stride_length_s`.")
|
|
3224
|
+
t=e.slice(0,this.config.n_samples)}else{t=new Float32Array(this.config.n_samples)
|
|
3225
|
+
t.set(e)}const{data:n,dims:s}=this._extract_fbank_features(t)
|
|
3226
|
+
return{input_features:new de("float32",n,[1,...s])}}}class uh extends Nu{_zero_mean_unit_var_norm(e){const t=e.reduce(((e,t)=>e+t),0)/e.length
|
|
3227
|
+
const n=e.reduce(((e,n)=>e+(n-t)**2),0)/e.length
|
|
3228
|
+
return e.map((e=>(e-t)/Math.sqrt(n+1e-7)))}async _call(e){Lu(e,"Wav2Vec2FeatureExtractor")
|
|
3229
|
+
e instanceof Float64Array&&(e=new Float32Array(e))
|
|
3230
|
+
let t=e
|
|
3231
|
+
this.config.do_normalize&&(t=this._zero_mean_unit_var_norm(t))
|
|
3232
|
+
const n=[1,t.length]
|
|
3233
|
+
return{input_values:new de("float32",t,n),attention_mask:new de("int64",new BigInt64Array(t.length).fill(1n),n)}}}class hh extends Nu{constructor(e){super(e)
|
|
3234
|
+
const t=this.config.sampling_rate
|
|
3235
|
+
const n=Au(256,this.config.num_mel_bins,20,Math.floor(t/2),t,null,"kaldi",!0)
|
|
3236
|
+
for(let e=0;e<n.length;++e)n[e].push(0)
|
|
3237
|
+
this.mel_filters=n
|
|
3238
|
+
this.window=zu(400,"povey",{periodic:!1})}_extract_fbank_features(e,t){return Fu(e=e.map((e=>32768*e)),this.window,400,160,{fft_length:512,power:2,center:!1,preemphasis:.97,mel_filters:this.mel_filters,log_mel:"log",mel_floor:1.192092955078125e-7,remove_dc_offset:!0,max_num_frames:t,transpose:!0})}async _call(e,{padding:t=!0,pad_to_multiple_of:n=2,do_normalize_per_mel_bins:s=!0,return_attention_mask:r=!0}={}){Lu(e,"SeamlessM4TFeatureExtractor")
|
|
3239
|
+
let o=this._extract_fbank_features(e,this.config.max_length)
|
|
3240
|
+
if(s){const[e,t]=o.dims
|
|
3241
|
+
for(let n=0;n<t;++n){let s=0
|
|
3242
|
+
for(let r=0;r<e;++r)s+=o.data[r*t+n]
|
|
3243
|
+
const r=s/e
|
|
3244
|
+
let i=0
|
|
3245
|
+
for(let s=0;s<e;++s)i+=(o.data[s*t+n]-r)**2
|
|
3246
|
+
i/=e-1
|
|
3247
|
+
const a=Math.sqrt(i+1e-7)
|
|
3248
|
+
for(let s=0;s<e;++s){const e=s*t+n
|
|
3249
|
+
o.data[e]=(o.data[e]-r)/a}}}let i
|
|
3250
|
+
if(t){const[e,t]=o.dims
|
|
3251
|
+
const s=e%n
|
|
3252
|
+
if(s>0){const n=new Float32Array(t*(e+s))
|
|
3253
|
+
n.set(o.data)
|
|
3254
|
+
n.fill(this.config.padding_value,o.data.length)
|
|
3255
|
+
const a=e+s
|
|
3256
|
+
o={data:n,dims:[a,t]}
|
|
3257
|
+
if(r){i=new de("int64",new BigInt64Array(a),[1,a])
|
|
3258
|
+
i.data.fill(1n,0,e)}}}const[a,l]=o.dims
|
|
3259
|
+
const c=this.config.stride
|
|
3260
|
+
if(0!=a%c)throw new Error(`The number of frames (${a}) must be a multiple of the stride (${c}).`)
|
|
3261
|
+
const d=new de("float32",o.data,o.dims).view(1,Math.floor(a/c),l*c)
|
|
3262
|
+
const u={input_features:d}
|
|
3263
|
+
if(r){const e=d.dims[1]
|
|
3264
|
+
const t=new de("int64",new BigInt64Array(e),[1,e])
|
|
3265
|
+
if(i)for(let e=1,n=0;e<a;e+=c,++n)t.data[n]=i.data[e]
|
|
3266
|
+
else t.data.fill(1n)
|
|
3267
|
+
u.attention_mask=t}return u}}class _h extends Nu{constructor(e){super(e)
|
|
3268
|
+
const t=this.config.sampling_rate
|
|
3269
|
+
const n=Au(256,this.config.num_mel_bins,20,Math.floor(t/2),t,null,"kaldi",!0)
|
|
3270
|
+
for(let e=0;e<n.length;++e)n[e].push(0)
|
|
3271
|
+
this.mel_filters=n
|
|
3272
|
+
this.window=zu(400,"hann",{periodic:!1})
|
|
3273
|
+
this.mean=this.config.mean
|
|
3274
|
+
this.std=this.config.std}_extract_fbank_features(e,t){return Fu(e,this.window,400,160,{fft_length:512,power:2,center:!1,preemphasis:.97,mel_filters:this.mel_filters,log_mel:"log",mel_floor:1.192092955078125e-7,remove_dc_offset:!0,max_num_frames:t,transpose:!0})}async _call(e){Lu(e,"ASTFeatureExtractor")
|
|
3275
|
+
const t=this._extract_fbank_features(e,this.config.max_length)
|
|
3276
|
+
if(this.config.do_normalize){const e=2*this.std
|
|
3277
|
+
for(let n=0;n<t.data.length;++n)t.data[n]=(t.data[n]-this.mean)/e}return{input_values:new de("float32",t.data,[1,...t.dims])}}}class ph extends Nu{constructor(e){super(e)
|
|
3278
|
+
this.mel_filters=Au(this.config.nb_frequency_bins,this.config.feature_size,this.config.frequency_min,this.config.frequency_max,this.config.sampling_rate,null,"htk")
|
|
3279
|
+
this.mel_filters_slaney=Au(this.config.nb_frequency_bins,this.config.feature_size,this.config.frequency_min,this.config.frequency_max,this.config.sampling_rate,"slaney","slaney")
|
|
3280
|
+
this.window=zu(this.config.fft_window_size,"hann")}_get_input_mel(e,t,n,s){let r
|
|
3281
|
+
let o=!1
|
|
3282
|
+
const i=e.length-t
|
|
3283
|
+
if(i>0){if("rand_trunc"!==n)throw new Error(`Truncation strategy "${n}" not implemented`)
|
|
3284
|
+
{o=!0
|
|
3285
|
+
const n=Math.floor(Math.random()*(i+1))
|
|
3286
|
+
e=e.subarray(n,n+t)
|
|
3287
|
+
r=this._extract_fbank_features(e,this.mel_filters_slaney,this.config.nb_max_samples)
|
|
3288
|
+
r.dims=[1,...r.dims]}}else{if(i<0){let n=new Float64Array(t)
|
|
3289
|
+
n.set(e)
|
|
3290
|
+
if("repeat"===s)for(let s=e.length;s<t;s+=e.length)n.set(e.subarray(0,Math.min(e.length,t-s)),s)
|
|
3291
|
+
else if("repeatpad"===s)for(let t=e.length;t<-i;t+=e.length)n.set(e,t)
|
|
3292
|
+
e=n}if("fusion"===n)throw new Error(`Truncation strategy "${n}" not implemented`)
|
|
3293
|
+
r=this._extract_fbank_features(e,this.mel_filters_slaney,this.config.nb_max_samples)
|
|
3294
|
+
r.dims=[1,...r.dims]}return{...r,longer:o}}_extract_fbank_features(e,t,n=null){return Fu(e,this.window,this.config.fft_window_size,this.config.hop_length,{power:2,mel_filters:t,log_mel:"dB",max_num_frames:n,do_pad:!1,transpose:!0})}async _call(e,{max_length:t=null}={}){Lu(e,"ClapFeatureExtractor")
|
|
3295
|
+
const n=this._get_input_mel(e,null!=t?t:this.config.nb_max_samples,this.config.truncation,this.config.padding)
|
|
3296
|
+
return{input_features:new de("float32",n.data,[1,...n.dims])}}}class fh extends Nu{}class mh extends r{constructor(e){super()
|
|
3297
|
+
this.feature_extractor=e}async _call(e,...t){return await this.feature_extractor(e,...t)}}class gh extends mh{async _call(...e){return await this.feature_extractor(...e)}post_process_masks(...e){return this.feature_extractor.post_process_masks(...e)}reshape_input_points(...e){return this.feature_extractor.reshape_input_points(...e)}}class wh extends mh{async _call(e){return await this.feature_extractor(e)}}class yh extends mh{async _call(e){return await this.feature_extractor(e)}}class xh extends mh{async _call(e){return await this.feature_extractor(e)}}class bh extends mh{}class vh{static FEATURE_EXTRACTOR_CLASS_MAPPING={ImageFeatureExtractor:$u,WhisperFeatureExtractor:dh,ViTFeatureExtractor:Qu,MobileViTFeatureExtractor:Ju,OwlViTFeatureExtractor:Zu,Owlv2ImageProcessor:eh,CLIPFeatureExtractor:Vu,ChineseCLIPFeatureExtractor:Gu,SiglipImageProcessor:Wu,ConvNextFeatureExtractor:Xu,ConvNextImageProcessor:Hu,SegformerFeatureExtractor:ju,BitImageProcessor:qu,DPTImageProcessor:Ru,DPTFeatureExtractor:Du,GLPNFeatureExtractor:Uu,BeitFeatureExtractor:nh,DeiTFeatureExtractor:th,DetrFeatureExtractor:oh,YolosFeatureExtractor:ih,DonutFeatureExtractor:sh,NougatImageProcessor:rh,EfficientNetImageProcessor:Yu,ViTImageProcessor:Ku,VitMatteImageProcessor:ch,SamImageProcessor:ah,Swin2SRImageProcessor:lh,Wav2Vec2FeatureExtractor:uh,SeamlessM4TFeatureExtractor:hh,SpeechT5FeatureExtractor:fh,ASTFeatureExtractor:_h,ClapFeatureExtractor:ph}
|
|
3298
|
+
static PROCESSOR_CLASS_MAPPING={WhisperProcessor:wh,Wav2Vec2ProcessorWithLM:yh,SamProcessor:gh,SpeechT5Processor:xh,OwlViTProcessor:bh}
|
|
3299
|
+
static async from_pretrained(e,{progress_callback:t=null,config:n=null,cache_dir:s=null,local_files_only:r=!1,revision:o="main"}={}){var i,a
|
|
3300
|
+
let l=null!=n?n:await U(e,"preprocessor_config.json",!0,{progress_callback:t,config:n,cache_dir:s,local_files_only:r,revision:o})
|
|
3301
|
+
let c=null!=(i=l.feature_extractor_type)?i:l.image_processor_type
|
|
3302
|
+
let d=this.FEATURE_EXTRACTOR_CLASS_MAPPING[c]
|
|
3303
|
+
if(!d){if(void 0===l.size)throw new Error(`Unknown Feature Extractor type: ${c}`)
|
|
3304
|
+
console.warn(`Feature extractor type "${c}" not found, assuming ImageFeatureExtractor due to size parameter in config.`)
|
|
3305
|
+
d=$u}return new(null!=(a=this.PROCESSOR_CLASS_MAPPING[l.processor_class])?a:mh)(new d(l))}}async function kh(e){Array.isArray(e)||(e=[e])
|
|
3306
|
+
return await Promise.all(e.map((e=>bu.read(e))))}async function Mh(e,t){Array.isArray(e)||(e=[e])
|
|
3307
|
+
return await Promise.all(e.map((e=>"string"==typeof e||e instanceof URL?vu(e,t):e instanceof Float64Array?new Float32Array(e):e)))}function Th(e,t){t&&(e=e.map((e=>0|e)))
|
|
3308
|
+
const[n,s,r,o]=e
|
|
3309
|
+
return{xmin:n,ymin:s,xmax:r,ymax:o}}class Eh extends r{constructor({task:e,model:t,tokenizer:n=null,processor:s=null}){super()
|
|
3310
|
+
this.task=e
|
|
3311
|
+
this.model=t
|
|
3312
|
+
this.tokenizer=n
|
|
3313
|
+
this.processor=s}async dispose(){await this.model.dispose()}}class Ch extends Eh{constructor(e){super(e)}async _call(e,{topk:t=1}={}){const n=this.tokenizer(e,{padding:!0,truncation:!0})
|
|
3314
|
+
const s=await this.model(n)
|
|
3315
|
+
const r="multi_label_classification"===this.model.config.problem_type?e=>e.sigmoid().data:e=>X(e.data)
|
|
3316
|
+
const o=this.model.config.id2label
|
|
3317
|
+
const i=[]
|
|
3318
|
+
for(const e of s.logits){const n=K(r(e),t).map((e=>({label:o[e[0]],score:e[1]})))
|
|
3319
|
+
1===t?i.push(...n):i.push(n)}return Array.isArray(e)||1===t?i:i[0]}}class Ah extends Eh{constructor(e){super(e)}async _call(e,{ignore_labels:t=["O"]}={}){const n=Array.isArray(e)
|
|
3320
|
+
const s=this.tokenizer(n?e:[e],{padding:!0,truncation:!0})
|
|
3321
|
+
const r=(await this.model(s)).logits
|
|
3322
|
+
const o=this.model.config.id2label
|
|
3323
|
+
const i=[]
|
|
3324
|
+
for(let e=0;e<r.dims[0];++e){const n=s.input_ids[e]
|
|
3325
|
+
const a=r[e]
|
|
3326
|
+
const l=[]
|
|
3327
|
+
for(let e=0;e<a.dims[0];++e){const s=a[e]
|
|
3328
|
+
const r=ee(s.data)[1]
|
|
3329
|
+
const i=o?o[r]:`LABEL_${r}`
|
|
3330
|
+
if(t.includes(i))continue
|
|
3331
|
+
const c=this.tokenizer.decode([n[e].item()],{skip_special_tokens:!0})
|
|
3332
|
+
if(""===c)continue
|
|
3333
|
+
const d=X(s.data)
|
|
3334
|
+
l.push({entity:i,score:d[r],index:e,word:c,start:null,end:null})}i.push(l)}return n?i:i[0]}}class Sh extends Eh{constructor(e){super(e)}async _call(e,t,{topk:n=1}={}){const s=this.tokenizer(e,{text_pair:t,padding:!0,truncation:!0})
|
|
3335
|
+
const r=await this.model(s)
|
|
3336
|
+
const o=[]
|
|
3337
|
+
for(let e=0;e<r.start_logits.dims[0];++e){const t=s.input_ids[e]
|
|
3338
|
+
const i=t.indexOf(this.tokenizer.sep_token_id)
|
|
3339
|
+
const a=d(Array.from(X(r.start_logits[e].data)).map(((e,t)=>[e,t])).filter((e=>e[1]>i)),Array.from(X(r.end_logits[e].data)).map(((e,t)=>[e,t])).filter((e=>e[1]>i))).filter((e=>e[0][1]<=e[1][1])).map((e=>[e[0][1],e[1][1],e[0][0]*e[1][0]])).sort(((e,t)=>t[2]-e[2]))
|
|
3340
|
+
for(let e=0;e<Math.min(a.length,n);++e){const[n,s,r]=a[e]
|
|
3341
|
+
const i=[...t].slice(n,s+1)
|
|
3342
|
+
const l=this.tokenizer.decode(i,{skip_special_tokens:!0})
|
|
3343
|
+
o.push({answer:l,score:r})}}return 1===n?o[0]:o}}class Fh extends Eh{constructor(e){super(e)}async _call(e,{topk:t=5}={}){const n=this.tokenizer(e,{padding:!0,truncation:!0})
|
|
3344
|
+
const s=await this.model(n)
|
|
3345
|
+
const r=[]
|
|
3346
|
+
for(let e=0;e<n.input_ids.dims[0];++e){const o=n.input_ids[e]
|
|
3347
|
+
const i=o.indexOf(this.tokenizer.mask_token_id)
|
|
3348
|
+
if(-1===i)throw Error(`Mask token (${this.tokenizer.mask_token}) not found in text.`)
|
|
3349
|
+
const a=K(X(s.logits[e][i].data),t)
|
|
3350
|
+
r.push(a.map((e=>{const t=[...o]
|
|
3351
|
+
t[i]=e[0]
|
|
3352
|
+
return{score:e[1],token:e[0],token_str:this.tokenizer.model.vocab[e[0]],sequence:this.tokenizer.decode(t,{skip_special_tokens:!0})}})))}return Array.isArray(e)?r:r[0]}}class zh extends Eh{_key="generated_text"
|
|
3353
|
+
constructor(e){super(e)}async _call(e,t={}){Array.isArray(e)||(e=[e])
|
|
3354
|
+
this.model.config.prefix&&(e=e.map((e=>this.model.config.prefix+e)))
|
|
3355
|
+
const n=this.model.config.task_specific_params
|
|
3356
|
+
n&&n[this.task]&&n[this.task].prefix&&(e=e.map((e=>n[this.task].prefix+e)))
|
|
3357
|
+
const s=this.tokenizer
|
|
3358
|
+
const r={padding:!0,truncation:!0}
|
|
3359
|
+
let o
|
|
3360
|
+
o=this instanceof Ih&&"_build_translation_inputs"in s?s._build_translation_inputs(e,r,t).input_ids:s(e,r).input_ids
|
|
3361
|
+
const i=await this.model.generate(o,t)
|
|
3362
|
+
return s.batch_decode(i,{skip_special_tokens:!0}).map((e=>({[this._key]:e})))}}class Ph extends zh{_key="summary_text"
|
|
3363
|
+
constructor(e){super(e)}}class Ih extends zh{_key="translation_text"
|
|
3364
|
+
constructor(e){super(e)}}function Lh(e){return Array.isArray(e)&&e.every((e=>"role"in e&&"content"in e))}class Bh extends Eh{constructor(e){super(e)}async _call(e,t={}){var n,s
|
|
3365
|
+
let r=!1
|
|
3366
|
+
let o=!1
|
|
3367
|
+
let i
|
|
3368
|
+
if("string"==typeof e)i=e=[e]
|
|
3369
|
+
else if(Array.isArray(e)&&e.every((e=>"string"==typeof e))){r=!0
|
|
3370
|
+
i=e}else{if(Lh(e))e=[e]
|
|
3371
|
+
else{if(!Array.isArray(e)||!e.every(Lh))throw new Error("Input must be a string, an array of strings, a Chat, or an array of Chats")
|
|
3372
|
+
r=!0}o=!0
|
|
3373
|
+
i=e.map((e=>this.tokenizer.apply_chat_template(e,{tokenize:!1,add_generation_prompt:!0})))}const a=null!=(n=t.add_special_tokens)&&n
|
|
3374
|
+
const l=!o&&(null==(s=t.return_full_text)||s)
|
|
3375
|
+
this.tokenizer.padding_side="left"
|
|
3376
|
+
const{input_ids:c,attention_mask:d}=this.tokenizer(i,{add_special_tokens:a,padding:!0,truncation:!0})
|
|
3377
|
+
const u=await this.model.generate(c,t,null,{inputs_attention_mask:d})
|
|
3378
|
+
let h=this.tokenizer.batch_decode(u,{skip_special_tokens:!0})
|
|
3379
|
+
let _
|
|
3380
|
+
!l&&c.dims.at(-1)>0&&(_=this.tokenizer.batch_decode(c,{skip_special_tokens:!0}).map((e=>e.length)))
|
|
3381
|
+
const p=Array.from({length:e.length},(e=>[]))
|
|
3382
|
+
for(let t=0;t<h.length;++t){const n=Math.floor(t/u.length*e.length)
|
|
3383
|
+
_&&(h[t]=h[t].slice(_[n]))
|
|
3384
|
+
p[n].push({generated_text:o?[...e[n],{role:"assistant",content:h[t]}]:h[t]})}return r||1!==p.length?p:p[0]}}class Oh extends Eh{constructor(e){var t
|
|
3385
|
+
super(e)
|
|
3386
|
+
this.label2id=Object.fromEntries(Object.entries(this.model.config.label2id).map((([e,t])=>[e.toLowerCase(),t])))
|
|
3387
|
+
this.entailment_id=this.label2id.entailment
|
|
3388
|
+
if(void 0===this.entailment_id){console.warn("Could not find 'entailment' in label2id mapping. Using 2 as entailment_id.")
|
|
3389
|
+
this.entailment_id=2}this.contradiction_id=null!=(t=this.label2id.contradiction)?t:this.label2id.not_entailment
|
|
3390
|
+
if(void 0===this.contradiction_id){console.warn("Could not find 'contradiction' in label2id mapping. Using 0 as contradiction_id.")
|
|
3391
|
+
this.contradiction_id=0}}async _call(e,t,{hypothesis_template:n="This example is {}.",multi_label:s=!1}={}){const r=Array.isArray(e)
|
|
3392
|
+
r||(e=[e])
|
|
3393
|
+
Array.isArray(t)||(t=[t])
|
|
3394
|
+
const o=t.map((e=>n.replace("{}",e)))
|
|
3395
|
+
const i=s||1===t.length
|
|
3396
|
+
const a=[]
|
|
3397
|
+
for(const n of e){const e=[]
|
|
3398
|
+
for(const t of o){const s=this.tokenizer(n,{text_pair:t,padding:!0,truncation:!0})
|
|
3399
|
+
const r=await this.model(s)
|
|
3400
|
+
i?e.push([r.logits.data[this.contradiction_id],r.logits.data[this.entailment_id]]):e.push(r.logits.data[this.entailment_id])}const s=(i?e.map((e=>X(e)[1])):X(e)).map(((e,t)=>[e,t])).sort(((e,t)=>t[0]-e[0]))
|
|
3401
|
+
a.push({sequence:n,labels:s.map((e=>t[e[1]])),scores:s.map((e=>e[0]))})}return r?a:a[0]}}class Nh extends Eh{constructor(e){super(e)}async _call(e,{pooling:t="none",normalize:n=!1,quantize:s=!1,precision:r="binary"}={}){var o
|
|
3402
|
+
const i=this.tokenizer(e,{padding:!0,truncation:!0})
|
|
3403
|
+
const a=await this.model(i)
|
|
3404
|
+
let l=null!=(o=a.last_hidden_state)?o:a.logits
|
|
3405
|
+
if("none"===t);else if("mean"===t)l=_e(l,i.attention_mask)
|
|
3406
|
+
else{if("cls"!==t)throw Error(`Pooling method '${t}' not supported.`)
|
|
3407
|
+
l=l.slice(null,0)}n&&(l=l.normalize(2,-1))
|
|
3408
|
+
s&&(l=Te(l,r))
|
|
3409
|
+
return l}}class $h extends Eh{constructor(e){super(e)}async _call(e,{pool:t=null}={}){const n=await kh(e)
|
|
3410
|
+
const{pixel_values:s}=await this.processor(n)
|
|
3411
|
+
const r=await this.model({pixel_values:s})
|
|
3412
|
+
let o
|
|
3413
|
+
if(t){if(!("pooler_output"in r))throw Error("No pooled output was returned. Make sure the model has a 'pooler' layer when using the 'pool' option.")
|
|
3414
|
+
o=r.pooler_output}else{var i,a
|
|
3415
|
+
o=null!=(i=null!=(a=r.last_hidden_state)?a:r.logits)?i:r.image_embeds}return o}}class jh extends Eh{constructor(e){super(e)}async _call(e,{topk:t=null}={}){const n=!Array.isArray(e)
|
|
3416
|
+
const s=this.processor.feature_extractor.config.sampling_rate
|
|
3417
|
+
const r=await Mh(e,s)
|
|
3418
|
+
const o=this.model.config.id2label
|
|
3419
|
+
const i=[]
|
|
3420
|
+
for(const e of r){const n=await this.processor(e)
|
|
3421
|
+
const s=K(X((await this.model(n)).logits[0].data),t).map((e=>({label:o[e[0]],score:e[1]})))
|
|
3422
|
+
1===t?i.push(...s):i.push(s)}return n&&1!==t?i[0]:i}}class Dh extends Eh{constructor(e){super(e)}async _call(e,t,{hypothesis_template:n="This is a sound of {}."}={}){const s=!Array.isArray(e)
|
|
3423
|
+
s&&(e=[e])
|
|
3424
|
+
const r=t.map((e=>n.replace("{}",e)))
|
|
3425
|
+
const o=this.tokenizer(r,{padding:!0,truncation:!0})
|
|
3426
|
+
const i=this.processor.feature_extractor.config.sampling_rate
|
|
3427
|
+
const a=await Mh(e,i)
|
|
3428
|
+
const l=[]
|
|
3429
|
+
for(const e of a){const n=await this.processor(e)
|
|
3430
|
+
const s=X((await this.model({...o,...n})).logits_per_audio.data)
|
|
3431
|
+
l.push([...s].map(((e,n)=>({score:e,label:t[n]}))))}return s?l[0]:l}}class Rh extends Eh{constructor(e){super(e)}async _call(e,t={}){switch(this.model.config.model_type){case"whisper":return this._call_whisper(e,t)
|
|
3432
|
+
case"wav2vec2":case"wav2vec2-bert":case"unispeech":case"unispeech-sat":case"hubert":return this._call_wav2vec2(e,t)
|
|
3433
|
+
default:throw new Error(`AutomaticSpeechRecognitionPipeline does not support model type '${this.model.config.model_type}'.`)}}async _call_wav2vec2(e,t={}){t.language&&console.warn('`language` parameter is not yet supported for `wav2vec2` models, defaulting to "English".')
|
|
3434
|
+
t.task&&console.warn('`task` parameter is not yet supported for `wav2vec2` models, defaulting to "transcribe".')
|
|
3435
|
+
const n=!Array.isArray(e)
|
|
3436
|
+
n&&(e=[e])
|
|
3437
|
+
const s=this.processor.feature_extractor.config.sampling_rate
|
|
3438
|
+
const r=await Mh(e,s)
|
|
3439
|
+
const o=[]
|
|
3440
|
+
for(const e of r){const t=await this.processor(e)
|
|
3441
|
+
const n=(await this.model(t)).logits[0]
|
|
3442
|
+
const s=[]
|
|
3443
|
+
for(const e of n)s.push(ee(e.data)[1])
|
|
3444
|
+
const r=this.tokenizer.decode(s)
|
|
3445
|
+
o.push({text:r})}return n?o[0]:o}async _call_whisper(e,t={}){var n,s,r,o,i
|
|
3446
|
+
const a=null!=(n=t.return_timestamps)&&n
|
|
3447
|
+
const c=null!=(s=t.chunk_length_s)?s:0
|
|
3448
|
+
const d=null!=(r=t.chunk_callback)?r:null
|
|
3449
|
+
const u=null!=(o=t.force_full_sequences)&&o
|
|
3450
|
+
let h=null!=(i=t.stride_length_s)?i:null
|
|
3451
|
+
"word"===a&&(t.return_token_timestamps=!0)
|
|
3452
|
+
const _=l(t,"language",null)
|
|
3453
|
+
const p=l(t,"task",null)
|
|
3454
|
+
if(_||p||a){if(t.forced_decoder_ids)throw new Error("Cannot specify `language`/`task`/`return_timestamps` and `forced_decoder_ids` at the same time.")
|
|
3455
|
+
const e=this.tokenizer.get_decoder_prompt_ids({language:_,task:p,no_timestamps:!a})
|
|
3456
|
+
e.length>0&&(t.forced_decoder_ids=e)}const f=!Array.isArray(e)
|
|
3457
|
+
f&&(e=[e])
|
|
3458
|
+
const m=this.processor.feature_extractor.config.chunk_length/this.model.config.max_source_positions
|
|
3459
|
+
const g=this.processor.feature_extractor.config.hop_length
|
|
3460
|
+
const w=this.processor.feature_extractor.config.sampling_rate
|
|
3461
|
+
const y=await Mh(e,w)
|
|
3462
|
+
const x=[]
|
|
3463
|
+
for(const e of y){let n=[]
|
|
3464
|
+
if(c>0){if(null===h)h=c/6
|
|
3465
|
+
else if(c<=h)throw Error("`chunk_length_s` must be larger than `stride_length_s`.")
|
|
3466
|
+
const t=w*c
|
|
3467
|
+
const s=w*h
|
|
3468
|
+
const r=t-2*s
|
|
3469
|
+
let o=0
|
|
3470
|
+
for(;o<e.length;){const i=e.subarray(o,o+t)
|
|
3471
|
+
const a=await this.processor(i)
|
|
3472
|
+
const l=0===o
|
|
3473
|
+
const c=o+r>=e.length
|
|
3474
|
+
n.push({stride:[i.length,l?0:s,c?0:s],input_features:a.input_features,is_last:c})
|
|
3475
|
+
o+=r}}else n=[{stride:[e.length,0,0],input_features:(await this.processor(e)).input_features,is_last:!0}]
|
|
3476
|
+
for(const e of n){t.num_frames=Math.floor(e.stride[0]/g)
|
|
3477
|
+
const n=await this.model.generate(e.input_features,t)
|
|
3478
|
+
if("word"===a){e.tokens=n.sequences[0]
|
|
3479
|
+
e.token_timestamps=n.token_timestamps.tolist()[0].map((e=>ie(e,2)))}else e.tokens=n[0]
|
|
3480
|
+
e.stride=e.stride.map((e=>e/w))
|
|
3481
|
+
null!==d&&d(e)}const[s,r]=this.tokenizer._decode_asr(n,{time_precision:m,return_timestamps:a,force_full_sequences:u})
|
|
3482
|
+
x.push({text:s,...r})}return f?x[0]:x}}class qh extends Eh{constructor(e){super(e)}async _call(e,t={}){const n=Array.isArray(e)
|
|
3483
|
+
const s=await kh(e)
|
|
3484
|
+
const{pixel_values:r}=await this.processor(s)
|
|
3485
|
+
const o=[]
|
|
3486
|
+
for(const e of r){e.dims=[1,...e.dims]
|
|
3487
|
+
const n=await this.model.generate(e,t)
|
|
3488
|
+
const s=this.tokenizer.batch_decode(n,{skip_special_tokens:!0}).map((e=>({generated_text:e.trim()})))
|
|
3489
|
+
o.push(s)}return n?o:o[0]}}class Uh extends Eh{constructor(e){super(e)}async _call(e,{topk:t=1}={}){const n=Array.isArray(e)
|
|
3490
|
+
const s=await kh(e)
|
|
3491
|
+
const{pixel_values:r}=await this.processor(s)
|
|
3492
|
+
const o=await this.model({pixel_values:r})
|
|
3493
|
+
const i=this.model.config.id2label
|
|
3494
|
+
const a=[]
|
|
3495
|
+
for(const e of o.logits){const n=K(X(e.data),t).map((e=>({label:i[e[0]],score:e[1]})))
|
|
3496
|
+
1===t?a.push(...n):a.push(n)}return n||1===t?a:a[0]}}class Vh extends Eh{constructor(e){super(e)
|
|
3497
|
+
this.subtasks_mapping={panoptic:"post_process_panoptic_segmentation",instance:"post_process_instance_segmentation",semantic:"post_process_semantic_segmentation"}}async _call(e,{threshold:t=.5,mask_threshold:n=.5,overlap_mask_area_threshold:s=.8,label_ids_to_fuse:r=null,target_sizes:o=null,subtask:i=null}={}){if(Array.isArray(e)&&1!==e.length)throw Error("Image segmentation pipeline currently only supports a batch size of 1.")
|
|
3498
|
+
const a=await kh(e)
|
|
3499
|
+
const l=a.map((e=>[e.height,e.width]))
|
|
3500
|
+
const{pixel_values:c,pixel_mask:d}=await this.processor(a)
|
|
3501
|
+
const u=await this.model({pixel_values:c,pixel_mask:d})
|
|
3502
|
+
let h=null
|
|
3503
|
+
if(null!==i)h=this.subtasks_mapping[i]
|
|
3504
|
+
else for(let[e,t]of Object.entries(this.subtasks_mapping))if(t in this.processor.feature_extractor){h=this.processor.feature_extractor[t].bind(this.processor.feature_extractor)
|
|
3505
|
+
i=e
|
|
3506
|
+
break}const _=this.model.config.id2label
|
|
3507
|
+
const p=[]
|
|
3508
|
+
if("panoptic"===i||"instance"===i){const e=h(u,t,n,s,r,null!=o?o:l)[0]
|
|
3509
|
+
const i=e.segmentation
|
|
3510
|
+
for(const t of e.segments_info){const e=new Uint8ClampedArray(i.data.length)
|
|
3511
|
+
for(let n=0;n<i.data.length;++n)i.data[n]===t.id&&(e[n]=255)
|
|
3512
|
+
const n=new bu(e,i.dims[1],i.dims[0],1)
|
|
3513
|
+
p.push({score:t.score,label:_[t.label_id],mask:n})}}else{if("semantic"!==i)throw Error(`Subtask ${i} not supported.`)
|
|
3514
|
+
{const{segmentation:e,labels:t}=h(u,null!=o?o:l)[0]
|
|
3515
|
+
for(const n of t){const t=new Uint8ClampedArray(e.data.length)
|
|
3516
|
+
for(let s=0;s<e.data.length;++s)e.data[s]===n&&(t[s]=255)
|
|
3517
|
+
const s=new bu(t,e.dims[1],e.dims[0],1)
|
|
3518
|
+
p.push({score:null,label:_[n],mask:s})}}}return p}}class Gh extends Eh{constructor(e){super(e)}async _call(e,t,{hypothesis_template:n="This is a photo of {}"}={}){const s=Array.isArray(e)
|
|
3519
|
+
const r=await kh(e)
|
|
3520
|
+
const o=t.map((e=>n.replace("{}",e)))
|
|
3521
|
+
const i=this.tokenizer(o,{padding:"siglip"!==this.model.config.model_type||"max_length",truncation:!0})
|
|
3522
|
+
const{pixel_values:a}=await this.processor(r)
|
|
3523
|
+
const l=await this.model({...i,pixel_values:a})
|
|
3524
|
+
const c="siglip"===this.model.config.model_type?e=>e.sigmoid().data:e=>X(e.data)
|
|
3525
|
+
const d=[]
|
|
3526
|
+
for(const e of l.logits_per_image){const n=[...c(e)].map(((e,n)=>({score:e,label:t[n]})))
|
|
3527
|
+
n.sort(((e,t)=>t.score-e.score))
|
|
3528
|
+
d.push(n)}return s?d:d[0]}}class Wh extends Eh{constructor(e){super(e)}async _call(e,{threshold:t=.9,percentage:n=!1}={}){const s=Array.isArray(e)
|
|
3529
|
+
if(s&&1!==e.length)throw Error("Object detection pipeline currently only supports a batch size of 1.")
|
|
3530
|
+
const r=await kh(e)
|
|
3531
|
+
const o=n?null:r.map((e=>[e.height,e.width]))
|
|
3532
|
+
const{pixel_values:i,pixel_mask:a}=await this.processor(r)
|
|
3533
|
+
const l=await this.model({pixel_values:i,pixel_mask:a})
|
|
3534
|
+
const c=this.processor.feature_extractor.post_process_object_detection(l,t,o)
|
|
3535
|
+
const d=this.model.config.id2label
|
|
3536
|
+
const u=c.map((e=>e.boxes.map(((t,s)=>({score:e.scores[s],label:d[e.classes[s]],box:Th(t,!n)})))))
|
|
3537
|
+
return s?u:u[0]}}class Xh extends Eh{constructor(e){super(e)}async _call(e,t,{threshold:n=.1,topk:s=null,percentage:r=!1}={}){const o=Array.isArray(e)
|
|
3538
|
+
const i=await kh(e)
|
|
3539
|
+
const a=this.tokenizer(t,{padding:!0,truncation:!0})
|
|
3540
|
+
const l=await this.processor(i)
|
|
3541
|
+
const c=[]
|
|
3542
|
+
for(let e=0;e<i.length;++e){const o=i[e]
|
|
3543
|
+
const d=r?null:[[o.height,o.width]]
|
|
3544
|
+
const u=l.pixel_values[e].unsqueeze_(0)
|
|
3545
|
+
const h=await this.model({...a,pixel_values:u})
|
|
3546
|
+
const _=this.processor.feature_extractor.post_process_object_detection(h,n,d,!0)[0]
|
|
3547
|
+
let p=_.boxes.map(((e,n)=>({score:_.scores[n],label:t[_.classes[n]],box:Th(e,!r)}))).sort(((e,t)=>t.score-e.score))
|
|
3548
|
+
null!==s&&(p=p.slice(0,s))
|
|
3549
|
+
c.push(p)}return o?c:c[0]}}class Hh extends Eh{constructor(e){super(e)}async _call(e,t,n={}){const s=(await kh(e))[0]
|
|
3550
|
+
const{pixel_values:r}=await this.processor(s)
|
|
3551
|
+
const o=`<s_docvqa><s_question>${t}</s_question><s_answer>`
|
|
3552
|
+
const i=this.tokenizer(o,{add_special_tokens:!1,padding:!0,truncation:!0}).input_ids
|
|
3553
|
+
const a=await this.model.generate(r,{...n,decoder_input_ids:i,max_length:this.model.config.decoder.max_position_embeddings})
|
|
3554
|
+
const l=this.tokenizer.batch_decode(a)[0].match(/<s_answer>(.*?)<\/s_answer>/)
|
|
3555
|
+
let c=null
|
|
3556
|
+
l&&l.length>=2&&(c=l[1].trim())
|
|
3557
|
+
return[{answer:c}]}}class Qh extends Eh{DEFAULT_VOCODER_ID="Xenova/speecht5_hifigan"
|
|
3558
|
+
constructor(e){var t
|
|
3559
|
+
super(e)
|
|
3560
|
+
this.vocoder=null!=(t=e.vocoder)?t:null}async _call(e,{speaker_embeddings:t=null}={}){return this.processor?this._call_text_to_spectrogram(e,{speaker_embeddings:t}):this._call_text_to_waveform(e)}async _call_text_to_waveform(e){const t=this.tokenizer(e,{padding:!0,truncation:!0})
|
|
3561
|
+
const{waveform:n}=await this.model(t)
|
|
3562
|
+
const s=this.model.config.sampling_rate
|
|
3563
|
+
return{audio:n.data,sampling_rate:s}}async _call_text_to_spectrogram(e,{speaker_embeddings:t}){if(!this.vocoder){console.log("No vocoder specified, using default HifiGan vocoder.")
|
|
3564
|
+
this.vocoder=await zd.from_pretrained(this.DEFAULT_VOCODER_ID,{quantized:!1})}("string"==typeof t||t instanceof URL)&&(t=new Float32Array(await(await fetch(t)).arrayBuffer()))
|
|
3565
|
+
if(t instanceof Float32Array)t=new de("float32",t,[1,t.length])
|
|
3566
|
+
else if(!(t instanceof de))throw new Error("Speaker embeddings must be a `Tensor`, `Float32Array`, `string`, or `URL`.")
|
|
3567
|
+
const{input_ids:n}=this.tokenizer(e,{padding:!0,truncation:!0})
|
|
3568
|
+
const{waveform:s}=await this.model.generate_speech(n,t,{vocoder:this.vocoder})
|
|
3569
|
+
const r=this.processor.feature_extractor.config.sampling_rate
|
|
3570
|
+
return{audio:s.data,sampling_rate:r}}}class Kh extends Eh{constructor(e){super(e)}async _call(e){const t=await kh(e)
|
|
3571
|
+
const n=await this.processor(t)
|
|
3572
|
+
const s=await this.model(n)
|
|
3573
|
+
const r=[]
|
|
3574
|
+
for(const e of s.reconstruction){const t=e.squeeze().clamp_(0,1).mul_(255).round_().to("uint8")
|
|
3575
|
+
r.push(bu.fromTensor(t))}return r.length>1?r:r[0]}}class Yh extends Eh{constructor(e){super(e)}async _call(e){const t=await kh(e)
|
|
3576
|
+
const n=await this.processor(t)
|
|
3577
|
+
const{predicted_depth:s}=await this.model(n)
|
|
3578
|
+
const r=[]
|
|
3579
|
+
for(let e=0;e<t.length;++e){const n=he(s[e],t[e].size.reverse(),"bilinear",!1)
|
|
3580
|
+
const o=n.mul_(255/ee(n.data)[0]).to("uint8")
|
|
3581
|
+
r.push({predicted_depth:s[e],depth:bu.fromTensor(o)})}return r.length>1?r:r[0]}}const Jh=Object.freeze({"text-classification":{tokenizer:Cs,pipeline:Ch,model:Pd,default:{model:"Xenova/distilbert-base-uncased-finetuned-sst-2-english"},type:"text"},"token-classification":{tokenizer:Cs,pipeline:Ah,model:Id,default:{model:"Xenova/bert-base-multilingual-cased-ner-hrl"},type:"text"},"question-answering":{tokenizer:Cs,pipeline:Sh,model:Dd,default:{model:"Xenova/distilbert-base-cased-distilled-squad"},type:"text"},"fill-mask":{tokenizer:Cs,pipeline:Fh,model:jd,default:{model:"Xenova/bert-base-uncased"},type:"text"},summarization:{tokenizer:Cs,pipeline:Ph,model:Ld,default:{model:"Xenova/distilbart-cnn-6-6"},type:"text"},translation:{tokenizer:Cs,pipeline:Ih,model:Ld,default:{model:"Xenova/t5-small"},type:"text"},"text2text-generation":{tokenizer:Cs,pipeline:zh,model:Ld,default:{model:"Xenova/flan-t5-small"},type:"text"},"text-generation":{tokenizer:Cs,pipeline:Bh,model:$d,default:{model:"Xenova/gpt2"},type:"text"},"zero-shot-classification":{tokenizer:Cs,pipeline:Oh,model:Pd,default:{model:"Xenova/distilbert-base-uncased-mnli"},type:"text"},"audio-classification":{pipeline:jh,model:Qd,processor:vh,default:{model:"Xenova/wav2vec2-base-superb-ks"},type:"audio"},"zero-shot-audio-classification":{tokenizer:Cs,pipeline:Dh,model:zd,processor:vh,default:{model:"Xenova/clap-htsat-unfused"},type:"multimodal"},"automatic-speech-recognition":{tokenizer:Cs,pipeline:Rh,model:[Bd,Hd],processor:vh,default:{model:"Xenova/whisper-tiny.en"},type:"multimodal"},"text-to-audio":{tokenizer:Cs,pipeline:Qh,model:[Nd,Od],processor:[vh,null],default:{model:"Xenova/speecht5_tts"},type:"text"},"image-to-text":{tokenizer:Cs,pipeline:qh,model:Rd,processor:vh,default:{model:"Xenova/vit-gpt2-image-captioning"},type:"multimodal"},"image-classification":{pipeline:Uh,model:qd,processor:vh,default:{model:"Xenova/vit-base-patch16-224"},type:"multimodal"},"image-segmentation":{pipeline:Vh,model:[Ud,Vd],processor:vh,default:{model:"Xenova/detr-resnet-50-panoptic"},type:"multimodal"},"zero-shot-image-classification":{tokenizer:Cs,pipeline:Gh,model:zd,processor:vh,default:{model:"Xenova/clip-vit-base-patch32"},type:"multimodal"},"object-detection":{pipeline:Wh,model:Gd,processor:vh,default:{model:"Xenova/detr-resnet-50"},type:"multimodal"},"zero-shot-object-detection":{tokenizer:Cs,pipeline:Xh,model:Wd,processor:vh,default:{model:"Xenova/owlvit-base-patch32"},type:"multimodal"},"document-question-answering":{tokenizer:Cs,pipeline:Hh,model:Jd,processor:vh,default:{model:"Xenova/donut-base-finetuned-docvqa"},type:"multimodal"},"image-to-image":{pipeline:Kh,model:eu,processor:vh,default:{model:"Xenova/swin2SR-classical-sr-x2-64"},type:"image"},"depth-estimation":{pipeline:Yh,model:tu,processor:vh,default:{model:"Xenova/dpt-large"},type:"image"},"feature-extraction":{tokenizer:Cs,pipeline:Nh,model:zd,default:{model:"Xenova/all-MiniLM-L6-v2"},type:"text"},"image-feature-extraction":{processor:vh,pipeline:$h,model:[nu,zd],default:{model:"Xenova/vit-base-patch16-224-in21k"},type:"image"}})
|
|
3582
|
+
const Zh=Object.freeze({"sentiment-analysis":"text-classification",ner:"token-classification",asr:"automatic-speech-recognition","text-to-speech":"text-to-audio",embeddings:"feature-extraction"})
|
|
3583
|
+
async function e_(e,n=null,{quantized:s=!0,progress_callback:r=null,config:o=null,cache_dir:i=null,local_files_only:a=!1,revision:l="main"}={}){var c
|
|
3584
|
+
e=null!=(c=Zh[e])?c:e
|
|
3585
|
+
const d=Jh[e.split("_",1)[0]]
|
|
3586
|
+
if(!d)throw Error(`Unsupported pipeline: ${e}. Must be one of [${Object.keys(Jh)}]`)
|
|
3587
|
+
if(!n){n=d.default.model
|
|
3588
|
+
console.log(`No model specified. Using default model: "${n}".`)}const u={quantized:s,progress_callback:r,config:o,cache_dir:i,local_files_only:a,revision:l}
|
|
3589
|
+
const h=new Map([["tokenizer",d.tokenizer],["model",d.model],["processor",d.processor]])
|
|
3590
|
+
const _=await async function(e,t,n){const s=Object.create(null)
|
|
3591
|
+
const r=[]
|
|
3592
|
+
for(let[o,i]of e.entries()){if(!i)continue
|
|
3593
|
+
let e
|
|
3594
|
+
e=Array.isArray(i)?new Promise((async(e,s)=>{let r
|
|
3595
|
+
for(let s of i){if(null===s){e(null)
|
|
3596
|
+
return}try{e(await s.from_pretrained(t,n))
|
|
3597
|
+
return}catch(e){r=e}}s(r)})):i.from_pretrained(t,n)
|
|
3598
|
+
s[o]=e
|
|
3599
|
+
r.push(e)}await Promise.all(r)
|
|
3600
|
+
for(let[e,t]of Object.entries(s))s[e]=await t
|
|
3601
|
+
return s}(h,n,u)
|
|
3602
|
+
_.task=e
|
|
3603
|
+
t(r,{status:"ready",task:e,model:n})
|
|
3604
|
+
return new(0,d.pipeline)(_)}})()
|
|
3605
|
+
module.exports=o})()
|