Git Product home page Git Product logo

ibm / serverless-language-translation Goto Github PK

View Code? Open in Web Editor NEW
17.0 20.0 35.0 409 KB

Create a communications channel using serverless technology to allow clients who speak different languages to seamlessly communicate with each other, built on Watson services.

Home Page: https://developer.ibm.com/code/patterns/deploy-serverless-multilingual-conference-room/

License: Apache License 2.0

JavaScript 72.29% Python 8.70% Shell 17.06% Dockerfile 1.95%
ibmcode language-translation watson-translator serverless openwhisk call-for-code

serverless-language-translation's Introduction

Read this in other languages: 日本語.

Build Status

Deploy a Serverless Multilingual Conference Room

In this code pattern, we will create the workings for a multilingual chat room using OpenWhisk, Watson Text to Speech and Watson Language Translator. The MQTT messaging protocol is also leveraged, which allows each client to publish and subscribe to one or more channels. This repository contains a series of serverless functions which are called in sequence determined by the channel to which a message is submitted.

When the reader has completed this code pattern, they will understand how to:

  • Deploy Cloud Function actions and triggers
  • Interact with the Watson IoT platform
  • Set up a CloudFoundry application

Architecture

Flow

  1. Message received from a client, which can be a web browser, CLI, OpenWhisk action, SMS text, etc.
  2. If message payload contains an audio file, it is transcribed to text.
  3. Transcribed text is translated to other supported languages.
  4. If message is sent via SMS, sender phone number is added to an etcd key-value store. etcd is used here to maintain a list of subscribers’ phone numbers, as well as their respective languages. An adjustable TTL value is used here to remove numbers from the store if the subscriber does not participate in the conversation for 300 seconds.
  5. Translated messages/audio streams are published to various channels on the MQTT broker, which then distributes the messages among subscribing clients.

Included components

  • IBM Cloud Functions: Execute code on demand in a highly scalable, serverless environment.
  • Watson Text to Speech: Converts written text into natural sounding audio in a variety of languages and voices.
  • Watson Language Translator: Translate text from one language to another. Take text from across the globe and present it in the language of your choice.

Featured technologies

  • Messaging: Messaging is a key technology for modern applications using loosely decoupled architecture patterns such as microservices.
  • Node.js: An open-source JavaScript run-time environment for executing server-side JavaScript code.
  • OpenWhisk: An open source event-driven platform that allows you to execute code in response to an event. This is the underlying technology for the IBM Cloud Functions offering

Watch the Video

Prerequisites:

To interact with the hosted offerings, the IBM Cloud CLI will need to be installed beforehand. Instructions can be found here.

After installation is complete, confirm the CLI is working by printing the version

ibmcloud -v

And then install the "Cloud Functions" plugin with

ibmcloud plugin install cloud-functions

If the IBM Cloud CLI was already installed, please make sure the Cloud Functions plugin is the latest version with

ibmcloud plugin update cloud-functions

Steps

  1. Create Services
  2. Deploy MQTT Feed
  3. Upload Actions
  4. Create Triggers
  5. Create Rules
  6. Deploy UI

1. Create Services

Create the required IBM Cloud services.

For SMS integration, create the following third party services.

Each service can be provisioned with the following steps

Navigate to the IBM Cloud dashboard at https://cloud.ibm.com and click the "Catalog" button in the upper right

Type in the name of the service and select the resulting icon

Select the pricing plan and click "Create". If deploying on an IBM Lite account, be sure to select the free "Lite" plan

Additional Configuration: Generate Watson IoT service credentials

After being provisioned, the Watson IoT Platform service will need a bit of additional configuration, as we'll need to generate a set of credentials for connecting to the broker. We can do so by entering the IoT Platform dashboard, selecting "Devices" from the left hand menu, and then clicking the "Add Device" button.

Next, provide a device type and ID.

The next few tabs (Device Information, Groups, Security) can be left as is with the default settings.

Clicking the "Finish" button will register a device and generate a set of credentials that can be used to publish messages to the IoT Platform. Be sure to take note of the Device type and Device ID, and place both in the cfcreds.env file.

We'll need to generate a different set of credentials to be able to publish and subscribe to the MQTT Broker

We can do so by selecting the "Apps" option in the left hand menu. Then, click the "Generate API Key" button

We can leave the fields in the "Information" blank and click next. In the "Permissions" tab, we'll select the "Backend Trusted Application" role. Once this is selected, click "Generate Key"

The result will give us an API Key and Authentication Token. These can be supplied as the username and password for a MQTT client. To make setup a bit easier, place these values in the cfcreds.env file as IOT_API_KEY and IOT_APP_AUTH_TOKEN

At this point, the values IOT_ORG_ID, IOT_DEVICE_TYPE, IOT_DEVICE_ID, IOT_AUTH_TOKEN, and IOT_API_TOKEN should be in the cfcreds.env file. In this example, we used the npm mqtt.js package, which can be installed with npm install -g mqtt. And then, we can run a sample MQTT publish/subscribe command to confirm that the credentials are valid.

Subscribe to the MQTT broker in one tab

source cfcreds.env
mqtt_sub -i "a:${IOT_ORG_ID}:test" -u "${IOT_API_KEY}" -P "${IOT_AUTH_TOKEN}" -h "${IOT_ORG_ID}.messaging.internetofthings.ibmcloud.com" -p 1883 -t "iot-2/type/${IOT_DEVICE_TYPE}/id/${IOT_DEVICE_ID}/evt/fromClient/fmt/json"

And then publish in another

source cfcreds.env
mqtt_pub -i "a:${IOT_ORG_ID}:client_pub" -u "${IOT_API_KEY}" -P "${IOT_AUTH_TOKEN}" -h 'agf5n9.messaging.internetofthings.ibmcloud.com' -p 1883 -t "iot-2/type/${IOT_DEVICE_TYPE}/id/${IOT_DEVICE_ID}/evt/fromClient/fmt/json" -m '{
    "d" : {
          "sourceLanguage" : "en",
          "payload" : "test",
	        "client": "client1"
    }
}'

2. Deploy MQTT Feed

Install the MQTT package/feed found in the openwhisk-package-mqtt-watson submodule here. This "feed" enables OpenWhisk to subscribe to one or more MQTT topics and invoke actions in response to incoming messages. To see more on how feeds work with IBM Cloud Functions, please visit these documents

3. Upload Actions

Upload each "Action" to the Cloud Functions codebase with the following commands.

ibmcloud fn action create translateText translateText.js
ibmcloud fn action create sendSMS sendSMS.js
ibmcloud fn action create iotPub iotPub.py
ibmcloud fn action create handleIncomingSMS handleIncomingSMS.js

After each action is created, set or bind default credentials for the corresponding services.

# Most IBM Cloud native service credentials can be easily imported to a Cloud function using the "service bind" command
# ibmcloud fn service bind <service> <action_name>
ibmcloud fn service bind language_translator translateText
ibmcloud fn service bind language_translator handleIncomingSMS


# Credentials for the Watson IoT Platform and third party services can be set using the "update command"
# ibmcloud fn action update <action_name> -p <param_name> <param_value>
ibmcloud fn action update iotPub -p iot_org_id "${IOT_ORG_ID}" -p iot_device_id "${IOT_DEVICE_ID}" -p iot_device_type "${IOT_DEVICE_TYPE}" -p iot_auth_token "${IOT_AUTH_TOKEN}" -p iot_api_key "${IOT_API_KEY}"
ibmcloud fn action update sendSMS -p twilioNumber "${TWILIO_NUMBER}" -p twilioSid "${TWILIO_SID}" -p twilioAuthToken "${TWILIO_AUTH_TOKEN}" -p redisUsername "${REDIS_USER}" -p redisPassword "${REDIS_PASSWORD}" -p redisHost "${REDIS_HOST}" -p redisPort "${REDIS_PORT}"
ibmcloud fn action update handleIncomingSMS -p twilioNumber "${TWILIO_NUMBER}" -p twilioSid "${TWILIO_SID}" -p twilioAuthToken "${TWILIO_AUTH_TOKEN}" -p redisUsername "${REDIS_USER}" -p redisPassword "${REDIS_PASSWORD}" -p redisHost "${REDIS_HOST}" -p redisPort "${REDIS_PORT}"

4. Create Triggers

Create Triggers to represent events.

ibmcloud fn trigger create audioMsgReceived
ibmcloud fn trigger create txtMsgReceived
ibmcloud fn trigger create SMSMsgReceived
ibmcloud fn trigger create msgTranslated

5. Create Rules

Create Rules, which execute actions when certain triggers are activated.

# ibmcloud fn rule create RULE_NAME TRIGGER_NAME ACTION_NAME
ibmcloud fn rule create handleTxtMessage txtMsgReceived translateText
ibmcloud fn rule create handleMQTTMessage mqttMsgReceived translateText
ibmcloud fn rule create publishtoIOT msgTranslated iotPub
ibmcloud fn rule create publishtoSMS msgTranslated sendSMS

After the Feed has been deployed and the rules have been established, test the process by sending a MQTT message to the topic registered with the feed like so

source cfcreds.env
mqtt_pub -i "a:${IOT_ORG_ID}:client_pub" -u "${IOT_API_KEY}" -P "${IOT_AUTH_TOKEN}" -h 'agf5n9.messaging.internetofthings.ibmcloud.com' -p 1883 -t "iot-2/type/${IOT_DEVICE_TYPE}/id/${IOT_DEVICE_ID}/evt/fromClient/fmt/json" -m '{
    "d" : {
          "sourceLanguage" : "en",
          "payload" : "test",
	        "client": "client1"
    }
}'

As soon as this command is published, we should be able to see a series of actions and triggers being called in the Cloud Functions logs. These logs can be viewed by visiting https://cloud.ibm.com/openwhisk/dashboard or by running ibmcloud fn activation poll in a separate tab.

6. Deploy UI

If all you need is the server side logic, you can stop here. But optionally, you can deploy the UI provided by https://github.com/IBM/language-translation-ui. Be sure to source the cfcreds.env file beforehand, as the UI expects the IOT_ORG_ID, IOT_DEVICE_TYPE, IOT_DEVICE_ID, IOT_AUTH_TOKEN, and IOT_API_TOKEN values to be set as environment variables

git clone https://github.com/IBM/language-translation-ui
cd language-translation-ui
npm install
npm start

When the npm start command succeeds, you should be able to access the UI at http://127.0.0.1:8080

Developer Notes

Flow:

  • MQTT message received as JSON object to topic iot-2/type/'${deviceType}/id/${orgId}/evt/${eventName}/fmt/json
{
  client: "client_1234",
  payload: "hello",
  sourceLanguage: "en"
}
  • Trigger associated with topic forwards message payload/language to translator action.
  • Translator action passes message payload through a loop, where each item is a language that the original message will be translated to. After translation is complete, another trigger will be fired, which kicks off two other "publish" actions simultaneously.
    • One action publishes results to all MQTT clients
    • The other action looks up SMS subscriber numbers/language in Redis and sends them the result via Twilio.

Links

License

This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 and the Apache License, Version 2.

Apache License FAQ

serverless-language-translation's People

Contributors

imgbotapp avatar kant avatar kkbankol-ibm avatar ljbennett62 avatar loafyloaf avatar markstur avatar stevemar avatar stevemart avatar yamachan avatar zmohammad01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

serverless-language-translation's Issues

Typo in README lins

In step 4 the link to the UI git repo contains a typo, so the link doesn't work = the 't' in github is missing.

Microphone & Speaker

The button for microphone does not seem to work and the speaker only works on the first chat window.

the code base is broken and untested

I initially tried to fix the travis CI failure since a PR to fix a typo was failing. The travis failures have gone untriaged for weeks and anyone using this code pattern will be broken from the get-go. I am now left wondering why this was published in the first place.

Travis CI

I had to chmod 775 the files in /tests to get them to run. There's a whole slew of errors:

8     E111 indentation is not a multiple of four
4     E114 indentation is not a multiple of four (comment)
2     E115 expected an indented block (comment)
3     E201 whitespace after '('
3     E202 whitespace before ')'
5     E203 whitespace before ','
1     E225 missing whitespace around operator
3     E231 missing whitespace after ':'
2     E261 at least two spaces before inline comment
2     E302 expected 2 blank lines, found 1
12    E501 line too long (140 > 79 characters)
1     E999 SyntaxError: invalid syntax
1     F401 'sys' imported but unused
7     F821 undefined name 'params'
1     H101: Use TODO(NAME)
2     H233: Python 3.x incompatible use of print operator
2     H306: imports not in alphabetical order (sys, requests)

IoT Pub.py

There is a syntax error here: https://github.com/IBM/serverless-language-translation/blob/master/iotPub.py#L22 which is rather necessary for the route to even work.

... + device_type '/devices/' + device_id

it should be:

... + device_type + '/devices/' + device_id

handleIncomingSMS.py

More things preventing the code from being executed are here, where is params defined? I have no idea how to fix this, I'm not that familiar with the code base.

def main(dict):
  sender = dict['From'] # dict['sender']
  message = dict['Body'] # dict['message']

  # Get etcd keys
  etcd_endpoint_keys = 'http://' + params.etcd_endpoint+ ':2379/v2/keys'

mqqt-watson/feed-action issue

The "wsk" CLI is not available. It is now "bx wsk".

  • There is a PR to fix this in the krook repo.
  • In the kkbankol-ibm fork, I can make it work using double quotes and "bx wsk" as the $CLI passed to the script. But that should be documented or just be the default (like the PR).

I can't raise any issues in the kkbankol-ibm fork and it might be out-of-sync with the krook version. So I guess I'll raise the issue here, but that's confusing.

3rd-party redis service unavailable

Near the end of June, several 3rd party services were removed from the IBM Cloud catalog including the Redis Cloud service. Compose for Redis could stand in as a replacement: https://console.bluemix.net/catalog/services/compose-for-redis but may need a little extra instruction on how to unpack the credentials URL that comes with Compose for Redis into the credentials environment file.

Note that this is a not a lite service, but the Redis Cloud service also required a paid account for deployment.

which credentials for which actions are needed?

There is a "..." and I think maybe that means read the code for the 4 actions and figure out which services you need to collect creds from to set as params for those actions.

Could use some more explanation.

Is getTTStoken.js needed?

There is code, but we don't create the action, but then we try to set credentials in the action and fail.

mqttMsgReceived does not exist

I went through this out-of-order while things were changing, but it looks to me like there's a step that requires mqttMsgReceived which does not exist (at least not yet).

bx wsk rule create handleMQTTMessage mqttMsgReceived translateText

error: Unable to create rule 'handleMQTTMessage': trigger /mark.sturdevant_myspace/mqttMsgReceived does not exist (code 03ac15c407bf50360aba72e460e6f7a6)

Additional Language Support

From my knowledge Watson LT supports multiple languages that are not reflected in the drop-down, perhaps these additional languages and support for them can be added?

For the languages not supported by STT/TTS (speech services) the mic/speaker can be disabled, but the language translation can still occur?

are the instructions complete?

I read through the README and this doesn't give me a lot of confidence that this is actually complete, see the screenshot:

screen shot 2018-05-15 at 11 42 29 am

It would also be great to have a screenshot of the app running or the serverless-translation-ui

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.