> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Artifacts

> 데이터 버전 관리를 위해 W&B Artifacts를 생성, 업데이트, 다운로드하고 관리하세요.

데이터 버전 관리를 위한 W\&B Artifacts를 생성, 업데이트, 다운로드하고 관리하세요.

<h2 id="create-and-log-an-artifact">
  아티팩트 생성 및 로깅
</h2>

```python theme={"system"}
"""
Creates and logs a W&B artifact. First create an
artifact object. Next, add files, directories, or external references to the artifact.
Finally, log the artifact to a W&B run.
"""
import wandb

# 아티팩트 객체를 생성합니다
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")

# 아티팩트에 하나 이상의 파일을 추가합니다
artifact.add_file(local_path="data/file1.csv")

# 아티팩트에 디렉터리를 재귀적으로 추가합니다
artifact.add_dir(local_path="data/directory")

# 아티팩트에 외부 URI 레퍼런스를 추가합니다
artifact.add_reference(uri="s3://my-bucket/path/to/data.csv")

with wandb.init(project="<project>") as run:
    # 여기에 트레이닝 및 로깅 코드를 작성합니다

    # 아티팩트 로깅 예시
    run.log_artifact(artifact)
```

<h2 id="track-an-external-artifact-by-adding-a-reference-to-a-cloud-storage-bucket-path">
  클라우드 저장소 버킷 경로에 대한 레퍼런스를 추가하여 외부 아티팩트 추적
</h2>

```python theme={"system"}
"""
Tracks an external artifact by adding a reference to a cloud storage bucket path.
"""

import wandb

# 아티팩트 객체를 생성합니다
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")

# 버킷 경로에 레퍼런스를 추가합니다
artifact.add_reference(uri = "uri/to/your/bucket/path")

# W&B run을 초기화합니다
with wandb.init(project="<project>") as run:

  # 아티팩트의 메타데이터를 로깅합니다
  run.log_artifact(artifact)
```

<h2 id="delete-specific-artifact-version-from-a-run">
  run에서 특정 아티팩트 버전 삭제
</h2>

```python theme={"system"}
"""
Delete specific artifact version from a W&B run. Set delete_aliaes to `True` 
if the artifact has an alias attached to it.
"""
import wandb

# W&B API 초기화
api = wandb.Api()

# 경로로 run을 조회합니다. 경로는 <entity>/<project>/<run_path> 형식입니다.
runs = api.run("<entity>/<project>/<run_path>")

# wandb.Api().Run.logged_artifacts()는 아티팩트 버전 목록을 반환합니다.
# 각 항목은 아티팩트 이름과 버전으로 구성된 <artifact_name>:v<version_number> 형식입니다.
for artifact_version in runs.logged_artifacts():
    # 아티팩트 버전 이름(str)에서 버전 번호에 해당하는
    # 마지막 두 문자를 인덱싱합니다.
    if artifact_version.name[-2:] == "v"+ "<version_number>":
        artifact_version.delete(delete_aliases=True)
```

<h2 id="delete-an-artifact-collection">
  아티팩트 컬렉션 삭제
</h2>

```python theme={"system"}
"""
Delete an artifact collection from W&B.
"""
import wandb

# W&B API 초기화
api = wandb.Api()

# 이름과 유형으로 아티팩트 컬렉션 삭제
# 이름 형식: <entity>/<project>/<run_path>
collection = api.artifact_collection(
    name="<entity>/<project>/<run_path>",
    type_name="<artifact_type>"
)

collection.delete()
```

<h2 id="download-specific-files-or-sub-folders-from-artifacts">
  아티팩트에서 특정 파일 또는 하위 폴더 다운로드
</h2>

```python theme={"system"}
"""
Downloads specific files or sub-folders from W&B artifacts. The same
logic applies to external artifacts.
"""
import wandb

with wandb.init(project="<project>") as run:
    # 사용할 아티팩트를 지정합니다. 형식은 "name:alias"입니다
    artifact = run.use_artifact("<artifact_name>:<alias>")

    # 특정 파일 또는 하위 폴더를 다운로드합니다
    artifact.download(path_prefix="<file_name>") # 지정한 파일 또는 폴더만 다운로드합니다
```

<h2 id="download-files-or-entire-artifacts">
  파일 또는 전체 아티팩트 다운로드
</h2>

```python theme={"system"}
"""
Downloads files or entire artifacts from W&B. The same
logic applies to external artifacts.
"""

import wandb    

with wandb.init(project="<project>") as run:
    # 사용할 아티팩트를 지정합니다. Format is "name:alias"
    artifact = run.use_artifact("<artifact_name>:<alias>")

    # 아티팩트에서 경로 이름의 파일을 다운로드합니다.
    # artifact.add_reference()가 사용된 경우 reference URL을 반환합니다.
    entry = artifact.get_entry("<file_name>")

    # 전체 아티팩트를 다운로드합니다.
    datadir = artifact.download()
```

<h2 id="add-one-or-more-aliases-to-an-artifact-when-logging-it">
  아티팩트를 로깅할 때 하나 이상의 별칭 추가하기
</h2>

```python theme={"system"}
"""
Add one or more aliases to an artifact when logging it to W&B.
"""
import wandb

# 아티팩트 생성
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")
# 아티팩트에 파일 추가
artifact.add_file("<file_path>")

with wandb.init(project="<project>") as run:
    # 별칭을 지정하여 아티팩트 로깅
    run.log_artifact(artifact, aliases=["<alias1>", "<alias2>"])
```

<h2 id="add-an-alias-to-an-existing-artifact">
  기존 아티팩트에 별칭 추가
</h2>

```python theme={"system"}
"""
Adds an alias to an existing W&B artifact.
"""
import wandb

# 기존 아티팩트를 가져와 별칭을 추가합니다
artifact = wandb.Api().artifact("entity/project/artifact:version")
artifact.aliases = ["<new-alias>"]
artifact.save()
```

<h2 id="add-a-tag-to-an-artifact-when-logging-it">
  로깅할 때 아티팩트에 태그 추가
</h2>

```python theme={"system"}
"""
Add a tag to an artifact when logging it to W&B.
"""
import wandb

# 아티팩트를 생성합니다
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")

# 태그와 함께 아티팩트를 로깅합니다
with wandb.init(project="<project>") as run:
    run.log_artifact(artifact, tags=["<tag1>", "<tag2>"])
```

<h2 id="add-a-tag-to-an-existing-artifact">
  기존 아티팩트에 태그 추가
</h2>

```python theme={"system"}
"""
Adds a tag to an existing W&B artifact.
"""
import wandb

# 기존 아티팩트를 가져와 태그를 추가합니다
artifact = wandb.Api().artifact("entity/project/artifact:version")
artifact.tags = ["new-tag"]
artifact.save()
```

<h2 id="create-and-log-an-artifact-with-a-ttl-policy">
  TTL 정책으로 아티팩트 생성 및 로깅하기
</h2>

```python theme={"system"}
"""
Create and log an artifact with a TTL policy in W&B.
"""
import wandb
from datetime import timedelta

# TTL 정책이 있는 아티팩트를 생성합니다
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")
artifact.add_file("<file_path>")
artifact.ttl = timedelta(days=30)  # TTL 정책을 설정합니다

with wandb.init(project="<project>", entity="<entity>") as run:
    # TTL이 있는 아티팩트를 로깅합니다
    run.log_artifact(artifact)
```

<h2 id="update-the-ttl-policy-of-an-existing-artifact">
  기존 아티팩트의 TTL 정책 업데이트
</h2>

```python theme={"system"}
"""
Update the TTL policy of an existing artifact in W&B.
"""
import wandb
from datetime import timedelta

# W&B API 초기화
api = wandb.Api()

# 기존 아티팩트 가져오기
artifact = api.artifact("<entity/project/artifact:alias>")

# TTL 정책 추가 또는 업데이트. 원하는 보존 기간을 지정하세요.
artifact.ttl = timedelta(days=365)  # 1년 후 삭제

# 업데이트된 아티팩트 저장
artifact.save()
```

<h2 id="update-an-existing-artifacts-description-within-a-run">
  run 내 기존 아티팩트의 설명 업데이트
</h2>

```python theme={"system"}
"""
Update an existing W&B artifact's description within a W&B run.

This code initializes a W&B run, retrieves the specified artifact by name and alias,
updates its description, and saves the changes.
"""
import wandb

with wandb.init(entity="<entity>", project="<project>") as run:
    # name과 별칭으로 아티팩트를 검색합니다
    artifact = run.use_artifact(artifact_or_name="<artifact>:<alias>")
    # 아티팩트의 설명을 업데이트합니다
    artifact.description = "<description>"
    # 업데이트된 아티팩트를 저장합니다
    artifact.save()
```

<h2 id="given-an-existing-artifact-update-its-description-metadata-and-aliases-without-creating-a-new-run">
  기존 아티팩트가 주어지면 새로운 run을 생성하지 않고 설명, 메타데이터, 별칭을 업데이트하세요
</h2>

```python theme={"system"}
"""
Given an existing artifact, update its description, metadata, and aliases
without creating a new run.
"""
import wandb

api = wandb.Api()

artifact = api.artifact(name="<entity/project/artifact:alias>")

# 설명 업데이트
artifact.description = "My new description"

# 메타데이터 키 선택적 업데이트
artifact.metadata["oldKey"] = "new value"

# 메타데이터 전체 교체
artifact.metadata = {"newKey": "new value"}

# 별칭 추가
artifact.aliases.append("best")

# 별칭 제거
artifact.aliases.remove("latest")

# 별칭 완전 교체
artifact.aliases = ["replaced"]

# 모든 아티팩트 수정 사항 저장
artifact.save()
```
