/* Python stays off the UI thread; only prediction arrays cross the worker boundary. */ let pyodide; async function initialize(){ const {loadPyodide}=await import('https://cdn.jsdelivr.net/pyodide/v314.0.6/full/pyodide.mjs'); pyodide=await loadPyodide(); postMessage({type:'progress',message:'Loading scikit-learn…'}); await pyodide.loadPackage('scikit-learn'); const response=await fetch('./backend.py', {cache:'no-store'}); if(!response.ok) throw new Error('Could not load the Python training code.'); await pyodide.runPythonAsync(await response.text()); postMessage({type:'ready',classifiers:JSON.parse(pyodide.runPython('json.dumps(catalog())'))}); } const ready=initialize(); ready.catch(error=>postMessage({type:'fatal',message:error.message})); let queue=Promise.resolve(); self.onmessage=event=>{ const {id,request}=event.data; queue=queue.then(async()=>{ try{ await ready; pyodide.globals.set('request_json',JSON.stringify(request)); const result=JSON.parse(await pyodide.runPythonAsync('handle_request(request_json)')); postMessage({id,...result}); }catch(error){postMessage({id,error:error.message});} }); };