Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ant-tfjs-lite

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ant-tfjs-lite - npm Package Compare versions

Comparing version 0.1.25 to 0.1.26

2

dist/index.js

@@ -1,2 +0,2 @@

!function(e){"use strict";var t,r={enabled:!1,setDebug:function(e){r.enabled=!!e},enable:function(){r.setDebug(!0)},disable:function(){r.setDebug(!1)},log:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];r.enabled&&console.log.apply(console,e)},info:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];r.enabled&&console.info.apply(console,e)},error:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];r.enabled&&console.error.apply(console,e)},warn:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];r.enabled&&console.log.apply(console,e)}},n={modelMap:{},register:function(e,t){if(e in n.modelMap)throw new Error(e+" already exists!");n.modelMap[e]=t},getModel:function(e){return n.modelMap[e]}};e.ModelType=void 0,(t=e.ModelType||(e.ModelType={})).tfModel="tfModel",t.ONNX="onnx",t.CUSTOM="custom",t.KMeans="KMeans",t.LR="lr";var o=function(e,t){return o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},o(e,t)};function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}var s,a,l,u,c,p,f,d=function(){return d=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},d.apply(this,arguments)};function h(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{l(n.next(e))}catch(e){i(e)}}function a(e){try{l(n.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}l((n=n.apply(e,t||[])).next())}))}function v(e,t){var r,n,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}}function y(e,t,r){if(r||2===arguments.length)for(var n,o=0,i=t.length;o<i;o++)!n&&o in t||(n||(n=Array.prototype.slice.call(t,0,o)),n[o]=t[o]);return e.concat(n||Array.prototype.slice.call(t))}!function(e){e.R0="R0",e.R1="R1",e.R2="R2",e.R3="R3",e.R4="R4",e.R5="R5",e.R6="R6"}(s||(s={})),function(e){e.float32="float32",e.int32="int32",e.bool="int32",e.complex64="complex64"}(a||(a={})),function(e){e.float32="float32",e.int32="int32",e.bool="bool",e.complex64="complex64"}(l||(l={})),function(e){e.float32="float32",e.int32="float32",e.bool="float32",e.complex64="complex64"}(u||(u={})),function(e){e.float32="complex64",e.int32="complex64",e.bool="complex64",e.complex64="complex64"}(c||(c={})),e.BACKEND_TYPE=void 0,(p=e.BACKEND_TYPE||(e.BACKEND_TYPE={})).CPU="cpu",p.WEBGL="webgl",p.WASM="wasm",function(e){e.ENV_INIT="aliphax.worker.env.init",e.INIT="alphax.worker.init",e.LOAD="alphax.worker.load",e.Execute="alphax.worker.execute",e.REG="alphax.worker.reg"}(f||(f={}));var m=function(){function t(t){this.backend=e.BACKEND_TYPE.CPU,this.useWorker=!1;var r={backend:e.BACKEND_TYPE.CPU},n=d(d({},t||{}),r).backend;this.backend=n}return t.prototype.loadUrl=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];throw new Error("Please impl the loadUrl function.")},t.prototype.load=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];throw new Error("Please impl the load function.")},t.prototype.fetchWorkerModel=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];throw new Error("Please impl the fetchWorkerModel function.")},t.prototype.execute=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];throw new Error("Please implement the execute function.")},t.prototype.setBackend=function(e){this.backend=e},t.prototype.getModelType=function(){return this.type},t}();function w(e){return null!=e}function g(e){return"claim_demo_num"===e?"claim_demo_one":"select_family_num"===e?"select_family_one":e}function M(e,t){var r,n="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),o=[];if(t=t||n.length,e)for(r=0;r<e;r++)o[r]=n[0|Math.random()*t];else{var i=void 0;for(o[8]=o[13]=o[18]=o[23]="-",o[14]="4",r=0;r<36;r++)o[r]||(i=0|16*Math.random(),o[r]=n[19===r?3&i|8:i])}return o.join("")}var k=function(e,t){var r=0;for(var n in e){var o=e[n]-+t[n];w(e[n])&&w(t[n])&&(r+=Math.pow(o,2))}return Math.sqrt(r)},b=function(e,t,r,n){var o=function(e,t,r){var n,o,i,s,a,l={};for(var u in e){var c=+e[u],p=g(u);!w(c)&&w(null==r?void 0:r[p])&&(c=+r[p]),l[p]=(i=c,s=+(null===(n=t[p])||void 0===n?void 0:n.max),a=+(null===(o=t[p])||void 0===o?void 0:o.min),w(i)&&w(s)&&w(a)?(i-a)/(s-a):null)}return l}(e,r,n);return Object.keys(t).map((function(e){return{distance:k(o,null==t?void 0:t[e]),index:e}}))},E=function(t){function r(){var r=null!==t&&t.apply(this,arguments)||this;return r.customFetch=!1,r.loadNeeded=!1,r.type=e.ModelType.KMeans,r}return i(r,t),r.prototype.execute=function(e,t){var r=t||{};return function(e,t,r,n,o){var i=(new Date).getTime(),s=b(e,t,r,n);if(!Array.isArray(s)||0===s.length)return null;var a=Math.min.apply(Math,null==s?void 0:s.map((function(e){return e.distance}))),l=s.find((function(e){return(null==e?void 0:e.distance)===a})),u=(new Date).getTime();if(u-i>(o||30))throw new Error(JSON.stringify({code:32,msg:"聚类kmeans执行时间过长,为"+(u-i)+"毫秒"}));return l}(e,r.modelData,r.scaleData,r.fillData,r.timeout)},r.prototype.load=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];console.log("no need to load kmeans")},r}(m),T=function(){function e(){this.model=new E}return e.prototype.load=function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=this.model.load).call.apply(e,y([this],t,!1))},e.prototype.execute=function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=this.model.execute).call.apply(e,y([this],t,!1))},e}();var R=function(e,t,r,n){var o,i=r;for(var s in e){var a=g(s),l=+e[s];!w(l)&&w(null==n?void 0:n[a])&&(l=+n[a]),t[a]&&l&&(i+=+t[a]*l)}return{result:(o=i,1/(1+Math.exp(-o))),oriResult:i}},D=function(t){function r(){var r=null!==t&&t.apply(this,arguments)||this;return r.customFetch=!1,r.loadNeeded=!1,r.type=e.ModelType.LR,r}return i(r,t),r.prototype.load=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];console.log("no need to load lr")},r.prototype.execute=function(e,t){var r=t||{};return function(e,t,r,n){var o=(new Date).getTime(),i=R(e,null==t?void 0:t.weights,null==t?void 0:t.intercept,r),s=(new Date).getTime();if(s-o>(n||30))throw new Error(JSON.stringify({code:31,msg:"逻辑回归执行时间过长,为"+(s-o)+"毫秒"}));return i}(e,r.modelData,r.fillData,r.timeout)},r}(m);var W=function(){function e(){this.model=new D}return e.prototype.load=function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=this.model.load).call.apply(e,y([this],t,!1))},e.prototype.execute=function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=this.model.execute).call.apply(e,y([this],t,!1))},e}(),_="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},x={},S={};!function(e){var t,r,n,o;Object.defineProperty(e,"__esModule",{value:!0}),e.Env=e.JSWorkerCommand=e.PRESERVEDCOMMAND=e.JSWorkerReqRespMessageType=void 0,(t=e.JSWorkerReqRespMessageType||(e.JSWorkerReqRespMessageType={})).Request="Request",t.Response="Response",(r=e.PRESERVEDCOMMAND||(e.PRESERVEDCOMMAND={})).TESTCOMMAND="__test__",r.REMOTELOGCOMMAND="__remotelog__",(n=e.JSWorkerCommand||(e.JSWorkerCommand={})).Request="Request",n.Response="Response",(o=e.Env||(e.Env={})).isBrowser="Browser",o.isBrowserWorker="BrowserWorker",o.isAlipayMini="AlipayMini",o.isAlipayJSWorker="AlipayJSWorker"}(S);var A={};Object.defineProperty(A,"__esModule",{value:!0}),A.debug=A.setDebug=A.getEnv=A.isJSWorkerEnv=A.uuid=void 0;var O=S;function j(){return"object"==typeof worker&&"function"==typeof worker.onMessage&&"function"==typeof worker.postMessage}A.uuid=function(e,t){var r,n="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),o=[];if(t=t||n.length,e)for(r=0;r<e;r++)o[r]=n[0|Math.random()*t];else{var i=void 0;for(o[8]=o[13]=o[18]=o[23]="-",o[14]="4",r=0;r<36;r++)o[r]||(i=0|16*Math.random(),o[r]=n[19===r?3&i|8:i])}return o.join("")},A.isJSWorkerEnv=j,A.getEnv=function(){var e;if("object"==typeof my)return O.Env.isAlipayMini;if(j())return O.Env.isAlipayJSWorker;if("object"==typeof window&&"object"==typeof document&&"function"==typeof Worker)return O.Env.isBrowser;if("object"==typeof globalThis&&"DedicatedWorkerGlobalScope"===(null===(e=null===globalThis||void 0===globalThis?void 0:globalThis.constructor)||void 0===e?void 0:e.name))return O.Env.isBrowserWorker;throw new Error("未判断出任何环境,请确认当前环境")};var N=!1,P=null;A.setDebug=function(e,t){N=e,t&&(P=t)},A.debug=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(N)try{P?P.apply(null,e):console.info.apply(null,e)}catch(e){console.warn("debug error",e.message)}},function(e){var t=_&&_.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r),Object.defineProperty(e,n,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),r=_&&_.__exportStar||function(e,r){for(var n in e)"default"===n||Object.prototype.hasOwnProperty.call(r,n)||t(r,e,n)},n=_&&_.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{l(n.next(e))}catch(e){i(e)}}function a(e){try{l(n.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}l((n=n.apply(e,t||[])).next())}))},o=_&&_.__generator||function(e,t){var r,n,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}},i=_&&_.__spreadArray||function(e,t){for(var r=0,n=t.length,o=e.length;r<n;r++,o++)e[o]=t[r];return e};Object.defineProperty(e,"__esModule",{value:!0}),e.JSWorker=e.setDebug=void 0;var s=S,a=A;Object.defineProperty(e,"setDebug",{enumerable:!0,get:function(){return a.setDebug}}),r(S,e);var l=function(){function e(e,t){var r=this;switch(this.env=a.getEnv(),this.cmdExecPool=new Map,this.regCmdList=new Map,e?(this.initMainThread(e),t&&this.heartDetect()):this.initWokerThread(),this.env){case s.Env.isAlipayMini:case s.Env.isAlipayJSWorker:this.jsworker.onMessage((function(e){r.onMessageListener(e)}));break;case s.Env.isBrowser:case s.Env.isBrowserWorker:this.jsworker.onmessage=function(e){var t=e.data;r.onMessageListener(t)}}this.log("init worker success")}return e.prototype.reg=function(e,t){if(this.regCmdList.has(e))throw new Error("["+this.env+"] 已存在该 cmd:"+e);this.regCmdList.set(e,t)},e.prototype.callAsync=function(e,t){var r=this,n=this._call(e,t);return new Promise((function(e){r.cmdExecPool.set(n,e)}))},e.prototype.call=function(e,t,r){void 0===t&&(t={});var n=this._call(e,t);this.cmdExecPool.set(n,r)},e.prototype.test=function(){var e=this,t=setTimeout((function(){console.warn("未收到 worker 响应"),e.onConnectError&&e.onConnectError()}),3e3);this.call(s.PRESERVEDCOMMAND.TESTCOMMAND,{},(function(r){clearTimeout(t),e.log("connect test result: "+r)}))},e.prototype.remoteLog=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return n(this,void 0,void 0,(function(){return o(this,(function(t){return this.call(s.PRESERVEDCOMMAND.REMOTELOGCOMMAND,{loginfo:i([],e)}),[2]}))}))},e.prototype.close=function(){var e;null===(e=this.jsworker)||void 0===e||e.terminate(),this.jsWorkerScriptPath=null},e.prototype.checkCommand=function(e){if(!this.regCmdList.has(e))throw new Error("["+this.env+"] 不已存在该 cmd:"+e)},e.prototype._call=function(e,t){if(!this.jsworker)throw new Error("["+this.env+"] 未找到 jsworker 实例,请确认 jsworker ");var r=a.uuid(),n={id:r,cmd:e,params:t,type:s.JSWorkerReqRespMessageType.Request,_request_timestamp:Date.now()};return this.jsworker.postMessage(n),this.log("[call]:",e,"[params]:",t,n),r},e.prototype.initMainThread=function(e){if(this.env===s.Env.isAlipayMini?this.jsworker=my.createWorker(e):this.jsworker=new Worker(e),this.jsWorkerScriptPath=e,!this.jsworker)throw new Error("["+this.env+"] 初始化worker失败 "+e)},e.prototype.initWokerThread=function(){this.jsworker=this.env===s.Env.isAlipayJSWorker?worker:globalThis},e.prototype.onMessageListener=function(e){return n(this,void 0,void 0,(function(){var t,r,n,i,a,l,u,c,p,f,d,h,v,y,m,w,g;return o(this,(function(o){switch(o.label){case 0:switch(t=e.type,r=e.id,n=e.cmd,i=Date.now(),t){case s.JSWorkerReqRespMessageType.Request:return[3,1];case s.JSWorkerReqRespMessageType.Response:return[3,6]}return[3,10];case 1:a=e._request_timestamp,u=(l=void 0===a?null:a)?Date.now()-l:null,c=null,p=null,o.label=2;case 2:return o.trys.push([2,4,,5]),[4,this.invoke(e)];case 3:return p=o.sent(),[3,5];case 4:return f=o.sent(),console.warn(f),c=f.message,[3,5];case 5:return n===s.PRESERVEDCOMMAND.REMOTELOGCOMMAND?[2]:(d={type:s.JSWorkerReqRespMessageType.Response,id:r,error:c,result:p,cmd:n,runningTime:Date.now()-i,requestedTime:u,_request_timestamp:l,_response_timestamp:Date.now()},this.jsworker.postMessage(d),this.log("[call executed]:",n,"[result]:",p,{req:e,resp:d}),[3,10]);case 6:return v=(h=e).error,y=h.result,m=h._response_timestamp,w=void 0===m?null:m,this.log("[call returned]:",n,"[result]:",y,"[error]:",v,e),g=this.cmdExecPool.get(r),w&&(e.responsedTime=Date.now()-w),v?[4,g(new Error("[JSWorker] ["+this.env+"] [cmd]: "+n+" [error]: "+JSON.stringify(v)))]:[3,8];case 7:return o.sent(),[2];case 8:return g||console.warn("JSWorker ["+this.env+"] 未找到 cmd 方法: "+n),[4,g(y)];case 9:return o.sent(),[3,10];case 10:return[2]}}))}))},e.prototype.invoke=function(e){return n(this,void 0,void 0,(function(){var t,r,n,a;return o(this,(function(o){switch(o.label){case 0:if(t=e.cmd,r=e.params,n=void 0===r?{}:r,t===s.PRESERVEDCOMMAND.TESTCOMMAND)return[2,"--- test success ---"];if(t===s.PRESERVEDCOMMAND.REMOTELOGCOMMAND)return this.log.apply(this,i(["[remote logger]:"],n.loginfo)),[2,"--- log success ---"];if(this.checkCommand(t),!(a=this.regCmdList.get(t)))throw new Error("["+this.env+"] 未找到该方法对应的函数,请确认已经注册");return[4,a(n)];case 1:return[2,o.sent()]}}))}))},e.prototype.log=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];a.debug.apply(void 0,i(["%c[JSWorker] "+this.env,"color: chocolate;"],e))},e.prototype.heartDetect=function(){var e=this;this.env===s.Env.isAlipayMini&&setInterval((function(){e.test()}),6e4)},e}();e.JSWorker=l}(x);var C=function(e){function t(t){var r=e.call(this)||this;return r.jsworker=t,r.useWorker=!0,r}return i(t,e),t.prototype.load=function(e,t){return h(this,void 0,void 0,(function(){var r;return v(this,(function(n){switch(n.label){case 0:return this.modelName=e,r={modelName:e,params:t},[4,this.jsworker.callAsync(f.LOAD,d({},r))];case 1:return[2,n.sent()]}}))}))},t.prototype.execute=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t;return v(this,(function(r){switch(r.label){case 0:return t={modelName:this.modelName,params:e},[4,this.jsworker.callAsync(f.Execute,d({},t))];case 1:return[2,r.sent()]}}))}))},t}(m),L=function(t){function r(r){var n=t.call(this,r)||this;return n.type=e.ModelType.CUSTOM,n.onLoad=r.onLoad,n.name=r.name,n.backend=r.backend||e.BACKEND_TYPE.CPU,n}return i(r,t),r.prototype.load=function(e){this.model=this.onLoad(e)},r.prototype.execute=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t;return v(this,(function(r){switch(r.label){case 0:return[4,(t=this.model).execute.apply(t,e)];case 1:return[2,r.sent()]}}))}))},r.prototype.getName=function(){return this.name},r}(m),J=function(){function t(e){this.modelList=new Map,this.backendsPriority={wasm:0,cpu:1,webgl:2};var t=(e||{}).worker,r=void 0===t?null:t;r&&r.path&&(x.setDebug(!0,r.logger||null),this.sessionWorker=new x.JSWorker(r.path))}return t.prototype.init=function(e){return h(this,void 0,void 0,(function(){return v(this,(function(e){return[2]}))}))},t.prototype.registerCustomModel=function(e){var t=new L(e);return this.modelList.set(e.name,t),!0},t.prototype.loadCustomModel=function(e){return h(this,void 0,void 0,(function(){var t,r;return v(this,(function(n){switch(n.label){case 0:if(!e.name)throw new Error("Please provide the name of the model.");return t=e.name,[4,(r=this.modelList.get(t)).load(e.loadParams)];case 1:return n.sent(),[2,r]}}))}))},t.prototype.loadMlModel=function(t,r){if(t!==e.ModelType.KMeans&&t!==e.ModelType.LR)throw new Error("Please confirm type of the model to be loaded. Only kmeans and lr are suppported among ml models currently.");var o=new(n.getModel(t))(r);return this.modelList.set(r.name,o),o},t.prototype.loadModel=function(t,r){return h(this,void 0,void 0,(function(){return v(this,(function(n){switch(n.label){case 0:return(r||{useWorker:!1}).useWorker?(console.log("here :>> "),[4,this.loadModelWorker(r)]):[3,2];case 1:return[2,n.sent()];case 2:switch(t){case e.ModelType.KMeans:case e.ModelType.LR:return[3,3];case e.ModelType.tfModel:return[3,4];case e.ModelType.CUSTOM:return[3,6]}return[3,8];case 3:return[2,this.loadMlModel(t,r&&r.name?r:{name:M()})];case 4:return[4,this.loadTfModel(r)];case 5:case 7:return[2,n.sent()];case 6:return[4,this.loadCustomModel(r)];case 8:throw new Error("Please check model types...")}}))}))},t.prototype.loadTfModel=function(t){return void 0===t&&(t=null),h(this,void 0,void 0,(function(){var r,o,i,s,a,l,u;return v(this,(function(c){switch(c.label){case 0:if(!t)throw new Error("url or model in options is required when loading a tf model");return r=t.url,o=void 0===r?null:r,i=t.modelArtifact,s=void 0===i?null:i,a=e.ModelType.tfModel,l=n.getModel(a),u=new l(t),o?[4,u.loadUrl(o)]:[3,2];case 1:return c.sent(),[3,4];case 2:return[4,u.load(s)];case 3:c.sent(),c.label=4;case 4:return this.modelList.set(t.name||M(),u),[2,u]}}))}))},t.prototype.loadModelWorker=function(e){return h(this,void 0,void 0,(function(){var t,r,n,o;return v(this,(function(i){switch(i.label){case 0:if(!this.sessionWorker)throw new Error("未设置 jsworker ,请在 new Session 的时候传入 jsworker 实例");if(!(null==e?void 0:e.name))throw new Error("Please define the name of the model");return t=new C(this.sessionWorker),r=e.name,n=e.params,[4,t.load(r,n)];case 1:return(o=i.sent())&&o.constructor===Error?[2,o]:[2,t]}}))}))},t.prototype.execute=function(e,t,r){return h(this,void 0,void 0,(function(){return v(this,(function(n){switch(n.label){case 0:return[4,e.execute(t,r)];case 1:return[2,n.sent()]}}))}))},t.prototype.executeParallel=function(e){return h(this,void 0,void 0,(function(){var t,r,n,o=this;return v(this,(function(i){return t=e.map((function(e){return{model:o.getModel(e.modelId),inputs:e.inputs}})),r=t.sort((function(e,t){return o.backendsPriority[e.model.backend]-o.backendsPriority[t.model.backend]})),n=r.map((function(e){var t=e.model,r=e.inputs;return t.execute.apply(t,r)})),[2,Promise.all(n)]}))}))},t.prototype.getModel=function(e){return this.modelList.get(e)},t}();x.setDebug(!0);var B,q=function(){function e(){this.jsworker=new x.JSWorker,this.regModels=new Map,this.onListenCommand()}return e.prototype.regModel=function(e,t){return h(this,void 0,void 0,(function(){return v(this,(function(r){return this.regModels.set(e,t),[2]}))}))},e.prototype.init=function(e){return h(this,void 0,void 0,(function(){return v(this,(function(e){return[2]}))}))},e.prototype.remoteLog=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t;return v(this,(function(r){return(t=this.jsworker).remoteLog.apply(t,e),[2]}))}))},e.prototype.onListenCommand=function(){var e=this;this.jsworker.reg(f.ENV_INIT,(function(t){return h(e,void 0,void 0,(function(){return v(this,(function(e){switch(e.label){case 0:return[4,this.init(t)];case 1:return e.sent(),"cpu",[2,{success:!0,backend:"cpu"}]}}))}))})),this.jsworker.reg(f.LOAD,(function(t){return h(e,void 0,void 0,(function(){var e,r,n,o;return v(this,(function(i){switch(i.label){case 0:return e=t.modelName,r=t.params,n=Date.now(),[4,(o=this.regModels.get(e)).load.apply(o,r)];case 1:return i.sent(),this.remoteLog(Date.now()-n,"load cost in worker"),[2,{success:!0}]}}))}))})),this.jsworker.reg(f.Execute,(function(t){var r=t.modelName,n=t.params;return h(e,void 0,void 0,(function(){var e,t,o;return v(this,(function(i){switch(i.label){case 0:return e=Date.now(),[4,(t=this.regModels.get(r)).execute.apply(t,n)];case 1:return o=i.sent(),this.remoteLog(Date.now()-e,"predict cost in worker"),[2,o]}}))}))}))},e}();e.EnvType=void 0,(B=e.EnvType||(e.EnvType={})).isAlipayH5="isAlipayH5",B.isAlipayMini="isAlipayMini",B.isAipayJSWorker="isAipayJSWorker",B.isBrowser="isBrowser",B.isBrowserWorker="isBrowserWorker",B.isWechatMini="isWechatMini",B.UNKNOWN="unKnown";var K=function(){var t,r,n="undefined"!=typeof navigator?navigator.userAgent:"";if(r="undefined"!=typeof my?e.EnvType.isAlipayMini:"undefined"!=typeof wx?e.EnvType.isWechatMini:-1!==n.search("NebulaSDK")?e.EnvType.isAlipayH5:"object"==typeof window&&"object"==typeof document&&"function"==typeof Worker?e.EnvType.isBrowser:"object"==typeof globalThis&&"DedicatedWorkerGlobalScope"===(null===(t=null===globalThis||void 0===globalThis?void 0:globalThis.constructor)||void 0===t?void 0:t.name)?e.EnvType.isBrowserWorker:"object"==typeof worker&&"function"==typeof worker.onMessage&&"function"==typeof worker.postMessage?e.EnvType.isAipayJSWorker:e.EnvType.UNKNOWN,console.info("current env >>",r),r===e.EnvType.UNKNOWN)throw new Error("unknown environment, please check your environment, current supported environment is alipay H5/Mini/Worker or Web Browser");return r}();n.register(e.ModelType.KMeans,E),n.register(e.ModelType.LR,D),e.BaseModel=m,e.Debug=r,e.ENV=K,e.KMeansRunner=T,e.LRRunner=W,e.ModelRegistry=n,e.ModelWorker=q,e.Session=J,Object.defineProperty(e,"__esModule",{value:!0})}(this.antTfjsLite=this.antTfjsLite||{});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).antTfjsLite=e.antTfjsLite||{})}(this,(function(e){"use strict";var t,r={enabled:!1,setDebug:function(e){r.enabled=!!e},enable:function(){r.setDebug(!0)},disable:function(){r.setDebug(!1)},log:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];r.enabled&&console.log.apply(console,e)},info:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];r.enabled&&console.info.apply(console,e)},error:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];r.enabled&&console.error.apply(console,e)},warn:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];r.enabled&&console.log.apply(console,e)}},n={modelMap:{},register:function(e,t){if(e in n.modelMap)throw new Error(e+" already exists!");n.modelMap[e]=t},getModel:function(e){return n.modelMap[e]}};e.ModelType=void 0,(t=e.ModelType||(e.ModelType={})).tfModel="tfModel",t.ONNX="onnx",t.CUSTOM="custom",t.KMeans="KMeans",t.LR="lr";var o=function(e,t){return o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},o(e,t)};function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}var s,a,l,u,c,p,f,d=function(){return d=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},d.apply(this,arguments)};function h(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{l(n.next(e))}catch(e){i(e)}}function a(e){try{l(n.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}l((n=n.apply(e,t||[])).next())}))}function v(e,t){var r,n,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}}function y(e,t,r){if(r||2===arguments.length)for(var n,o=0,i=t.length;o<i;o++)!n&&o in t||(n||(n=Array.prototype.slice.call(t,0,o)),n[o]=t[o]);return e.concat(n||Array.prototype.slice.call(t))}!function(e){e.R0="R0",e.R1="R1",e.R2="R2",e.R3="R3",e.R4="R4",e.R5="R5",e.R6="R6"}(s||(s={})),function(e){e.float32="float32",e.int32="int32",e.bool="int32",e.complex64="complex64"}(a||(a={})),function(e){e.float32="float32",e.int32="int32",e.bool="bool",e.complex64="complex64"}(l||(l={})),function(e){e.float32="float32",e.int32="float32",e.bool="float32",e.complex64="complex64"}(u||(u={})),function(e){e.float32="complex64",e.int32="complex64",e.bool="complex64",e.complex64="complex64"}(c||(c={})),e.BACKEND_TYPE=void 0,(p=e.BACKEND_TYPE||(e.BACKEND_TYPE={})).CPU="cpu",p.WEBGL="webgl",p.WASM="wasm",function(e){e.ENV_INIT="aliphax.worker.env.init",e.INIT="alphax.worker.init",e.LOAD="alphax.worker.load",e.Execute="alphax.worker.execute",e.REG="alphax.worker.reg"}(f||(f={}));var m=function(){function t(t){this.backend=e.BACKEND_TYPE.CPU,this.useWorker=!1;var r={backend:e.BACKEND_TYPE.CPU},n=d(d({},t||{}),r).backend;this.backend=n}return t.prototype.loadUrl=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];throw new Error("Please impl the loadUrl function.")},t.prototype.load=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];throw new Error("Please impl the load function.")},t.prototype.fetchWorkerModel=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];throw new Error("Please impl the fetchWorkerModel function.")},t.prototype.execute=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];throw new Error("Please implement the execute function.")},t.prototype.setBackend=function(e){this.backend=e},t.prototype.getModelType=function(){return this.type},t}();function w(e){return null!=e}function g(e){return"claim_demo_num"===e?"claim_demo_one":"select_family_num"===e?"select_family_one":e}function M(e,t){var r,n="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),o=[];if(t=t||n.length,e)for(r=0;r<e;r++)o[r]=n[0|Math.random()*t];else{var i=void 0;for(o[8]=o[13]=o[18]=o[23]="-",o[14]="4",r=0;r<36;r++)o[r]||(i=0|16*Math.random(),o[r]=n[19===r?3&i|8:i])}return o.join("")}var b=function(e,t){var r=0;for(var n in e){var o=e[n]-+t[n];w(e[n])&&w(t[n])&&(r+=Math.pow(o,2))}return Math.sqrt(r)},k=function(e,t,r,n){var o=function(e,t,r){var n,o,i,s,a,l={};for(var u in e){var c=+e[u],p=g(u);!w(c)&&w(null==r?void 0:r[p])&&(c=+r[p]),l[p]=(i=c,s=+(null===(n=t[p])||void 0===n?void 0:n.max),a=+(null===(o=t[p])||void 0===o?void 0:o.min),w(i)&&w(s)&&w(a)?(i-a)/(s-a):null)}return l}(e,r,n);return Object.keys(t).map((function(e){return{distance:b(o,null==t?void 0:t[e]),index:e}}))},E=function(t){function r(){var r=null!==t&&t.apply(this,arguments)||this;return r.customFetch=!1,r.loadNeeded=!1,r.type=e.ModelType.KMeans,r}return i(r,t),r.prototype.execute=function(e,t){var r=t||{};return function(e,t,r,n,o){var i=(new Date).getTime(),s=k(e,t,r,n);if(!Array.isArray(s)||0===s.length)return null;var a=Math.min.apply(Math,null==s?void 0:s.map((function(e){return e.distance}))),l=s.find((function(e){return(null==e?void 0:e.distance)===a})),u=(new Date).getTime();if(u-i>(o||30))throw new Error(JSON.stringify({code:32,msg:"聚类kmeans执行时间过长,为"+(u-i)+"毫秒"}));return l}(e,r.modelData,r.scaleData,r.fillData,r.timeout)},r.prototype.load=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];console.log("no need to load kmeans")},r}(m),T=function(){function e(){this.model=new E}return e.prototype.load=function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=this.model.load).call.apply(e,y([this],t,!1))},e.prototype.execute=function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=this.model.execute).call.apply(e,y([this],t,!1))},e}();var R=function(e,t,r,n){var o,i=r;for(var s in e){var a=g(s),l=+e[s];!w(l)&&w(null==n?void 0:n[a])&&(l=+n[a]),t[a]&&l&&(i+=+t[a]*l)}return{result:(o=i,1/(1+Math.exp(-o))),oriResult:i}},D=function(t){function r(){var r=null!==t&&t.apply(this,arguments)||this;return r.customFetch=!1,r.loadNeeded=!1,r.type=e.ModelType.LR,r}return i(r,t),r.prototype.load=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];console.log("no need to load lr")},r.prototype.execute=function(e,t){var r=t||{};return function(e,t,r,n){var o=(new Date).getTime(),i=R(e,null==t?void 0:t.weights,null==t?void 0:t.intercept,r),s=(new Date).getTime();if(s-o>(n||30))throw new Error(JSON.stringify({code:31,msg:"逻辑回归执行时间过长,为"+(s-o)+"毫秒"}));return i}(e,r.modelData,r.fillData,r.timeout)},r}(m);var W=function(){function e(){this.model=new D}return e.prototype.load=function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=this.model.load).call.apply(e,y([this],t,!1))},e.prototype.execute=function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=this.model.execute).call.apply(e,y([this],t,!1))},e}(),x="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},_={},S={};!function(e){var t,r,n,o;Object.defineProperty(e,"__esModule",{value:!0}),e.Env=e.JSWorkerCommand=e.PRESERVEDCOMMAND=e.JSWorkerReqRespMessageType=void 0,(t=e.JSWorkerReqRespMessageType||(e.JSWorkerReqRespMessageType={})).Request="Request",t.Response="Response",(r=e.PRESERVEDCOMMAND||(e.PRESERVEDCOMMAND={})).TESTCOMMAND="__test__",r.REMOTELOGCOMMAND="__remotelog__",(n=e.JSWorkerCommand||(e.JSWorkerCommand={})).Request="Request",n.Response="Response",(o=e.Env||(e.Env={})).isBrowser="Browser",o.isBrowserWorker="BrowserWorker",o.isAlipayMini="AlipayMini",o.isAlipayJSWorker="AlipayJSWorker"}(S);var A={};Object.defineProperty(A,"__esModule",{value:!0}),A.debug=A.setDebug=A.getEnv=A.isJSWorkerEnv=A.uuid=void 0;var O=S;function j(){return"object"==typeof worker&&"function"==typeof worker.onMessage&&"function"==typeof worker.postMessage}A.uuid=function(e,t){var r,n="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),o=[];if(t=t||n.length,e)for(r=0;r<e;r++)o[r]=n[0|Math.random()*t];else{var i=void 0;for(o[8]=o[13]=o[18]=o[23]="-",o[14]="4",r=0;r<36;r++)o[r]||(i=0|16*Math.random(),o[r]=n[19===r?3&i|8:i])}return o.join("")},A.isJSWorkerEnv=j,A.getEnv=function(){var e;if("object"==typeof my)return O.Env.isAlipayMini;if(j())return O.Env.isAlipayJSWorker;if("object"==typeof window&&"object"==typeof document&&"function"==typeof Worker)return O.Env.isBrowser;if("object"==typeof globalThis&&"DedicatedWorkerGlobalScope"===(null===(e=null===globalThis||void 0===globalThis?void 0:globalThis.constructor)||void 0===e?void 0:e.name))return O.Env.isBrowserWorker;throw new Error("未判断出任何环境,请确认当前环境")};var N=!1,P=null;A.setDebug=function(e,t){N=e,t&&(P=t)},A.debug=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(N)try{P?P.apply(null,e):console.info.apply(null,e)}catch(e){console.warn("debug error",e.message)}},function(e){var t=x&&x.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r),Object.defineProperty(e,n,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),r=x&&x.__exportStar||function(e,r){for(var n in e)"default"===n||Object.prototype.hasOwnProperty.call(r,n)||t(r,e,n)},n=x&&x.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{l(n.next(e))}catch(e){i(e)}}function a(e){try{l(n.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}l((n=n.apply(e,t||[])).next())}))},o=x&&x.__generator||function(e,t){var r,n,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}},i=x&&x.__spreadArray||function(e,t){for(var r=0,n=t.length,o=e.length;r<n;r++,o++)e[o]=t[r];return e};Object.defineProperty(e,"__esModule",{value:!0}),e.JSWorker=e.setDebug=void 0;var s=S,a=A;Object.defineProperty(e,"setDebug",{enumerable:!0,get:function(){return a.setDebug}}),r(S,e);var l=function(){function e(e,t){var r=this;switch(this.env=a.getEnv(),this.cmdExecPool=new Map,this.regCmdList=new Map,e?(this.initMainThread(e),t&&this.heartDetect()):this.initWokerThread(),this.env){case s.Env.isAlipayMini:case s.Env.isAlipayJSWorker:this.jsworker.onMessage((function(e){r.onMessageListener(e)}));break;case s.Env.isBrowser:case s.Env.isBrowserWorker:this.jsworker.onmessage=function(e){var t=e.data;r.onMessageListener(t)}}this.log("init worker success")}return e.prototype.reg=function(e,t){if(this.regCmdList.has(e))throw new Error("["+this.env+"] 已存在该 cmd:"+e);this.regCmdList.set(e,t)},e.prototype.callAsync=function(e,t){var r=this,n=this._call(e,t);return new Promise((function(e){r.cmdExecPool.set(n,e)}))},e.prototype.call=function(e,t,r){void 0===t&&(t={});var n=this._call(e,t);this.cmdExecPool.set(n,r)},e.prototype.test=function(){var e=this,t=setTimeout((function(){console.warn("未收到 worker 响应"),e.onConnectError&&e.onConnectError()}),3e3);this.call(s.PRESERVEDCOMMAND.TESTCOMMAND,{},(function(r){clearTimeout(t),e.log("connect test result: "+r)}))},e.prototype.remoteLog=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return n(this,void 0,void 0,(function(){return o(this,(function(t){return this.call(s.PRESERVEDCOMMAND.REMOTELOGCOMMAND,{loginfo:i([],e)}),[2]}))}))},e.prototype.close=function(){var e;null===(e=this.jsworker)||void 0===e||e.terminate(),this.jsWorkerScriptPath=null},e.prototype.checkCommand=function(e){if(!this.regCmdList.has(e))throw new Error("["+this.env+"] 不已存在该 cmd:"+e)},e.prototype._call=function(e,t){if(!this.jsworker)throw new Error("["+this.env+"] 未找到 jsworker 实例,请确认 jsworker ");var r=a.uuid(),n={id:r,cmd:e,params:t,type:s.JSWorkerReqRespMessageType.Request,_request_timestamp:Date.now()};return this.jsworker.postMessage(n),this.log("[call]:",e,"[params]:",t,n),r},e.prototype.initMainThread=function(e){if(this.env===s.Env.isAlipayMini?this.jsworker=my.createWorker(e):this.jsworker=new Worker(e),this.jsWorkerScriptPath=e,!this.jsworker)throw new Error("["+this.env+"] 初始化worker失败 "+e)},e.prototype.initWokerThread=function(){this.jsworker=this.env===s.Env.isAlipayJSWorker?worker:globalThis},e.prototype.onMessageListener=function(e){return n(this,void 0,void 0,(function(){var t,r,n,i,a,l,u,c,p,f,d,h,v,y,m,w,g;return o(this,(function(o){switch(o.label){case 0:switch(t=e.type,r=e.id,n=e.cmd,i=Date.now(),t){case s.JSWorkerReqRespMessageType.Request:return[3,1];case s.JSWorkerReqRespMessageType.Response:return[3,6]}return[3,10];case 1:a=e._request_timestamp,u=(l=void 0===a?null:a)?Date.now()-l:null,c=null,p=null,o.label=2;case 2:return o.trys.push([2,4,,5]),[4,this.invoke(e)];case 3:return p=o.sent(),[3,5];case 4:return f=o.sent(),console.warn(f),c=f.message,[3,5];case 5:return n===s.PRESERVEDCOMMAND.REMOTELOGCOMMAND?[2]:(d={type:s.JSWorkerReqRespMessageType.Response,id:r,error:c,result:p,cmd:n,runningTime:Date.now()-i,requestedTime:u,_request_timestamp:l,_response_timestamp:Date.now()},this.jsworker.postMessage(d),this.log("[call executed]:",n,"[result]:",p,{req:e,resp:d}),[3,10]);case 6:return v=(h=e).error,y=h.result,m=h._response_timestamp,w=void 0===m?null:m,this.log("[call returned]:",n,"[result]:",y,"[error]:",v,e),g=this.cmdExecPool.get(r),w&&(e.responsedTime=Date.now()-w),v?[4,g(new Error("[JSWorker] ["+this.env+"] [cmd]: "+n+" [error]: "+JSON.stringify(v)))]:[3,8];case 7:return o.sent(),[2];case 8:return g||console.warn("JSWorker ["+this.env+"] 未找到 cmd 方法: "+n),[4,g(y)];case 9:return o.sent(),[3,10];case 10:return[2]}}))}))},e.prototype.invoke=function(e){return n(this,void 0,void 0,(function(){var t,r,n,a;return o(this,(function(o){switch(o.label){case 0:if(t=e.cmd,r=e.params,n=void 0===r?{}:r,t===s.PRESERVEDCOMMAND.TESTCOMMAND)return[2,"--- test success ---"];if(t===s.PRESERVEDCOMMAND.REMOTELOGCOMMAND)return this.log.apply(this,i(["[remote logger]:"],n.loginfo)),[2,"--- log success ---"];if(this.checkCommand(t),!(a=this.regCmdList.get(t)))throw new Error("["+this.env+"] 未找到该方法对应的函数,请确认已经注册");return[4,a(n)];case 1:return[2,o.sent()]}}))}))},e.prototype.log=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];a.debug.apply(void 0,i(["%c[JSWorker] "+this.env,"color: chocolate;"],e))},e.prototype.heartDetect=function(){var e=this;this.env===s.Env.isAlipayMini&&setInterval((function(){e.test()}),6e4)},e}();e.JSWorker=l}(_);var C=function(e){function t(t){var r=e.call(this)||this;return r.jsworker=t,r.useWorker=!0,r}return i(t,e),t.prototype.load=function(e,t){return h(this,void 0,void 0,(function(){var r;return v(this,(function(n){switch(n.label){case 0:return this.modelName=e,r={modelName:e,params:t},[4,this.jsworker.callAsync(f.LOAD,d({},r))];case 1:return[2,n.sent()]}}))}))},t.prototype.execute=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t;return v(this,(function(r){switch(r.label){case 0:return t={modelName:this.modelName,params:e},[4,this.jsworker.callAsync(f.Execute,d({},t))];case 1:return[2,r.sent()]}}))}))},t}(m),L=function(t){function r(r){var n=t.call(this,r)||this;return n.type=e.ModelType.CUSTOM,n.onLoad=r.onLoad,n.name=r.name,n.backend=r.backend||e.BACKEND_TYPE.CPU,n}return i(r,t),r.prototype.load=function(e){this.model=this.onLoad(e)},r.prototype.execute=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t;return v(this,(function(r){switch(r.label){case 0:return[4,(t=this.model).execute.apply(t,e)];case 1:return[2,r.sent()]}}))}))},r.prototype.getName=function(){return this.name},r}(m),J=function(){function t(e){this.modelList=new Map,this.backendsPriority={wasm:0,cpu:1,webgl:2};var t=(e||{}).worker,r=void 0===t?null:t;r&&r.path&&(_.setDebug(!0,r.logger||null),this.sessionWorker=new _.JSWorker(r.path))}return t.prototype.init=function(e){return h(this,void 0,void 0,(function(){return v(this,(function(e){return[2]}))}))},t.prototype.registerCustomModel=function(e){var t=new L(e);return this.modelList.set(e.name,t),!0},t.prototype.loadCustomModel=function(e){return h(this,void 0,void 0,(function(){var t,r;return v(this,(function(n){switch(n.label){case 0:if(!e.name)throw new Error("Please provide the name of the model.");return t=e.name,[4,(r=this.modelList.get(t)).load(e.loadParams)];case 1:return n.sent(),[2,r]}}))}))},t.prototype.loadMlModel=function(t,r){if(t!==e.ModelType.KMeans&&t!==e.ModelType.LR)throw new Error("Please confirm type of the model to be loaded. Only kmeans and lr are suppported among ml models currently.");var o=new(n.getModel(t))(r);return this.modelList.set(r.name,o),o},t.prototype.loadModel=function(t,r){return h(this,void 0,void 0,(function(){return v(this,(function(n){switch(n.label){case 0:return(r||{useWorker:!1}).useWorker?(console.log("here :>> "),[4,this.loadModelWorker(r)]):[3,2];case 1:return[2,n.sent()];case 2:switch(t){case e.ModelType.KMeans:case e.ModelType.LR:return[3,3];case e.ModelType.tfModel:return[3,4];case e.ModelType.CUSTOM:return[3,6]}return[3,8];case 3:return[2,this.loadMlModel(t,r&&r.name?r:{name:M()})];case 4:return[4,this.loadTfModel(r)];case 5:case 7:return[2,n.sent()];case 6:return[4,this.loadCustomModel(r)];case 8:throw new Error("Please check model types...")}}))}))},t.prototype.loadTfModel=function(t){return void 0===t&&(t=null),h(this,void 0,void 0,(function(){var r,o,i,s,a,l,u;return v(this,(function(c){switch(c.label){case 0:if(!t)throw new Error("url or model in options is required when loading a tf model");return r=t.url,o=void 0===r?null:r,i=t.modelArtifact,s=void 0===i?null:i,a=e.ModelType.tfModel,l=n.getModel(a),u=new l(t),o?[4,u.loadUrl(o)]:[3,2];case 1:return c.sent(),[3,4];case 2:return[4,u.load(s)];case 3:c.sent(),c.label=4;case 4:return this.modelList.set(t.name||M(),u),[2,u]}}))}))},t.prototype.loadModelWorker=function(e){return h(this,void 0,void 0,(function(){var t,r,n,o;return v(this,(function(i){switch(i.label){case 0:if(!this.sessionWorker)throw new Error("未设置 jsworker ,请在 new Session 的时候传入 jsworker 实例");if(!(null==e?void 0:e.name))throw new Error("Please define the name of the model");return t=new C(this.sessionWorker),r=e.name,n=e.params,[4,t.load(r,n)];case 1:return(o=i.sent())&&o.constructor===Error?[2,o]:[2,t]}}))}))},t.prototype.execute=function(e,t,r){return h(this,void 0,void 0,(function(){return v(this,(function(n){switch(n.label){case 0:return[4,e.execute(t,r)];case 1:return[2,n.sent()]}}))}))},t.prototype.executeParallel=function(e){return h(this,void 0,void 0,(function(){var t,r,n,o=this;return v(this,(function(i){return t=e.map((function(e){return{model:o.getModel(e.modelId),inputs:e.inputs}})),r=t.sort((function(e,t){return o.backendsPriority[e.model.backend]-o.backendsPriority[t.model.backend]})),n=r.map((function(e){var t=e.model,r=e.inputs;return t.execute.apply(t,r)})),[2,Promise.all(n)]}))}))},t.prototype.getModel=function(e){return this.modelList.get(e)},t}();_.setDebug(!0);var B,q=function(){function e(){this.jsworker=new _.JSWorker,this.regModels=new Map,this.onListenCommand()}return e.prototype.regModel=function(e,t){return h(this,void 0,void 0,(function(){return v(this,(function(r){return this.regModels.set(e,t),[2]}))}))},e.prototype.init=function(e){return h(this,void 0,void 0,(function(){return v(this,(function(e){return[2]}))}))},e.prototype.remoteLog=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t;return v(this,(function(r){return(t=this.jsworker).remoteLog.apply(t,e),[2]}))}))},e.prototype.onListenCommand=function(){var e=this;this.jsworker.reg(f.ENV_INIT,(function(t){return h(e,void 0,void 0,(function(){return v(this,(function(e){switch(e.label){case 0:return[4,this.init(t)];case 1:return e.sent(),"cpu",[2,{success:!0,backend:"cpu"}]}}))}))})),this.jsworker.reg(f.LOAD,(function(t){return h(e,void 0,void 0,(function(){var e,r,n,o;return v(this,(function(i){switch(i.label){case 0:return e=t.modelName,r=t.params,n=Date.now(),[4,(o=this.regModels.get(e)).load.apply(o,r)];case 1:return i.sent(),this.remoteLog(Date.now()-n,"load cost in worker"),[2,{success:!0}]}}))}))})),this.jsworker.reg(f.Execute,(function(t){var r=t.modelName,n=t.params;return h(e,void 0,void 0,(function(){var e,t,o;return v(this,(function(i){switch(i.label){case 0:return e=Date.now(),[4,(t=this.regModels.get(r)).execute.apply(t,n)];case 1:return o=i.sent(),this.remoteLog(Date.now()-e,"predict cost in worker"),[2,o]}}))}))}))},e}();e.EnvType=void 0,(B=e.EnvType||(e.EnvType={})).isAlipayH5="isAlipayH5",B.isAlipayMini="isAlipayMini",B.isAipayJSWorker="isAipayJSWorker",B.isBrowser="isBrowser",B.isBrowserWorker="isBrowserWorker",B.isWechatMini="isWechatMini",B.UNKNOWN="unKnown";var K=function(){var t,r,n="undefined"!=typeof navigator?navigator.userAgent:"";if(r="undefined"!=typeof my?e.EnvType.isAlipayMini:"undefined"!=typeof wx?e.EnvType.isWechatMini:-1!==n.search("NebulaSDK")?e.EnvType.isAlipayH5:"object"==typeof window&&"object"==typeof document&&"function"==typeof Worker?e.EnvType.isBrowser:"object"==typeof globalThis&&"DedicatedWorkerGlobalScope"===(null===(t=null===globalThis||void 0===globalThis?void 0:globalThis.constructor)||void 0===t?void 0:t.name)?e.EnvType.isBrowserWorker:"object"==typeof worker&&"function"==typeof worker.onMessage&&"function"==typeof worker.postMessage?e.EnvType.isAipayJSWorker:e.EnvType.UNKNOWN,console.info("current env >>",r),r===e.EnvType.UNKNOWN)throw new Error("unknown environment, please check your environment, current supported environment is alipay H5/Mini/Worker or Web Browser");return r}();n.register(e.ModelType.KMeans,E),n.register(e.ModelType.LR,D),e.BaseModel=m,e.Debug=r,e.ENV=K,e.KMeansRunner=T,e.LRRunner=W,e.ModelRegistry=n,e.ModelWorker=q,e.Session=J,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=ant-tfjs-lite.min.js.map
{
"name": "ant-tfjs-lite",
"version": "0.1.25",
"version": "0.1.26",
"description": "ant tfjs lite sdk",

@@ -5,0 +5,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc