-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (88 loc) · 3.21 KB
/
index.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
const http = require('http');
const _ = require('lodash');
const { readFile } = require('./utils');
const NeuralNetwork = require('./neural-network');
const knex = require('./knex');
const port = 80;
const getBody = (req) => {
return new Promise(resolve => {
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
let parsed;
try {
parsed = JSON.parse(body);
} catch (e) {
console.error(`Could not parse body ${body}`);
parsed = body;
}
resolve(parsed);
});
});
};
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
const renderView = async (view, ctx = {}, res, statusCode = 200) => {
res.statusCode = statusCode;
res.setHeader('Content-Type', 'text/html');
const viewContent = await readFile(`./views/${view}.html`);
const content = _.template(viewContent)(ctx);
res.end(content);
};
const renderJson = (data = {}, res, statusCode = 200) => {
res.statusCode = statusCode;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
};
(async () => {
const neuralNetwork = await NeuralNetwork.loadFromMinFile(`./theta/min-5`,50 * 50, 25, 10);
const server = http.createServer(async (req, res) => {
const { method, url } = req;
const body = await getBody(req);
if (method === 'GET' && url == '/') {
const data = await knex('digits').select('digit').count().groupBy('digit');
const samples = await knex('digits').orderByRaw('random()').limit(10);
const totalSet = data.reduce((count, digit) => count += parseInt(digit.count, 10), 0);
return renderView('index', {data, totalSet,samples: JSON.stringify(samples)}, res);
} else if (method === 'POST' && url === '/predict') {
const { imgData } = body;
if (!imgData) {
return renderJson({ status: 'bad request' }, res, 400);
}
const prediction = neuralNetwork.predictMatrix(imgData);
return renderJson({ ...prediction }, res);
} else if (method === 'POST' && url === '/feed') {
const { imgData, digit } = body;
if (!imgData || !digit || digit > 9) {
return renderJson({ status: 'bad request' }, res, 400);
}
const { prediction } = neuralNetwork.predictMatrix(imgData);
const row = await knex('digits')
.insert({ digit, image: JSON.stringify(imgData) }, ['id', 'digit']);
return renderJson({ row, prediction }, res);
} else if (url.startsWith('/digits/')) {
// const match = url.match('^\/digits\/(\d)$');
const match = url.match('^\/digits\/([0-9]+)(.*)$');
if (match) {
const [,id, format] = match;
const row = await knex('digits').where({ id }).first();
if (method === 'PATCH') {
await knex('digits').update(body).where({ id });
Object.assign(row, body);
}
if (row) {
if (format === '.html') {
return renderView('digit', { row }, res);
}
return renderJson(row, res);
}
return renderJson({}, res, 404);
}
}
return renderView('404', {}, res, 404);
});
server.listen(port, () => {
console.log(`Server running on port ${port}`);
});
})();