Spaces:
Running
Running
File size: 3,744 Bytes
78e34bc eccf3fa 7be4bb6 78e34bc eccf3fa 7be4bb6 78e34bc 7be4bb6 78e34bc 7be4bb6 eccf3fa 7be4bb6 eccf3fa 7be4bb6 27da4cd eccf3fa 27da4cd eccf3fa 27da4cd eccf3fa |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
function updateGetModelProgress(name, loaded, total) {
const progressElement = document.getElementById('model-progress');
if (total === 0) {
progressElement.innerHTML = `Model ${name} is already in local cache`;
return;
}
const percent = ((loaded / total) * 100).toFixed(2);
let progress;
if (loaded >= total) {
progress = "Downloaded";
} else {
progress = "Downloading";
}
progressElement.innerHTML = `${progress} model ${name}: Total ${total} bytes, loaded ${loaded} bytes, ${percent}%`;
}
// Get model via Origin Private File System
async function getModelOPFS(name, url, updateModel) {
const root = await navigator.storage.getDirectory();
let fileHandle;
async function updateFile() {
const response = await fetch(url);
const buffer = await readResponse(name, response, updateGetModelProgress);
fileHandle = await root.getFileHandle(name, { create: true });
const writable = await fileHandle.createWritable();
await writable.write(buffer);
await writable.close();
return buffer;
}
if (updateModel) {
return await updateFile();
}
try {
fileHandle = await root.getFileHandle(name);
const blob = await fileHandle.getFile();
updateGetModelProgress(name, 0, 0);
return await blob.arrayBuffer();
} catch (e) {
return await updateFile();
}
}
async function readResponse(name, response, progressCallback) {
const contentLength = response.headers.get('Content-Length');
let total = parseInt(contentLength ?? '0');
let buffer = new Uint8Array(total);
let loaded = 0;
const reader = response.body.getReader();
async function read() {
const { done, value } = await reader.read();
if (done) return;
let newLoaded = loaded + value.length;
if (newLoaded > total) {
total = newLoaded;
let newBuffer = new Uint8Array(total);
newBuffer.set(buffer);
buffer = newBuffer;
}
buffer.set(value, loaded);
loaded = newLoaded;
if (progressCallback) {
progressCallback(name, loaded, total);
}
return read();
}
await read();
return buffer;
}
function isInternal() {
if (window.location.href.includes('intel')) {
return true;
} else {
return false;
}
}
function getModelsPath() {
if (isInternal()) {
return "https://wp-27.sh.intel.com/workspace/project/models/";
} else {
return "https://huggingface.co/webai-community/models/resolve/main/";
}
}
function getParam(name, type, _default) {
name = name.replace(/[\[\]]/g, "\\$&");
let regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i");
let results = regex.exec(window.location.href);
if (!results || !results[2]) return _default;
const result = decodeURIComponent(results[2].replace(/\+/g, " "));
if (type === "Boolean") {
if (result === "true") {
return true;
} else if (result === "false") {
return false;
}
} else if (type === "Number") {
return parseInt(result);
} else {
return result;
}
}
async function loadOrt() {
const ortUrl = getParam("ortUrl", "String", "default");
if (ortUrl !== "default") {
await loadScript(ortUrl);
} else if (isInternal()) {
await loadScript("https://wp-27.sh.intel.com/workspace/project/onnxruntime/js/web/dist/ort.webgpu.min.js");
} else {
await loadScript("https://cdn.jsdelivr.net/npm/onnxruntime-web@dev/dist/ort.webgpu.min.js");
}
}
async function loadScript(url) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.onload = resolve;
script.onerror = reject;
script.src = url;
if (url.startsWith('http')) {
script.crossOrigin = 'anonymous';
}
document.body.append(script);
})
}
|