Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
_telemetry.py126 linesDownload Raw Back to utils
1from queue import Queue2from threading import Lock, Thread3from urllib.parse import quote4 5from .. import constants, logging6from . import build_hf_headers, get_session, hf_raise_for_status7 8 9logger = logging.get_logger(__name__)10 11# Telemetry is sent by a separate thread to avoid blocking the main thread.12# A daemon thread is started once and consume tasks from the _TELEMETRY_QUEUE.13# If the thread stops for some reason -shouldn't happen-, we restart a new one.14_TELEMETRY_THREAD: Thread | None = None15_TELEMETRY_THREAD_LOCK = Lock()  # Lock to avoid starting multiple threads in parallel16_TELEMETRY_QUEUE: Queue = Queue()17 18 19def send_telemetry(20    topic: str,21    *,22    library_name: str | None = None,23    library_version: str | None = None,24    user_agent: dict | str | None = None,25) -> None:26    """27    Sends telemetry that helps track usage of different HF libraries.28 29    This usage data helps us debug issues and prioritize new features. However, we understand that not everyone wants30    to share additional information, and we respect your privacy. You can disable telemetry collection by setting the31    `HF_HUB_DISABLE_TELEMETRY=1` as environment variable. Telemetry is also disabled in offline mode (i.e. when setting32    `HF_HUB_OFFLINE=1`).33 34    Telemetry collection is run in a separate thread to minimize impact for the user.35 36    Args:37        topic (`str`):38            Name of the topic that is monitored. The topic is directly used to build the URL. If you want to monitor39            subtopics, just use "/" separation. Examples: "gradio", "transformers/examples",...40        library_name (`str`, *optional*):41            The name of the library that is making the HTTP request. Will be added to the user-agent header.42        library_version (`str`, *optional*):43            The version of the library that is making the HTTP request. Will be added to the user-agent header.44        user_agent (`str`, `dict`, *optional*):45            The user agent info in the form of a dictionary or a single string. It will be completed with information about the installed packages.46 47    Example:48    ```py49    >>> from huggingface_hub.utils import send_telemetry50 51    # Send telemetry without library information52    >>> send_telemetry("ping")53 54    # Send telemetry to subtopic with library information55    >>> send_telemetry("gradio/local_link", library_name="gradio", library_version="3.22.1")56 57    # Send telemetry with additional data58    >>> send_telemetry(59    ...     topic="examples",60    ...     library_name="transformers",61    ...     library_version="4.26.0",62    ...     user_agent={"pipeline": "text_classification", "framework": "flax"},63    ... )64    ```65    """66    if constants.is_offline_mode() or constants.HF_HUB_DISABLE_TELEMETRY:67        return68 69    _start_telemetry_thread()  # starts thread only if doesn't exist yet70    _TELEMETRY_QUEUE.put(71        {"topic": topic, "library_name": library_name, "library_version": library_version, "user_agent": user_agent}72    )73 74 75def _start_telemetry_thread():76    """Start a daemon thread to consume tasks from the telemetry queue.77 78    If the thread is interrupted, start a new one.79    """80    with _TELEMETRY_THREAD_LOCK:  # avoid to start multiple threads if called concurrently81        global _TELEMETRY_THREAD82        if _TELEMETRY_THREAD is None or not _TELEMETRY_THREAD.is_alive():83            _TELEMETRY_THREAD = Thread(target=_telemetry_worker, daemon=True)84            _TELEMETRY_THREAD.start()85 86 87def _telemetry_worker():88    """Wait for a task and consume it."""89    while True:90        kwargs = _TELEMETRY_QUEUE.get()91        _send_telemetry_in_thread(**kwargs)92        _TELEMETRY_QUEUE.task_done()93 94 95def _send_telemetry_in_thread(96    topic: str,97    *,98    library_name: str | None = None,99    library_version: str | None = None,100    user_agent: dict | str | None = None,101) -> None:102    """Contains the actual data sending data to the Hub.103 104    This function is called directly in gradio's analytics because105    it is not possible to send telemetry from a daemon thread.106 107    See here: https://github.com/gradio-app/gradio/pull/8180108 109    Please do not rename or remove this function.110    """111    path = "/".join(quote(part) for part in topic.split("/") if len(part) > 0)112    try:113        r = get_session().head(114            f"{constants.ENDPOINT}/api/telemetry/{path}",115            headers=build_hf_headers(116                token=False,  # no need to send a token for telemetry117                library_name=library_name,118                library_version=library_version,119                user_agent=user_agent,120            ),121        )122        hf_raise_for_status(r)123    except Exception as e:124        # We don't want to error in case of connection errors of any kind.125        logger.debug(f"Error while sending telemetry: {e}")126 
codekingpro/portable-devtools · Team Ai