Skip to content

Commit

Permalink
Merge pull request #33 from ibelem/known-compatible-ort-version
Browse files Browse the repository at this point in the history
Update the fixed dev version for demos
  • Loading branch information
fdwr authored Aug 21, 2024
2 parents 1089122 + 136ce10 commit 721dd67
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 39 deletions.
46 changes: 19 additions & 27 deletions assets/js/common_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: <a href="${ortLink(version)}">${version}</a>`;
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: <a href="${ortLink(version)}">${version}</a>`;
}
} 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;
};
Expand Down
2 changes: 1 addition & 1 deletion demos/sd-turbo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ const ui = async () => {
dataElement.setAttribute("class", "hide");
}

await setupORT();
await setupORT('sd-turbo', 'dev');
showCompatibleChromiumVersion('sd-turbo');

if (
Expand Down
12 changes: 3 additions & 9 deletions demos/segment-anything/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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/';
Expand Down
2 changes: 1 addition & 1 deletion demos/stable-diffusion-1.5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") &&
Expand Down
2 changes: 1 addition & 1 deletion demos/whisper-base/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 721dd67

Please sign in to comment.