nokoxxx1212 / keiba-hacke

Horse Racing Forecast App
Apache License 2.0
0 stars 0 forks source link

パイプライン(VAP)環境準備 #6

Closed nokoxxx1212 closed 4 months ago

nokoxxx1212 commented 5 months ago

🧑 Story

🔨 Acceptance Criteria

📝 Task List

📚 Resources

🎯 Deliverables

🔜 Remaining Tasks

nokoxxx1212 commented 5 months ago

ディレクトリ構成検討

https://tech.timee.co.jp/entry/2023/07/21/114309

# Vertex AI Pipelines開発用のテンプレート
.
├── .github
│   ├── PULL_REQUEST_TEMPLATE.md
│   └── workflows
│       ├── CI/CDのyamlファイル
├── .gitignore
├── Makefile
├── README.md
├── pyproject.toml
├── src
│   └── pipeline_template
│       ├── components
│       │   └── component
│       │       ├── パイプラインを構成するコンポーネントのソースコードとDockerfile
│       ├── pipelines
│       │   ├── パイプラインをコンパイル、実行するためのソースコード
│       ├── pyproject.toml
└── tests
    ├── テストコード

https://tech.layerx.co.jp/entry/2023/11/16/185944

├── poetry.lock
├── pyproject.toml
├── tasks.py
├── {{ project-name-a }}
│   ├── invoke.yaml
│   ├── invoke_prd.yaml
│   ├── tasks.py
│   ├── yamls
│   │   ├── preprocess.yaml
│   │   ├── train.yaml
│   │   ├── pipelines.yaml
│       └── ...
│   ├── pipelines
│   │   ├── train_pipeline.py
│   │   └── ...
│   ├── components
│       ├── preprocess
│       ├── train
│       └── ...
├── {{ project-name-b }}
├── libs
    └── layerx-ml-utils
        ├── pyproject.toml
        └── layerx_ml_utils
          ├── vertex_pipeline.py
          ├── bq.py
          ├── invoke.py
          └── ...
├── {{ project-name-a }}
│   ├── pipelines
│   │   ├── sample_pipeline.py
│   │   ├── train_pipeline.py
│   │   └── ...
│   ├── components
│   |   ├── sample/
|   |   |  |── Dockerfile
|   |   |  |── pyproject.toml
|   |   |  └── sample.py
│   |   ├── preprocess/
│   |   ├── train/
│   |   └── ...
|   ├── tests
├── {{ project-name-b }}
|   ├── data
|   │   ├── external
|   │   ├── raw
|   │   ├── interim
|   │   └── processed

他 libs notebooks reports とか

->

.
├── .github
│   ├── PULL_REQUEST_TEMPLATE.md
│   └── workflows
│       ├── CI/CDのyamlファイル
├── .gitignore
├── README.md
├── pyproject.toml
├── src
│   └── pipeline_template
│       ├── components
│       │   └── component
│       │       ├── パイプラインを構成するコンポーネントのソースコードとDockerfile
│       ├── pipelines
│       │   ├── パイプラインをコンパイル、実行するためのソースコード
└── tests
    ├── テストコード
mkdir src/sample
mkdir src/sample/components
mkdir src/sample/components/sample
touch src/sample/components/sample/Dockerfile
touch src/sample/components/sample/sample.py
mkdir src/sample/pipelines
touch src/sample/pipelines/sample_pipeline.py
nokoxxx1212 commented 5 months ago

VAPテンプレート作成

①Dockerfileからコンテナ起動・ログイン

docker build -t khsampledocker .
docker run -itd -v $(pwd):/opt/mnt khsampledocker
docker exec -it 17ce /bin/bash

②pythonコード作り、動作確認

①Dockerfile作りこみ

-> > [4/6] RUN poetry config virtualenvs.create false && poetry install --no-root --extras "test lint sample":

8 0.680 Extra [test] is not specified.

③Component.py作り

if __name__ == '__main__':
    results = is_prime()
    print(type(results))
clickライブラリを使用してコマンドラインインターフェースを作成すると、通常、コマンド関数(この場合はis_prime)の戻り値は無視されます。その代わり、結果は通常、click.echo()やprint()などの関数を使用して直接出力されます。

したがって、results = is_prime()というコードは、is_prime関数の戻り値をresultsに代入しようとしますが、is_prime関数はclickによってコマンドとして実行され、その結果は出力されますが戻り値としては返されません。そのため、resultsはNoneになります。

④pipeline.py作り、動作確認

from google.cloud import aiplatform
from kfp import dsl
from kfp.v2 import compiler
from kfp.v2.dsl import ContainerSpec

# コンポーネントを定義
@dsl.container_component
def sample_component() -> ContainerSpec:
    return ContainerSpec(
        image='python:3.7',
        command=['python', 'sample.py'],
    )

# パイプラインを定義
@dsl.pipeline(
    name='sample-pipeline',
    description='A pipeline that runs the sample.py script.'
)
def sample_pipeline():
    # コンポーネントのインスタンスを作成
    sample_task = sample_component()

# パイプラインをコンパイル
compiler.Compiler().compile(
    pipeline_func=sample_pipeline,
    package_path='sample_pipeline_job.json'
)

# パイプラインを実行
def run_pipeline(project: str, location: str, pipeline_root: str):
    # Vertex AI clientを初期化
    aiplatform.init(project=project, location=location)

    # パイプラインを実行
    job = aiplatform.PipelineJob(
        display_name='sample-pipeline',
        template_path='sample_pipeline_job.json',
        pipeline_root=pipeline_root,
    )
    job.run()

# パイプラインを実行するためのパラメータ
PROJECT_ID = 'keiba-hacke'
LOCATION = 'us-central1'
PIPELINE_ROOT = 'gs://dev-kh-gcs-bucket/sample/pipeline_root'

if __name__ == '__main__':
    run_pipeline(PROJECT_ID, LOCATION, PIPELINE_ROOT)
PROJECT_ID = 'XXX'
LOCATION = 'us-central1'
PIPELINE_ROOT = 'gs://XXX/sample/pipeline_root'

COPY . /opt/mnt に変更 poetry add python-dotenv --group sample addした後にinstallすることが必要

poetry install --no-root --with test,lint,sampleがエラーになりました dotenvをインストールしたいです • Installing dotenv (0.0.5): Failed ChefBuildError Backend 'setuptools.build_meta:legacy' is not available. Note: This error originates from the build backend, and is likely not a problem with poetry but with dotenv (0.0.5) not supporting PEP 517 builds. You can verify this by running 'pip wheel --no-cache-dir --use-pep517 "dotenv (==0.0.5)"'.

GitHub Copilot: Google Container Registry(GCR)にDockerイメージをpushするには、以下の手順を実行します。

  1. Dockerイメージをビルドします。この際、イメージ名にはGCRのホスト名とプロジェクトIDを含めます。
docker build -t gcr.io/your-project-id/your-image-name .
->
docker build -t gcr.io/your-project-id/khsampledocker .
  1. gcloudコマンドラインツールを使用して、GCRに認証します。
gcloud auth configure-docker
  1. DockerイメージをGCRにpushします。
docker push gcr.io/your-project-id/your-image-name

これらのコマンドを実行する前に、Google Cloud SDKがインストールされていること、gcloudが正しく設定されていること(gcloud initを実行)、そして適切な権限を持つGCPプロジェクトが存在することを確認してください。

"The replica workerpool0-0 exited with a non-zero status of 1. Termination reason: Error. To find out more about why your job exited please check the logs: https://console.cloud.google.com/logs/viewer?project=216392008494&resource=ml_job%2Fjob_id%2F2858859356114386944&advancedFilter=resource.type%3D%22ml_job%22%0Aresource.labels.job_id%3D%222858859356114386944%22"