Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Programmatic API for the community tab #930

Merged
merged 44 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b1308e5
✨ WIP: programmatic API for the community tab
SBrandeis Jun 24, 2022
bcb460b
Layout the API
SBrandeis Jun 24, 2022
95b6f0b
📝 Autodoc data structures
SBrandeis Jun 24, 2022
6c239d6
📝 Docstring (wip)
SBrandeis Jun 24, 2022
59eab1b
💄 Code quality
SBrandeis Jun 24, 2022
f35a810
📝 Fix doc build & add parent attributes
SBrandeis Jun 24, 2022
4567a6b
✨ Implement the API
SBrandeis Jun 24, 2022
907fac4
🚧 Placeholder guide
SBrandeis Jun 24, 2022
5e1aa1a
✨ git_reference property
SBrandeis Jun 24, 2022
2f772f8
✨ Comment edit history helpers
SBrandeis Jun 24, 2022
da133d5
💄 Code qualityyyy
SBrandeis Jun 24, 2022
0290b2b
♻ refactor Pagination a bit
SBrandeis Jun 27, 2022
a4a7300
🩹 Documentation + fix POST requests URL
SBrandeis Jun 27, 2022
515e8ad
🩹 Fix GET requests URLs
SBrandeis Jun 27, 2022
3d4ca1d
✨ Programmatically create_discussion
SBrandeis Jun 27, 2022
b935d36
💄 Code quality
SBrandeis Jun 27, 2022
1f4dbab
📝 how-to-community guide
SBrandeis Jun 27, 2022
9e7d8a5
Re-exports
SBrandeis Jun 27, 2022
4979ff0
✅ Add tests
SBrandeis Jun 27, 2022
b4111e9
📝 links and typo
SBrandeis Jun 27, 2022
a48a34d
🚚 Rename how-to-community guide
SBrandeis Jun 28, 2022
1ebb9b0
✨ create_pull_request convenience wrapper
SBrandeis Jun 28, 2022
cafd6c5
Mention `create_commit` API in guide & docstrings
SBrandeis Jun 28, 2022
8216baa
Use self._validate_or_retrieve_token
SBrandeis Jun 28, 2022
9580c26
Apply suggestions from code review
SBrandeis Jul 7, 2022
391b7a5
💄 Capitalize "Discussion" and "Pull Request"
SBrandeis Jul 7, 2022
76a214f
Use Literal type hint
SBrandeis Jul 7, 2022
c49f6ec
💄 Fix typo
SBrandeis Jul 7, 2022
e838db3
🔊 Add warning when hiding a comment
SBrandeis Jul 7, 2022
3fd010f
💚 CI fix
SBrandeis Jul 7, 2022
ba92953
🔥 Remove Pagination util
SBrandeis Jul 7, 2022
27f280f
Update docs/source/how-to-discussions-and-pull-requests.mdx
SBrandeis Jul 11, 2022
eb755c9
📝 Documentation improvements
SBrandeis Jul 11, 2022
5d7dc2f
🎨 Move ternary expressions out of the constructor call
SBrandeis Jul 11, 2022
c72072d
💄 Code quality
SBrandeis Jul 11, 2022
0a27cfb
Update docs/source/how-to-discussions-and-pull-requests.mdx
Wauplin Aug 9, 2022
ce0a416
Update docs/source/how-to-discussions-and-pull-requests.mdx
Wauplin Aug 9, 2022
7c0fb16
Update src/huggingface_hub/community.py
Wauplin Aug 9, 2022
2b38154
Update src/huggingface_hub/hf_api.py
Wauplin Aug 9, 2022
2e35491
Update src/huggingface_hub/hf_api.py
Wauplin Aug 9, 2022
bca0c04
Update src/huggingface_hub/hf_api.py
Wauplin Aug 9, 2022
db54a7a
Update src/huggingface_hub/hf_api.py
Wauplin Aug 9, 2022
542832f
Update src/huggingface_hub/community.py
Wauplin Aug 9, 2022
d5fce45
Update src/huggingface_hub/community.py
Wauplin Aug 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
title: Searching the Hub
- local: how-to-inference
title: Access the Inference API
- local: how-to-discussions-and-pull-requests
title: Interact with Discussions and Pull Requests
title: "Guides"
- sections:
- local: package_reference/repository
Expand All @@ -29,4 +31,6 @@
title: Inference API
- local: package_reference/utilities
title: Utilities
- local: package_reference/community
title: Discussions and Pull Requests
title: "Reference"
129 changes: 129 additions & 0 deletions docs/source/how-to-discussions-and-pull-requests.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Interact with Discussions and Pull Requests

The `huggingface_hub` library provides a Python interface to interact with Pull Requests and Discussions on the Hub.
Visit [the dedicated documentation page](https://huggingface.co/docs/hub/repositories-pull-requests-discussions)
for a deeper view of what Discussions and Pull Requests on the Hub are, and how they work under the hood.

## Retrieve Discussions and Pull Requests from the Hub

The `HfApi` class allows you to retrieve Discussions and Pull Requests on a given repo:

```python
>>> from huggingface_hub import get_repo_discussions
>>> for discussion in get_repo_discussions(repo_id="bigscience/bloom-1b3"):
... print(f"{discussion.num} - {discussion.title}, pr: {discussion.is_pull_request}")
Wauplin marked this conversation as resolved.
Show resolved Hide resolved

# 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` returns a [generator](https://docs.python.org/3.7/howto/functional.html#generators) that yields
[`Discussion`] objects. To get all the Discussions in a single list, run:

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

The [`Discussion`] object returned by [`HfApi.get_repo_discussions`] contains high-level overview of the
Discussion or Pull Request. You can also get more detailed information using [`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`] returns a [`DiscussionWithDetails`] object, which is a subclass of [`Discussion`]
with more detailed information about the Discussion or Pull Request. Information includes all the comments, status changes,
and renames of the Discussion via [`DiscussionWithDetails.events`].

In case of a Pull Request, you can retrieve the raw git diff with [`DiscussionWithDetails.diff`]. All the commits of the
Pull Request are listed in [`DiscussionWithDetails.events`].


## Create and edit a Discussion or Pull Request programatically
Wauplin marked this conversation as resolved.
Show resolved Hide resolved

The [`HfApi`] class also offers ways to create and edit Discussions and Pull Requests.
You will need an [access token](https://huggingface.co/docs/hub/security-tokens) to create and edit Discussions
or Pull Requests.

The simplest way to propose changes on a repo on the Hub is via the [`create_commit`] API. [`create_commit`]: just
Wauplin marked this conversation as resolved.
Show resolved Hide resolved
set the `create_pr` parameter to `True`. This parameter is also available on other methods that wrap [`create_commit`]:

* [`upload_file`]
* [`upload_folder`]
* [`delete_file`]
* [`metadata_update`]
Wauplin marked this conversation as resolved.
Show resolved Hide resolved

```python
>>> from huggingface_hub import metadata_update

>>> metadata_update(
... repo_id="username/repo_name",
... metadata={"tags": ["computer-vision", "awesome-model"]},
... create_pr=True,
osanseviero marked this conversation as resolved.
Show resolved Hide resolved
... )
```

You can also use [`HfApi.create_discussion`] (respectively [`HfApi.create_pull_request`]) to create a Discussion (respectively a Pull Request) on a repo.
Opening a Pull Request this way can be useful if you need to work on changes locally. Pull Requests opened this way will be in `"draft"` mode.

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

Managing Pull Requests and Discussions can be done entirely with the [`HfApi`] class. For example:

* [`comment_discussion`] to add comments
* [`edit_discussion_comment`] to edit comments
* [`rename_discussion`] to rename a Discussion or Pull Request
* [`change_discussion_status`] to open or close a Discussion / Pull Request
* [`merge_pull_request`] to merge a Pull Request


Visit the [`HfApi`] documentation page for an exhaustive reference of all available methods.

## Push changes to a Pull Request

*Coming soon !*

## See also

For a more detailed reference, visit the [community](/source/package_reference/community) and the [hf_api](/source/package_reference/hf_api) documentation page.
1 change: 1 addition & 0 deletions docs/source/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Keep reading to learn even more about how to:
- [Search thousands of models and datasets on the Hub with specific filters and
parameters to only return the best results](searching-the-hub).
- [Access the Inference API for accelerated inference](how-to-inference).
- [Interact with Discussions and Pull Requests ](how-to-discussions-and-pull-requests).

## Contribute

Expand Down
30 changes: 30 additions & 0 deletions docs/source/package_reference/community.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Interacting with Discussions and Pull Requests

Check the [`HfApi`] documentation page for the reference of methods enabling
interaction with Pull Requests and Discussions on the Hub.

- [`get_repo_discussions`]
- [`get_discussion_details`]
- [`create_discussion`]
- [`create_pull_request`]
- [`rename_discussion`]
- [`comment_discussion`]
- [`edit_discussion_comment`]
- [`change_discussion_status`]
- [`merge_pull_request`]

## Data structures

[[autodoc]] Discussion

[[autodoc]] DiscussionWithDetails

[[autodoc]] DiscussionEvent

[[autodoc]] DiscussionComment

[[autodoc]] DiscussionStatusChange

[[autodoc]] DiscussionCommit

[[autodoc]] DiscussionTitleChange
18 changes: 18 additions & 0 deletions src/huggingface_hub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,31 @@ def __dir__():
"HfApi",
"HfFolder",
"ModelSearchArguments",
"change_discussion_status",
"comment_discussion",
"create_commit",
"create_discussion",
"create_pull_request",
"create_repo",
"dataset_info",
"delete_file",
"delete_repo",
"edit_discussion_comment",
"get_dataset_tags",
"get_discussion_details",
"get_full_repo_name",
"get_model_tags",
"get_repo_discussions",
"list_datasets",
"list_metrics",
"list_models",
"list_repo_files",
"login",
"logout",
"merge_pull_request",
"model_info",
"move_repo",
"rename_discussion",
"repo_type_and_id_from_hf_id",
"set_access_token",
"space_info",
Expand Down Expand Up @@ -193,5 +202,14 @@ def __dir__():
"metadata_save",
"metadata_update",
],
"community": [
"Discussion",
"DiscussionWithDetails",
"DiscussionEvent",
"DiscussionComment",
"DiscussionStatusChange",
"DiscussionCommit",
"DiscussionTitleChange",
],
},
)
Loading