-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (47 loc) · 1.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
const imageClassifier = require('./imageClassifier')
const PORT = process.env.PORT || 3000
const fastify = require('fastify')({
logger: {
serializers: {
req (request) {
return {
reqId: request.reqId
}
}
}
}
})
const schema = {
body: {
type: 'object',
properties: {
modelUrl: { type: 'string' },
imageUrl: { type: 'string' }
},
required: ['modelUrl', 'imageUrl']
}
}
fastify.get('/', (request, reply) =>{
return {
'Hey': 'Serious Machine Learning things going on here!',
'README':'https://github.com/andrasbacsai/machine-learning-api'
}
})
fastify.post('/api/tensorflowjs/imageClassifier', { schema }, async (request, reply) => {
const { modelUrl, imageUrl } = request.body
try {
const predictions = await imageClassifier({ modelUrl, imageUrl })
reply.code(200).send({ predictions })
} catch (error) {
throw { statusCode: 500, error: error.message }
}
})
const start = async () => {
try {
await fastify.listen(PORT, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()