o
    0i+                     @   s  d Z ddlZddlZddlmZmZmZ ddlZddlm	Z	m
Z
 ddlmZmZmZmZmZmZmZmZmZ ee	Zejddd	 eD ed
Zee
Zejddd	 eD ed
ZeddZejdg ddddddejddfdeee ejddf deee ejddf deee ejddf dededededdfdd Zejd!d"gd	d`d#eeej d$df deddfd%d&Z!ejd'g d(d					dad)eeej d*df d+eee ejd,df d-eee ejd.df d/ee"ejd0df d1ee"ejd2df deddfd3d4Z#ejd5g d6d						dbd#eeej d$df d)eee ejd7df d-eee ejd8df d9eee$ ejd:df d/eee" ejd;df d<eee ejd=df deddfd>d?Z%ejd@dAdBgd		dcd#eeej d$df dCee"ejdDdf deddfdEdFZ&ejdGg dHd			ddd#eeej d$df dIeeej dJdf dKeeej dLdf dMeee ejdNdf d1ee"ejdOdf deddfdPdQZ'ejdRdSdTgd			ded#eeej d$df dUeeej dVdf dMeee ejdWdf d9eee$ ejdXdf deddfdYdZZ(ed[		dcd#eeej d$df dUeeej d\df dCee"ejd]df deddf
d^d_Z)dS )fa_  Contains commands to interact with collections on the Hugging Face Hub.

Usage:
    # list collections on the Hub
    hf collections ls

    # list collections for a specific user
    hf collections ls --owner username

    # get info about a collection
    hf collections info username/collection-slug

    # create a new collection
    hf collections create "My Collection" --description "A collection of models"

    # add an item to a collection
    hf collections add-item username/collection-slug username/model-name model

    # delete a collection
    hf collections delete username/collection-slug
    N)	AnnotatedOptionalget_args)CollectionItemType_TCollectionSort_T   )		FormatOptLimitOptOutputFormatQuietOptTokenOptapi_object_to_dict
get_hf_apiprint_list_outputtyper_factoryCollectionItemTypec                 C      i | ]}||qS  r   ).0tr   r   Y/home/kim/smarthome/.venv/lib/python3.10/site-packages/huggingface_hub/cli/collections.py
<dictcomp>;       r   )typeCollectionSortc                 C   r   r   r   )r   sr   r   r   r   >   r   z%Interact with collections on the Hub.)helpz	list | ls)zhf collections lsz hf collections ls --owner nvidiazLhf collections ls --item models/teknium/OpenHermes-2.5-Mistral-7B --limit 10)Zexamples
   Fownerz)Filter by owner username or organization.itemzkFilter collections containing a specific item (e.g., "models/gpt2", "datasets/squad", "papers/2311.12983").sortz4Sort results by last modified, trending, or upvotes.limitformatquiettokenreturnc           
      C   sF   t |d}|r
|jnd}dd |j| |||dD }	t|	||d dS )zList collections on the Hub.r$   Nc                 S   s   g | ]}t |qS r   )r   )r   
collectionr   r   r   
<listcomp>c   s    z"collections_ls.<locals>.<listcomp>)r   r   r    r!   )r"   r#   )r   valueZlist_collectionsr   )
r   r   r    r!   r"   r#   r$   apisort_keyresultsr   r   r   collections_lsD   s   
	r-   infoz/hf collections info username/my-collection-slugcollection_slugz7The collection slug (e.g., 'username/collection-slug').c                 C   s.   t |d}|| }ttjt|dd dS )zAGet info about a collection on the Hub. Output is in JSON format.r&      indentN)r   Zget_collectionprintjsondumpsr   )r/   r$   r*   r'   r   r   r   collections_infoo   s   

r6   create)z!hf collections create "My Models"z^hf collections create "My Models" --description "A collection of my favorite models" --privatez9hf collections create "Org Collection" --namespace my-orgtitlezThe title of the collection.	namespacezMThe namespace (username or organization). Defaults to the authenticated user.descriptionz!A description for the collection.privatezCreate a private collection.	exists_okz7Do not raise an error if the collection already exists.c                 C   sH   t |d}|j| ||||d}td|j  ttjt|dd dS )z#Create a new collection on the Hub.r&   )r8   r9   r:   r;   r<   zCollection created: r0   r1   N)r   Zcreate_collectionr3   urlr4   r5   r   )r8   r9   r:   r;   r<   r$   r*   r'   r   r   r   collections_create   s   
r>   update)z@hf collections update username/my-collection --title "New Title"zPhf collections update username/my-collection --description "Updated description"zDhf collections update username/my-collection --private --theme greenz!The new title for the collection.z'The new description for the collection.positionz7The new position of the collection in the owner's list.z)Whether the collection should be private.themez;The theme color for the collection (e.g., 'green', 'blue').c           	      C   sJ   t |d}|j| |||||d}td|j  ttjt|dd dS )z*Update a collection's metadata on the Hub.r&   )r/   r8   r:   r@   r;   rA   zCollection updated: r0   r1   N)r   Zupdate_collection_metadatar3   r=   r4   r5   r   )	r/   r8   r:   r@   r;   rA   r$   r*   r'   r   r   r   collections_update   s   
!rB   deletez,hf collections delete username/my-collectionz9hf collections delete username/my-collection --missing-ok
missing_okz6Do not raise an error if the collection doesn't exist.c                 C   s*   t |d}|j| |d td|   dS )z!Delete a collection from the Hub.r&   )rD   zCollection deleted: N)r   Zdelete_collectionr3   )r/   rD   r$   r*   r   r   r   collections_delete   s   
rE   zadd-item)zGhf collections add-item username/my-collection moonshotai/kimi-k2 modelz`hf collections add-item username/my-collection Qwen/DeepPlanning dataset --note "Useful dataset"zGhf collections add-item username/my-collection Tongyi-MAI/Z-Image spaceitem_idzCThe ID of the item to add (repo_id for repos, paper ID for papers).	item_typez?The type of item (model, dataset, space, paper, or collection).notez2A note to attach to the item (max 500 characters).z?Do not raise an error if the item is already in the collection.c                 C   sH   t |d}|j| ||j||d}td|   ttjt|dd dS )zAdd an item to a collection.r&   )r/   rF   rG   rH   r<   zItem added to collection: r0   r1   N)r   Zadd_collection_itemr)   r3   r4   r5   r   )r/   rF   rG   rH   r<   r$   r*   r'   r   r   r   collections_add_item   s   
rI   zupdate-itemzVhf collections update-item username/my-collection ITEM_OBJECT_ID --note "Updated note"zMhf collections update-item username/my-collection ITEM_OBJECT_ID --position 0item_object_idzTThe ID of the item in the collection (from 'item_object_id' field, not the repo_id).z-A new note for the item (max 500 characters).z/The new position of the item in the collection.c                 C   s.   t |d}|j| |||d td|   dS )zUpdate an item in a collection.r&   )r/   rJ   rH   r@   zItem updated in collection: N)r   Zupdate_collection_itemr3   )r/   rJ   rH   r@   r$   r*   r   r   r   collections_update_item  s   
rK   zdelete-itemznThe ID of the item in the collection (retrieved from `item_object_id` field returned by 'hf collections info'.z0Do not raise an error if the item doesn't exist.c                 C   s,   t |d}|j| ||d td|   dS )z!Delete an item from a collection.r&   )r/   rJ   rD   zItem deleted from collection: N)r   Zdelete_collection_itemr3   )r/   rJ   rD   r$   r*   r   r   r   collections_delete_item5  s   
rL   )N)NNFFN)NNNNNN)FN)NFN)NNN)*__doc__enumr4   typingr   r   r   ZtyperZhuggingface_hub.hf_apir   r   Z
_cli_utilsr   r	   r
   r   r   r   r   r   r   Z_COLLECTION_ITEM_TYPESEnumstrr   Z_COLLECTION_SORT_OPTIONSr   Zcollections_clicommandtableOptionr-   ZArgumentr6   boolr>   intrB   rE   rI   rK   rL   r   r   r   r   <module>   s  ,


#





!





&

	

 




