User:Ebrahim/Gadget-DeepLearningServices.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
$(function () {
	function createTagButton(name, caption, type) {
		var isRunOnce = false;
		$(mw.util.addPortletLink('p-tb', '#', name, undefined, caption)).click(function (e) {
			e.preventDefault();
			var self = $(this);
			self.empty().append(name + ', wait…');
			if (!isRunOnce) {
				$('#content .image img, .fullImageLink img').get().map(function (img) {
					return function () {
						return $.getJSON('https://tools.wmflabs.org/deep-learning-services/' + type + '/' + img.src.replace(/\d+px-/, '300px-')).then(function (result) {
							$(img).parent().before('<div>' + (type === 'nsfw' ? 'NSFW: ' + (result.result * 100).toFixed(1) + '%' : result.map(function (row) {
						    	return row[0].replace(/_/g, ' ') + ': ' + (row[1] * 100).toFixed(1) + '%';
						    }).join('<br>')) + '</div>');
						}, function () {
							return $.Deferred().resolve(); // revive
						});
					};
				}).reduce(function (defer, job) {
					return defer.then(job);
				}, $.Deferred().resolve()).then(function () {
					self.empty().append(name + ', done');
				});
			}
			isRunOnce = true;
		});
	}
	
	createTagButton('TAG (xception)', 'Tag images of a page with deep learning', 'classify/xception');
	createTagButton('NSFW?', 'Predict "Not Safe For Work" possibility of images on a page with deep learning', 'nsfw');
	mw.util.addPortletLink('p-tb', '/wiki/Special:BlankPage/commonsgame?withJS=MediaWiki:CommonsGame.js', 'CommonsGame (WIP)');
	
	// createTagButton('TAG (inception)', 'Tag images of a page with deep learning', 'classify/inception');
	// createTagButton('TAG (vgg16)', 'Tag images of a page with deep learning', 'classify/vgg16');
	// createTagButton('TAG (vgg19)', 'Tag images of a page with deep learning', 'classify/vgg19');
	// createTagButton('TAG (mobilenet)', 'Tag images of a page with deep learning', 'classify/mobilenet');
	// createTagButton('TAG (resnet)', 'Tag images of a page with deep learning', 'classify/resnet');
});