Skip to content

Instantly share code, notes, and snippets.

@ishida330
Created February 7, 2019 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishida330/06da484b28c776d59c57e5a9bd06683f to your computer and use it in GitHub Desktop.
Save ishida330/06da484b28c776d59c57e5a9bd06683f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Watson OpenScale用にトランザクションをセットする方法"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Replace with your WML Credentials\n",
"wml_credentials={\n",
"\n",
" \"apikey\": \"xxxxxxxxxx\",\n",
" \"iam_apikey_description\": \"xxxxxxxxxx\",\n",
" \"iam_apikey_name\": \"xxxxxxxxxx\",\n",
" \"iam_role_crn\": \"xxxxxxxxxx\",\n",
" \"iam_serviceid_crn\": \"xxxxxxxxxx\",\n",
" \"instance_id\": \"xxxxxxxxxx\",\n",
" \"password\": \"xxxxxxxxxx\",\n",
" \"url\": \"xxxxxxxxxx\",\n",
" \"username\": \"xxxxxxxxxx\"\n",
"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from watson_machine_learning_client import WatsonMachineLearningAPIClient\n",
"client = WatsonMachineLearningAPIClient(wml_credentials)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# scoring url はMachine Learningの管理機能で取得したものを利用します。\n",
"scoring_url = 'https://us-south.ml.cloud.ibm.com/v3/wml_instances/xxxxxxxx/deployments/xxxxxxx/online'\n",
"\n",
"# 呼出し時のパラメータ\n",
"params = {\n",
" \"fields\": ['NA','K','AGE','CHOLESTEROL','SEX','BP'],\n",
" \"values\": [[ 0.66,0.05,43.0,'HIGH','M','LOW']]\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. スコアリング・エンドポイントをWML-Pythonクライアントから使う場合\n",
"## client.deployments.score(scoring_url, params, transaction_id=\"XXX\")で指定"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [WML Python Document - deployments - score](https://wml-api-pyclient.mybluemix.net/#deployments)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#モデル呼出し時のパラメーターでtransaction_id=を指定\n",
"scores = client.deployments.score(scoring_url, params, transaction_id=\"CUSTOMER-001-01\")\n",
"# 結果表示\n",
"print(scores)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. スコアリング・エンドポイントをREST(POST)で使う場合\n",
"# HTTPヘダーのX-Global-Transaction-Idで指定"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [AIOS Explaining transactions](https://cloud.ibm.com/docs/services/ai-openscale/insight-explain.html#view)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# AI OpenScaleのinternalDBの接続情報の入手\n",
"import requests, json\n",
"\n",
"# IAM Tokenの入手\n",
"def getToken(apikey):\n",
" url = 'https://iam.bluemix.net/identity/token'\n",
" header = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}\n",
" payload = {'grant_type': 'urn:ibm:params:oauth:grant-type:apikey', 'apikey': apikey}\n",
" response = requests.post(url, payload, headers=header).json()\n",
" return response['access_token']\n",
"## CHANGE ME\n",
"## platform_apikey ... IBM Cloudの 管理 - セキュリティ - プラットフォームAPIキー より作成\n",
"platform_apikey = \"xxxxxxxxxx\"\n",
"header = {'Content-Type': 'application/json', 'Accept':'application/json',\n",
" 'Authorization': 'Bearer ' + getToken(platform_apikey), 'X-Global-Transaction-Id':'CUSTOMER-002-01'}\n",
"response = requests.post(scoring_url, data=json.dumps(params), headers=header).json()\n",
"print(response)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment