§
    }Štj—=  ã                   ó¨  — U d Z ddl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
mZ ddlmZmZmZ e	r
ddlZddlmZ  e¦   «         rdd	lmZmZ dd
lmZ ndxZxZZdaed         ed<   ej                             d¦  «        du Ze G d„ d¦  «        ¦   «         Zeddedz  defd„¦   «         Zdefd„Zdedz  ddfd„Z dededefd„Z!dededefd„Z"dS )zSContains `WebhooksServer` and `webhook_endpoint` to create a webhook server easily.é    N)ÚCallable)Úwraps)ÚTYPE_CHECKINGÚAnyÚOptionalé   )ÚexperimentalÚis_fastapi_availableÚis_gradio_available)ÚRequest)ÚFastAPIr   )ÚJSONResponseÚWebhooksServerÚ_global_appÚSPACE_IDc                   ó†   ‡ — e Zd ZdZdˆ fd„Z	 	 dded         dedz  ddfd„Zdd	edz  defd
„Z	dde
deddfd„Zdd„Zˆ xZS )r   a  
    The [`WebhooksServer`] class lets you create an instance of a Gradio app that can receive Huggingface webhooks.
    These webhooks can be registered using the [`~WebhooksServer.add_webhook`] decorator. Webhook endpoints are added to
    the app as a POST endpoint to the FastAPI router. Once all the webhooks are registered, the `launch` method has to be
    called to start the app.

    It is recommended to accept [`WebhookPayload`] as the first argument of the webhook function. It is a Pydantic
    model that contains all the information about the webhook event. The data will be parsed automatically for you.

    Check out the [webhooks guide](../guides/webhooks_server) for a step-by-step tutorial on how to set up your
    WebhooksServer and deploy it on a Space.

    > [!WARNING]
    > `WebhooksServer` is experimental. Its API is subject to change in the future.

    > [!WARNING]
    > You must have `gradio` installed to use `WebhooksServer` (`pip install --upgrade gradio`).

    Args:
        ui (`gradio.Blocks`, optional):
            A Gradio UI instance to be used as the Space landing page. If `None`, a UI displaying instructions
            about the configured webhooks is created.
        webhook_secret (`str`, optional):
            A secret key to verify incoming webhook requests. You can set this value to any secret you want as long as
            you also configure it in your [webhooks settings panel](https://huggingface.co/settings/webhooks). You
            can also set this value as the `WEBHOOK_SECRET` environment variable. If no secret is provided, the
            webhook endpoints are opened without any security.

    Example:

        ```python
        import gradio as gr
        from huggingface_hub import WebhooksServer, WebhookPayload

        with gr.Blocks() as ui:
            ...

        app = WebhooksServer(ui=ui, webhook_secret="my_secret_key")

        @app.add_webhook("/say_hello")
        async def hello(payload: WebhookPayload):
            return {"message": "hello"}

        app.launch()
        ```
    Úreturnc                 óº   •— t          ¦   «         st          d¦  «        ‚t          ¦   «         st          d¦  «        ‚t          ¦   «                              | ¦  «        S )NzjYou must have `gradio` installed to use `WebhooksServer`. Please run `pip install --upgrade gradio` first.zlYou must have `fastapi` installed to use `WebhooksServer`. Please run `pip install --upgrade fastapi` first.)r   ÚImportErrorr
   ÚsuperÚ__new__)ÚclsÚargsÚkwargsÚ	__class__s      €ú^/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/huggingface_hub/_webhooks_server.pyr   zWebhooksServer.__new__[   sg   ø€ Ý"Ñ$Ô$ð 	Ýðñô ð õ $Ñ%Ô%ð 	Ýðñô ð õ ‰wŒw�Š˜sÑ#Ô#Ð#ó    NÚuiú	gr.BlocksÚwebhook_secretc                 ó€   — || _         |pt          j        d¦  «        | _        i | _        t          | j        ¦  «         d S )NÚWEBHOOK_SECRET)Ú_uiÚosÚgetenvr    Úregistered_webhooksÚ_warn_on_empty_secret)Úselfr   r    s      r   Ú__init__zWebhooksServer.__init__h   sB   € ð
 ˆŒà,ÐKµ´	Ð:JÑ0KÔ0KˆÔØ8:ˆÔ Ý˜dÔ1Ñ2Ô2Ð2Ð2Ð2r   Úpathc                 ó¬   ‡ ‡— t          ‰¦  «        r ‰                      ¦   «         ‰¦  «        S t          t          j        ¦  «        ˆˆ fd„¦   «         }|S )ax  
        Decorator to add a webhook to the [`WebhooksServer`] server.

        Args:
            path (`str`, optional):
                The URL path to register the webhook function. If not provided, the function name will be used as the
                path. In any case, all webhooks are registered under `/webhooks`.

        Raises:
            ValueError: If the provided path is already registered as a webhook.

        Example:
            ```python
            from huggingface_hub import WebhooksServer, WebhookPayload

            app = WebhooksServer()

            @app.add_webhook
            async def trigger_training(payload: WebhookPayload):
                if payload.repo.type == "dataset" and payload.event.action == "update":
                    # Trigger a training job if a dataset is updated
                    ...

            app.launch()
        ```
        c                  ó¢   •— | d         }d‰p|j                              d¦  «        › �}|‰j        v rt          d|› d�¦  «        ‚|‰j        |<   d S )Nr   z
/webhooks/ú/zWebhook z already exists.)Ú__name__Ústripr&   Ú
ValueError)r   r   ÚfuncÚabs_pathr*   r(   s       €€r   Ú_inner_postz/WebhooksServer.add_webhook.<locals>._inner_post”   si   ø€ à˜”7ˆDØH TÐ%:¨T¬]×$AÒ$AÀ#Ñ$FÔ$FÐHÐHˆHØ˜4Ô3Ð3Ð3Ý Ð!F¨HÐ!FÐ!FÐ!FÑGÔGÐGØ15ˆDÔ$ XÑ.Ð.Ð.r   )ÚcallableÚadd_webhookr   r   Úpost)r(   r*   r3   s   `` r   r5   zWebhooksServer.add_webhooks   sm   øø€ õ8 �D‰>Œ>ð 	,à%�4×#Ò#Ñ%Ô% dÑ+Ô+Ð+õ 
�wŒ|Ñ	Ô	ð	6ð 	6ð 	6ð 	6ð 	6ñ 
Ô	ð	6ð Ðr   FÚprevent_thread_lockÚlaunch_kwargsc                 óÊ  ‡	— | j         p|                      ¦   «         }|                     dt          ¦  «          |j        dddi|¤Ž\  | _        }}| j                             ¦   «         D ]E\  }}| j        �t          || j        ¬¦  «        } | j         
                    |¦  «        |¦  «         ŒFt          j                             d¦  «        }|�d|z   n|j        p|j        Š	‰	€t!          d¦  «        ‚‰	                     d	¦  «        Š	d
}|dd                     ˆ	fd„| j        D ¦   «         ¦  «        z   z  }|dz  }t'          |¦  «         |s|                     ¦   «          dS dS )z Launch the Gradio app and register webhooks to the underlying FastAPI server.

        Input parameters are forwarded to Gradio when launching the app.
        Úsharer7   TN©r    Ú
SPACE_HOSTzhttps://zWCannot find the URL of the app. Please provide a valid `ui` or update `gradio` version.r-   z/
Webhooks are correctly setup and ready to use:ú
c              3   ó&   •K  — | ]}d ‰› |› �V — ŒdS )z	  - POST N© )Ú.0ÚwebhookÚurls     €r   ú	<genexpr>z(WebhooksServer.launch.<locals>.<genexpr>»   s1   øè è € Ð#gÐ#gÀ7Ð$>°Ð$>°WÐ$>Ð$>Ð#gÐ#gÐ#gÐ#gÐ#gÐ#gr   zG
Go to https://huggingface.co/settings/webhooks to setup your webhooks.r?   )r#   Ú_get_default_uiÚ
setdefaultÚ	_is_localÚlaunchÚfastapi_appr&   Úitemsr    Ú_wrap_webhook_to_check_secretr6   r$   ÚenvironÚgetÚ	share_urlÚ	local_urlr0   r/   ÚjoinÚprintÚblock_thread)
r(   r7   r8   r   Ú_r*   r1   Ú
space_hostÚmessagerB   s
            @r   rG   zWebhooksServer.launchž   s�  ø€ ð
 ŒXÐ/˜×-Ò-Ñ/Ô/ˆð
 	× Ò  ­)Ñ4Ô4Ð4Ø!* ¤Ð!UÐ!U¸tÐ!UÀ}Ð!UÐ!UÑˆÔ˜!˜Qð Ô2×8Ò8Ñ:Ô:ð 	.ð 	.‰JˆD�$àÔ"Ð.Ý4°TÈ$ÔJ]Ð^Ñ^Ô^�ð (ˆDÔ×!Ò! $Ñ'Ô'¨Ñ-Ô-Ð-Ð-õ ”Z—^’^ LÑ1Ô1ˆ
Ø)3Ð)?ˆj˜:Ñ%Ð%ÀbÄlÐFbÐVXÔVbˆØˆ;ÝÐvÑwÔwÐwØ�iŠi˜‰nŒnˆØDˆØ�4˜$Ÿ)š)Ð#gÐ#gÐ#gÐ#gÈdÔNfÐ#gÑ#gÔ#gÑgÔgÑgÑgˆØÐ]Ñ]ˆÝˆg‰Œˆà"ð 	Ø�OŠOÑÔÐÐÐð	ð 	r   c           	      óŒ  — ddl } |j        ¦   «         5 } |j        d¦  «          |j        d¦  «          |j        t          | j        ¦  «        › d�dz   d                     d„ | j                             ¦   «         D ¦   «         ¦  «        z   ¦  «          |j        t          rd	nd
¦  «         ddd¦  «         n# 1 swxY w Y   |S )zLDefault UI if not provided (lists webhooks and provides basic instructions).r   Nu)   # This is an app to process ðŸ¤— WebhooksaT  Webhooks are a foundation for MLOps-related features. They allow you to listen for new changes on specific repos or to all repos belonging to particular set of users/organizations (not just your repos, but any repo). Check out this [guide](https://huggingface.co/docs/hub/webhooks) to get to know more about webhooks on the Huggingface Hub.z webhook(s) are registered:z

z
 c              3   óT   K  — | ]#\  }}d |› dt          |j        |¦  «        › d�V — Œ$dS )z- [z](ú)N)Ú_get_webhook_doc_urlr.   )r@   Úwebhook_pathrA   s      r   rC   z1WebhooksServer._get_default_ui.<locals>.<genexpr>Ñ   s]   è è € ð ð á-˜ gð b˜,ÐaÐaÕ*>¸wÔ?OÐQ]Ñ*^Ô*^ÐaÐaÐaðð ð ð ð ð r   z¡Go to https://huggingface.co/settings/webhooks to setup your webhooks.
You app is running locally. Please look at the logs to check the full URL you need to set.z¶
This app is running on a Space. You can find the corresponding URL in the options menu (top-right) > 'Embed the Space'. The URL looks like 'https://{username}-{repo_name}.hf.space'.)ÚgradioÚBlocksÚMarkdownÚlenr&   rO   rI   rF   )r(   Úgrr   s      r   rD   zWebhooksServer._get_default_uiÂ   sP  € àÐÐÐàˆRŒY‰[Œ[ð 	˜BØˆBŒKÐCÑDÔDÐDØˆBŒKðDñô ð ð ˆBŒKÝ�tÔ/Ñ0Ô0ÐMÐMÐMØñà—*’*ð ð à15Ô1I×1OÒ1OÑ1QÔ1Qðñ ô ñ ô ññô ð ð ˆBŒKõ ðð qð qðvñô ð ð!	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð2 ˆ	s   ”BB9Â9B=Ã B=)r   r   )NN©N)F)r   r   )r.   Ú
__module__Ú__qualname__Ú__doc__r   r   Ústrr)   r   r5   Úboolr   rG   rD   Ú__classcell__)r   s   @r   r   r   *   sþ   ø€ € € € € ð-ð -ð^$ð $ð $ð $ð $ð $ð %)Ø%)ð	3ð 	3à�[Ô!ð	3ð ˜d™
ð	3ð 
ð		3ð 	3ð 	3ð 	3ð)ð )  d¡
ð )°hð )ð )ð )ð )ðV"ð "¨$ð "Èð "ÐQUð "ð "ð "ð "ðHð ð ð ð ð ð ð r   r*   r   c                 óº   ‡ — t          ‰ ¦  «        r t          ¦   «         ‰ ¦  «        S t          t          j        ¦  «        dt
          dt
          fˆ fd„¦   «         }|S )a§  Decorator to start a [`WebhooksServer`] and register the decorated function as a webhook endpoint.

    This is a helper to get started quickly. If you need more flexibility (custom landing page or webhook secret),
    you can use [`WebhooksServer`] directly. You can register multiple webhook endpoints (to the same server) by using
    this decorator multiple times.

    Check out the [webhooks guide](../guides/webhooks_server) for a step-by-step tutorial on how to set up your
    server and deploy it on a Space.

    > [!WARNING]
    > `webhook_endpoint` is experimental. Its API is subject to change in the future.

    > [!WARNING]
    > You must have `gradio` installed to use `webhook_endpoint` (`pip install --upgrade gradio`).

    Args:
        path (`str`, optional):
            The URL path to register the webhook function. If not provided, the function name will be used as the path.
            In any case, all webhooks are registered under `/webhooks`.

    Examples:
        The default usage is to register a function as a webhook endpoint. The function name will be used as the path.
        The server will be started automatically at exit (i.e. at the end of the script).

        ```python
        from huggingface_hub import webhook_endpoint, WebhookPayload

        @webhook_endpoint
        async def trigger_training(payload: WebhookPayload):
            if payload.repo.type == "dataset" and payload.event.action == "update":
                # Trigger a training job if a dataset is updated
                ...

        # Server is automatically started at the end of the script.
        ```

        Advanced usage: register a function as a webhook endpoint and start the server manually. This is useful if you
        are running it in a notebook.

        ```python
        from huggingface_hub import webhook_endpoint, WebhookPayload

        @webhook_endpoint
        async def trigger_training(payload: WebhookPayload):
            if payload.repo.type == "dataset" and payload.event.action == "update":
                # Trigger a training job if a dataset is updated
                ...

        # Start the server manually
        trigger_training.launch()
        ```
    r1   r   c                 ó  •‡— t          ¦   «         Š ‰                     ‰¦  «        | ¦  «         t          ‰j        ¦  «        dk    rt	          j        ‰j        ¦  «         t          ‰j        ¦  «        ˆfd„¦   «         }|| _        | S )Nr   c                  ób   •— t          j        ‰ j        ¦  «         ‰                      ¦   «          d S r_   )ÚatexitÚ
unregisterrG   )Úapps   €r   Ú_launch_nowz5webhook_endpoint.<locals>._inner.<locals>._launch_now$  s)   ø€ õ Ô˜cœjÑ)Ô)Ð)Ø�JŠJ‰LŒLˆLˆLˆLr   )Ú_get_global_appr5   r]   r&   ri   ÚregisterrG   r   )r1   rl   rk   r*   s     @€r   Ú_innerz webhook_endpoint.<locals>._inner  s�   øø€ åÑÔˆØˆ�Š˜ÑÔ˜dÑ#Ô#Ð#ÝˆsÔ&Ñ'Ô'¨1Ò,Ð,åŒO˜CœJÑ'Ô'Ð'å	ˆsŒzÑ	Ô	ð	ð 	ð 	ð 	ñ 
Ô	ð	ð
 "ˆŒØˆr   )r4   Úwebhook_endpointr   r   r5   r   )r*   ro   s   ` r   rp   rp   â   st   ø€ õl ��~„~ð (à!ÕÑ!Ô! $Ñ'Ô'Ð'å
�>Ô%Ñ&Ô&ð•Xð ¥(ð ð ð ð ð ñ 'Ô&ðð  €Mr   c                  ó:   — t           €t          ¦   «         a t           S r_   )r   r   r?   r   r   rm   rm   0  s   € åÐÝ$Ñ&Ô&ˆÝÐr   r    c                 ó†   — | €/t          d¦  «         t          d¦  «         t          d¦  «         d S t          d¦  «         d S )NzZWebhook secret is not defined. This means your webhook endpoints will be open to everyone.z•To add a secret, set `WEBHOOK_SECRET` as environment variable or pass it at initialization: 
	`app = WebhooksServer(webhook_secret='my_secret', ...)`zpFor more details about webhook secrets, please refer to https://huggingface.co/docs/hub/webhooks#webhook-secret.z$Webhook secret is correctly defined.)rP   r;   s    r   r'   r'   7  sj   € ØÐÝÐjÑkÔkÐkÝðJñ	
ô 	
ð 	
õ 	ðHñ	
ô 	
ð 	
ð 	
ð 	
õ
 	Ð4Ñ5Ô5Ð5Ð5Ð5r   Úwebhook_namerY   c                 ó@   — d| z   |                      dd¦  «        z   dz   S )z@Returns the anchor to a given webhook in the docs (experimental)z/docs#/default/r-   rR   Ú_post)Úreplace)rs   rY   s     r   rX   rX   F  s'   € à˜|Ñ+¨l×.BÒ.BÀ3ÈÑ.LÔ.LÑLÈwÑVÐVr   r1   c                 óf  ‡ ‡‡— t          j        ‰ ¦  «        Št          ‰ ¦  «        dt          fˆ ˆˆfd„¦   «         }d‰j        vrl‰                     t          j        dt           j        j        t          ¬¦  «        ft          ‰j         	                    ¦   «         ¦  «        z   ¬¦  «        |_
        |S )a²  Wraps a webhook function to check the webhook secret before calling the function.

    This is a hacky way to add the `request` parameter to the function signature. Since FastAPI based itself on route
    parameters to inject the values to the function, we need to hack the function signature to retrieve the `Request`
    object (and hence the headers). A far cleaner solution would be to use a middleware. However, since
    `fastapi==0.90.1`, a middleware cannot be added once the app has started. And since the FastAPI app is started by
    Gradio internals (and not by us), we cannot add a middleware.

    This method is called only when a secret has been defined by the user. If a request is sent without the
    "x-webhook-secret", the function will return a 401 error (unauthorized). If the header is sent but is incorrect,
    the function will return a 403 error (forbidden).

    Inspired by https://stackoverflow.com/a/33112180.
    Úrequestc              ›   ó  •K  — | j                              d¦  «        }|€t          ddid¬¦  «        S |‰k    rt          ddid¬¦  «        S d‰j        v r| |d<   t	          j        ‰¦  «        r ‰d	i |¤Žƒ d {V —†S  ‰d	i |¤ŽS )
Nzx-webhook-secretÚerrorz x-webhook-secret header not set.i‘  )Ústatus_codezInvalid webhook secret.i“  rx   r?   )ÚheadersrL   r   Ú
parametersÚinspectÚiscoroutinefunction)rx   r   Úrequest_secretr1   Úinitial_sigr    s      €€€r   Ú_protected_funcz6_wrap_webhook_to_check_secret.<locals>._protected_func\  sÇ   øè è € à œ×,Ò,Ð-?Ñ@Ô@ˆØÐ!Ý Ð*LÐ MÐ[^Ð_Ñ_Ô_Ð_Ø˜^Ò+Ð+Ý Ð*CÐ DÐRUÐVÑVÔVÐVð ˜Ô.Ð.Ð.Ø 'ˆF�9Ñõ Ô& tÑ,Ô,ð 	"Ø˜˜˜ ˜˜Ð'Ð'Ð'Ð'Ð'Ð'Ð'à�4�>�>˜&�>�>Ð!r   )ÚnameÚkindÚ
annotation)r}   )r~   Ú	signaturer   r   r}   rv   Ú	ParameterÚPOSITIONAL_OR_KEYWORDÚtupleÚvaluesÚ__signature__)r1   r    r‚   r�   s   `` @r   rJ   rJ   K  sÍ   øøø€ õ Ô# DÑ)Ô)€Kå
ˆ4�[„[ð"¥wð "ð "ð "ð "ð "ð "ð "ñ „[ð"ð$ ˜Ô.Ð.Ð.Ø(3×(;Ò(;åÔ! yµwÔ7HÔ7^ÕkrÐsÑsÔsðõ �KÔ*×1Ò1Ñ3Ô3Ñ4Ô4ñ5ð )<ñ )
ô )
ˆÔ%ð Ðr   r_   )#rb   ri   r~   r$   Úcollections.abcr   Ú	functoolsr   Útypingr   r   r   Úutilsr	   r
   r   rZ   r^   Úfastapir   r   Úfastapi.responsesr   r   Ú__annotations__rK   rL   rF   r   rc   rp   rm   r'   rX   rJ   r?   r   r   ú<module>r“      sS  ðð ZÐ YÐ Yà €€€Ø €€€Ø 	€	€	€	Ø $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /à JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ Jð ð  ØÐÐÐØÐÐÐÐÐàÐÑÔð ,Ø(Ð(Ð(Ð(Ð(Ð(Ð(Ð(Ø.Ð.Ð.Ð.Ð.Ð.Ð.ð (,Ð+€GÐ+ˆg˜ð +/€ˆXÐ&Ô'Ð .Ð .Ñ .ØŒJ�NŠN˜:Ñ&Ô&¨$Ð.€	ð ðtð tð tð tð tñ tô tñ „ðtðn ðJð J˜3 ™:ð J°ð Jð Jð Jñ „ðJðZ˜ð ð ð ð ð6¨#°©*ð 6¸ð 6ð 6ð 6ð 6ðW sð W¸#ð WÀ#ð Wð Wð Wð Wð
-¨ð -À#ð -È(ð -ð -ð -ð -ð -ð -r   