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


## AI Solution Provider向け

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

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

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

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

+ AutoMLの実現可能

## Data Scientist team向け

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

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

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


## SDK Scenario

![end2end-automl.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1623438/c5b6fa22-1950-d756-376d-3e3f6fbbb647.png)


![mlops-retrain-redeploy.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1623438/2edfc94d-22ab-4571-9381-2299ae325f79.png)

![mlops-regular-retrain-redeploy.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1623438/bb40b056-3b42-670f-b5ee-caa6fce00490.png)


![mlops-monitor.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1623438/47ce93b8-cdfb-62ff-739a-9ece19846548.png)



## 必要なもの

+ [MLflowを伴う運営しているPrimeHub EE インスタンス](https://qiita.com/GabxInfuseAI/items/29132e25eddc622194b4) with a running 
+ Admin-level user account
+ Python Package - [primehub-python-sdk](https://github.com/InfuseAI/primehub-python-sdk)


### InstallとConfigure

Install PrimeHub Python SDK(CLI付き)

```bash
pip install primehub-python-sdk
```

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

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

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

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

検証する

```bash
primehub me

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

又は

```bash
cat ~/.primehub/config.json

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


---

## Python SDKの使用

PrimeHub SDK instanceの初期化する

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

ファンクションの実例

```python
ph.<command>.command() 

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

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


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

https://github.com/InfuseAI/primehub-python-sdk/tree/main/docs/notebook


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

```python
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を提出して、生成されたモデルを登録する

```python
# 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)
```

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

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

モデルをデプロイする

```python
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へ予測を提出する

```python
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実例

+ [PrimeHub SDK CT pipeline](https://colab.research.google.com/github/InfuseAI/showcase/blob/main/primehub-sdk-ct-pipeline/notebook/PrimeHub_SDK_Iris_Model.ipynb)

+ [Credit card recommendation](https://colab.research.google.com/github/InfuseAI/showcase/blob/main/credit-card-recommendation/notebook/primehub_sdk_credit_card_recommendation.ipynb)

---

## CLIの使用

主要なコマンド

```bash
primehub                                                         
```

子コマンドの一覧表

```bash
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

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

### admin

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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

```bash
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](https://github.com/InfuseAI/primehub-python-sdk)

Medium: [What PrimeHub SDK can do towards MLOps](https://blog.infuseai.io/what-our-primehub-sdk-can-do-towards-mlops-b6592aec96db)

Medium: [Integrating PrimeHub and Apache Airflow](https://blog.infuseai.io/integrating-primehub-and-apache-airflow-9cfa9fbfde21)