o
    ϖi,                  	   @   s0  d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZmZ d dlZer@d dlmZ d dlmZ zd dlZdZW n eyS   d	ZY nw 	dd
eddded defddZ	dd
eddded defddZdejdefddZG dd dejejZ dedddefddZ!dS )    N)MappingSequence)Path)GeneratorType)TYPE_CHECKINGAnyAsyncIteratorIteratorOptional)Client)FileEncodingStrategyTFobjclientr   file_encoding_strategyr   returnc                    s   t | tr fdd|  D S t | tttttfr% fdd| D S t | trE| 	d}t
| W  d   S 1 s@w   Y  t | tjr\dkrSt| S  j| jd S tr|t | tjrht| S t | tjrrt| S t | tjr||  S | S )	z9
    Return a JSON-compatible version of the object.
    c                    s   i | ]\}}|t | qS  encode_json.0keyvaluer   r   r   E/home/ubuntu/.local/lib/python3.10/site-packages/replicate/helpers.py
<dictcomp>#   s    zencode_json.<locals>.<dictcomp>c                    s   g | ]}t | qS r   r   r   r   r   r   r   
<listcomp>(   s    zencode_json.<locals>.<listcomp>rbNbase64get)
isinstancedictitemslistset	frozensetr   tupler   openr   ioIOBasebase64_encode_filefilescreateurls	HAS_NUMPYnpintegerintfloatingfloatndarraytolistr   r   r   filer   r   r   r      s,   
	

 r   c                    s  t | tr fdd|  D I dH S t | tttttfr, fdd| D I dH S t | trO| 	d}t
| I dH W  d   S 1 sJw   Y  t | tjridkr]t| S  j| I dH jd S trt | tjrut| S t | tjrt| S t | tjr|  S | S )	zH
    Asynchronously return a JSON-compatible version of the object.
    c                    s&   i | ]\}}|t | I d H qS Nasync_encode_jsonr   r   r   r   r   D   s
    z%async_encode_json.<locals>.<dictcomp>Nc                    s    g | ]}t | I d H qS r8   r9   r   r   r   r   r   I   s
    z%async_encode_json.<locals>.<listcomp>r   r   r   )r    r!   r"   r#   r$   r%   r   r&   r   r'   r:   r(   r)   r*   r+   async_creater-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r   r   r   r:   :   s2   
	
 r:   r7   c                 C   sd   |  d |  }t|tr|dn|}t|d}t	t
| ddd p)d}d| d| S )z
    Base64 encode a file.

    Args:
        file: A file handle to upload.
    Returns:
        str: A base64-encoded data URI.
    r   zutf-8name zapplication/octet-streamdata:z;base64,)seekreadr    strencoder   	b64encodedecode	mimetypes
guess_typegetattr)r7   bodyencoded_body	mime_typer   r   r   r*   _   s   

r*   c                   @   s   e Zd ZU dZeed< 	 ded< deddddfdd	Zdefd
dZde	e fddZ
defddZdee fddZdefddZdS )
FileOutputzu
    An object that can be used to read the contents of an output file
    created by running a Replicate model.
    urlr   _clientr   r   Nc                 C   s   || _ || _d S r8   )rL   rM   )selfrL   r   r   r   r   __init__   s   
zFileOutput.__init__c                 C   sn   | j dr| j dd\}}t|S | jjd| j }|  | W  d    S 1 s0w   Y  d S Nr>   ,   GET)	rL   
startswithsplitr   	b64decoderM   streamraise_for_statusr@   rN   _encodedresponser   r   r   r@      s   
$zFileOutput.readc                 c   sj    | j dr|  V  d S | jjd| j }|  | E d H  W d    d S 1 s.w   Y  d S Nr>   rS   )rL   rT   r@   rM   rW   rX   
iter_bytes)rN   r\   r   r   r   __iter__   s   
"zFileOutput.__iter__c              	      s   | j dr| j dd\}}t|S | jjd| j 4 I d H }|  |	 I d H W  d   I d H  S 1 I d H s>w   Y  d S rP   )
rL   rT   rU   r   rV   rM   _async_clientrW   rX   areadrY   r   r   r   ra      s   
0zFileOutput.areadc              	   C  s   | j dr|  I d H V  d S | jjd| j 4 I d H }|  | 2 z	3 d H W }|V  q'6 W d   I d H  d S 1 I d H sCw   Y  d S r]   )rL   rT   ra   rM   r`   rW   rX   aiter_bytes)rN   r\   chunkr   r   r   	__aiter__   s   .zFileOutput.__aiter__c                 C   s   | j S r8   )rL   )rN   r   r   r   __str__   s   zFileOutput.__str__)__name__
__module____qualname____doc__rA   __annotations__rO   bytesr@   r	   r_   ra   r   rd   re   r   r   r   r   rK   v   s   
 			
rK   r   c                    s    dt dt f fdd| S )zV
    Transform the output of a prediction to a `FileOutput` object if it's a URL.
    r   r   c                    sr   t | trfdd|  D S t | tr#t | ts#fdd| D S t | tr7| ds2| dr7t|  S | S )Nc                    s   i | ]	\}}| |qS r   r   )r   kv	transformr   r   r      s    z7transform_output.<locals>.transform.<locals>.<dictcomp>c                    s   g | ]} |qS r   r   )r   itemrn   r   r   r      s    z7transform_output.<locals>.transform.<locals>.<listcomp>zhttps:r>   )r    r   r"   r   rA   rT   rK   )r   r   ro   r   r   ro      s   


z#transform_output.<locals>.transform)r   )r   r   r   rq   r   transform_output   s   rr   r8   )"r   r(   rE   collections.abcr   r   pathlibr   typesr   typingr   r   r   r	   r
   httpxreplicate.clientr   replicate.filer   numpyr/   r.   ImportErrorr   r:   r)   rA   r*   SyncByteStreamAsyncByteStreamrK   rr   r   r   r   r   <module>   sP    
$
%: