o
    ϖi&5                     @   s  d dl mZmZmZmZmZmZmZmZm	Z	m
Z
 d dlmZmZ d dlmZm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 zd d	lmZ W n ey]   d d
lZY nw erld dlm Z  d dl!m"Z" G dd deZ#G dd deZ$	
d(d
d
d
d
ddeee%ef  dee	e%ee%e%f df  dee% dee% deee%  dee%ef fddZ&de%de%fddZ'de	e%ee%e%f df de	e%d f de%fd!d"Z(d#d$d%ee%ef de#fd&d'Z)d
S ))    )
TYPE_CHECKINGAnyDictListLiteralOptionalTuple	TypedDictUnionoverload)NotRequiredUnpack)async_encode_jsonencode_json)ModelVersionIdentifier)Model)Page)	NamespaceResource)Version)v1N)Client)FileEncodingStrategyc                   @   s  e Zd ZU dZe Zded< eed< 	 eed< 	 e	ee
f ed< 	 ee ed< 	 ed ed	< 	 eeeef  ed
< 	 ee ed< 	 ee ed< 	 ee ed< 	 ee ed< 	 ee ed< 	 ee ed< 	 eeeef  ed< 	 dddZdddZdddZdddZdS )Trainingz:
    A training made for a model hosted on Replicate.
    r   _clientidmodelversiondestination)starting
processing	succeededfailedcanceledstatusinputoutputlogserror
created_at
started_atcompleted_aturlsreturnNc                 C   6   | j j| j}|  D ]
\}}t| || qdS )z,
        Cancel a running training.
        N)r   	trainingscancelr   dictitemssetattrselfr#   namevalue r8   F/home/ubuntu/.local/lib/python3.10/site-packages/replicate/training.pyr0   U      zTraining.cancelc                    >   | j j| jI dH }|  D ]
\}}t| || qdS )z;
        Cancel a running training asynchronously.
        N)r   r/   async_cancelr   r1   r2   r3   r4   r8   r8   r9   r<   ^   
   zTraining.async_cancelc                 C   r.   )z4
        Load the training from the server.
        N)r   r/   getr   r1   r2   r3   r5   updatedr6   r7   r8   r8   r9   reloadg   r:   zTraining.reloadc                    r;   )zC
        Load the training from the server asynchronously.
        N)r   r/   	async_getr   r1   r2   r3   r?   r8   r8   r9   async_reloadp   r=   zTraining.async_reload)r-   N)__name__
__module____qualname____doc__pydanticPrivateAttrr   __annotations__strr
   r   r   r   r   r   r0   r<   rA   rC   r8   r8   r8   r9   r   !   sB   
 

	
		r   c                   @   s  e Zd ZdZd&deeddf dee fddZ	d&deeddf dee fd	d
Z	dedefddZ
dedefddZG dd deZe		d'dedeeef dedee deee  defddZe	d(deeeeef df deeef deeeef  ded def
ddZdddddeeeeeef df  deeeef  deeeef  ded def
ddZdeeeeef df deeef deeef ded def
d d!Zdedefd"d#Zdedefd$d%ZdS ))	Trainingsz8
    Namespace for operations related to trainings.
    .cursorellipsisNr-   c                    s^   |du rt d jd|du rdn|}| } fdd|d D |d< tt d	i |S )
#  
        List your trainings.

        Parameters:
            cursor: The cursor to use for pagination. Use the value of `Page.next` or `Page.previous`.
        Returns:
            Page[Training]: A page of trainings.
        Raises:
            ValueError: If `cursor` is `None`.
        Ncursor cannot be NoneGET./v1/trainingsc                       g | ]}t  j|qS r8   _json_to_trainingr   .0resultr5   r8   r9   
<listcomp>       z"Trainings.list.<locals>.<listcomp>resultsr8   )
ValueErrorr   _requestjsonr   r   r5   rM   respobjr8   rY   r9   list   s   

zTrainings.listc                    sf   |du r	t d jd|du rdn|I dH }| } fdd|d D |d< tt d	i |S )
rO   NrP   rQ   .rR   c                    rS   r8   rT   rV   rY   r8   r9   rZ      r[   z(Trainings.async_list.<locals>.<listcomp>r\   r8   )r]   r   _async_requestr_   r   r   r`   r8   rY   r9   
async_list   s   


zTrainings.async_listr   c                 C   s$   | j dd| }t| j | S )
        Get a training by ID.

        Args:
            id: The ID of the training.
        Returns:
            Training: The training object.
        rQ   /v1/trainings/r   r^   rU   r_   r5   r   ra   r8   r8   r9   r>      s
   
zTrainings.getc                    s,   | j dd| I dH }t| j | S )rf   rQ   rg   Nr   rd   rU   r_   ri   r8   r8   r9   rB      s   

zTrainings.async_getc                   @   sh   e Zd ZU dZeeeeef df ed< 	 ee ed< 	 ee ed< 	 ee	e  ed< 	 ed ed< d	S )
Trainings.CreateTrainingParamsz#Parameters for creating a training.r   r   webhookwebhook_completedwebhook_events_filterr   file_encoding_strategyN)
rD   rE   rF   rG   r
   rK   r   rJ   r   r   r8   r8   r8   r9   CreateTrainingParams   s   
 rp   r   r%   r   rl   rn   c                 K      d S Nr8   )r5   r   r%   r   rl   rn   kwargsr8   r8   r9   create   s   	zTrainings.creater   r   paramsrk   c                 K   rq   rr   r8   )r5   r   r   r%   ru   r8   r8   r9   rt      s   )r   r   r%   c                O   s6  d}|rSt |dkr|d nd }rt|}t |dkr |d n|}t |dkr.|d |d< t |dkr:|d |d< t |dkrF|d |d	< t |d
krR|d
 |d< n|r]|r]t||}n|du rjt|trjt|}|sptd|dd}|durt|| j|d}t	|fi |}	| jj
d||	d}
t| j|
 S )zT
        Create a new training using the specified model version as a base.
        Nr         r      rl      rm      rn   z8model and version or shorthand version must be specifiedro   clientro   POSTr_   )len#_create_training_url_from_shorthand+_create_training_url_from_model_and_version
isinstancerK   r]   popr   r   _create_training_bodyr^   rU   r_   )r5   r   r   r%   argsru   url	shorthandro   bodyra   r8   r8   r9   rt      sD   c           	         sn   t ||}|dd}|durt|| j|dI dH }t|fi |}| jjd||dI dH }t| j| S )a  
        Create a new training using the specified model version as a base.

        Args:
            version: The ID of the base model version that you're using to train a new model version.
            input: The input to the training.
            destination: The desired model to push to in the format `{owner}/{model_name}`. This should be an existing model owned by the user or organization making the API request.
            webhook: The URL to send a POST request to when the training is completed. Defaults to None.
            webhook_completed: The URL to receive a POST request when the prediction is completed.
            webhook_events_filter: The events to send to the webhook. Defaults to None.
        Returns:
            The training object.
        ro   Nr{   r}   r~   )r   r   r   r   r   rd   rU   r_   )	r5   r   r   r%   ru   r   ro   r   ra   r8   r8   r9   async_create4  s    
zTrainings.async_createc                 C   s&   | j dd| d}t| j | S )
        Cancel a training.

        Args:
            id: The ID of the training to cancel.
        Returns:
            Training: The canceled training object.
        r}   rg   /cancelrh   ri   r8   r8   r9   r0   \  s
   

zTrainings.cancelc                    s.   | j dd| dI dH }t| j | S )r   r}   rg   r   Nrj   ri   r8   r8   r9   r<   m  s   


zTrainings.async_cancel).)NNrr   )rD   rE   rF   rG   r
   rK   r   r   rc   re   r>   rB   r	   rp   r   r   r   r   r   rt   r   r   r   r   r0   r<   r8   r8   r8   r9   rL   z   s    "





6


(rL   )r   rl   rm   rn   r%   r   r   rl   rm   rn   r-   c                K   s   i }| d ur
| |d< |d u rt dt|tr!|j d|j }nt|tr1|d  d|d  }||d< |d ur=||d< |d urE||d< |d urM||d	< |S )
Nr%   zCA destination must be provided as a positional or keyword argument./r   rv   r   rl   rm   rn   )r]   r   r   ownerr6   tuple)r%   r   rl   rm   rn   _kwargsr   r8   r8   r9   r     s&   	

r   refc                 C   s(   t | \}}}d| d| d| dS )N/v1/models/r   
/versions/
/trainings)r   parse)r   r   r6   
version_idr8   r8   r9   r     s   r   r   r   r   c                 C   s   t | tr| j| j}}n!t | tr| d | d }}nt | tr*t| \}}}ntdt |t	r7|j
}n|}d| d| d| dS )Nr   rv   zWmodel must be a Model, a tuple of (owner, name), or a string in the format 'owner/name'r   r   r   r   )r   r   r   r6   r   rK   r   r   r]   r   r   )r   r   r   r6   _r   r8   r8   r9   r     s   



r   r|   r   r_   c                 C   sX   t di |}| |_|jr*t|jtr*d|jv r*t|jd }|j d|j |_	|S )Nr   r   r8   )
r   r   r&   r   r1   r   r   r   r6   r   )r|   r_   trainingr   r8   r8   r9   rU     s   

rU   rr   )*typingr   r   r   r   r   r   r   r	   r
   r   typing_extensionsr   r   replicate.helpersr   r   replicate.identifierr   replicate.modelr   replicate.paginationr   replicate.resourcer   r   replicate.versionr   rH   r   ImportErrorreplicate.clientr   replicate.filer   r   rL   rK   r   r   r   rU   r8   r8   r8   r9   <module>   s`   0 Y  


$

"