Hub Python Library documentation

디스커션 및 풀 리퀘스트와 상호 작용하기

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v2.4.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

디스커션 및 풀 리퀘스트와 상호 작용하기

huggingface_hub 라이브러리는 Hub의 Pull Requests와 Discussions를 조작하기 위한 Python 인터페이스를 제공합니다. Hub의 Discussions와 Pull Requests가 무엇이며 내부적으로 어떻게 작동하는지에 대한 자세한 내용은 전용 문서 페이지를 참고하세요.

Hub에서 Discussions와 Pull Requests 가져오기

HfApi 클래스를 사용하면 특정 저장소의 Discussions와 Pull Requests를 가져올 수 있습니다:

>>> from huggingface_hub import get_repo_discussions
>>> for discussion in get_repo_discussions(repo_id="bigscience/bloom"):
...     print(f"{discussion.num} - {discussion.title}, pr: {discussion.is_pull_request}")

# 11 - Add Flax weights, pr: True
# 10 - Update README.md, pr: True
# 9 - Training languages in the model card, pr: True
# 8 - Update tokenizer_config.json, pr: True
# 7 - Slurm training script, pr: False
[...]

HfApi.get_repo_discussions는 작성자, 유형(Pull Request 또는 Discussion), 상태(open 또는 closed)로 필터링하는 것을 지원합니다:

>>> from huggingface_hub import get_repo_discussions
>>> for discussion in get_repo_discussions(
...    repo_id="bigscience/bloom",
...    author="ArthurZ",
...    discussion_type="pull_request",
...    discussion_status="open",
... ):
...     print(f"{discussion.num} - {discussion.title} by {discussion.author}, pr: {discussion.is_pull_request}")

# 19 - Add Flax weights by ArthurZ, pr: True

HfApi.get_repo_discussions는 Discussion 객체를 반환하는 generator를 반환합니다. 모든 Discussions를 단일 리스트로 가져오려면 다음을 실행하세요:

>>> from huggingface_hub import get_repo_discussions
>>> discussions_list = list(get_repo_discussions(repo_id="bert-base-uncased"))

HfApi.get_repo_discussions()이 반환하는 Discussion 객체는 Discussion 또는 Pull Request의 고위 수준 개요를 포함합니다. HfApi.get_discussion_details()를 사용하면 더 자세한 정보를 가져올 수도 있습니다:

>>> from huggingface_hub import get_discussion_details

>>> get_discussion_details(
...     repo_id="bigscience/bloom-1b3",
...     discussion_num=2
... )
DiscussionWithDetails(
    num=2,
    author='cakiki',
    title='Update VRAM memory for the V100s',
    status='open',
    is_pull_request=True,
    events=[
        DiscussionComment(type='comment', author='cakiki', ...),
        DiscussionCommit(type='commit', author='cakiki', summary='Update VRAM memory for the V100s', oid='1256f9d9a33fa8887e1c1bf0e09b4713da96773a', ...),
    ],
    conflicting_files=[],
    target_branch='refs/heads/main',
    merge_commit_oid=None,
    diff='diff --git a/README.md b/README.md\nindex a6ae3b9294edf8d0eda0d67c7780a10241242a7e..3a1814f212bc3f0d3cc8f74bdbd316de4ae7b9e3 100644\n--- a/README.md\n+++ b/README.md\n@@ -132,7 +132,7 [...]',
)

HfApi.get_discussion_details()은 DiscussionWithDetails 객체를 반환합니다. 이 객체는 Discussion의 하위 클래스로, Discussion 또는 Pull Request에 대한 보다 상세한 정보를 제공합니다. DiscussionWithDetails.events를 통해 Discussion의 모든 댓글, 상태 변경 및 이름 변경 정보를 포함합니다.

Pull Request인 경우, DiscussionWithDetails.diff를 통해 원시 git diff를 가져올 수 있습니다. Pull Request의 모든 커밋은 DiscussionWithDetails.events에 나열됩니다.

Discussion 또는 Pull Request를 프로그래밍 방식으로 생성 및 편집

HfApi 클래스는 Discussions와 Pull Requests를 생성하고 편집하는 방법도 제공합니다. Discussions 또는 Pull Requests를 생성하고 편집하려면 access token이 필요합니다.

Hub의 저장소에서 변경 사항을 제안하는 가장 간단한 방법은 create_commit() API를 사용하는 것입니다. create_pr 매개변수를 True로 설정하면 됩니다. 이 매개변수는 create_commit()을 래핑하는 다른 메서드에서도 사용할 수 있습니다:

>>> from huggingface_hub import metadata_update

>>> metadata_update(
...     repo_id="username/repo_name",
...     metadata={"tags": ["computer-vision", "awesome-model"]},
...     create_pr=True,
... )

HfApi.create_discussion()(각각 HfApi.create_pull_request())를 사용하여 리포지토리에 Discussion(각각 Pull Request)을 생성할 수도 있습니다. 로컬에서 변경 사항을 작업해야 하는 경우 이 방식으로 Pull Request를 여는 것이 유용할 수 있습니다. 이 방식으로 여는 Pull Request는 "draft" 모드로 생성됩니다.

>>> from huggingface_hub import create_discussion, create_pull_request

>>> create_discussion(
...     repo_id="username/repo-name",
...     title="Hi from the huggingface_hub library!",
...     token="<insert your access token here>",
... )
DiscussionWithDetails(...)

>>> create_pull_request(
...     repo_id="username/repo-name",
...     title="Hi from the huggingface_hub library!",
...     token="<insert your access token here>",
... )
DiscussionWithDetails(..., is_pull_request=True)

Pull Request 및 Discussion 관리는 HfApi 클래스만으로 완전히 수행할 수 있습니다. 예를 들어:

사용 가능한 모든 메서드에 대한 포괄적인 참고 문서는 HfApi 문서 페이지를 참조하세요.

CLI에서 토론 및 Pull Request 관리

위에서 설명한 모든 작업은 hf discussions를 통해 명령줄에서도 사용할 수 있습니다. 이는 Python 코드를 작성하지 않고 스크립팅, CI 파이프라인 또는 빠른 상호작용을 수행할 때 유용합니다.

# List open discussions and PRs on a repo
hf discussions list bigscience/bloom

# List discussions on a dataset repo
hf discussions list nebius/SWE-rebench-V2 --type dataset

# Get info for a specific discussion, including its comment thread
hf discussions info bigscience/bloom 2

# Create a new discussion
hf discussions create username/repo-name --title "Bug report" --body "Description here"

# Create a pull request
hf discussions create username/repo-name --title "Fix typo" --pull-request

# Comment on a discussion or PR
hf discussions comment username/repo-name 5 --body "LGTM!"

# Attach a screenshot to a comment
hf discussions comment username/repo-name 5 --body "Here is the fix:" --attach screenshot.png

# Merge a pull request
hf discussions merge username/repo-name 5 --yes

# Show the diff of a pull request
hf discussions diff username/repo-name 5

전체 옵션 목록을 확인하려면 hf discussions --help를 실행하거나 CLI 참조를 참고하세요.

Pull Request에 변경 사항을 push

곧 출시 예정!

참고

보다 자세한 참고를 위해 토론 및 풀 리퀘스트와 hf_api 문서 페이지를 방문하세요.

Update on GitHub