File size: 815 Bytes
1ed908c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { geoai } from "geoai";
 
let modelInstance = null;
 
// Use a Function to obtain the global object without referencing `globalThis` or `self`
const workerGlobal = Function("return this")();
 
workerGlobal.onmessage = async e => {
  const { type, payload } = e.data;

  try {
    switch (type) {
      case "init":
        console.log({payload})
        modelInstance = await geoai.pipeline(
          payload.tasks,
          payload.providerParams
        );
        workerGlobal.postMessage({ type: "ready" });
        break;

      case "inference":
        const result = await modelInstance.inference(payload);
        workerGlobal.postMessage({ type: "result", payload: result });
        break;
    }
  } catch (error) {
    workerGlobal.postMessage({ type: "error", payload: error.message });
  }
};