-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-models.ts
69 lines (58 loc) · 1.86 KB
/
fetch-models.ts
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
const request = require('request'),
fs = require('fs'),
_cliProgress = require('cli-progress'),
targz = require('node-tar.gz')
const download = (url: string, filename: string, callback: any) => {
const progressBar = new _cliProgress.SingleBar({
format: '{bar} {percentage}%'
}, _cliProgress.Presets.shades_classic)
const file = fs.createWriteStream(filename)
let receivedBytes = 0
request.get(url)
.on('response', (response) => {
if (response.statusCode !== 200) {
return callback('Response status was ' + response.statusCode)
}
const totalBytes = response.headers['content-length']
progressBar.start(totalBytes, 0)
})
.on('data', (chunk) => {
receivedBytes += chunk.length
progressBar.update(receivedBytes)
})
.pipe(file)
.on('error', (err) => {
fs.unlink(filename)
progressBar.stop()
return callback(err.message)
})
file.on('finish', () => {
progressBar.stop()
file.close(callback)
})
file.on('error', (err) => {
fs.unlink(filename)
progressBar.stop()
return callback(err.message)
})
}
const fileUrl = `https://github.com/mozilla/DeepSpeech/releases/download/v0.6.0/deepspeech-0.6.0-models.tar.gz`
const fileName = 'packed-models.tar.gz'
const unpackedFolder = 'models'
if (!fs.existsSync(`./${unpackedFolder}`)) {
download(fileUrl, fileName, () => {
// unpack
console.log('finished downloading, will now unpack')
targz()
.extract(fileName, unpackedFolder)
.then(function(){
console.log('Successfully unpacked and created "models" folder.');
})
.catch(function(err){
console.log('Unpacking failed ', err.stack)
process.exit(1)
})
})
} else {
console.log('models folder already exists, skipping...')
}