Skip to content

Better support for collection retrieval #3856

@gsaltintas

Description

@gsaltintas

Hello,

Thank you for all the updates to the api, it has been very useful. When creating models or datasets I often pass a relevant collection too. Currently all functions associated with the huggingface_hub.HfApi() expect collection_slug, which is a unique id exposed by the api and not accessible in the web interface. Unfortunately, there is no good way of retrieving the slug.

I think the cleanest would be to make the collection title unique and pass that to these functions similar to how we retrieve models or datasets, i.e. namespace/collection-name or at least if api. get_collection could support this format one can first check if the collection exists to retrieve its slug.

This is the current workaround I am currently using:

import huggingface_hub as hf_hub

def create_or_get_collection_slug(hf_collection: str) -> str:
    """Return the slug for a collection identified by 'namespace/title', creating it if needed.    """
    api = hf_hub.HfApi()
    namespace, title = hf_collection.split("/", 1) if "/" in hf_collection else (None, hf_collection)

    existing = next(
        (col for col in api.list_collections(owner=namespace) if col.title == hf_collection),
        None,
    )
    if existing:
        return existing.slug

    collection = api.create_collection(title=title, namespace=namespace, exists_ok=True)
    return collection.slug

Is your feature request related to a problem? Please describe.
This is related to the general improvements listed under #3702

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions