o
    VhI                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
Z
d dlmZ d dlmZmZmZ d dlmZmZmZmZmZmZ d dlmZ erRd	e d
nd	e dZG dd dZdS )    N)
HTTPStatus)Path)parse_qsurlparse)__version__)HELP_MSGHUB_WEB_ROOTPREFIX)IS_COLABLOGGERSETTINGSTQDMchecksemojis)HUBModelErrorzpython-z-colabz-localc                   @   s   e Zd ZdZdd Zed,ddZdd Zd	d
 Ze	dd Z
dd Z						d-ddZe	dd ZdejdedefddZdd Z			d.ded ed!ed"ed#ed$dfd%d&Ze	d'edejd$dfd(d)Ze	dejd$dfd*d+ZdS )/HUBTrainingSessiona  
    HUB training session for Ultralytics HUB YOLO models. Handles model initialization, heartbeats, and checkpointing.

    This class encapsulates the functionality for interacting with Ultralytics HUB during model training, including
    model creation, metrics tracking, and checkpoint uploading.

    Attributes:
        model_id (str): Identifier for the YOLO model being trained.
        model_url (str): URL for the model in Ultralytics HUB.
        rate_limits (dict): Rate limits for different API calls (in seconds).
        timers (dict): Timers for rate limiting.
        metrics_queue (dict): Queue for the model's metrics.
        metrics_upload_failed_queue (dict): Queue for metrics that failed to upload.
        model (dict): Model data fetched from Ultralytics HUB.
        model_file (str): Path to the model file.
        train_args (dict): Arguments for training the model.
        client (HUBClient): Client for interacting with Ultralytics HUB.
        filename (str): Filename of the model.

    Examples:
        >>> session = HUBTrainingSession("https://hub.ultralytics.com/models/example-model")
        >>> session.upload_metrics()
    c                 C   s   ddl m} dddd| _i | _i | _i | _d| _d| _d| _d| _	| 
|\}}| _|p1td}|r8d|ind}||| _z|rJ| | W dS | j | _W dS  tyv   |t d	rp| jjsstt d
 Y dS Y dS Y dS w )a  
        Initialize the HUBTrainingSession with the provided model identifier.

        Args:
            identifier (str): Model identifier used to initialize the HUB training session.
                It can be a URL string or a model key with specific format.

        Raises:
            ValueError: If the provided model identifier is invalid.
            ConnectionError: If connecting with global API key is not supported.
            ModuleNotFoundError: If hub-sdk package is not installed.
        r   )	HUBClient   i  ,  )metricsckpt	heartbeatNapi_key/models/zzPlease log in using 'yolo login API_KEY'. You can find your API Key at: https://hub.ultralytics.com/settings?tab=api+keys.)hub_sdkr   rate_limitsmetrics_queuemetrics_upload_failed_queuetimersmodel	model_url
model_file
train_args_parse_identifierfilenamer   getclient
load_model	Exception
startswithr   authenticatedr   warningr	   )self
identifierr   r   model_id
active_keycredentials r1   K/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/hub/session.py__init__-   s0   

zHUBTrainingSession.__init__Nc              
   C   sX   z| |}|r| t ds|| |jjsJ d|W S  tttfy+   Y dS w )a  
        Create an authenticated HUBTrainingSession or return None.

        Args:
            identifier (str): Model identifier used to initialize the HUB training session.
            args (dict, optional): Arguments for creating a new model if identifier is not a HUB model URL.

        Returns:
            (HUBTrainingSession | None): An authenticated session or None if creation fails.
        r   zHUB model not loaded correctlyN)r)   r   create_modelr   idPermissionErrorModuleNotFoundErrorAssertionError)clsr-   argssessionr1   r1   r2   create_session\   s   
z!HUBTrainingSession.create_sessionc                 C   s   | j || _| jjsttdt d| jj | _| j rDt	
d| j d | jd}tj|ttd d | jj d| _d	S |   | j| jd
  t	
t d| j d d	S )a  
        Load an existing model from Ultralytics HUB using the provided model identifier.

        Args:
            model_id (str): The identifier of the model to load.

        Raises:
            ValueError: If the specified HUB model does not exist.
        u*   ❌ The specified HUB model does not existr   zLoading trained HUB model     🚀bestweights_dirhub)download_dirNr   View model at )r&   r   data
ValueErrorr   r   r5   r    
is_trainedr   infoget_weights_urlr   
check_filer   r   r!   _set_train_argsstart_heartbeatr   r	   )r,   r.   urlr1   r1   r2   r'   r   s   

$zHUBTrainingSession.load_modelc              
   C   s   | dd| dd| dd| ddt| d	d
t| dddd| did| jdd
dd
ii dd| jid}| jdrO| j|d d d< | j| | jjs[dS t d| jj | _	| j
| jd  tt d| j	 d dS )a  
        Initialize a HUB training session with the specified model arguments.

        Args:
            model_args (dict): Arguments for creating the model, including batch size, epochs, image size, etc.

        Returns:
            (None): If the model could not be created.
        batchepochsr   imgszi  patienced   device cacheram)	batchSizerN   	imageSizerP   rR   rT   namerC   .pt.yaml)architectureparent)configdatasetlineagemetar_   r\   Nr   r   rB   r=   )r%   strr$   replaceendswithr   r4   r5   r   r    rJ   r   r   rF   r	   )r,   
model_argspayloadr1   r1   r2   r4      s*   



zHUBTrainingSession.create_modelc                 C   s   d\}}}t | jdv r| }n,| t dr0t| }t |jj}t|j}|	ddgd }nt
d|  dt d	|||fS )
a  
        Parse the given identifier to determine the type and extract relevant components.

        The method supports different identifier formats:
            - A HUB model URL https://hub.ultralytics.com/models/MODEL
            - A HUB model URL with API Key https://hub.ultralytics.com/models/MODEL?api_key=APIKEY
            - A local filename that ends with '.pt' or '.yaml'

        Args:
            identifier (str): The identifier string to be parsed.

        Returns:
            (tuple): A tuple containing the API key, model ID, and filename as applicable.

        Raises:
            HUBModelError: If the identifier format is not recognized.
        )NNN>   rY   rZ   r   r   Nr   zmodel='z invalid, correct format is z/models/MODEL_ID)r   suffixr)   r   r   pathstemr   queryr%   r   )r-   r   r.   r$   
parsed_urlquery_paramsr1   r1   r2   r#      s   


z$HUBTrainingSession._parse_identifierc                 C   s   | j  r| j  dd| _| j d| _n| j jd| _| j  r)| j dn| j 	 | _d| jvr8t
dtj| jdd	| _| j j| _d
S )a.  
        Initialize training arguments and create a model entry on the Ultralytics HUB.

        This method sets up training arguments based on the model's state and updates them with any additional
        arguments provided. It handles different states of the model, such as whether it's resumable, pretrained,
        or requires specific file setup.

        Raises:
            ValueError: If the model is already trained, if required dataset information is missing, or if there are
                issues with the provided training arguments.
        T)rC   resumelastr"   r\   rC   zDDataset may still be processing. Please wait a minute and try again.F)verboseN)r   is_resumableget_dataset_urlr"   rG   r!   rC   r%   is_pretrainedget_architecturerD   r   check_yolov5u_filenamer5   r.   r,   r1   r1   r2   rI      s   

z"HUBTrainingSession._set_train_argsr      Tc              	      s<    f	dd}
|rt j|
dd  dS |
 S )av  
        Attempt to execute `request_func` with retries, timeout handling, optional threading, and progress tracking.

        Args:
            request_func (callable): The function to execute.
            retry (int): Number of retry attempts.
            timeout (int): Maximum time to wait for the request to complete.
            thread (bool): Whether to run the request in a separate thread.
            verbose (bool): Whether to log detailed messages.
            progress_total (int, optional): Total size for progress tracking.
            stream_response (bool, optional): Whether to stream the response.
            *args (Any): Additional positional arguments for request_func.
            **kwargs (Any): Additional keyword arguments for request_func.

        Returns:
            (requests.Response | None): The response object if thread=False, otherwise None.
        c               
      st  t   } d}td D ]}t   |  kr"tt dt   n i }|du r?tt dt  t d|  qrH| nrO| t	j
|j  kr\t	jk rjn ndrfi _|  S |dkr|}rtt | dt d	|j d
 |jstt dt d	|j   nt d|  q|du rdrjd |S )zMAttempt to call `request_func` with retries, timeout, and optional threading.N   zTimeout for request reached. z'Received no response from the request.    r   r    z ()zRequest failed. )timeranger   r+   r	   r   sleep_show_upload_progress_iterate_contentr   OKstatus_codeMULTIPLE_CHOICESr%   r   _get_failure_message_should_retryupdate)t0responseimessage	r:   kwargsprogress_totalrequest_funcretryr,   stream_responsetimeoutrn   r1   r2   retry_request  s<   

"z7HUBTrainingSession.request_queue.<locals>.retry_requestT)targetdaemonN)	threadingThreadstart)r,   r   r   r   threadrn   r   r   r:   r   r   r1   r   r2   request_queue   s   -z HUBTrainingSession.request_queuec                 C   s   t jt jt jh}| |v S )a  
        Determine if a request should be retried based on the HTTP status code.

        Args:
            status_code (int): The HTTP status code from the response.

        Returns:
            (bool): True if the request should be retried, False otherwise.
        )r   REQUEST_TIMEOUTBAD_GATEWAYGATEWAY_TIMEOUT)r   retry_codesr1   r1   r2   r   I  s
   z HUBTrainingSession._should_retryr   r   r   c                 C   s   |  |jr|rd| d| dS dS |jtjkr.|j}d|d  d|d  d	|d
  dS z	| ddW S  tyA   Y dS w )aP  
        Generate a retry message based on the response status code.

        Args:
            response (requests.Response): The HTTP response object.
            retry (int): The number of retry attempts allowed.
            timeout (int): The maximum timeout duration.

        Returns:
            (str): The retry message.
        z	Retrying zx for zs.rS   zRate limit reached (zX-RateLimit-Remaining/zX-RateLimit-Limitz). Please retry after zRetry-Afterr   zNo JSON message.zUnable to read JSON.)r   r   r   TOO_MANY_REQUESTSheadersjsonr%   AttributeError)r,   r   r   r   r   r1   r1   r2   r   [  s   z'HUBTrainingSession._get_failure_messagec                 C   s   | j | jj| j ddS )z(Upload model metrics to Ultralytics HUB.T)r   r   )r   r   upload_metricsr   copyrt   r1   r1   r2   r   u  s   z!HUBTrainingSession.upload_metricsF        epochweightsis_bestmapfinalreturnc                 C   s   t |}| s3|d|j }|r&| r&tt d t|| ntt d| d dS | j	| j
j|t||||dd| |rJ| jnddd	 dS )
a  
        Upload a model checkpoint to Ultralytics HUB.

        Args:
            epoch (int): The current training epoch.
            weights (str): Path to the model weights file.
            is_best (bool): Indicates if the current model is the best one so far.
            map (float): Mean average precision of the model.
            final (bool): Indicates if the model is the final model after training.
        rm   a!   Model 'best.pt' not found, copying 'last.pt' to 'best.pt' and uploading. This often happens when resuming training in transient environments like Google Colab. For more reliable training, consider using Ultralytics HUB Cloud. Learn more at https://docs.ultralytics.com/hub/cloud-training.z# Model upload issue. Missing model .N
   i  T)
r   r   r   r   r   r   r   r   r   r   )r   is_file	with_namerf   r   r+   r	   shutilr   r   r   upload_modelra   statst_size)r,   r   r   r   r   r   rm   r1   r1   r2   r   y  s.   
zHUBTrainingSession.upload_modelcontent_lengthc                 C   sV   t | dddd}|jddD ]	}|t| qW d   dS 1 s$w   Y  dS )a!  
        Display a progress bar to track the upload progress of a file download.

        Args:
            content_length (int): The total size of the content to be downloaded in bytes.
            response (requests.Response): The response object from the file download request.
        BT   )totalunit
unit_scaleunit_divisor
chunk_sizeN)r   iter_contentr   len)r   r   pbarrC   r1   r1   r2   r}     s
   	"z(HUBTrainingSession._show_upload_progressc                 C   s   | j ddD ]}qdS )z
        Process the streamed HTTP response data.

        Args:
            response (requests.Response): The response object from the file download request.
        r   r   N)r   )r   _r1   r1   r2   r~     s   z#HUBTrainingSession._iterate_content)N)r   ru   TTNN)Fr   F)__name__
__module____qualname____doc__r3   classmethodr<   r'   r4   staticmethodr#   rI   r   r   requestsResponseintr   r   ra   boolfloatr   r}   r~   r1   r1   r1   r2   r      sR    /,
#
R

/r   )r   r   rz   httpr   pathlibr   urllib.parser   r   r   ultralyticsr   ultralytics.hub.utilsr   r   r	   ultralytics.utilsr
   r   r   r   r   r   ultralytics.utils.errorsr   
AGENT_NAMEr   r1   r1   r1   r2   <module>   s    