-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
105 lines (89 loc) · 3.33 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
98
99
100
101
102
103
104
105
var express = require('express');
var app = express();
var fs = require('fs');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.get('/gameSubmit/:img', (req, res) => {
var VisualRecognitionV3 = require('ibm-watson/visual-recognition/v3');
const imagem_API_KEY = require('./API_KEYs').imagem_API_KEY;
var visualRecognition = new VisualRecognitionV3({
url: 'https://gateway.watsonplatform.net/visual-recognition/api',
version: '2018-03-19',
iam_apikey: imagem_API_KEY,
});
var params = { images_file: fs.createReadStream("public/imagens/"+req.params.img+".jpg") };
visualRecognition.classify(params)
.then(result => {
res.send(JSON.stringify(result.images[0].classifiers[0].classes[0].class));
})
.catch(err => {
console.log(err);
});
});
app.get('/qtdImg', (req, res) => {
fs.readdir('public/imagens', (err, files) => {
res.send(JSON.stringify(files.length));
});
});
app.get('/ajuda/:imgid', (req, res) => {
var VisualRecognitionV3 = require('ibm-watson/visual-recognition/v3');
const imagem_API_KEY = require('./API_KEYs').imagem_API_KEY;
var visualRecognition = new VisualRecognitionV3({
url: 'https://gateway.watsonplatform.net/visual-recognition/api',
version: '2018-03-19',
iam_apikey: imagem_API_KEY,
});
var params = { images_file: fs.createReadStream("public/imagens/"+req.params.imgid+".jpg") };
visualRecognition.classify(params)
.then(result => {
const LanguageTranslatorV3 = require('ibm-watson/language-translator/v3');
const tradutor_API_KEY = require('./API_KEYs').tradutor_API_KEY;
const languageTranslator = new LanguageTranslatorV3({
iam_apikey: tradutor_API_KEY,
url: 'https://gateway.watsonplatform.net/language-translator/api/',
version: '2018-08-07',
});
var tempstring = result.images[0].classifiers[0].classes[0].class;
languageTranslator.translate({
text: tempstring,
source: 'en',
target: 'pt'
})
.then(translation => {
res.send(JSON.stringify(translation.translations[0].translation));
})
.catch(err => {
console.log('error in translator: ', err);
});
})
.catch(err => {
console.log('error in visual recognition: '+err);
});
});
app.post('/mandarmsg', (req, res) => {
var input = req.body.msg;
var contexto = (req.body.idconversa == 1 ? "" : req.body.idconversa);
const watson = require('./API_KEYs').watson;
var AssistantV1 = require('ibm-watson/assistant/v1');
var assistant = new AssistantV1({
iam_apikey: watson.API_KEY,
url: 'https://gateway.watsonplatform.net/assistant/api/',
version: '2018-09-19'
});
assistant.message( {
input: { text: input },
assistant_id: watson.assistant_id,
workspace_id: watson.workspace_id,
context: { conversation_id: contexto }
})
.then(result => {
res.json(result, null, 2);
})
.catch(err => {
console.log(err);
});
});
var port = process.env.PORT || 3000;
app.listen(port, () => console.log("App Iniciada: http://localhost:" + port));