From 136ce10c942122542675ebe8949371bbc9a3e6b6 Mon Sep 17 00:00:00 2001 From: Belem Zhang Date: Wed, 21 Aug 2024 08:46:37 +0800 Subject: [PATCH] Update the fixed dev version for demos --- assets/js/common_utils.js | 46 ++++++++++++----------------- demos/sd-turbo/index.js | 2 +- demos/segment-anything/index.js | 12 ++------ demos/stable-diffusion-1.5/index.js | 2 +- demos/whisper-base/main.js | 2 +- 5 files changed, 25 insertions(+), 39 deletions(-) diff --git a/assets/js/common_utils.js b/assets/js/common_utils.js index 6845876..111617f 100644 --- a/assets/js/common_utils.js +++ b/assets/js/common_utils.js @@ -122,51 +122,43 @@ export const getTime = () => { return `${hour}:${min}:${sec}`; }; -// Get the latest dev version of ONNX Runtime Web dists -const getLatestOrtWebDevVersion = async () => { - try { - const response = await fetch('https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/'); - const htmlString = await response.text(); - const parser = new DOMParser(); - const doc = parser.parseFromString(htmlString, 'text/html'); - let selectElement = doc.querySelector('select.versions.select-css'); - let options = Array.from(selectElement.querySelectorAll('option')).map( - (option) => option.value - ); - let filteredOptions = options.filter(item => item.includes('-dev.')); - return filteredOptions[0].replace('onnxruntime-web@', ''); - } catch (error) { - console.error('Error:', error.message); - } +const KNOWN_COMPATIBLE_ORT_VERSION = { + 'stable-diffusion-1.5': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' }, + 'sd-turbo': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' }, + 'segment-anything': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' }, + 'whisper-base': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' }, + 'image-classification': { 'dev': '1.19.0-dev.20240804-ee2fe87e2d', 'stable': '1.20.0', 'test': 'test' }, }; const ORT_BASE_URL = 'https://www.npmjs.com/package/onnxruntime-web/v/'; const ORT_CDN_URL = 'https://cdn.jsdelivr.net/npm/onnxruntime-web@'; const ortLink = (version) => `${ORT_BASE_URL}${version}?activeTab=versions`; -const loadScriptWithMessage = async (version, url) => { +const loadScriptWithMessage = async (version) => { try { - await loadScript('onnxruntime-web', url); - return `ONNX Runtime Web: ${version}`; + if (version === 'test') { + await loadScript('onnxruntime-web', '../../assets/dist/ort.all.min.js'); + return 'ONNX Runtime Web: Test version'; + } else { + await loadScript('onnxruntime-web', `${ORT_CDN_URL}${version}/dist/ort.all.min.js`); + return `ONNX Runtime Web: ${version}`; + } } catch (error) { console.error('Failed to load ORT script:', error); return 'Failed to load ONNX Runtime Web'; } }; -export const setupORT = async () => { +export const setupORT = async (key, branch) => { + const version = KNOWN_COMPATIBLE_ORT_VERSION[key][branch]; const ortVersionElement = document.querySelector('#ortversion'); removeElement('onnxruntime-web'); const queryOrt = getQueryValue('ort')?.toLowerCase(); let versionHtml; - if (queryOrt?.includes('-dev.')) { - versionHtml = await loadScriptWithMessage(queryOrt, `${ORT_CDN_URL}${queryOrt}/dist/ort.all.min.js`); - } else if (queryOrt === 'test') { - await loadScript('onnxruntime-web', '../../assets/dist/ort.all.min.js'); - versionHtml = 'ONNX Runtime Web: Test version'; + if (queryOrt) { + versionHtml = await loadScriptWithMessage(queryOrt); } else { - const latestVersion = await getLatestOrtWebDevVersion(); - versionHtml = await loadScriptWithMessage(latestVersion, `${ORT_CDN_URL}${latestVersion}/dist/ort.all.min.js`); + versionHtml = await loadScriptWithMessage(version); } ortVersionElement.innerHTML = versionHtml; }; diff --git a/demos/sd-turbo/index.js b/demos/sd-turbo/index.js index 6993424..5c0baac 100644 --- a/demos/sd-turbo/index.js +++ b/demos/sd-turbo/index.js @@ -1045,7 +1045,7 @@ const ui = async () => { dataElement.setAttribute("class", "hide"); } - await setupORT(); + await setupORT('sd-turbo', 'dev'); showCompatibleChromiumVersion('sd-turbo'); if ( diff --git a/demos/segment-anything/index.js b/demos/segment-anything/index.js index 88cfb5a..4ded022 100644 --- a/demos/segment-anything/index.js +++ b/demos/segment-anything/index.js @@ -4,7 +4,7 @@ // An example how to run segment-anything with webnn in onnxruntime-web. // -import { showCompatibleChromiumVersion } from "../../assets/js/common_utils.js"; +import { showCompatibleChromiumVersion, setupORT } from "../../assets/js/common_utils.js"; // the image size on canvas const MAX_WIDTH = 480; @@ -101,6 +101,7 @@ function getConfig() { provider: "webnn", device: "gpu", threads: "1", + ort: "test" }; let vars = query.split("&"); for (let i = 0; i < vars.length; i++) { @@ -705,13 +706,6 @@ const webNnStatus = async () => { } }; -const setupORT = async () => { - const ortversion = document.querySelector("#ortversion"); - removeElement("onnxruntime-web"); - await loadScript("onnxruntime-web", "../../assets/dist/ort.all.min.js"); - ortversion.innerHTML = `ONNX Runtime Web: Test version`; -}; - const loadScript = async (id, url) => { return new Promise((resolve, reject) => { const script = document.createElement("script"); @@ -756,7 +750,7 @@ const ui = async () => { samDecoderIndicator = document.querySelector("#sam-decoder-indicator"); canvas.setAttribute("class", "none"); - await setupORT(); + await setupORT('segment-anything', 'test'); showCompatibleChromiumVersion('segment-anything'); // ort.env.wasm.wasmPaths = 'dist/'; diff --git a/demos/stable-diffusion-1.5/index.js b/demos/stable-diffusion-1.5/index.js index a25de3b..f4e27a3 100644 --- a/demos/stable-diffusion-1.5/index.js +++ b/demos/stable-diffusion-1.5/index.js @@ -1472,7 +1472,7 @@ const checkWebNN = async () => { }; const ui = async () => { - await setupORT(); + await setupORT('stable-diffusion-1.5', 'dev'); showCompatibleChromiumVersion('stable-diffusion-1.5'); if ( Utils.getQueryValue("provider") && diff --git a/demos/whisper-base/main.js b/demos/whisper-base/main.js index fb3b2cc..2fdfda9 100644 --- a/demos/whisper-base/main.js +++ b/demos/whisper-base/main.js @@ -602,7 +602,7 @@ const main = async () => { speech.disabled = true; // progress.parentNode.style.display = "none"; - await setupORT(); + await setupORT('whisper-base', 'dev'); showCompatibleChromiumVersion('whisper-base'); ort.env.wasm.numThreads = 1; ort.env.wasm.simd = true;