-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·121 lines (86 loc) · 3.91 KB
/
app.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
var
string = require('string'),
express = require('express'),
app = express(),
fs = require('fs'),
formidable = require('formidable'),
config = {
applicationRoute : "http://v-app.mybluemix.net",
applicationId : "v-app"
},
ibmbluemix = require('ibmbluemix'),
watson = require('watson-developer-cloud'),
language_translation = watson.language_translation({
username: 'YOUR_USR',
password: 'YOUR_PWD',
version: 'v2'
}),
visual_recognition = watson.visual_recognition({
username: 'YOUR_USR',
password: 'YOUR_PWD',
version: 'v1-beta'
}),
Cloudant = require('cloudant'),
cloudant = Cloudant({
host: "19c3624f-3f57-4f5f-9ba6-8914d9e1138f-bluemix.cloudant.com",
port: 443,
url: "https://19c3624f-3f57-4f5f-9ba6-8914d9e1138fbluemix:fe09cfbc4ebb2e2bb4a58b636959c450f01fe5b715e2a40cdaf8ddfa87ba34f8@19c3624f-3f57-4f5f-9ba6-8914d9e1138fbluemix.cloudant.com",
username: 'YOUR_USR',
password: 'YOUR_PWD'
});
// init core sdk
ibmbluemix.initialize(config);
var logger = ibmbluemix.getLogger();
app.get('/apps', function(req, res){
res.sendfile('public/apps.html');
});
app.post('/upload', function(req, result) {
//console.log('uploads');
var form = new formidable.IncomingForm();
form.keepExtensions = true;
form.parse(req, function(err, fields, files) {
var params = {
image_file: fs.createReadStream(files.image.path)
};
visual_recognition.recognize(params, function(err, res) {
var results = [];
for(var i=0;i<res.images[0].labels.length;i++) {
results.push(res.images[0].labels[i].label_name);
}
result.send("<h1>Visual Recognition Results</h1>"+results.join(', '));
});
});
});
app.post('/translate', function(req, result) {
var form = new formidable.IncomingForm();
form.keepExtensions = true;
form.parse(req, function(err, fields, files) {
console.log(fields);
language_translation.translate({
text: fields.text, source : 'en', target: 'es' },
function (err, translation) {
console.log(translation);
if (err)
console.log('error:', err);
else
console.log(JSON.stringify(translation, null, 2));
var vDb = cloudant.db.use('v-db');
vDb.insert(translation, function (er, resultDb) {
if (er) {
console.log("error: "+er);
throw er;
}
});
result.send("<h1>Language Translation Results</h1>" + translation.translations[0].translation );
});
});
});
// init services
app.use(function(req, res, next) {
req.logger = logger;
next();
});
app.use(require('./lib/setup'));
var ibmconfig = ibmbluemix.getConfig();
app.use(ibmconfig.getContextRoot(), require('./lib/staticfile'));
app.listen(ibmconfig.getPort());