o
    ٷi                     @  s   d Z ddlmZ ddlZddl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 dd	lmZmZ dd
lmZ ddlmZ e
rLddlmZ e G dd deZdZdS )zAPredefined button to sign in with Hugging Face in a Gradio Space.    )annotationsN)Sequence)Path)TYPE_CHECKINGLiteral)document)utils)Button	Component)get_blocks_context)Request)Timerc                      sf   e Zd ZdZdZ		d0ddddeddddddddd	ddd
d1 fd'd(Zd)d* Zd2d.d/Z	  Z
S )3LoginButtona  
    Creates a "Sign In" button that redirects the user to sign in with Hugging Face OAuth.
    Once the user is signed in, the button will act as a logout button, and you can
    retrieve a signed-in user's profile by adding a parameter of type `gr.OAuthProfile`
    to any Gradio function. This will only work if this Gradio app is running in a
    Hugging Face Space. Permissions for the OAuth app can be configured in the Spaces
    README file, as described here: https://huggingface.co/docs/hub/en/spaces-oauth.
    For local development, instead of OAuth, the local Hugging Face account that is
    logged in (via `hf auth login`) will be available through the `gr.OAuthProfile`
    object.

    Demos: login_with_huggingface
    Guides: sharing-your-app
    TSign in with Hugging FaceLogout ({})Nhuggingfacelgzhuggingface-logo.svgvalueeveryinputsvariantsizeiconlinkvisibleinteractiveelem_idelem_classesrenderkeypreserved_by_keyscale	min_widthstrlogout_valuer   Timer | float | Noner   7Component | Sequence[Component] | set[Component] | Noner   6Literal['primary', 'secondary', 'stop', 'huggingface']r   Literal['sm', 'md', 'lg']r   str | Path | Noner   
str | Noner   bool | Literal['hidden']r   boolr   r   list[str] | str | Noner   r    (int | str | tuple[int | str, ...] | Noner!   r"   
int | Noner#   c                  sL   || _ t j|f|||||||	|
|||||||d t r$|   dS dS )z
        Parameters:
            logout_value: The text to display when the user is signed in. The string should contain a placeholder for the username with a call-to-action to logout, e.g. "Logout ({})".
        r   N)r%   super__init__r   activate)selfr   r%   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   	__class__ R/home/ubuntu/.local/lib/python3.10/site-packages/gradio/components/login_button.pyr2   )   s.   zLoginButton.__init__c                 C  sD   t dt| jd| j}| jd | gd |d | | jd  d S )NBUTTON_DEFAULT_VALUEREDIRECT_URL)fnr   outputsjs)	_js_handle_redirectreplacejsondumpsr   pageclickattach_load_event_check_login_status)r4   _jsr7   r7   r8   r3   X   s   
zLoginButton.activaterequestr   returnc                 C  s   t |dd pt |jdd }|d u sd|vrt| jddS |d }|d}|d ur<|t k r<|dd  t| jddS |d d }t| j|ddS )Nsession
oauth_infoT)r   
expires_atuserinfopreferred_username)	getattrrG   r   r   gettimepopr%   format)r4   rG   rI   rJ   rK   usernamer7   r7   r8   rE   c   s   
zLoginButton._check_login_status)r   r   )"r   r$   r%   r$   r   r&   r   r'   r   r(   r   r)   r   r*   r   r+   r   r,   r   r-   r   r+   r   r.   r   r-   r    r/   r!   r.   r"   r0   r#   r0   )rG   r   rH   r   )__name__
__module____qualname____doc__is_templater   get_icon_pathr2   r3   rE   __classcell__r7   r7   r5   r8   r      s0    /r   aN  
(buttonValue) => {
    uri = buttonValue === BUTTON_DEFAULT_VALUE ? '/login/huggingface?_target_url=/REDIRECT_URL' : '/logout?_target_url=/REDIRECT_URL';
    window.parent?.postMessage({ type: "SET_SCROLLING", enabled: true }, "*");
    setTimeout(() => {
        window.location.assign(uri + window.location.search);
    }, 500);
}
)rW   
__future__r   r@   rP   collections.abcr   pathlibr   typingr   r   gradio_client.documentationr   gradior   gradio.componentsr	   r
   gradio.contextr   gradio.routesr   r   r   r>   r7   r7   r7   r8   <module>   s"    e