forked from microsoft/webnn-developer-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_models.js
163 lines (156 loc) · 6.26 KB
/
fetch_models.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import fetch from 'node-fetch';
import fs from 'fs';
import path from 'path';
import ProgressBar from 'progress';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
// Uncomment the following line if proxy is required to fetch files in your network
// import { HttpsProxyAgent } from 'https-proxy-agent';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const models = [
{
url: "xenova/resnet-50/resolve/main/onnx/model_fp16.onnx",
path: "./demos/image-classification/models/xenova/resnet-50/onnx"
},
{
url: "webnn/mobilenet-v2/resolve/main/onnx/model_fp16.onnx",
path: "./demos/image-classification/models/webnn/mobilenet-v2/onnx"
},
{
url: "webnn/efficientnet-lite4/resolve/main/onnx/model_fp16.onnx",
path: "./demos/image-classification/models/webnn/efficientnet-lite4/onnx"
},
{
url: "microsoft/sd-turbo-webnn/resolve/main/text_encoder/model_layernorm.onnx",
path: "./demos/sd-turbo/models/text_encoder"
},
{
url: "microsoft/sd-turbo-webnn/resolve/main/unet/model_layernorm.onnx",
path: "./demos/sd-turbo/models/unet"
},
{
url: "microsoft/sd-turbo-webnn/resolve/main/vae_decoder/model.onnx",
path: "./demos/sd-turbo/models/vae_decoder"
},
{
url: "microsoft/sd-turbo-webnn/resolve/main/safety_checker/safety_checker_int32_reduceSum.onnx",
path: "./demos/sd-turbo/models/safety_checker"
},
{
url: "microsoft/stable-diffusion-v1.5-webnn/resolve/main/text-encoder.onnx",
path: "./demos/stable-diffusion-1.5/models"
},
{
url: "microsoft/stable-diffusion-v1.5-webnn/resolve/main/sd-unet-v1.5-model-b2c4h64w64s77-float16-compute-and-inputs-layernorm.onnx",
path: "./demos/stable-diffusion-1.5/models"
},
{
url: "microsoft/stable-diffusion-v1.5-webnn/resolve/main/Stable-Diffusion-v1.5-vae-decoder-float16-fp32-instancenorm.onnx",
path: "./demos/stable-diffusion-1.5/models"
},
{
url: "microsoft/stable-diffusion-v1.5-webnn/resolve/main/safety_checker_int32_reduceSum.onnx",
path: "./demos/stable-diffusion-1.5/models"
},
{
url: "microsoft/segment-anything-model-webnn/resolve/main/sam_vit_b_01ec64.encoder-fp16.onnx",
path: "./demos/segment-anything/models"
},
{
url: "microsoft/segment-anything-model-webnn/resolve/main/sam_vit_b_01ec64.decoder-fp16.onnx",
path: "./demos/segment-anything/models"
},
{
url: "microsoft/segment-anything-model-webnn/resolve/main/sam_vit_b-encoder-int8.onnx",
path: "./demos/segment-anything/models"
},
{
url: "microsoft/segment-anything-model-webnn/resolve/main/sam_vit_b-decoder-int8.onnx",
path: "./demos/segment-anything/models"
},
{
url: "microsoft/whisper-base-webnn/resolve/main/whisper_base_decoder_static_kvcache_128_lm_fp16_layernorm_4dmask.onnx",
path: "./demos/whisper-base/models",
note: "Decoder model with 4dmask for WebNN GPU"
},
{
url: "microsoft/whisper-base-webnn/resolve/main/whisper_base_decoder_static_kvcache_128_lm_fp16_layernorm_gelu_4dmask.onnx",
path: "./demos/whisper-base/models",
note: "Gelu and 4dmask are used for decoder on WebNN NPU"
},
{
url: "microsoft/whisper-base-webnn/resolve/main/whisper_base_decoder_static_non_kvcache_lm_fp16_layernorm_4dmask.onnx",
path: "./demos/whisper-base/models",
note: "Decoder model with 4dmask for WebNN GPU"
},
{
url: "microsoft/whisper-base-webnn/resolve/main/whisper_base_decoder_static_non_kvcache_lm_fp16_layernorm_gelu_4dmask.onnx",
path: "./demos/whisper-base/models",
note: "Gelu and 4dmask are used for decoder on WebNN NPU"
},
{
url: "microsoft/whisper-base-webnn/resolve/main/whisper_base_encoder_lm_fp16_layernorm.onnx",
path: "./demos/whisper-base/models",
note: "Encoder model for WebNN GPU"
},
{
url: "microsoft/whisper-base-webnn/resolve/main/whisper_base_encoder_lm_fp16_layernorm_gelu.onnx",
path: "./demos/whisper-base/models",
note: "Gelu is used for encoder on WebNN NPU"
}
];
const downloadFile = async (url, outputPath, retries = 2) => {
// Proxy configuration
const proxyHost = '';
const proxyPort = 8080;
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
const proxyAgent = undefined;
// Enable this line to use the proxy:
// const proxyAgent = new HttpsProxyAgent(proxyUrl);
try {
const response = await fetch(url, {agent: proxyAgent});
if (response.ok) {
const totalLength = response.headers.get('content-length');
const progressBar = new ProgressBar('-> downloading [:bar] :percent :etas', {
width: 40,
complete: '=',
incomplete: ' ',
renderThrottle: 1,
total: parseInt(totalLength)
});
const fileStream = fs.createWriteStream(outputPath);
response.body.on('data', (chunk) => progressBar.tick(chunk.length));
response.body.pipe(fileStream);
return new Promise((resolve, reject) => {
fileStream.on('finish', resolve);
fileStream.on('error', reject);
});
} else {
throw new Error(`HTTP error! Status: ${response.status} ${response.statusText}`);
}
} catch (error) {
if (retries === 0) {
console.error(`Failed to download ${url} after multiple attempts`, error);
throw error;
} else {
console.warn(`Retrying download for ${url} (${retries} retries left)`);
return downloadFile(url, outputPath, retries - 1);
}
}
}
(async () => {
let i = 1;
for (const model of models) {
const outputPath = path.join(__dirname, model.path, path.basename(model.url));
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
console.log(`[${i}/${models.length}] Downloading https://huggingface.co/${model.url} to ${outputPath}`);
try {
await downloadFile(`https://huggingface.co/${model.url}`, outputPath);
console.log(`[${i}/${models.length}] Downloaded https://huggingface.co/${model.url} to ${outputPath}`);
} catch (error) {
console.error(`Failed to download https://huggingface.co/${model.url}`, error);
}
i++;
}
})();