Spaces:
Running
Running
| importScripts( "https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js" ) | |
| //importScripts( "https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.webgpu.min.js" ) | |
| let id | |
| let session | |
| const init = async args =>{ | |
| id = args.id | |
| try { | |
| ort.env.wasm.numThreads = 0 | |
| ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/" | |
| session = await ort.InferenceSession.create( args.model_array_buffer, { executionProviders : [ "wasm" ]}) | |
| //session = await ort.InferenceSession.create( args.array_buffer, { executionProviders : [ "webgpu" ]}) | |
| } catch( error ){ | |
| console.log( error ) | |
| } | |
| self.postMessage ({ selector : "init" }) | |
| } | |
| const run_model = async args =>{ | |
| const spec_tensor = new ort.Tensor( "float32", args.spec, [ 1, 4, 3072, 256 ]) | |
| const outputs = await session.run({ input : spec_tensor }) | |
| spec_tensor.dispose() | |
| self.postMessage ({ selector : "run_model", id, index : args.index, spec : outputs.output.cpuData }) | |
| } | |
| self.onmessage = function( event ){ | |
| switch( event.data.selector ){ | |
| case "init" : init({ id : event.data.id, model_array_buffer : event.data.model_array_buffer }); break | |
| case "run_model": run_model({ index : event.data.index, spec : event.data.spec }); break | |
| } | |
| } | |