Skip to content

Commit

Permalink
chore: Host dependencies with the site instead of using CDN
Browse files Browse the repository at this point in the history
TensorflowJS and D3 were being loaded from unpkg.com but are now
hosted with the rest of dependencies fetched by bower.

I've also updated to a newer version of tfjs, and fixed a minor
bug that caused console errors if you click away from the models
page while leaving the model running for a test.

Closes: #350

Signed-off-by: Dale Lane <[email protected]>
  • Loading branch information
dalelane committed Nov 10, 2020
1 parent 316faf5 commit ea327e5
Show file tree
Hide file tree
Showing 11 changed files with 837 additions and 58 deletions.
29 changes: 28 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@ module.exports = function(grunt) {
cwd : 'public/images',
src : '**',
dest : 'web/static/images'
},
tensorflowjs : {
expand : true,
cwd : 'node_modules/@tensorflow/tfjs/dist',
src : [ 'tf.min.js', 'tf.min.js.map' ],
dest : 'web/static/bower_components/tensorflowjs'
},
tensorflowspeechcommands : {
expand : true,
cwd : 'node_modules/@tensorflow-models/speech-commands/dist',
src : [ 'speech-commands.min.js' ],
dest : 'web/static/bower_components/tensorflow-models/speech-commands'
}
},
downloadfile : {
options : {
dest : './web/static/bower_components/tensorflow-models/speech-commands',
overwriteEverytime : false
},
files: {
'metadata.json' : 'https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v0.4/browser_fft/18w/metadata.json',
'model.json' : 'https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v0.4/browser_fft/18w/model.json',
'group1-shard1of2' : 'https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v0.4/browser_fft/18w/group1-shard1of2',
'group1-shard2of2' : 'https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v0.4/browser_fft/18w/group1-shard2of2'
}
},
concat : {
Expand Down Expand Up @@ -219,6 +243,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-simple-nyc');
grunt.loadNpmTasks('grunt-bower-install-simple');
grunt.loadNpmTasks('grunt-downloadfile');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-copy');
Expand All @@ -242,6 +267,8 @@ module.exports = function(grunt) {
//-----------------------------------
// fetch UI third-party dependencies
grunt.registerTask('bower', ['bower-install-simple']);
grunt.registerTask('tfjs', ['copy:tensorflowjs', 'copy:tensorflowspeechcommands', 'downloadfile']);
grunt.registerTask('uidependencies', ['bower', 'tfjs']);
// install Scratch into the deployment
grunt.registerTask('scratch2', ['copy:scratchx', 'copy:scratchxhelp', 'copy:crossdomain']);
grunt.registerTask('scratch3', ['copy:scratch3', 'copy:scratch3help']);
Expand All @@ -260,7 +287,7 @@ module.exports = function(grunt) {
// bring the UI together
grunt.registerTask('ui', ['css', 'javascript', 'html', 'concat:jsapp']);
// prepare the main web app
grunt.registerTask('frontend', ['bower', 'scratch', 'staticfiles', 'ui']);
grunt.registerTask('frontend', ['uidependencies', 'scratch', 'staticfiles', 'ui']);



Expand Down
39 changes: 37 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const autoprefixer = require('gulp-autoprefixer');
const rename = require('gulp-rename');
const ngAnnotate = require('gulp-ng-annotate');
const sourcemaps = require('gulp-sourcemaps');
const download = require('gulp-download2');
const del = require('del');


Expand Down Expand Up @@ -57,6 +58,29 @@ gulp.task('twitter', function() {
return gulp.src('public/twitter-card.html').pipe(gulp.dest('web/dynamic'));
});

gulp.task('tensorflowjs', function() {
return gulp.src([
'node_modules/@tensorflow/tfjs/dist/tf.min.js',
'node_modules/@tensorflow/tfjs/dist/tf.min.js.map'
]).pipe(gulp.dest('web/static/bower_components/tensorflowjs'));
});
gulp.task('tensorflowspeechcommands', function() {
return gulp.src([
'node_modules/@tensorflow-models/speech-commands/dist/speech-commands.min.js'
]).pipe(gulp.dest('web/static/bower_components/tensorflow-models/speech-commands'));
});
gulp.task('speechcommandsmodel', function() {
const files = [
{ url : 'https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v0.4/browser_fft/18w/metadata.json', file : 'metadata.json' },
{ url : 'https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v0.4/browser_fft/18w/model.json', file : 'model.json' },
{ url : 'https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v0.4/browser_fft/18w/group1-shard1of2', file : 'group1-shard1of2' },
{ url : 'https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v0.4/browser_fft/18w/group1-shard2of2', file : 'group1-shard2of2' }
];
return download(files)
.pipe(gulp.dest('web/static/bower_components/tensorflow-models/speech-commands'));
});
gulp.task('tfjs', gulp.parallel('tensorflowjs', 'tensorflowspeechcommands', 'speechcommandsmodel'));

gulp.task('scratchblocks', function() {
return gulp.src('public/third-party/scratchblocks-v3.1-min.js').pipe(gulp.dest('web/static'));
});
Expand Down Expand Up @@ -210,6 +234,9 @@ gulp.task('test', () => {
gulp.task('web',
gulp.series('css', 'minifyjs', 'images', 'html', 'angularcomponents', 'languages', 'datasets', 'scratchxinstall', 'scratch3install', 'scratchblocks'));

gulp.task('uidependencies',
gulp.series('bower', 'tfjs'));

gulp.task('build',
gulp.parallel('web', 'compile'));

Expand All @@ -219,7 +246,15 @@ gulp.task('default', gulp.series('build', 'lint', 'test'));
gulp.task('buildprod',
gulp.series(
'clean',
'bower',
gulp.parallel('css', 'minifyprodjs', 'images', 'prodhtml', 'angularcomponents', 'languages', 'datasets', 'scratchxinstall', 'scratch3install', 'scratchblocks'),
'uidependencies',
gulp.parallel(
'css',
'minifyprodjs',
'images',
'prodhtml',
'angularcomponents',
'languages',
'datasets',
'scratchxinstall', 'scratch3install', 'scratchblocks'),
'compile',
'lint'));
Loading

0 comments on commit ea327e5

Please sign in to comment.