0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python SDKでPrimeHubを統合するのメリットは〜

0
Last updated at Posted at 2022-01-19

AI Solution Provider向け

PrimeHubを統合してする自身のAI solutionにすると、

  • 複雑なMLインフラの作り時間の無駄にしなくで、ソリューションの開発は急速に進む

  • アルゴリズムやモデルに貴重な開発力を集中する

  • フロントエンドやUIに貴重な開発力を集中する

  • AutoMLの実現可能

Data Scientist team向け

PrimeHubを統合してするMLインフラにすると、

  • ローカルPython codeでPrimeHubに時間を費やすタスクを任せる

  • 計算リソース配置、アクセス制御、開発環境管理

SDK Scenario

end2end-automl.png

mlops-retrain-redeploy.png

mlops-regular-retrain-redeploy.png

mlops-monitor.png

必要なもの

InstallとConfigure

Install PrimeHub Python SDK(CLI付き)

pip install primehub-python-sdk

コンフィギュレーションをする.

# Bracket URL by ""
primehub config generate-token "https://<where_primehub_is>"

URLにログインしてauthentication codeをコピーして入力する

Go to this URL in the browser https://<where_primehub_is>/console/oidc/auth-flow/request
Enter your authorization code:

検証する

primehub me

# Output
id:             xxxxxx-yyyy-zzzz-wwww-aaaaaaaaa
username:       <username>
firstName:      <first_name>
lastName:       <last_name>
email:          <email>
isAdmin:        <True/False>

又は

cat ~/.primehub/config.json

# Output
{
  "api-token": "<token>",
  "endpoint": "https://<where_primehub_is>/api/graphql",
  "group": {
    "name": "<group_name>"
  }
}

Python SDKの使用

PrimeHub SDK instanceの初期化する

from primehub import PrimeHub, PrimeHubConfig
ph = PrimeHub(PrimeHubConfig())

ファンクションの実例

ph.<command>.command() 

# e.g.
ph.deployments.create()
ph.jobs.submit()
ph.models.get()

# help(ph.<command>)
help(ph.jobs)

詳しい実例はこちらのJupyter Notebookを参考する

Iris Model ショーケースのコード

from primehub import PrimeHub, PrimeHubConfig

ph = PrimeHub(PrimeHubConfig())

# Check the SDK setup is ready.
if ph.is_ready():
    print("PrimeHub Python SDK setup successfully")
    print("Current Group:", ph.primehub_config.current_group)
else:
    print("PrimeHub Python SDK couldn't get the group information, please check the configuration.")

モデルトレーニングのjobを提出して、生成されたモデルを登録する

# A Job context
config = {
    "instanceType": "cpu-1",
    "image": "tf-2",
    "displayName": "iris-model-training",
    "command": """git clone https://github.com/InfuseAI/showcase.git
ls
cd ./showcase/primehub-sdk-ct-pipeline
pip install -r requirements.txt
python main.py
""",
}

short_job = ph.jobs.submit(config)
print(short_job)

# Wait until the job is done
print('[ Waiting ]')
ph.jobs.wait(short_job['id'])
print('[ Job Done ]')

# Get the Job logs
logs = ph.jobs.logs(short_job['id'])
print('[ Job Logs ]')
for l in logs:
    print(l)

登録されたモデルのバージョンを出力する

model_information = []
model_versions = ph.models.list_versions('iris-model')
for version in model_versions:
  model_information.append(version)

モデルをデプロイする

from primehub.utils import ResourceNotFoundException

# Create a deployment
deployment_id=f"iris-model" 
config = {
    "id": deployment_id,
    "name": f"Iris model",
    "modelImage": "infuseai/sklearn-prepackaged:v0.1.0",
    "modelURI": "models:/iris-model/{}".format(model_information[-1]['version']),
    "instanceType": "cpu-1",
    "replicas": 1
}

# Delete an existing deployment
try:
  ph.deployments.delete(deployment_id)
  print("Finish delete model deployment")
except ResourceNotFoundException as e:
  print("Model deployment resource not found.")

# Create a deployment
deployment = ph.deployments.create(config)
print(f"""
Deployment Id: {deployment_id} 
Deployment Page: {PRIMEHUB_CLUSTER}/console/g/phusers/deployments/{deployment_id}
""")

# Wait for the deployment ready
print('[ Waiting ]')
ph.deployments.wait(deployment['id'])
print('[ Deployment Ready ]')
deployment = ph.deployments.get(deployment['id'])

デプロイしたモデルのAPIへ予測を提出する

import json
import requests

url = deployment['endpoint']
data = {
    "data": {
        "ndarray": [[1, 1, 1, 0.5]]
    }
}
headers = {'Content-type': 'application/json'}

result = requests.post(url, data=json.dumps(data), headers=headers)
if result.status_code != 200:
  print(f"{result.status_code}: {result.reason}")

predict_result = json.loads(result.content.decode())

print(predict_result)

Colab実例


CLIの使用

主要なコマンド

primehub                                                         

子コマンドの一覧表

Usage:
  primehub <command>

Available Commands:
  admin                Commands for system administrator
  apps                 Manage PrimeHub Applications
  apptemplates         Get PhAppTemplates
  config               Update the settings of PrimeHub SDK
  datasets             Manage datasets
  deployments          Get a deployment or list deployments
  files                List and download shared files
  groups               Get a group or list groups
  images               Get a image or list images
  info                 Display the user information and the selected group information
  instancetypes        Get an instance types of list instance types
  jobs                 Manage jobs
  me                   Show user account
  models               Manage models
  notebooks            Get notebooks logs
  recurring-jobs       Manage recurring jobs
  secrets              Get a secret or list secrets
  version              Display the version of PrimeHub Python SDK
  volumes              Get a volume or list volumes

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

詳しいことはこちらへ https://github.com/InfuseAI/primehub-python-sdk/tree/main/docs/CLI

子コマンドの使用

Help

# e.g. primehub apps --help
primehub <command> [<command>]--help

admin

Usage:
  primehub admin <command>

Available Commands:
  groups               Manage groups
  images               Manage images
  instancetypes        Manage instance type
  secrets              Manage secrets
  users                Manage users
  volumes              Manage volumes

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

apps

Usage:
  primehub admin groups <command>

Manage groups

Available Commands:
  create               Create a group
  delete               Delete the group by id
  get                  Get the group info by id
  list                 List groups
  update               Update the group

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

apptemplates

Usage:
  primehub apptemplates <command>

Get PhAppTemplates

Available Commands:
  get                  Get a PhApp template
  list                 List PhApp templates

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

config

Usage:
  primehub config <command>

Update the settings of PrimeHub SDK

Available Commands:
  generate-token       Generate Token Flow
  set-endpoint         Set endpoint and save to the config file
  set-group            Set group and save to the config file
  set-token            Set token and save to the config file

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

datasets

Usage:
  primehub datasets <command>

Manage datasets

Available Commands:
  create               Create a datasets
  delete               Delete the dataset
  files-delete         delete files from the dataset
  files-download       download files from the dataset
  files-list           lists files of the dataset
  files-upload         upload files to the dataset
  get                  Get the dataset
  list                 List datasets
  update               Update a dataset

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

deployments

Usage:
  primehub deployments <command>

Get a deployment or list deployments

Available Commands:
  create               Create a deployment
  delete               Delete a deployment by id
  get                  Get a deployment by id
  get-history          Get history of a deployment by id
  list                 List deployments
  logs                 Get deployment logs by id
  start                Start a deployment by id
  stop                 Stop a deployment by id
  update               Update a deployment by id
  wait                 Wait a deployment to complete

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

files

Usage:
  primehub files <command>

List and download shared files

Available Commands:
  delete               delete shared files
  download             Download shared files
  list                 List shared files
  upload               Upload shared files

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

groups

Usage:
  primehub groups <command>

Get a group or list groups

Available Commands:
  get                  Get group by name
  list                 List groups

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

images

Usage:
  primehub images <command>

Get a image or list images

Available Commands:
  get                  Get a image by name
  list                 List images

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

info

Usage:
  primehub info <command>

Display the user information and the selected group information

Available Commands:
  info                 Show PrimeHub Cli information

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

instancetypes

Usage:
  primehub instancetypes <command>

Get an instance types of list instance types

Available Commands:
  get                  Get an instance type by name
  list                 List instance types

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

jobs

Usage:
  primehub jobs <command>

Manage jobs

Available Commands:
  cancel               Cancel a job by id
  download-artifacts   Download artifacts
  get                  Get a job by id
  list                 List jobs
  list-artifacts       List artifacts of a job by id
  logs                 Get job logs by id
  rerun                Rerun a job by id
  submit               Submit a job
  wait                 Wait a job by id

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

me

Usage:
  primehub me <command>

Show user account

Available Commands:
  me                   Get user information

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

models

Usage:
  primehub models <command>

Manage models

Available Commands:
  deploy               Deploy the model version to the speific deployment
  get                  Get the model
  get-version          Get a version of the model
  list                 List models
  list-versions        List versions of the model

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

notebooks

Usage:
  primehub notebooks <command>

Get notebooks logs

Available Commands:
  logs                 Get notebooks logs

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

recurring-jobs

Usage:
  primehub recurring-jobs <command>

Manage recurring jobs

Available Commands:
  create               Create a recurring job
  delete               Delete a recurring job by id
  get                  Get a recurring job by id
  list                 List recurring jobs
  update               Update a recurring job by id

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

secrets

Usage:
  primehub secrets <command>

Get a secret or list secrets

Available Commands:
  get                  Get a secret by id
  list                 List secrets

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

volumes

Usage:
  primehub volumes <command>

Get a volume or list volumes

Available Commands:
  get                  Get a volume by name
  list                 List volumes

Options:
  -h, --help           Show the help

Global Options:
  --config CONFIG      Change the path of the config file (Default: ~/.primehub/config.json)
  --endpoint ENDPOINT  Override the GraphQL API endpoint
  --token TOKEN        Override the API Token
  --group GROUP        Override the current group
  --json               Output the json format (output human-friendly format by default)

参考

GitHub: primehub-python-sdk

Medium: What PrimeHub SDK can do towards MLOps

Medium: Integrating PrimeHub and Apache Airflow

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?