Spaces:
Paused
Paused
Create postinstall.js
Browse files- postinstall.js +122 -0
postinstall.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fs from 'fs';
|
| 2 |
+
import path from 'path';
|
| 3 |
+
import os from 'os';
|
| 4 |
+
import https from 'https';
|
| 5 |
+
import yauzl from 'yauzl';
|
| 6 |
+
import { fileURLToPath } from 'url';
|
| 7 |
+
|
| 8 |
+
const __filename = fileURLToPath(import.meta.url);
|
| 9 |
+
const __dirname = path.dirname(__filename);
|
| 10 |
+
|
| 11 |
+
const URL = "https://alphacephei.com/vosk/models/vosk-model-small-en-us-0.15.zip";
|
| 12 |
+
const MODEL_DIR = path.resolve(__dirname, "model");
|
| 13 |
+
|
| 14 |
+
async function main() {
|
| 15 |
+
if (!fs.existsSync(MODEL_DIR)) {
|
| 16 |
+
fs.mkdirSync(MODEL_DIR, { recursive: true });
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
if (fs.existsSync(path.resolve(MODEL_DIR, "DONE"))) {
|
| 20 |
+
console.log("Model already downloaded");
|
| 21 |
+
return;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
const zip = path.resolve(os.tmpdir(), path.basename(URL));
|
| 25 |
+
await download(URL, zip);
|
| 26 |
+
console.log("Downloaded model to", zip);
|
| 27 |
+
|
| 28 |
+
await unzip(zip, MODEL_DIR);
|
| 29 |
+
fs.unlinkSync(zip);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
function download(url, to, redirect = 0) {
|
| 33 |
+
if (redirect === 0) {
|
| 34 |
+
console.log(`Downloading ${url} to ${to}`);
|
| 35 |
+
} else {
|
| 36 |
+
console.log(`Redirecting to ${url}`);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return new Promise((resolve, reject) => {
|
| 40 |
+
if (!fs.existsSync(path.dirname(to))) {
|
| 41 |
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
let done = true;
|
| 45 |
+
const file = fs.createWriteStream(to);
|
| 46 |
+
const request = https.get(url, (res) => {
|
| 47 |
+
if (res.statusCode === 302 && res.headers.location !== undefined) {
|
| 48 |
+
done = false;
|
| 49 |
+
file.close();
|
| 50 |
+
resolve(download(res.headers.location, to, redirect + 1));
|
| 51 |
+
return;
|
| 52 |
+
}
|
| 53 |
+
res.pipe(file);
|
| 54 |
+
});
|
| 55 |
+
|
| 56 |
+
file.on("finish", () => {
|
| 57 |
+
if (done) {
|
| 58 |
+
resolve(to);
|
| 59 |
+
}
|
| 60 |
+
});
|
| 61 |
+
|
| 62 |
+
request.on("error", (err) => {
|
| 63 |
+
fs.unlink(to, () => reject(err));
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
file.on("error", (err) => {
|
| 67 |
+
fs.unlink(to, () => reject(err));
|
| 68 |
+
});
|
| 69 |
+
|
| 70 |
+
request.end();
|
| 71 |
+
});
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
function unzip(zip, dest) {
|
| 75 |
+
const dir = path.basename(zip, ".zip");
|
| 76 |
+
return new Promise((resolve, reject) => {
|
| 77 |
+
yauzl.open(zip, { lazyEntries: true }, (err, zipfile) => {
|
| 78 |
+
if (err) {
|
| 79 |
+
reject(err);
|
| 80 |
+
}
|
| 81 |
+
zipfile.readEntry();
|
| 82 |
+
zipfile
|
| 83 |
+
.on("entry", (entry) => {
|
| 84 |
+
if (/\/$/.test(entry.fileName)) {
|
| 85 |
+
zipfile.readEntry();
|
| 86 |
+
} else {
|
| 87 |
+
zipfile.openReadStream(entry, (err, stream) => {
|
| 88 |
+
if (err) {
|
| 89 |
+
reject(err);
|
| 90 |
+
}
|
| 91 |
+
const f = path.resolve(dest, entry.fileName.replace(`${dir}/`, ""));
|
| 92 |
+
if (!fs.existsSync(path.dirname(f))) {
|
| 93 |
+
fs.mkdirSync(path.dirname(f), { recursive: true });
|
| 94 |
+
console.log("Created directory", path.dirname(f));
|
| 95 |
+
}
|
| 96 |
+
stream.pipe(fs.createWriteStream(f));
|
| 97 |
+
stream
|
| 98 |
+
.on("end", () => {
|
| 99 |
+
console.log("Extracted", f);
|
| 100 |
+
zipfile.readEntry();
|
| 101 |
+
})
|
| 102 |
+
.on("error", (err) => {
|
| 103 |
+
reject(err);
|
| 104 |
+
});
|
| 105 |
+
});
|
| 106 |
+
}
|
| 107 |
+
})
|
| 108 |
+
.on("error", (err) => {
|
| 109 |
+
reject(err);
|
| 110 |
+
})
|
| 111 |
+
.on("end", () => {
|
| 112 |
+
console.log("Extracted all files");
|
| 113 |
+
fs.writeFileSync(path.resolve(dest, "DONE"), "");
|
| 114 |
+
})
|
| 115 |
+
.on("close", () => {
|
| 116 |
+
resolve();
|
| 117 |
+
});
|
| 118 |
+
});
|
| 119 |
+
});
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
main().catch(console.error);
|