-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFunctions.js
286 lines (247 loc) · 6.77 KB
/
Functions.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
var Clarifai = require('clarifai');
var models = require('./Models');
var fs = require('fs');
var Promise = require('bluebird');
require('dotenv').config();
/* FUNCTIONS EXPORTED
addConcepts(id, concepts) adds a concept (keyword) to the model specified by id
\\ String, [String] -> Void
removeConcepts(id, concepts) removes a concept from the model. If no concept specified, removes all concepts from model
\\ String, [String] -> Void
getModel(id) outputs the list of concepts from the model to console
\\ String -> Void
trainModel(id) trains model on given inputs
\\ String -> Void
predictModel(id, url) produces a 2D array of [{keyword, confidence}] given an image url or image in base64
\\ String, String -> Void
createModel(id, concepts) creates a new model with given id and concepts.
\\ String, [String] -> Void
base64 (file) converts the image from specified file path to base-64
\\ String -> String
get_JSON(arr) parses a 2D array into a JSON object
\\ 2D array -> JSON
getScore(url, buzzwords) produces the score based on the image provided
\\ String [String] -> Promise
*/
const src_dir = "src/";
const score_multiplier = 20; // multiples by confidence score
// instantiate new Clarifai app
var app = new Clarifai.App(
process.env.CLARIFAI_ID,
process.env.CLARIFAI_SECRET
);
// list training models
function getModels() {
const names = []
app.models.list().then(
function(response){
response.rawData.forEach(model => names.push({name: model.name, id: model.id}));
console.log(names);
return names;
},
errorHandler
);
}
// once inputs are created, create model by giving name and list of concepts
function createModel(id, concepts) {
app.models.create(id, concepts).then(
train,
errorHandler
);
}
function initModel(id, fn, param) {
if (param){
return app.models.initModel(id).then(
x => fn(x, param),
errorHandler
)
} else {
app.models.initModel(id).then(
fn,
errorHandler
);
}
}
// Add concepts to a model
function addConcepts(id, concepts) {
initModel(id, addConceptsToModel);
}
// Remove concepts to a model
function removeConcepts(id, concepts) {
initModel(id, removeConceptsFromModel, concepts)
}
// Train model with given dataset
function trainModel(id) {
initModel(id, train);
}
// Predict keywords and probability of url with model
function predictModel(id, url) {
return initModel(id, predict, url);
}
// Model -> Void
function addConceptsToModel(model, concepts) {
model.mergeConcepts(concepts).then(
function(response) {
console.log(response);
},
function(err){
console.error(err);
}
);
}
// Model -> Void
function removeConceptsFromModel(model) {
model.deleteConcepts({"id": "boscoe"}).then(
function(response) {
console.log(response);
},
errorHandler
);
}
// string -> {Promise(Model, error)}
function getModel(id) {
app.models.get(id).then(
function(response){
getConcepts(response);
},
errorHandler
);
}
// Model -> {Promise(Model, error)}
function getConcepts(model) {
model.getOutputInfo().then(
function (response) {
console.log(response);
const data = response.outputInfo.data.concepts;
const concepts = [];
data.forEach(concept => concepts.push(concept.name));
console.log(concepts);
},
errorHandler
);
}
// after model is created, you can now train the model
// Model -> Void
function train(model) {
model.train().then(
function (model) {
console.log(model);
},
errorHandler
);
}
// after training the model, you can now use it to predict on other inputs
function predict(model, url) {
return model.predict(url).then(
function(response) {
const outputs = [];
var concepts;
for (var i in response.outputs){
concepts = response.outputs[i].data.concepts;
var keywords = [];
concepts.forEach(concept => keywords.push([concept.name, concept.value]));
return keywords;
}
}, errorHandler
);
}
// outputs errors
function errorHandler(err) {
console.error(err);
}
// convert image to base 64
function base64(bitmap) {
return new Buffer(bitmap).toString('base64');
}
function generateWords(){
// https://github.com/first20hours/google-10000-english
const filepath = src_dir + "wordbank.txt";
var words = fs.readFileSync(filepath).toString('utf8').split("\n");
words = words.filter(word => word.length > 2 && word.slice(-1) != 's'); // remove single and double letters
var buzzwords = [];
while(buzzwords.length < 5) {
// Implement better randomizer
var num = Math.floor(Math.random() * words.length);
var buzz = words[num];
var dup = buzzwords.reduce((x, y) => ((y === buzz) || x), false);
if (!dup) buzzwords.push(buzz.toUpperCase());
}
return buzzwords;
}
// Determines if image is NSFW
function is_NSFW(url){
return predictModel(Clarifai.NSFW_MODEL, url).then(results => {
results = get_JSON(results);
const confidence = results.NSFW;
const tolerance = 0.95;
return (confidence > tolerance);
});
}
// parses 2D array into JSON
function get_JSON(arr){
var json = {};
arr.forEach(x => {
json[x[0].toUpperCase()] = x[1];
});
return json;
}
var promiseWhile = function(condition, action) {
var resolver = Promise.defer();
var loop = function() {
if (!condition()) return resolver.resolve();
return Promise.cast(action()).then(loop).catch(resolver.reject);
};
process.nextTick(loop);
return resolver.promise;
}
function getScore(url, buzzwords) {
const penalty = -10;
return is_NSFW(url)
.then(nsfw => {
if (nsfw) return penalty;
else {
var c_models = Object.keys(models);
var count = 1;
var score = 0;
return promiseWhile(() => {
return count < c_models.length;
}, function() {
return new Promise (function(resolve, reject) {
predictModel(eval(`models.${c_models[count-1]}`), url).then(results => {
results = get_JSON(results);
score += scoreMatches(results, buzzwords);
count++;
resolve(score);
}, errorHandler), function(err) {
reject(err);
}
});
}).then(() => {
score = Math.floor(score);
console.log("SCORE: "+ score);
return score;
});
}
})
}
function scoreMatches(results, buzzwords) {
var score = 0;
buzzwords.forEach(b => {
var confidence = eval(`results.${b.toUpperCase()}`);
if(typeof confidence === 'number') score += (confidence * score_multiplier);
});
return score;
}
// Functions exported
module.exports = {
addConcepts,
removeConcepts,
getModel,
trainModel,
predictModel,
createModel,
base64,
generateWords,
get_JSON,
getScore
}