o
    ϖi0                     @   s  d dl Z d dlZd dlZd dlZd dlmZ 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Zd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
l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*m+Z+ d dl,m-Z- d dl.m/Z/ erd dl)m0Z0 G dd dZ1G dd dej2ej3Z4dee5 fddZ6			d#deeej1ej7f  dee5 dee5 deej8 deej1ej7f f
ddZ9d ej:ddfd!d"Z;dS )$    N)datetime)
TYPE_CHECKINGAnyAsyncIteratorDictIterableIteratorMappingOptionalTypeUnion)Unpack)__version__AccountsCollectionsDeployments)ReplicateErrorFiles)HardwareNamespaceModelsPredictions)	async_runrun)async_streamstream	TrainingsWebhooks)ServerSentEventc                       sz  e Zd ZU dZdZeej ed< dZ	eej
 ed< 	d9ddddee dee deej d	df fd
dZed	ejfddZed	ej
fddZdeded	ejfddZdeded	ejfddZed	efddZed	efddZed	efddZed	efddZed	efddZed	efd d!Zed	e fd"d#Z!ed	e"fd$d%Z#ed	e$fd&d'Z%	d9d(d)d*ed+ee&ee'f  d,ee( d-e)d. d	e*e'e+e' f f
d/d0Z,	d9d(d)d*ed+ee&ee'f  d,ee( d-e)d. d	e*e'e-e' f f
d1d2Z.dd(d3d*ed+ee&ee'f  d,ee( d-e)d. d	e+d4 f
d5d6Z/	d9d(d)d*ed+ee&ee'f  d,ee( d-e)d. d	e-d4 f
d7d8Z0  Z1S ):ClientzA Replicate API client libraryN_Client__client_Client__async_client)base_urltimeout	api_tokenr)   r*   returnc                   s:   t    || _|| _|| _|| _ttj	dd| _
d S )NREPLICATE_POLL_INTERVALz0.5)super__init__
_api_token	_base_url_timeout_client_kwargsfloatosenvirongetpoll_interval)selfr+   r)   r*   kwargs	__class__ D/home/ubuntu/.local/lib/python3.10/site-packages/replicate/client.pyr/   .   s   
zClient.__init__c                 C   .   | j sttj| j| j| jfi | j| _ | j S N)r'   _build_httpx_clienthttpxr&   r0   r1   r2   r3   r9   r=   r=   r>   _client?      zClient._clientc                 C   r?   r@   )r(   rA   rB   AsyncClientr0   r1   r2   r3   rC   r=   r=   r>   _async_clientK   rE   zClient._async_clientmethodpathc                 K   s"   | j j||fi |}t| |S r@   )rD   request_raise_for_statusr9   rH   rI   r:   respr=   r=   r>   _requestW   s   zClient._requestc                    s*   | j j||fi |I d H }t| |S r@   )rG   rJ   rK   rL   r=   r=   r>   _async_request]   s   zClient._async_requestc                 C   
   t | dS )z?
        Namespace for operations related to accounts.
        clientr   rC   r=   r=   r>   accountsc   s   
zClient.accountsc                 C   rP   )zL
        Namespace for operations related to collections of models.
        rQ   r   rC   r=   r=   r>   collectionsk      
zClient.collectionsc                 C   rP   )zB
        Namespace for operations related to deployments.
        rQ   r   rC   r=   r=   r>   deploymentsr   rU   zClient.deploymentsc                 C   rP   )z<
        Namespace for operations related to files.
        rQ   r   rC   r=   r=   r>   filesy   rU   zClient.filesc                 C   rP   )z?
        Namespace for operations related to hardware.
        rQ   )HardwarerC   r=   r=   r>   hardware   rU   zClient.hardwarec                 C   rP   )z=
        Namespace for operations related to models.
        rQ   r   rC   r=   r=   r>   models   rU   zClient.modelsc                 C   rP   )zB
        Namespace for operations related to predictions.
        rQ   r   rC   r=   r=   r>   predictions   rU   zClient.predictionsc                 C   rP   )z@
        Namespace for operations related to trainings.
        rQ   r!   rC   r=   r=   r>   	trainings   rU   zClient.trainingsc                 C   rP   )z?
        Namespace for operations related to webhooks.
        rQ   r#   rC   r=   r=   r>   webhooks   rU   zClient.webhooksT)use_file_outputrefinputr^   paramsz"Predictions.CreatePredictionParamsc                K      t | ||fd|i|S )z6
        Run a model and wait for its output.
        r^   )r   r9   r_   r`   r^   ra   r=   r=   r>   r         z
Client.runc                   s    t | ||fd|i|I dH S )zE
        Run a model and wait for its output asynchronously.
        r^   N)r   rc   r=   r=   r>   r      s   zClient.async_run)r`   r^   r%   c                K   rb   )z*
        Stream a model's output.
        r^   )r    rc   r=   r=   r>   r       rd   zClient.streamc                   s   t | ||fd|i|S )z9
        Stream a model's output asynchronously.
        r^   )r   rc   r=   r=   r>   r      s   zClient.async_streamr@   )2__name__
__module____qualname____doc__r'   r
   rB   r&   __annotations__r(   rF   strTimeoutr/   propertyrD   rG   ResponserN   rO   r   rS   r   rT   r   rV   r   rW   rX   rY   r   rZ   r   r[   r"   r\   r$   r]   r   r   boolr   r   r   r   r   r   r    r   __classcell__r=   r=   r;   r>   r&   (   s   
 	


r&   c                   @   s   e Zd ZdZeg dZeg dZdZdedddddd	ee	j
e	jf d
ededededeee  deee  ddfddZdedee	jeeef f defddZde	jde	jfddZde	jde	jfddZd ddZd ddZdS )!RetryTransportzA custom HTTP transport that automatically retries requests using an exponential backoff strategy
    for specific HTTP status codes and request methods.
    )HEADGETPUTDELETEOPTIONSTRACE)i  i  i  <   
   g?N)max_attemptsmax_backoff_waitbackoff_factorjitter_ratioretryable_methodsretry_status_codeswrapped_transportry   rz   r{   r|   r}   r~   r,   c                C   sh   || _ |dk s|dkrtd| || _|| _|rt|n| j| _|r(t|n| j| _|| _	|| _
d S )Nr   g      ?z1jitter ratio should be between 0 and 0.5, actual )_wrapped_transport
ValueErrorry   r{   	frozensetRETRYABLE_METHODSr}   RETRYABLE_STATUS_CODESr~   r|   rz   )r9   r   ry   rz   r{   r|   r}   r~   r=   r=   r>   r/      s"   


zRetryTransport.__init__attempts_madeheadersc           	      C   s   | dpd }|r;| rt|S zt| }|t    }|dkr0t	|| j
W S W n	 ty:   Y nw | jd|d   }|| j tddg }|| }t	|| j
S )NzRetry-After r         )r7   stripisdigitr4   r   fromisoformat
astimezonenowtotal_secondsminrz   r   r{   r|   randomchoice)	r9   r   r   retry_after_headerparsed_datediffbackoffjittertotal_backoffr=   r=   r>   _calculate_sleep  s"   zRetryTransport._calculate_sleeprJ   c                 C   s   | j |}|j| jvr|S | jd }d}	 |dk s |j| jvr"|S |  | ||j	}t
| | j |}|d7 }|d8 }qNr   )r   handle_requestrH   r}   ry   status_coder~   closer   r   timesleepr9   rJ   responseremaining_attemptsr   	sleep_forr=   r=   r>   r   $  s    

zRetryTransport.handle_requestc                    s   | j |I d H }|j| jvr|S | jd }d}	 |dk s$|j| jvr&|S | I d H  | ||j	}t
|I d H  | j |I d H }|d7 }|d8 }qr   )r   handle_async_requestrH   r}   ry   r   r~   acloser   r   asyncior   r   r=   r=   r>   r   >  s"   
z#RetryTransport.handle_async_requestc                    s   | j  I d H  d S r@   )r   r   rC   r=   r=   r>   r   X  s   zRetryTransport.aclosec                 C   s   | j   d S r@   )r   r   rC   r=   r=   r>   r   [  s   zRetryTransport.close)r,   N)re   rf   rg   rh   r   r   r   MAX_BACKOFF_WAITr   rB   BaseTransportAsyncBaseTransportintr4   r
   r   rj   r/   Headersr	   r   Requestrm   r   r   r   r   r=   r=   r=   r>   rp      sR    

	

!

rp   r,   c                  C   sP   zddl } |  j D ]\}}| dkr|  W S qW n   Y tjdS )zNGet API token from cog current scope if available, otherwise from environment.r   NREPLICATE_API_TOKEN)cogcurrent_scopecontextitemsupperr5   r6   r7   )r   keyvaluer=   r=   r>   _get_api_token_from_environment_  s   
r   client_typer+   r)   r*   c                 K   s   | di }d|vrdt |d< d|vr#|pt  }r#d| |d< |p,tjdp,d}|dkr3d}|p>tjd	d
d
d	dd}| dd pQ| tju rNt	 nt
 }| d|||t|dd|S )Nr   z
User-Agentzreplicate-python/AuthorizationzBearer REPLICATE_BASE_URLzhttps://api.replicate.comr   g      @g      >@g      $@)readwriteconnectpool	transport)r   )r)   r   r*   r   r=   )popr   r   r5   r6   r7   rB   rk   r&   HTTPTransportAsyncHTTPTransportrp   )r   r+   r)   r*   r:   r   r   r=   r=   r>   rA   m  s6   

rA   rM   c                 C   s*   d| j   krdk rn d S t| d S )Ni  iX  )r   r   from_response)rM   r=   r=   r>   rK     s   
rK   )NNN)<r   r5   r   r   r   typingr   r   r   r   r   r   r	   r
   r   r   rB   typing_extensionsr   replicate.__about__r   replicate.accountr   replicate.collectionr   replicate.deploymentr   replicate.exceptionsr   replicate.filer   replicate.hardwarer   rX   replicate.modelr   replicate.predictionr   replicate.runr   r   replicate.streamr   r    replicate.trainingr"   replicate.webhookr$   r%   r&   r   r   rp   rj   r   rF   rk   rA   rm   rK   r=   r=   r=   r>   <module>   sV    0 8 
(