Ë
    Fêñi»G  ã                  óÔ   — d dl mZ 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
 d dlmZm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 d d
lmZ erde› d�nde› d�Z G d„ d«      Zy)é    )ÚannotationsN)Ú
HTTPStatus)ÚPath)ÚAny)Úparse_qsÚurlparse)Ú__version__)ÚHELP_MSGÚHUB_WEB_ROOTÚPREFIX)ÚIS_COLABÚLOGGERÚSETTINGSÚTQDMÚchecks)ÚHUBModelErrorzpython-z-colabz-localc                  óê   — 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„ Z	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd	„Ze	dd
„«       Zdd„Zd„ Z	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd„Ze	dd„«       Ze	dd„«       Zy)ÚHUBTrainingSessiona¨  HUB training session for Ultralytics HUB YOLO models.

    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[str, int]): Rate limits for different API calls in seconds.
        timers (dict[str, Any]): Timers for rate limiting.
        metrics_queue (dict[str, Any]): Queue for the model's metrics.
        metrics_upload_failed_queue (dict[str, Any]): Queue for metrics that failed to upload.
        model (Any): Model data fetched from Ultralytics HUB.
        model_file (str): Path to the model file.
        train_args (dict[str, Any]): Arguments for training the model.
        client (Any): Client for interacting with Ultralytics HUB.
        filename (str): Filename of the model.

    Examples:
        Create a training session with a model URL
        >>> session = HUBTrainingSession("https://hub.ultralytics.com/models/example-model")
        >>> session.upload_metrics()
    c                ó:  — ddl m} ddddœ| _        i | _        i | _        i | _        d| _        d| _        d| _        d| _	        | j                  |«      \  }}| _        |xs t        j                  d«      }|rd|ind} ||«      | _        	 |r| j                  |«       y| j                  j                  «       | _        y# t         $ rQ |j#                  t$        › d	�«      r6| j                  j&                  st)        j*                  t,        › d
�«       Y yY yY yw xY 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Úcredentialss          úY/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/hub/session.pyÚ__init__zHUBTrainingSession.__init__.   s  € õ 	&à'(°#ÀCÑHˆÔØˆÔØ+-ˆÔ(ØˆŒØˆŒ
ØˆŒØˆŒØˆŒð ,0×+AÑ+AÀ*Ó+MÑ(ˆ�˜4œ=ð Ò7¤§¡¨YÓ 7ˆ
Ù1;�y *Ñ-Àˆñ   Ó,ˆŒð
	ÙØ—‘ Õ)à!Ÿ[™[×.Ñ.Ó0�•
øÜò 	Ø×$Ñ$¬ ~°XÐ%>Ô?ÈÏÉ×HaÒHaÜ—‘Ü�hð gð göñ IbÑ?ð	ús   ÂC  Â C  Ã ADÄDNc                óÜ   — 	  | |«      }|rF|j                  t        › d�«      s.|j                  |«       |j                  j                  sJ d«       ‚|S # t
        t        t        f$ r Y yw xY w)a›  Create an authenticated HUBTrainingSession or return None.

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

        Returns:
            session (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)Úclsr1   ÚargsÚsessions       r5   Úcreate_sessionz!HUBTrainingSession.create_session\   sm   € ð	Ù˜*“oˆGÙ˜J×1Ñ1´\°NÀ(Ð2KÔLØ×$Ñ$ TÔ*Ø—}‘}×'Ò'ÐIÐ)IÓIÐ'ØˆNøäÔ!4´nÐEò 	Ùð	ús   ‚AA ÁA+Á*A+c                óÖ  — | j                   j                  |«      | _        | j                  j                  st        d|› d�«      ‚t        › d| j                  j
                  › �| _        | j                  j                  «       r…t        j                  d| j                  › d�«       | j                  j                  d«      }t        j                  |t        t        d   «      dz  | j                  j
                  z  ¬	«      | _        y
| j!                  «        | j                  j#                  | j$                  d   «       t        j                  t&        › d| j                  › d�«       y
)zý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   â�Œ Model not found: 'z"'. Verify the model ID is correct.r   zLoading trained HUB model õ    ðŸš€ÚbestÚweights_dirÚhub)Údownload_dirNr   úView model at )r*   r#   Údatar   r   r9   r$   Ú
is_trainedr   ÚinfoÚget_weights_urlr   Ú
check_filer   r   r%   Ú_set_train_argsÚstart_heartbeatr   r   )r0   r2   Úurls      r5   r+   zHUBTrainingSession.load_modelq   s  € ð —[‘[×&Ñ& xÓ0ˆŒ
Ø�z‰z�ŠÜÐ"8¸¸
ÐBdÐ eÓfÐfä(˜>¨°$·*±*·-±-°ÐAˆŒØ�:‰:× Ñ Ô"Ü�K‰KÐ4°T·^±^Ð4DÀEÐJÔKØ—*‘*×,Ñ,¨VÓ4ˆCÜ$×/Ñ/°Ä$ÄxÐP]ÑG^ÓB_ÐbgÑBgÐjn×jtÑjt×jwÑjwÑBwÔxˆDŒOØð 	×ÑÔØ�
‰
×"Ñ" 4×#3Ñ#3°KÑ#@ÔAÜ�‰”v�h˜n¨T¯^©^Ð,<¸EÐBÕCó    c                óz  — |j                  dd«      |j                  dd«      |j                  dd«      |j                  dd«      t        |j                  d	d
«      «      t        |j                  dd«      «      dœd|j                  d«      id| j                  j                  dd
«      j                  dd
«      ii dœd| j                  idœ}| j                  j	                  d«      r| j                  |d   d   d<   | j
                  j                  |«       | j
                  j                  st        d| j                  › d�«      ‚t        › d| j
                  j                  › �| _
        | j
                  j                  | j                  d   «       t        j                  t        › d| j                  › d�«       y)a0  Initialize a HUB training session with the specified model arguments.

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

        Returns:
            (None): If the model could not be created.
        ÚbatchéÿÿÿÿÚepochsr   Úimgszi€  Úpatienceéd   ÚdeviceÚ ÚcacheÚram)Ú	batchSizerT   Ú	imageSizerV   rX   rZ   ÚnamerH   ú.ptú.yaml)ÚarchitectureÚparent)ÚconfigÚdatasetÚlineageÚmetare   rb   u   â�Œ Failed to create model 'z'' on Ultralytics HUB. Please try again.r   r   rG   rB   N)r)   Ústrr(   ÚreplaceÚendswithr#   r8   r9   r   r   r$   rN   r   r   rJ   r   )r0   Ú
model_argsÚpayloads      r5   r8   zHUBTrainingSession.create_modelŠ   sy  € ð (Ÿ^™^¨G°RÓ8Ø$Ÿ.™.¨°3Ó7Ø'Ÿ^™^¨G°SÓ9Ø&ŸN™N¨:°sÓ;Ü˜jŸn™n¨X°rÓ:Ó;Ü˜ZŸ^™^¨G°UÓ;Ó<ñð  
§¡¨vÓ 6Ð7à!'¨¯©×)>Ñ)>¸uÀbÓ)I×)QÑ)QÐRYÐ[]Ó)^Ð _Øñð ˜TŸ]™]Ð+ñ
ˆð" �=‰=×!Ñ! %Ô(Ø37·=±=ˆG�IÑ˜xÑ(¨Ñ0à�
‰
×Ñ Ô(à�z‰z�}Š}ÜÐ">¸t¿}¹}¸oÐMtÐ uÓvÐvä(˜>¨°$·*±*·-±-°ÐAˆŒð 	�
‰
×"Ñ" 4×#3Ñ#3°KÑ#@ÔAä�‰”v�h˜n¨T¯^©^Ð,<¸EÐBÕCrP   c                óH  — d\  }}}| j                  d«      r| }n„| j                  t        › d�«      rVt        | «      }t	        |j
                  «      j                  }t        |j                  «      }|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:
            api_key (str | None): Extracted API key if present.
            model_id (str | None): Extracted model ID if present.
            filename (str | None): Extracted filename if present.

        Raises:
            HUBModelError: If the identifier format is not recognized.
        )NNN)r_   r`   r   r   Nr   zmodel='z invalid, correct format is z/models/MODEL_ID)ri   r-   r   r   r   ÚpathÚstemr   Úqueryr)   r   )r1   r   r2   r(   Ú
parsed_urlÚquery_paramss         r5   r'   z$HUBTrainingSession._parse_identifier´   s«   € ð( '7Ñ#ˆ�˜8Ø×ÑÐ/Ô0Ø!‰HØ×"Ñ"¤l ^°8Ð#<Ô=Ü! *Ó-ˆJÜ˜JŸO™OÓ,×1Ñ1ˆHÜ# J×$4Ñ$4Ó5ˆLØ"×&Ñ& y°4°&Ó9¸!Ñ<‰Gä '¨*¨Ð5QÔR^ÐQ_Ð_oÐ pÓqÐqØ˜ (Ð*Ð*rP   c                ón  — | j                   j                  «       rC| j                   j                  «       ddœ| _        | j                   j	                  d«      | _        n~| j                   j                  j                  d«      | _        | j                   j                  «       r| j                   j	                  d«      n| j                   j                  «       | _        d| j                  vrt        d«      ‚t        j                  | j
                  d¬	«      | _        | j                   j                  | _        y
)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)rH   ÚresumeÚlastr&   rb   rH   zDDataset may still be processing. Please wait a minute and try again.F)ÚverboseN)r#   Úis_resumableÚget_dataset_urlr&   rK   r%   rH   r)   Úis_pretrainedÚget_architectureÚ
ValueErrorr   Úcheck_yolov5u_filenamer9   r2   ©r0   s    r5   rM   z"HUBTrainingSession._set_train_argsÔ   sÝ   € ð �:‰:×"Ñ"Ô$à'+§z¡z×'AÑ'AÓ'CÈtÑTˆDŒOØ"Ÿj™j×8Ñ8¸Ó@ˆD�Oð #Ÿj™jŸo™o×1Ñ1°,Ó?ˆDŒOð 9=¿
¹
×8PÑ8PÔ8R�—
‘
×*Ñ*¨8Ô4ÐX\×XbÑXb×XsÑXsÓXuð ŒOð ˜Ÿ™Ñ(äÐcÓdÐdä ×7Ñ7¸¿¹ÐQVÔWˆŒØŸ
™
Ÿ™ˆ�rP   c           	     óŒ   ‡ ‡‡‡‡‡‡‡‡	— ˆˆ	ˆˆˆˆ ˆˆˆf	d„}
|r&t        j                  |
d¬«      j                  «        y |
«       S )a`  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            
     ó0  •	— t        j                   «       } d}t        ‰dz   «      D �]¯  }t        j                   «       | z
  ‰kD  r%t        j                  t        › dt
        › �«        �np ‰‰i ‰¤Ž}|€;t        j                  t        › dt
        › �«       t        j                  d|z  «       Œˆ‰r‰	j                  ‰|«       n‰
r‰	j                  |«       t        j                  |j                  cxk  rt        j                  k  rn n‰j                  d«      ri ‰	_        |c S |dk(  rG‰	j                  |‰‰«      }‰r2t        j                  t        › |› dt
        › d	|j                  › d
�«       ‰	j!                  |j                  «      s1t        j                  t        › dt
        › d	|j                  › �«        nt        j                  d|z  «       �Œ² |€;‰j                  d«      r*‰	j                  j#                  ‰j                  d«      «       |S )zKAttempt 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Úmessager>   ÚkwargsÚprogress_totalÚrequest_funcÚretryr0   Ústream_responseÚtimeoutru   s       €€€€€€€€€r5   Úretry_requestz7HUBTrainingSession.request_queue.<locals>.retry_request  sÁ  ø€ ä—‘“ˆBØˆHÜ˜5 1™9Ó%ó !!�Ü—I‘I“K "Ñ$¨Ò/Ü—N‘N¤f XÐ-JÌ8È*Ð#UÔVÚá'¨Ð8°Ñ8�ØÐ#Ü—N‘N¤f XÐ-TÔU]ÐT^Ð#_Ô`Ü—J‘J˜q !™tÔ$Øá!Ø×.Ñ.¨~¸xÕHÙ$Ø×)Ñ)¨(Ô3ä—=‘= H×$8Ñ$8ÔV¼:×;VÑ;VÕVà—z‘z )Ô,Ø;=˜Ô8Ø#’Oà˜’6à"×7Ñ7¸À%ÈÓQ�GáÜŸ™¬&¨°'°¸!¼H¸:ÀRÈ×H\ÑH\ÐG]Ð]^Ð'_Ô`à×)Ñ)¨(×*>Ñ*>Ô?Ü—N‘N¤f XÐ-=¼h¸ZÀrÈ(×J^ÑJ^ÐI_Ð#`ÔaÙä—
‘
˜1˜a™4Ö ðC!!ðH Ð F§J¡J¨yÔ$9Ø×0Ñ0×7Ñ7¸¿
¹
À9Ó8MÔNàˆOrP   T)ÚtargetÚdaemonN)Ú	threadingÚThreadÚstart)r0   r”   r•   r—   Úthreadru   r“   r–   r>   r’   r˜   s   ```` ````` r5   Úrequest_queuez HUBTrainingSession.request_queueó   s7   ÿø€ ÷:+	ô +	ñZ ä×Ñ M¸$Ô?×EÑEÕGñ !“?Ð"rP   c                óh   — t         j                  t         j                  t         j                  h}| |v S )zGDetermine if a request should be retried based on the HTTP status code.)r   ÚREQUEST_TIMEOUTÚBAD_GATEWAYÚGATEWAY_TIMEOUT)r‰   Úretry_codess     r5   rŒ   z HUBTrainingSession._should_retryD  s3   € ô ×&Ñ&Ü×"Ñ"Ü×&Ñ&ð
ˆð
 ˜kÐ)Ð)rP   c                ó.  — | j                  |j                  «      r|r	d|› d|› d�S dS |j                  t        j                  k(  r!|j                  }d|d   › d|d   › d	|d
   › d�S 	 |j                  «       j                  dd«      S # t        $ r Y yw xY w)aG  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.rY   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)r0   r�   r•   r—   r¨   s        r5   r‹   z'HUBTrainingSession._get_failure_messageN  s½   € ð ×Ñ˜h×2Ñ2Ô3Ù;@�Y˜u˜g V¨G¨9°BÐ7ÐHÀbÐHØ×!Ñ!¤Z×%AÑ%AÒAØ×&Ñ&ˆGà& wÐ/FÑ'GÐ&HÈÈ'ÐReÑJfÐIgð h&Ø&-¨mÑ&<Ð%=¸RðAðð
.Ø—}‘}“×*Ñ*¨9Ð6HÓIÐIøÜ!ò .Ù-ð.ús   Á(B Â	BÂBc                ó‚   — | j                  | j                  j                  | j                  j	                  «       d¬«      S )z(Upload model metrics to Ultralytics HUB.T)r   rž   )rŸ   r#   Úupload_metricsr    Úcopyr|   s    r5   r¬   z!HUBTrainingSession.upload_metricsg  s4   € à×!Ñ! $§*¡*×";Ñ";ÀT×EWÑEW×E\ÑE\ÓE^ÐgkÐ!ÓlÐlrP   c                óì  — t        |«      }|j                  «       sƒ|j                  d|j                  › �«      }|rC|j                  «       r3t	        j
                  t        › d�«       t        j                  ||«       n t	        j
                  t        › d|› d�«       y| j                  | j                  j                  |t        |«      |||dd| |r|j                  «       j                  ndd¬	«       y)
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.
        rt   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)
ÚepochÚweightsÚis_bestÚmapÚfinalr•   r—   rž   r“   r–   )r   Úis_fileÚ	with_nameÚsuffixr   r/   r   Úshutilr­   rŸ   r#   Úupload_modelrg   ÚstatÚst_size)r0   r±   r²   r³   r´   rµ   rt   s          r5   rº   zHUBTrainingSession.upload_modelk  sÝ   € ô" �w“-ˆØ�‰Ô Ø×$Ñ$ t¨G¯N©NÐ+;Ð%<Ó=ˆDÙ˜Ÿ™œÜ—‘Ü�hð Uð Uôô —‘˜D 'Õ*ä—‘¤& Ð)LÈWÈIÐUVÐWÔXØà×ÑØ�J‰J×#Ñ#ØÜ˜“LØØØØØØ�9Ù5:˜7Ÿ<™<›>×1Ò1ÀØ ð 	õ 	
rP   c                ó°   — t        | ddd¬«      5 }|j                  d¬«      D ]  }|j                  t        |«      «       Œ 	 ddd«       y# 1 sw Y   yxY w)zGDisplay a progress bar to track the upload progress of a file download.ÚBTé   )ÚtotalÚunitÚ
unit_scaleÚunit_divisor©Ú
chunk_sizeN)r   Úiter_contentr�   Úlen)Úcontent_lengthr�   ÚpbarrH   s       r5   r†   z(HUBTrainingSession._show_upload_progress™  sX   € ô ˜¨S¸TÐPTÔUð 	'ÐY]Ø ×-Ñ-¸Ð-Ó>ò '�Ø—‘œC ›IÕ&ñ'÷	'÷ 	'ñ 	'ús   �2AÁAc                ó2   — | j                  d¬«      D ]  }Œ y)z(Process the streamed HTTP response data.r¿   rÄ   N)rÆ   )r�   Ú_s     r5   r‡   z#HUBTrainingSession._iterate_content   s$   € ð ×&Ñ&°$Ð&Ó7ò 	ˆAØñ	rP   )r1   rg   )N)r1   rg   r>   zdict[str, Any] | None)r2   rg   )rj   zdict[str, Any])r   é   TTNN)r•   Úintr—   rÍ   rž   Úboolru   rÎ   r“   z
int | Noner–   zbool | None)r‰   rÍ   ÚreturnrÎ   )r•   rÍ   r—   rÍ   rÏ   rg   )Fg        F)r±   rÍ   r²   rg   r³   rÎ   r´   Úfloatrµ   rÎ   rÏ   ÚNone)rÈ   rÍ   rÏ   rÑ   )rÏ   rÑ   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r6   Úclassmethodr@   r+   r8   Ústaticmethodr'   rM   rŸ   rŒ   r‹   r¬   rº   r†   r‡   © rP   r5   r   r      s7  „ ñó0,ð\ óó ðó(Dó2(DðT ò+ó ð+ò>&ðD ØØØØ%)Ø'+ðO#ð ðO#ð ð	O#ð
 ðO#ð ðO#ð #ðO#ð %óO#ðb ò*ó ð*ó.ò2mð ØØð,
àð,
ð ð,
ð ð	,
ð
 ð,
ð ð,
ð 
ó,
ð\ ò'ó ð'ð òó ñrP   r   )Ú
__future__r   r¹   r›   rƒ   Úhttpr   Úpathlibr   Útypingr   Úurllib.parser   r   Úultralyticsr	   Úultralytics.hub.utilsr
   r   r   Úultralytics.utilsr   r   r   r   r   Úultralytics.utils.errorsr   Ú
AGENT_NAMEr   rØ   rP   r5   ú<module>rã      sY   ðõ #ã Û Û Ý Ý Ý ß +å #ß @Ñ @ß FÕ FÝ 2á.6ˆw�{�m 6Ñ*¸gÀkÀ]ÐRXÐ<Y€
÷Oò OrP   