
    g                     L   U d dl mZ d dlmZmZ d dlmZmZmZ d dl	m
Z
 ddlmZmZ ddlmZmZmZ  ej"                  e      Zd	aee   ed
<    e       Z e       Zeed<   d	d	d	ddedee   dee   deeed	f   dd	f
dZd Zd Zd	d	d	ddedee   dee   deeed	f   dd	f
dZy	)    )Queue)LockThread)DictOptionalUnion)quote   )	constantslogging   )build_hf_headersget_sessionhf_raise_for_statusN_TELEMETRY_THREAD_TELEMETRY_QUEUE)library_namelibrary_version
user_agenttopicr   r   r   returnc                    t         j                  st         j                  ryt                t        j                  | |||d       y)a_  
    Sends telemetry that helps tracking usage of different HF libraries.

    This usage data helps us debug issues and prioritize new features. However, we understand that not everyone wants
    to share additional information, and we respect your privacy. You can disable telemetry collection by setting the
    `HF_HUB_DISABLE_TELEMETRY=1` as environment variable. Telemetry is also disabled in offline mode (i.e. when setting
    `HF_HUB_OFFLINE=1`).

    Telemetry collection is run in a separate thread to minimize impact for the user.

    Args:
        topic (`str`):
            Name of the topic that is monitored. The topic is directly used to build the URL. If you want to monitor
            subtopics, just use "/" separation. Examples: "gradio", "transformers/examples",...
        library_name (`str`, *optional*):
            The name of the library that is making the HTTP request. Will be added to the user-agent header.
        library_version (`str`, *optional*):
            The version of the library that is making the HTTP request. Will be added to the user-agent header.
        user_agent (`str`, `dict`, *optional*):
            The user agent info in the form of a dictionary or a single string. It will be completed with information about the installed packages.

    Example:
    ```py
    >>> from huggingface_hub.utils import send_telemetry

    # Send telemetry without library information
    >>> send_telemetry("ping")

    # Send telemetry to subtopic with library information
    >>> send_telemetry("gradio/local_link", library_name="gradio", library_version="3.22.1")

    # Send telemetry with additional data
    >>> send_telemetry(
    ...     topic="examples",
    ...     library_name="transformers",
    ...     library_version="4.26.0",
    ...     user_agent={"pipeline": "text_classification", "framework": "flax"},
    ... )
    ```
    Nr   r   r   r   )r   HF_HUB_OFFLINEHF_HUB_DISABLE_TELEMETRY_start_telemetry_threadr   putr   s       U/var/www/openai/venv/lib/python3.12/site-packages/huggingface_hub/utils/_telemetry.pysend_telemetryr      s:    ^ 9#E#E/ist    c                      t         5  t        t        j                         s%t        t        d      at        j                          ddd       y# 1 sw Y   yxY w)zyStart a daemon thread to consume tasks from the telemetry queue.

    If the thread is interrupted, start a new one.
    NT)targetdaemon)_TELEMETRY_THREAD_LOCKr   is_aliver   _telemetry_workerstart r    r   r   r   L   s?    
 
 $,=,F,F,H &.? M##%	 
 		s   A AAc                  l    	 t         j                         } t        di |  t         j                          4)zWait for a task and consume it.r(   )r   get_send_telemetry_in_thread	task_done)kwargss    r   r&   r&   X   s/    
!%%'!+F+""$ r    c          	      <   dj                  d | j                  d      D              }	 t               j                  t        j
                   d| t        d|||            }t        |       y# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)	a*  Contains the actual data sending data to the Hub.

    This function is called directly in gradio's analytics because
    it is not possible to send telemetry from a daemon thread.

    See here: https://github.com/gradio-app/gradio/pull/8180

    Please do not rename or remove this function.
    /c              3   P   K   | ]  }t        |      d kD  st        |         yw)r   N)lenr	   ).0parts     r   	<genexpr>z,_send_telemetry_in_thread.<locals>.<genexpr>p   s!     N,<DD	AE$K,<s   &&z/api/telemetry/F)tokenr   r   r   )headerszError while sending telemetry: N)joinsplitr   headr   ENDPOINTr   r   	Exceptionloggerdebug)r   r   r   r   pathres          r   r+   r+   `   s      88NEKK,<NND<M!!"/$8$) /%	  
 	A <6qc:;;<s   AA0 0	B9BB)queuer   	threadingr   r   typingr   r   r   urllib.parser	    r   r   r   r   r   
get_logger__name__r<   r   __annotations__r$   r   strr   r   r&   r+   r(   r    r   <module>rJ      s	    " ( (  ! @ @ 
		H	%
 '+ 8F# * ' % ! #'%))-55 3-5 c]	5
 dCo&5 
5p	&% #'%))-<< 3-< c]	<
 dCo&< 
<r    