§
    AxÅj{$  ã                   ó¤   — d Z ddlZddlmZmZmZ ddlZddlmZm	Z	 ddl
mZmZmZmZ  G d„ d¦  «        Z G d„ d	¦  «        Z G d
„ d¦  «        ZdS )z#
Runpod | Python | Endpoint Runner
é    N)ÚAnyÚDictÚOptional)ÚHTTPAdapterÚRetry)ÚAPI_KEY_NOT_SET_MSGÚFINAL_STATESÚUNAUTHORIZED_MSGÚis_completedc            	       ó~   — e Zd ZdZddee         fd„Z	 	 ddededee         d	efd
„Z	ddeded	efd„Z
dded	efd„ZdS )ÚRunPodClientz$A client for running endpoint calls.NÚapi_keyc                 ó:  — ddl m}m} |p|| _        | j        €t          t          ¦  «        ‚t          j        ¦   «         | _        t          ddddg¬¦  «        }| j         	                    d	t          |¬
¦  «        ¦  «         dd| j        › �dœ| _        || _        dS )zå
        Initialize a RunPodClient instance.

        Args:
            api_key: Optional API key. If not provided, uses global api_key.

        Raises:
            RuntimeError: If the API key has not been initialized.
        r   )r   Úendpoint_url_baseNé   é   i˜  i­  )ÚtotalÚbackoff_factorÚstatus_forcelistzhttp://)Úmax_retrieszapplication/jsonzBearer )zContent-TypeÚAuthorization)Úrunpodr   r   ÚRuntimeErrorr   ÚrequestsÚSessionÚ
rp_sessionr   Úmountr   Úheaders)Úselfr   Úglobal_api_keyr   Úretriess        úGC:\Projects\Historical\venv\Lib\site-packages\runpod/endpoint/runner.pyÚ__init__zRunPodClient.__init__   sÊ   € ð	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð Ð0 .ˆŒàŒ<ÐÝÕ2Ñ3Ô3Ð3å"Ô*Ñ,Ô,ˆŒÝ˜a°ÀSÈ#ÀJÐOÑOÔOˆØŒ×Ò˜i­ÀÐ)IÑ)IÔ)IÑJÔJÐJð /Ø5 t¤|Ð5Ð5ð
ð 
ˆŒð
 "3ˆÔÐÐó    é
   ÚmethodÚendpointÚdataÚtimeoutc                 óð   — | j         › d|› �}| j                             ||| j        ||¬¦  «        }|j        dk    rt          t          ¦  «        ‚|                     ¦   «          |                     ¦   «         S )a‚  
        Make a request to the specified endpoint using the given HTTP method.

        Args:
            method: The HTTP method to use ('GET' or 'POST').
            endpoint: The endpoint path to which the request will be made.
            data: The JSON payload to send with the request.
            timeout: The number of seconds to wait for the server to send data before giving up.

        Returns:
            The JSON response from the server.

        Raises:
            RuntimeError: If the response returns a 401 Unauthorized status.
            requests.HTTPError: If the response contains an unsuccessful status code.
        ú/)r   Újsonr)   i‘  )	r   r   Úrequestr   Ústatus_coder   r
   Úraise_for_statusr,   )r   r&   r'   r(   r)   ÚurlÚresponses          r"   Ú_requestzRunPodClient._request9   s‚   € ð( Ô'Ð4Ð4¨(Ð4Ð4ˆØ”?×*Ò*Ø�C ¤°DÀ'ð +ñ 
ô 
ˆð Ô 3Ò&Ð&ÝÕ/Ñ0Ô0Ð0à×!Ò!Ñ#Ô#Ð#Ø�}Š}‰ŒÐr$   c                 ó2   — |                       d|||¦  «        S )zPost to the endpoint.ÚPOST©r2   )r   r'   r(   r)   s       r"   ÚpostzRunPodClient.postX   s   € à�}Š}˜V X¨t°WÑ=Ô=Ð=r$   c                 ó2   — |                       d||¬¦  «        S )zGet from the endpoint.ÚGET©r)   r5   )r   r'   r)   s      r"   ÚgetzRunPodClient.get\   s   € à�}Š}˜U H°gˆ}Ñ>Ô>Ð>r$   ©N)Nr%   )r%   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr#   ÚdictÚintr2   r6   r:   © r$   r"   r   r      sÛ   € € € € € Ø.Ð.ð3ð 3 ¨¤ð 3ð 3ð 3ð 3ðB BFØðð ØðØ%(ðØ08¸´ðàðð ð ð ð>>ð >˜Sð >¨ð >°sð >ð >ð >ð >ð?ð ?˜Cð ?¨#ð ?ð ?ð ?ð ?ð ?ð ?r$   r   c                   ó~   — e Zd ZdZdededefd„Zddedeeef         fd	„Z	d
„ Z
ddedefd„Zdefd„Zddedefd„ZdS )ÚJobz1Represents a job to be run on the Runpod service.Úendpoint_idÚjob_idÚclientc                 óL   — || _         || _        || _        d| _        d| _        dS )a  
        Initialize a Job instance with the given endpoint ID and job ID.

        Args:
            endpoint_id: The identifier for the endpoint.
            job_id: The identifier for the job.
            client: An instance of the RunPodClient to make requests with.
        N)rF   rG   Ú	rp_clientÚ
job_statusÚ
job_output)r   rF   rG   rH   s       r"   r#   zJob.__init__g   s,   € ð 'ˆÔØˆŒØˆŒàˆŒØˆŒˆˆr$   ÚstatusÚsourceÚreturnc                 óÞ   — | j         › d|› d| j        › �}| j                             |¬¦  «        }t	          |d         ¦  «        r(|d         | _        |                     dd¦  «        | _        |S )zBReturns the raw json of the status, raises an exception if invalidr+   )r'   rM   ÚoutputN)rF   rG   rJ   r:   r   rK   rL   )r   rN   Ú
status_urlÚ	job_states       r"   Ú
_fetch_jobzJob._fetch_jobw   sv   € àÔ(ÐAÐA¨6ÐAÐA°D´KÐAÐAˆ
Ø”N×&Ò&°
Ð&Ñ;Ô;ˆ	å˜	 (Ô+Ñ,Ô,ð 	<Ø'¨Ô1ˆDŒOØ'Ÿmšm¨H°dÑ;Ô;ˆDŒOàÐr$   c                 óR   — | j         �| j         S |                      ¦   «         d         S )z&Returns the status of the job request.NrM   )rK   rT   )r   s    r"   rM   z
Job.status‚   s'   € àŒ?Ð&Ø”?Ð"à�ŠÑ Ô  Ô*Ð*r$   r   r)   c                 óZ  — |dk    rpt          |                      ¦   «         ¦  «        sOt          j        d¦  «         |dz  }|dk    rt	          d¦  «        ‚t          |                      ¦   «         ¦  «        ¯O| j        �| j        S |                      ¦   «                              dd¦  «        S )z¨
        Returns the output of the job request.

        Args:
            timeout: The number of seconds to wait for the server to send data before giving up.
        r   r   zJob timed out.NrQ   )r   rM   ÚtimeÚsleepÚTimeoutErrorrL   rT   r:   ©r   r)   s     r"   rQ   z
Job.output‰   sŸ   € ð �QŠ;ˆ;Ý" 4§;¢;¡=¤=Ñ1Ô1ð 9Ý”
˜1‘”�Ø˜1‘�Ø˜a’<�<Ý&Ð'7Ñ8Ô8Ð8õ	 # 4§;¢;¡=¤=Ñ1Ô1ð 9ð Œ?Ð&Ø”?Ð"à�ŠÑ Ô ×$Ò$ X¨tÑ4Ô4Ð4r$   c              #   ó  K  — 	 t          j        d¦  «         |                      d¬¦  «        }|d         t          vst	          |d         ¦  «        dk    r$|                     dg ¦  «        D ]}|d         V — Œn|d         t          v rdS Œˆ)	z>Returns a generator that yields the output of the job request.Tr   Ústream)rN   rM   r   rQ   N)rW   rX   rT   r	   Úlenr:   )r   Ústream_partialÚchunks      r"   r\   z
Job.streamœ   s¤   è è € ð
	ÝŒJ�q‰MŒMˆMØ!Ÿ_š_°H˜_Ñ=Ô=ˆNà˜xÔ(µÐ<Ð<Ý�~ hÔ/Ñ0Ô0°1Ò4Ð4à+×/Ò/°¸"Ñ=Ô=ð *ð *�EØ œ/Ð)Ð)Ð)Ð)ð*à Ô)­\Ð9Ð9Ø�ð
	r$   é   c                 óZ   — | j                              | j        › d| j        › �d|¬¦  «        S )zÃ
        Cancels the job and returns the result of the cancellation request.

        Args:
            timeout: The number of seconds to wait for the server to respond before giving up.
        z/cancel/N©r(   r)   )rJ   r6   rF   rG   rZ   s     r"   Úcancelz
Job.cancelª   s?   € ð Œ~×"Ò"ØÔÐ6Ð6¨¬Ð6Ð6ØØð #ñ 
ô 
ð 	
r$   N)rM   )r   ©r`   )r<   r=   r>   r?   r@   r   r#   r   r   rT   rM   rB   rQ   r\   rc   rC   r$   r"   rE   rE   d   sê   € € € € € Ø;Ð;ð Cð °ð ¸lð ð ð ð ð 	ð 	 ð 	°D¸¸c¸´Nð 	ð 	ð 	ð 	ð+ð +ð +ð5ð 5˜cð 5¨#ð 5ð 5ð 5ð 5ð&˜ð ð ð ð ð
ð 
˜cð 
¨#ð 
ð 
ð 
ð 
ð 
ð 
r$   rE   c            	       óÌ   — e Zd ZdZddedee         fd„Zdeeef         de	fd„Z
	 ddeeef         d
edeeef         fd„Zdd
edeeef         fd„Zdd
edeeef         fd„ZdS )ÚEndpointz6Manages an endpoint to run jobs on the Runpod service.NrF   r   c                 ó>   — || _         t          |¬¦  «        | _        dS )aÛ  
        Initialize an Endpoint instance with the given endpoint ID.

        Args:
            endpoint_id: The identifier for the endpoint.
            api_key: Optional API key for this endpoint instance.

        Example:
            >>> endpoint = runpod.Endpoint("ENDPOINT_ID")
            >>> run_request = endpoint.run({"your_model_input_key": "your_model_input_value"})
            >>> print(run_request.status())
            >>> print(run_request.output())
        )r   N)rF   r   rJ   )r   rF   r   s      r"   r#   zEndpoint.__init__¾   s"   € ð 'ˆÔÝ%¨gÐ6Ñ6Ô6ˆŒˆˆr$   Úrequest_inputrO   c                 ó¼   — |                      d¦  «        sd|i}| j                             | j        › d�|¦  «        }t	          | j        |d         | j        ¦  «        S )zÉ
        Run the endpoint with the given input.

        Args:
            request_input: The input to pass into the endpoint.

        Returns:
            A Job instance for the run request.
        Úinputz/runÚid)r:   rJ   r6   rF   rE   )r   rh   Újob_requests      r"   ÚrunzEndpoint.runÏ   sk   € ð × Ò  Ñ)Ô)ð 	5Ø$ mÐ4ˆMà”n×)Ò)ØÔÐ%Ð%Ð%Øñ
ô 
ˆõ �4Ô# [°Ô%6¸¼ÑGÔGÐGr$   é€Q r)   c                 ó2  — |                      d¦  «        sd|i}| j                             | j        › d�||¬¦  «        }|d         t          v r|                      dd¦  «        S t          | j        |d         | j        ¦  «                             |¬¦  «        S )zØ
        Run the endpoint with the given input synchronously.

        Args:
            request_input: The input to pass into the endpoint.
            timeout: Maximum time to wait for the job to complete.
        rj   z/runsyncr9   rM   rQ   Nrk   )r:   rJ   r6   rF   r	   rE   rQ   )r   rh   r)   rl   s       r"   Úrun_synczEndpoint.run_syncâ   s­   € ð × Ò  Ñ)Ô)ð 	5Ø$ mÐ4ˆMà”n×)Ò)ØÔÐ)Ð)Ð)ØØð *ñ 
ô 
ˆð �xÔ ¥LÐ0Ð0Ø—?’? 8¨TÑ2Ô2Ð2åØÔ˜k¨$Ô/°´ñ
ô 
ç
Š&˜ˆ&Ñ
!Ô
!ð	"r$   r`   c                 óJ   — | j                              | j        › d�|¬¦  «        S )zÕ
        Check the health of the endpoint (number/state of workers, number/state of requests).

        Args:
            timeout: The number of seconds to wait for the server to respond before giving up.
        z/healthr9   )rJ   r:   rF   rZ   s     r"   ÚhealthzEndpoint.healthü   s5   € ð Œ~×!Ò!ØÔÐ(Ð(Ð(Øð "ñ 
ô 
ð 	
r$   c                 óL   — | j                              | j        › d�d|¬¦  «        S )zÌ
        Purges the endpoint's job queue and returns the result of the purge request.

        Args:
            timeout: The number of seconds to wait for the server to respond before giving up.
        z/purge-queueNrb   )rJ   r6   rF   rZ   s     r"   Úpurge_queuezEndpoint.purge_queue  s8   € ð Œ~×"Ò"ØÔÐ-Ð-Ð-ØØð #ñ 
ô 
ð 	
r$   r;   )rn   rd   )r<   r=   r>   r?   r@   r   r#   r   r   rE   rm   rB   rp   rr   rt   rC   r$   r"   rf   rf   »   s  € € € € € Ø@Ð@ð7ð 7 Cð 7°(¸3´-ð 7ð 7ð 7ð 7ð"H  c¨3 h¤ð H°Cð Hð Hð Hð Hð( =Bð"ð "Ø! # s (œ^ð"Ø69ð"à	ˆc�3ˆhŒð"ð "ð "ð "ð4

ð 

˜cð 

¨$¨s°C¨x¬.ð 

ð 

ð 

ð 

ð
ð 
 3ð 
¨t°C¸°H¬~ð 
ð 
ð 
ð 
ð 
ð 
r$   rf   )r?   rW   Útypingr   r   r   r   Úrequests.adaptersr   r   Úrunpod.endpoint.helpersr   r	   r
   r   r   rE   rf   rC   r$   r"   ú<module>rx      s3  ððð ð €€€Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à €€€Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0ðð ð ð ð ð ð ð ð ð ð ð ðH?ð H?ð H?ð H?ð H?ñ H?ô H?ð H?ð\Q
ð Q
ð Q
ð Q
ð Q
ñ Q
ô Q
ð Q
ðnX
ð X
ð X
ð X
ð X
ñ X
ô X
ð X
ð X
ð X
r$   