0

I have already downloaded Personality Insights from Bluemix,and I can run it on my command line successfully, but how can i insert it on my webpage and run it locally?

Does anyone have sample code for index.js? I have set up the server but I am finding it difficult to integrate it on my page.

var express = require('express');
var app = express();
var http = require('http').Server(app);
var cfenv = require("cfenv");

var appEnv = cfenv.getAppEnv();

http.listen(appEnv.port, appEnv.bind);

var PersonalityInsightsV2 = require('watson-developer-cloud/personality-insights/v2');

var personality_insights = new PersonalityInsightsV2({
  username: '<YOUR-USERNAME>',
  password: '<YOUR-PASSWORD>'
});

personality_insights.profile({
  text: "<YOUR-100-UNIQUE-WORDS>",
  language: 'en' },
  function (err, response) {
    if (err)
      console.log('error:', err);
    else
      console.log(JSON.stringify(response, null, 2));
});    
ralphearle
  • 1,696
  • 13
  • 18
Demotivated
  • 117
  • 3
  • 13
  • 1
    Can you add what you're seeing go wrong here? I need more output, like console errors, etc. – broguinn Sep 06 '16 at 21:06
  • there is nothing wrog it works in my comand line and want to use it on my locahost website.I want to integrate in on my page.I am looking for an example or if sb could provide me with the steps that would be great – Demotivated Sep 07 '16 at 06:59

2 Answers2

0

This is not recommended because in theory you could directly make the call from the web page (browser side java script) to the Watson Personality Insights REST service but then you are giving away the credentials.

So the recommended way to do it is using an intermediate http proxy/gateway style component (which can be implemented in Node.js as well).

So basically your browser based java script will invoke your own REST service on IBM Bluemix (e.g. a Node.js application, but can also use OpenWhisk, NodeRED, JAVA, ...) and the call to the Watson Service is done from there, so your credentials are save (either hard code it, or use VCAP_SERVICES)

Edit: 7.9.16: In case you really want to do it locally just use a local node.js instance for doing it and you can obtain the service credentials of the Watson service by logging in into the Bluemix web interface. It is described here

The UI slightly changed, so you have to click on the top left button, choose Watson->Personality Insights->Service Credentials->View Credentials (on the Credentials-1 entry)

Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58
  • this is only for my master theisis.I will not use it on a live website,but since i am a begginer I dont know thbe steps or at least an example how to do it.I want to add a button on my page,onclick displays the output of the service – Demotivated Sep 07 '16 at 07:02
  • O @Romeo if it was about bluemix I would already had done it.my problem are not the credentials.I just want an exmple of who has already done it,I already told you I run it on my commad line is ok,but how to import it on my localhost,basicaly just include index.js into my other file?I gives me an err require is not defined – Demotivated Sep 07 '16 at 17:55
  • Can you please post the error? Are you running in a web browser (e.g. html file) or on a local Node.JS server? – Romeo Kienzler Sep 10 '16 at 09:41
  • i am already running it on localhost,i found it out,now i want to know how can i pass a php var from a php file to this node app – Demotivated Sep 10 '16 at 17:06
0

Here's the code I used to query the API from my local machine for testing.

var watson = require('watson-developer-cloud');
var personality_insights = watson.personality_insights({
  username: '<username>',
  password: '<password>',
  version: 'v2'
});

var express = require('express');
var app = express();


var http = require('http').Server(app);

var server_port = 8080
var server_ip_address = '127.0.0.1'

var bodyParser = require('body-parser')
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({
  extended: true
})); 

app.use('/', express.static(__dirname + '/public'));

app.get("/", function(req, res){
    res.sendFile(__dirname + '/public/index.html');
});

app.post("/post", function(req, res){
    getInsights(req.body.text,res);
});

function getInsights(text,res) {
    var params = {};
    params.text = text;
    personality_insights.profile(params, function(error, response) {
      if (error)
        console.log('error:', error);
      else {
        res.send(JSON.stringify(response));
      }
    });
}

http.listen(server_port,server_ip_address, function(){
  console.log( "Listening on " + server_ip_address + ", server_port " + server_port )
});

I have a simple index.html in the public folder for me to type in the text and send over to nodejs

<!doctype html>
<html>
  <head>
    <title>Status</title>
    <style>

    </style>
  </head>
  <body>
    <div style="margin:10px"><textarea style="width:100%;height:500px;" id="text"></textarea></div>
    <div>Words: <span id="count">0</span> <button onClick="onSubmit()">Submit</button></div>
    <br/><br/>
    <div id="response">

    </div>
  </body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="Countable.js"></script>
<script>

  var word_counter = 0;

  var area = document.getElementById('text')

  Countable.live(area, function (counter) {
    $("#count").html(counter.words);
    //console.log(counter);
    word_counter = counter.words;
  });

  function onSubmit() {
    if (word_counter < 100) {
      $("#response").html("Please provide at least 100 words");
      return;
    }
    var text = $("#text").val();
    $.ajax({
      type: "POST",
      url: '/post',
      data: {text:text.trim()},
      dataType: "json",
      success: function(data) {
        $("#response").html(data);
      }
    })
  }
</script>

</html>

hope that helps

Adrian
  • 1
  • 1