top of page

JavaScript API using Cylon.js to interact with IBM Watson cloud

Cylon is a Javascript framework with support for over 43 platforms for robotics, physical computing and internet of things.




Cylon can run directly in the browser (through browserify) or any Chrome-connected app, and support http/https, mqtt and socket.io plugins.

Here is an example of how one can write a JavaScript API using Cylon.js to interact with IBM Watson cloud services:

// Require the cylon module and watson developer cloud module
var Cylon = require('cylon');
var watson = require('watson-developer-cloud');

// Define the Watson speech-to-text service credentials
var speechToText = watson.speech_to_text({
  username: 'your_username_here',
  password: 'your_password_here',
  version: 'v1'
});

// Define the robot
Cylon.robot({
  // Define connections to hardware devices
  connections: {
    watson: { adaptor: 'watson', speech_to_text: speechToText }
  },

  // Define API
  work: function(my) {
    // Define functions to be exposed by the API
    my.apiFunctions = {
      transcribeAudio: function(audioFile, callback) {
        // Use the Watson speech-to-text service to transcribe the audio file
        var params = {
          audio: audioFile,
          content_type: 'audio/wav',
          model: 'en-US_BroadbandModel'
        };
        my.watson.speech_to_text.recognize(params, function(err, res) {
          if (err) {
            console.log(err);
            callback(err, null);
          } else {
            console.log(JSON.stringify(res, null, 2));
            callback(null, res);
          }
        });
      }
    };
  }
}).start();

// Expose API functions
module.exports = function(callback) {
  Cylon.api({
    host: '0.0.0.0',
    port: '3000',
    ssl: false,
    auth: false,
    CORS: false,
    logger: false,
    // Expose the apiFunctions object as the API
    api: Cylon.robots[0].apiFunctions
  });

  // Call the callback function to indicate that the API is ready
  callback();
};

In this example, we first require the watson-developer-cloud module and create a speechToText object with the credentials for the Watson speech-to-text service.

We then define a robot with a connection to the Watson service using the watson adaptor. Inside the work function, we define an object called apiFunctions that contains a single function called transcribeAudio, which takes an audio file and a callback function as parameters. This function uses the recognize method of the Watson speech-to-text service to transcribe the audio file and calls the callback function with the result or an error.

We then use the Cylon.api function to expose the apiFunctions object as an API on port 3000. Finally, we export a function that takes a callback parameter, which is called when the API is ready. This allows us to start the API server asynchronously.


Agilytics is a silver partner of IBM Watson cloud

29 views0 comments

Comments


bottom of page