February 2, 2018 By Carlos Santana 3 min read

Simplifying the use of IBM services and data science

IBM Cloud Functions sets a limit of 48 MB for your action code, including dependencies, which can be large. This means you wouldn’t be able to upload certain actions (if too large), or the upload could take a long time, specifically when you update often. To relieve you from that burden, we preinstalled several important packages.

The new runtime includes packages that are useful when integrating with IBM Cloud services such as DB2, Watson, Cloudant, and Cloud Object Storage.

In addition, some popular Python packages for data science are also pre-installed, such as numpy, pandas, scipy, and scikit-learn.

The version of Python is 3.6 and is based on a Debian/Ubuntu (Jessie release) operating system.

If you have your own python modules, or third party packages you can always package them using the virtualenv approach, and uploading a zip. If the zip is too large, you can extend the runtime, and install additional python packages, or add system utilities and libraries by using apt-get.

For more details on the Python 3 environment, check the documentation.

Using IBM Cloud Services from Python 3 Functions

Let’s build an application that handles customer feedback that is composed with two actions in a sequence. The first one takes the text from a customer feedback form, and translates it with the IBM Watson Language service. The second action stores the content in a database by using the IBM DB2 on Cloud service.

Watson: Translate Action using Python 3

Create a Python file translateForm.py with the following code:

<br>
from watson_developer_cloud import LanguageTranslatorV2<p></p>
<p>def main(args):<br>
    customer_id = args["customer_id"]<br>
    text = args["payload"]<br>
    language_translator = LanguageTranslatorV2(<br>
        url=args["__bx_creds"]["language_translator"]["url"],<br>
        username=args["__bx_creds"]["language_translator"]["username"],<br>
        password=args["__bx_creds"]["language_translator"]["password"])<br>
    lang_id = language_translator.identify(text)["languages"][0]["language"]<br>
    response = language_translator.translate(text, source=lang_id, target='en')<br>
    return {"payload": response["translations"][0]["translation"],<br>
            "customer_id": customer_id}<br>
</p>

DB2: Store Action using Python 3

Create a Python file storeFormDB.py with the following code:

<br>
import ibm_db<br>
import datetime<p></p>
<p>def main(args):<br>
    global conn<br>
    customer_id = args["customer_id"]<br>
    text = args["payload"]<br>
    ssldsn = args["__bx_creds"]["dashDB"]["ssldsn"]<br>
    if globals().get("conn") is None:<br>
        conn = ibm_db.connect(ssldsn, "", "")<br>
    statement = "INSERT INTO CUSTOMER_FEEDBACK (CUST_ID, FEEDBACK, date) VALUES (?, ?, ?)"<br>
    stmt = ibm_db.prepare(conn, statement)<br>
    ts_val = datetime.datetime.today()<br>
    result = ibm_db.execute(stmt,(customer_id, text, ts_val))<br>
    return {"result": f"Stored feedback {text} for customer {customer_id} at {ts_val}"}<br>
</p>

Tip: The Action assumes that a DB table named CUSTOMER_FEEDBACK is already created and available.

Sequence: Deploy the Actions

Deploy your actions that use the kind python-jessie:3 to use the new Python 3 runtime:

<br>
bx wsk action create translateForm translateForm.py --kind python-jessie:3<br>
<br>
bx wsk action create storeFormDB storeFormDB.py --kind python-jessie:3<br>
<br>
bx wsk action create myCustomerFormSeq translateForm,storeFormDB --sequence<br>

Set up the credentials for the Actions

Bind the service credentials to the actions parameters. You can use the IBM Cloud Functions CLI to bind your actions to IBM Cloud Services.

Bind the action `translateForm` to the `language_translator` service credentials:

<br>
bx wsk service bind language_translator translateForm<br>

Bind the action `storeFormDB` to the `dashDB` service credentials:

<br>
bx wsk service bind dashDB storeFormDB<br>

Tip: You must have at least one set of Credentials for the service before running the bx wsk service bindcommand.

This CLI command copied the service credentials and set them as default action parameter, by convention a single parameter of type JSON object named __bx_creds.

You can also do this directly by setting the default parameters with the values from your service credentials into any parameter, and then adjust your action code.

Note: Cloud Object Storage can’t be bound to actions by using the CLI, but support will be added soon. This blog post will be updated when support becomes available.

Update: Now the CLI support for Cloud Object Storage is added with bx wsk service bind command.

Test your Customer Feedback Application

You can now test your application by invoking your sequence action:

<br>
bx wsk action invoke myCustomerFormSeq -p customer_id 42 -p payload "Muy buen servicio" -r<br>
<br>
{<br>
"result": "Stored feedback Very good service from customer ..."<br>
}<br>

Python and Data Science

Python is often the programming language of choice for data scientists for prototyping, visualization, and running data analyses on data sets. Many libraries, applications, and techniques are available to analyze data in Python.

The new runtime includes a few libraries that you can leverage from your Actions. Here is an example that uses Pandas and Numpy:

<br>
import pandas as pd<br>
import numpy as np<p></p>
<p>def main(args):<br>
    dates = pd.date_range('20130101', periods=2)<br>
    df = pd.DataFrame(np.random.randn(2,2), index=dates, columns=list('AB'))<br>
    print(df)<br>
    return df.to_dict('split')<br>
</p>

Deploy your data science action

<br>
bx wsk action update pandas pandas.py --kind python-jessie:3<br>

Invoke your data science action

<br>
bx wsk action invoke pandas -r<br>

 

<br>
{<br>
    "columns": [<br>
        "A",<br>
        "B"<br>
    ],<br>
    "data": [<br>
        [<br>
            -1.2921755448830252,<br>
            0.5663123130894548<br>
        ],<br>
        [<br>
            1.0437128172227568,<br>
            1.0506338576086816<br>
        ]<br>
    ],<br>
    "index": [<br>
        "Tue, 01 Jan 2013 00:00:00 GMT",<br>
        "Wed, 02 Jan 2013 00:00:00 GMT"<br>
    ]<br>
}<br>

Learn more about IBM Cloud Functions

More from Announcements

IBM named a Leader in Gartner Magic Quadrant for SIEM, for the 14th consecutive time

3 min read - Security operations is getting more complex and inefficient with too many tools, too much data and simply too much to do. According to a study done by IBM, SOC team members are only able to handle half of the alerts that they should be reviewing in a typical workday. This potentially leads to missing the important alerts that are critical to an organization's security. Thus, choosing the right SIEM solution can be transformative for security teams, helping them manage alerts…

IBM and MuleSoft expand global relationship to accelerate modernization on IBM Power 

2 min read - As companies undergo digital transformation, they rely on APIs as the backbone for providing new services and customer experiences. While APIs can simplify application development and deliver integrated solutions, IT shops must have a robust solution to effectively manage and govern them to ensure that response times and costs are kept low for all applications. Many customers use Salesforce’s MuleSoft, named a leader by Gartner® in full lifecycle API management for seven consecutive times, to manage and secure APIs across…

IBM Consulting augments expertise with AWS Competencies: A win-win for clients 

3 min read - In today's dynamic economic landscape, businesses demand continuous innovation and speed of execution. At IBM Consulting®, our unwavering focus on partnerships and shared commitment to delivering enterprise-level solutions to mutual clients have been core to our success.   We are thrilled to announce that IBM® has recently gained five competencies from Amazon Web Services (AWS) in vital domains including Cloud Operations, Internet of Things (IoT), Life Sciences, Mainframe Modernization, and Telecommunications. With these credentials, IBM further establishes its position as a…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters