posting text from DataFrame to IBM PersonalityInsights API

241 views Asked by At

I'm trying to post the data from a DataFrame file to the Watson Personality Insights API using Object storage in IBM DataScienceExperience..

I've loaded the txt file into ObjectStorage and created a DataFrame. Works fine. Don't understand how to post the data in the dataframe to the API. The provided documentation does not point me into the right direction.

This is what I've done

from io import StringIO
import requests
import json
import pandas as pd

def get_object_storage_file_with_credentials(container, filename):
    """This functions returns a StringIO object containing
    the file content from Bluemix Object Storage."""

    url1 = ''.join(['https://identity.open.softlayer.com', '/v3/auth/tokens'])
    data = {
        'auth': {
            'identity': {
                'methods': ['password'],
                'password': {
                    'user': {
                        'name': 'UID UID UID',
                        'domain': {
                            'id': 'ID ID ID'
                        },
                    'password': 'PASS PASS'
                    }
                }
            }
        }
    }
    headers1 = {'Content-Type': 'application/json'}
    resp1 = requests.post(url=url1, data=json.dumps(data), headers=headers1)
    resp1_body = resp1.json()

    for e1 in resp1_body['token']['catalog']:
        if(e1['type']=='object-store'):
            for e2 in e1['endpoints']:
                if(e2['interface']=='public'and e2['region']=='dallas'):
                    url2 = ''.join([e2['url'],'/', container, '/', filename])

    s_subject_token = resp1.headers['x-subject-token']
    headers2 = {'X-Auth-Token': s_subject_token, 'accept': 'application/json'}
    resp2 = requests.get(url=url2, headers=headers2)

    return StringIO(resp2.text)

PI_text = get_object_storage_file_with_credentials('MyDSXProjects', 'myPI.txt')

Next I want to post the DataFrame content to the API I would like to know how, hope someone can provide a tip... My Python knowledge is lacking here.

1

There are 1 answers

2
Sumit Goyal On

According to the Watson Personality Insights API reference, you can provide text, HTML or JSON input. You dataset is available as a pandas dataframe. Try converting the relevant column in the DataFrame to text format. For example by:

pi_api_text = PI_text['<TEXT_COLUMN>'].str.cat(sep='. ').encode('ascii', 'ignore')

Make sure you have the Python package installed:

pip install --upgrade watson-developer-cloud

Once you have the relevant data in text format make a call to the Watson Personality Insights API. For example as:

personality_insights = PersonalityInsightsV3(
 version='xxxxxxxxx',
 username='xxxxxxxxxx',
 password='xxxxxxxxxx')
profile = personality_insights.profile(
 pi_api_text, content_type='text/plain',
 raw_scores=True, consumption_preferences=True)

The response will be a JSON object containing the personality traits, which you can re transform to a pandas dataframe.