Spaces:
Running
Running
Download frontends/react/src/pyodide.worker.ts from elvis-hf/optimization: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/spaces/elvis-hf/optimization/resolve/main/frontends/react/src/pyodide.worker.ts
- Command line
-
hf download hf://spaces/elvis-hf/optimization/frontends/react/src/pyodide.worker.ts
-
curl -L -o pyodide.worker.ts https://huggingface.co/spaces/elvis-hf/optimization/resolve/main/frontends/react/src/pyodide.worker.ts
3.17 kB
| const PYODIDE_URL = "https://cdn.jsdelivr.net/pyodide/v0.26.1/full/pyodide.mjs"; | |
| // @ts-ignore | |
| import managerCode from "../../../backend/src/optimization_manager.py?raw"; | |
| import logicCode from "../../../backend/src/optimization_logic.py?raw"; | |
| import type { WorkerMessage } from "./types"; | |
| let pyodide: any = null; | |
| let manager: any = null; | |
| async function init() { | |
| const { loadPyodide } = await import(/* @vite-ignore */ PYODIDE_URL); | |
| pyodide = await loadPyodide({ | |
| indexURL: "https://cdn.jsdelivr.net/pyodide/v0.26.1/full/" | |
| }); | |
| await pyodide.loadPackage(["numpy", "sympy"]); | |
| pyodide.FS.writeFile("optimization_logic.py", logicCode); | |
| pyodide.FS.writeFile("optimization_manager.py", managerCode); | |
| pyodide.runPython(`from optimization_manager import OptimizationManager; manager = OptimizationManager();`); | |
| manager = pyodide.globals.get("manager"); | |
| if (!manager) { | |
| console.error("Failed to initialize optimization manager"); | |
| } | |
| self.postMessage({ type: "READY" }); | |
| } | |
| function handlePythonResult(result: any) { | |
| if (!result) { | |
| return null; | |
| } | |
| try { | |
| const data = result.toJs({ dict_converter: Object.fromEntries }); | |
| if (result.destroy) { | |
| result.destroy(); | |
| } | |
| self.postMessage({ type: "RESULT", data: data }); | |
| } catch (error) { | |
| console.error("Error handling Python result:", error); | |
| } | |
| } | |
| self.onmessage = async (event) => { | |
| const message = event.data as WorkerMessage; | |
| if (!manager) { | |
| console.warn("Pyodide is not ready yet"); | |
| return; | |
| } | |
| switch (message.type) { | |
| case "COMPARE": { | |
| const trajectories = message.settings.map((settings, index) => { | |
| const managerClass = pyodide.globals.get("OptimizationManager"); | |
| const comparisonManager = managerClass(); | |
| managerClass.destroy(); | |
| const pythonSettings = pyodide.toPy(settings); | |
| try { | |
| comparisonManager.handle_update_settings(pythonSettings)?.destroy(); | |
| comparisonManager._compute_trajectory_values(pythonSettings, message.iteration + 1); | |
| const values = comparisonManager.trajectory_values; | |
| try { | |
| return { id: message.ids[index], values: values.toJs({ dict_converter: Object.fromEntries }) }; | |
| } finally { values.destroy(); } | |
| } catch (error) { | |
| return { id: message.ids[index], values: { x: [], y: [] }, error: String(error) }; | |
| } finally { | |
| pythonSettings.destroy(); | |
| comparisonManager.destroy(); | |
| } | |
| }); | |
| self.postMessage({ type: "COMPARISON_RESULT", requestId: message.requestId, trajectories }); | |
| break; | |
| } | |
| case "INIT": | |
| const pythonSettings = pyodide.toPy(message.settings); | |
| handlePythonResult(manager.handle_update_settings(pythonSettings)); | |
| break; | |
| case "NEXT_STEP": | |
| handlePythonResult(manager.handle_next_step()); | |
| break; | |
| case "PREV_STEP": | |
| handlePythonResult(manager.handle_prev_step()); | |
| break; | |
| case "RESET": | |
| handlePythonResult(manager.handle_reset()); | |
| break; | |
| default: | |
| console.error("Unknown message type:", message); | |
| break; | |
| } | |
| } | |
| // Start of execution | |
| init(); | |