在 GitHub 上编辑
dvc.api.artifacts_show()
获取在 模型注册表 中跟踪的某个 制品(artifact) 的路径和 Git 版本。
def artifacts_show(
name: str,
version: Optional[str] = None,
stage: Optional[str] = None,
repo: Optional[str] = None,
) -> Dict[str, str]:
如果你已将 DVC Studio 项目配置了远程存储凭据,也可以使用 DVC Studio REST API 以编程方式访问制品。这种方式不需要客户端拥有除 DVC Studio 客户端访问令牌 之外的任何凭据,也无需安装 DVC。
用法:
import dvc.api
artifact = dvc.api.artifacts_show(
'text-classification',
repo='https://github.com/iterative/example-get-started.git',
)
描述
返回一个命名制品的路径和 Git 版本,可用于其他 Python API 调用。
返回的字典格式如下:
{
'path': 'model.pkl',
'rev': 'c7c6ae0',
}
其中 path
包含该制品在 DVC 仓库中的相对路径,rev
包含指定制品版本或阶段的 Git 版本。
当未提供 version
或 stage
时,将返回该模型最新版本的 Git 版本。
参数
-
name
(必需) - 制品名称。默认情况下,DVC 将搜索位于 DVC 仓库根目录下的dvc.yaml
文件中声明的制品。在其他dvc.yaml
文件中声明的制品应使用如下格式引用:path/to/dvc.yaml:artifact_name
或path/to:artifact_name
(其中省略了dvc.yaml
)。 -
version
- 制品的版本(与stage
互斥)。 -
stage
- 制品的阶段(与version
互斥)。 -
repo
- DVC 项目的地址。可以是 URL 或文件系统路径。在线 Git 仓库支持 HTTP 和 SSH 协议(例如[user@]server:project.git
)。默认值:当前项目(从当前工作目录向上查找确定)。
示例:读取制品内容
import pickle
import dvc.api
artifact = dvc.api.artifacts_show(
'text-classification',
version='v1.0.0',
repo='https://github.com/iterative/example-get-started.git',
)
data = dvc.api.read(
artifact['path'],
rev=artifact['rev'],
repo='https://github.com/iterative/example-get-started.git',
mode='rb',
)
model = pickle.loads(data)
此示例结合使用返回的路径和 Git 版本与 dvc.api.read()
来读取该制品的文件内容。
示例:下载制品
import os
import dvc.api
artifact = dvc.api.artifacts_show(
'text-classification',
stage='prod',
repo='https://github.com/iterative/example-get-started.git',
)
fs = dvc.api.DVCFileSystem(
'https://github.com/iterative/example-get-started.git',
rev=artifact['rev'],
)
fs.get_file(artifact['path'], os.path.basename(artifact['path']))
此示例结合使用返回的路径和 Git 版本与 dvc.api.DVCFileSystem
将制品下载到当前工作目录。