o
    Vh                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZmZmZ dZd d	ed
edefddZdd Zd!defddZdd Zd"dedefddZd"dedefddZdededdfddZdS )#    N)HUBDatasetStats)Auth)HUBTrainingSession)HUB_API_ROOTHUB_WEB_ROOTPREFIXevents)LOGGERSETTINGSchecks)r   r   r   loginlogoutreset_modelexport_fmts_hubexport_model
get_exportcheck_datasetr   Tapi_keysavereturnc           	      C   s   t d ddlm} t d}td}| p|}|r#|dkr#d|ind}||}|jrQ|r;|j|kr;t	d|ji |j| ksB|sDdnd	}t
t |  d
S t
t d| d dS )aa  
    Log in to the Ultralytics HUB API using the provided API key.

    The session is not stored; a new session is created when needed using the saved SETTINGS or the HUB_API_KEY
    environment variable if successfully authenticated.

    Args:
        api_key (str, optional): API key to use for authentication. If not provided, it will be retrieved from SETTINGS
            or HUB_API_KEY environment variable.
        save (bool, optional): Whether to save the API key to SETTINGS if authentication is successful.

    Returns:
        (bool): True if authentication is successful, False otherwise.
    zhub-sdk>=0.0.12r   )	HUBClientz/settings?tab=api+keysr    Nu!   New authentication successful ✅u   Authenticated ✅TzGet API key from z" and then run 'yolo login API_KEY'F)r   check_requirementshub_sdkr   r   r
   getauthenticatedr   updater	   infor   )	r   r   r   api_key_url	saved_key
active_keycredentialsclientlog_message r$   L/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/hub/__init__.pyr      s    


r   c                   C   s   dt d< tt d dS )z
    Log out of Ultralytics HUB by removing the API key from the settings file. To log in again, use 'yolo login'.

    Examples:
        >>> from ultralytics import hub
        >>> hub.logout()
    r   r   u2   logged out ✅. To log in again, use 'yolo login'.N)r
   r	   r   r   r$   r$   r$   r%   r   F   s   r   r   model_idc                 C   sb   t jt dd| idt jid}|jdkr tt d dS t	t d|j d	|j
  dS )
z,Reset a trained model to an untrained state.z/model-resetmodelId	x-api-keyjsonheaders   zModel reset successfullyNzModel reset failure  )requestspostr   r   r   status_coder	   r   r   warningreason)r&   rr$   r$   r%   r   R   s
   "
"r   c                  C   s*   ddl m}  t|  d dd ddg S )z/Returns a list of HUB-supported export formats.r   export_formatsArgument   Nultralytics_tfliteultralytics_coreml)ultralytics.engine.exporterr5   listr4   r$   r$   r%   r   [   s   r   torchscriptformatc                 C   s   |t  v sJ d| dt   tjt d|  dd|idt jid}|jdks9J t | d	|j d
|j t	
t | d dS )aC  
    Export a model to a specified format for deployment via the Ultralytics HUB API.

    Args:
        model_id (str): The ID of the model to export. An empty string will use the default model.
        format (str): The format to export the model to. Must be one of the supported formats returned by
            export_fmts_hub().

    Raises:
        AssertionError: If the specified format is not supported or if the export request fails.

    Examples:
        >>> from ultralytics import hub
        >>> hub.export_model(model_id="your_model_id", format="torchscript")
    Unsupported export format '', valid formats are z/v1/models/z/exportr=   r(   r)   r,   z export failure r-   u    export started ✅N)r   r.   r/   r   r   r   r0   r   r2   r	   r   r&   r=   r3   r$   r$   r%   r   b   s    *r   c                 C   s|   |t  v sJ d| dt   tjt dt j| |ddt jid}|jdks:J t | d|j d	|j |	 S )
a$  
    Retrieve an exported model in the specified format from Ultralytics HUB using the model ID.

    Args:
        model_id (str): The ID of the model to retrieve from Ultralytics HUB.
        format (str): The export format to retrieve. Must be one of the supported formats returned by export_fmts_hub().

    Raises:
        AssertionError: If the specified format is not supported or if the API request fails.

    Examples:
        >>> from ultralytics import hub
        >>> hub.get_export(model_id="your_model_id", format="torchscript")
    r>   r?   z/get-export)apiKeyr'   r=   r(   r)   r,   z get_export failure r-   )
r   r.   r/   r   r   r   r0   r   r2   r*   r@   r$   r$   r%   r   z   s    
*r   pathtaskc                 C   s&   t | |d  tdt d dS )a  
    Check HUB dataset Zip file for errors before upload.

    Args:
        path (str): Path to data.zip (with data.yaml inside data.zip).
        task (str): Dataset task. Options are 'detect', 'segment', 'pose', 'classify', 'obb'.

    Examples:
        >>> from ultralytics.hub import check_dataset
        >>> check_dataset("path/to/coco8.zip", task="detect")  # detect dataset
        >>> check_dataset("path/to/coco8-seg.zip", task="segment")  # segment dataset
        >>> check_dataset("path/to/coco8-pose.zip", task="pose")  # pose dataset
        >>> check_dataset("path/to/dota8.zip", task="obb")  # OBB dataset
        >>> check_dataset("path/to/imagenet10.zip", task="classify")  # classification dataset

    Note:
        Download *.zip files from https://github.com/ultralytics/hub/tree/main/example_datasets
        i.e. https://github.com/ultralytics/hub/raw/main/example_datasets/coco8.zip for coco8.zip.
    rB   rC   u7   Checks completed correctly ✅. Upload this dataset to z/datasets/.N)r   get_jsonr	   r   r   rD   r$   r$   r%   r      s   r   )NT)r   )r   r<   )r.   ultralytics.data.utilsr   ultralytics.hub.authr   ultralytics.hub.sessionr   ultralytics.hub.utilsr   r   r   r   ultralytics.utilsr	   r
   r   __all__strboolr   r   r   r   r   r   r   r$   r$   r$   r%   <module>   s   ,	