# Tương tác với Thảo luận và Yêu cầu Gộp

Thư viện `huggingface_hub` cung cấp giao diện Python để tương tác với Pull Requests và Discussions trên Hub. Xem [trang tài liệu chuyên biệt](https://huggingface.co/docs/hub/repositories-pull-requests-discussions) để tìm hiểu sâu hơn về Discussions và Pull Requests trên Hub là gì, cũng như cách chúng hoạt động bên trong.

## Lấy Discussions và Pull Requests từ Hub

Lớp `HfApi` cho phép bạn lấy các Discussions và Pull Requests trên một repo nhất định:

```python
>>> 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` hỗ trợ lọc theo tác giả, loại (Pull Request hoặc Discussion) và trạng thái (`open` hoặc `closed`):

```python
>>> 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` trả về một [generator](https://docs.python.org/3.7/howto/functional.html#generators) tạo ra các đối tượng [Discussion](/docs/huggingface_hub/v2.4.0/vi/package_reference/community#huggingface_hub.Discussion). Để lấy tất cả các Discussion trong một danh sách duy nhất, hãy chạy:

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

Đối tượng [Discussion](/docs/huggingface_hub/v2.4.0/vi/package_reference/community#huggingface_hub.Discussion) được trả về bởi [HfApi.get_repo_discussions()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.get_repo_discussions) chứa tổng quan cấp cao về Discussion hoặc Pull Request. Bạn cũng có thể lấy thông tin chi tiết hơn bằng cách sử dụng [HfApi.get_discussion_details()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.get_discussion_details):

```python
>>> 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()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.get_discussion_details) trả về một đối tượng [DiscussionWithDetails](/docs/huggingface_hub/v2.4.0/vi/package_reference/community#huggingface_hub.DiscussionWithDetails), đây là một lớp con của [Discussion](/docs/huggingface_hub/v2.4.0/vi/package_reference/community#huggingface_hub.Discussion) với thông tin chi tiết hơn về Discussion hoặc Pull Request. Thông tin bao gồm tất cả các bình luận, thay đổi trạng thái và đổi tên của Discussion thông qua `DiscussionWithDetails.events`.

Trong trường hợp của một Pull Request, bạn có thể lấy git diff thô bằng `DiscussionWithDetails.diff`. Tất cả các commit của các Pull Request được liệt kê trong `DiscussionWithDetails.events`.

## Tạo và chỉnh sửa Discussion hoặc Pull Request bằng cách lập trình

Lớp [HfApi](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi) cũng cung cấp các cách để tạo và chỉnh sửa Discussions và Pull Requests. Bạn sẽ cần một [access token](https://huggingface.co/docs/hub/security-tokens) để tạo và chỉnh sửa Discussions hoặc Pull Requests.

Cách đơn giản nhất để đề xuất thay đổi trên một kho trên Hub là thông qua API [create_commit()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_commit): chỉ cần đặt tham số `create_pr` thành `True`. Tham số này cũng có sẵn trên các phương thức khác bao bọc [create_commit()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_commit):

    * [upload_file()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.upload_file)
    * [upload_folder()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.upload_folder)
    * [delete_file()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.delete_file)
    * [delete_folder()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.delete_folder)
    * [metadata_update()](/docs/huggingface_hub/v2.4.0/vi/package_reference/cards#huggingface_hub.metadata_update)

```python
>>> from huggingface_hub import metadata_update

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

Bạn cũng có thể sử dụng [HfApi.create_discussion()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_discussion) (tương ứng là [HfApi.create_pull_request()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_pull_request)) để tạo một Discussion (tương ứng là một Pull Request) trên một repo. Việc mở một Pull Request theo cách này có thể hữu ích nếu bạn cần làm việc với các thay đổi cục bộ. Các Pull Request được mở theo cách này sẽ ở chế độ `"draft"`.

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

Việc quản lý Pull Requests và Discussions có thể được thực hiện hoàn toàn bằng lớp [HfApi](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi). Ví dụ:

    * [comment_discussion()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.comment_discussion) để thêm bình luận
    * [edit_discussion_comment()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.edit_discussion_comment) để chỉnh sửa bình luận
    * [rename_discussion()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.rename_discussion) để đổi tên một Discussion hoặc Pull Request
    * [change_discussion_status()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.change_discussion_status) để mở hoặc đóng một Discussion / Pull Request
    * [merge_pull_request()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.merge_pull_request) để hợp nhất một Pull Request
    * [upload_attachment()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.upload_attachment) để tải lên hình ảnh hoặc video và nhúng URL của nó vào mô tả hoặc bình luận

Truy cập trang tài liệu [HfApi](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi) để tham khảo đầy đủ tất cả các phương pháp khả dụng.

## Quản lý Thảo luận và Yêu cầu Gộp từ CLI

Tất cả các thao tác nêu trên cũng có thể được thực hiện từ dòng lệnh thông qua `hf discussions`. Điều này hữu ích cho việc viết script, các pipeline CI, hoặc tương tác nhanh mà không cần viết mã Python.

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

Để xem danh sách đầy đủ các tùy chọn, hãy chạy `hf discussions --help` hoặc xem [tham chiếu CLI](./cli#hf-discussions).

## Đẩy các thay đổi lên một Pull Request

*Sắp ra mắt!*

## Xem thêm

Để tham khảo chi tiết hơn, hãy truy cập [Discussions and Pull Requests](../package_reference/community) và trang tài liệu [hf_api](../package_reference/hf_api).

