python - posting text from DataFrame to IBM PersonalityInsights API -
i'm trying post data dataframe file watson personality insights api using object storage in ibm datascienceexperience..
i've loaded txt file objectstorage , created dataframe. works fine. don't understand how post data in dataframe api. provided documentation not point me right direction.
this i've done
from io import stringio import requests import json import pandas pd def get_object_storage_file_with_credentials(container, filename): """this functions returns stringio object containing file content 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() e1 in resp1_body['token']['catalog']: if(e1['type']=='object-store'): 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 want post dataframe content api know how, hope can provide tip... python knowledge lacking here.
according watson personality insights api reference, can provide text, html or json input. dataset available pandas dataframe. try converting relevant column in dataframe text format. example by:
pi_api_text = pi_text['<text_column>'].str.cat(sep='. ').encode('ascii', 'ignore')
make sure have python package installed:
pip install --upgrade watson-developer-cloud
once have relevant data in text format make call watson personality insights api. 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 json object containing personality traits, can re transform pandas dataframe.
Comments
Post a Comment