File size: 5,798 Bytes
ea1e5cc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | """ Functions related to analytics and telemetry. """
from __future__ import annotations
import json
import os
import pkgutil
import threading
import warnings
from distutils.version import StrictVersion
from typing import Any
import requests
import gradio
from gradio.context import Context
from gradio.utils import GRADIO_VERSION
ANALYTICS_URL = "https://api.gradio.app/"
PKG_VERSION_URL = "https://api.gradio.app/pkg-version"
def analytics_enabled() -> bool:
"""
Returns: True if analytics are enabled, False otherwise.
"""
return os.getenv("GRADIO_ANALYTICS_ENABLED", "True") == "True"
def _do_analytics_request(url: str, data: dict[str, Any]) -> None:
try:
requests.post(url, data=data, timeout=5)
except (requests.ConnectionError, requests.exceptions.ReadTimeout):
pass # do not push analytics if no network
def version_check():
if not analytics_enabled():
return
try:
version_data = pkgutil.get_data(__name__, "version.txt")
if not version_data:
raise FileNotFoundError
current_pkg_version = version_data.decode("ascii").strip()
latest_pkg_version = requests.get(url=PKG_VERSION_URL, timeout=3).json()[
"version"
]
if StrictVersion(latest_pkg_version) > StrictVersion(current_pkg_version):
print(
f"IMPORTANT: You are using gradio version {current_pkg_version}, "
f"however version {latest_pkg_version} is available, please upgrade."
)
print("--------")
except json.decoder.JSONDecodeError:
warnings.warn("unable to parse version details from package URL.")
except KeyError:
warnings.warn("package URL does not contain version info.")
except Exception:
pass
def get_local_ip_address() -> str:
"""
Gets the public IP address or returns the string "No internet connection" if unable
to obtain it or the string "Analytics disabled" if a user has disabled analytics.
Does not make a new request if the IP address has already been obtained in the
same Python session.
"""
if not analytics_enabled():
return "Analytics disabled"
if Context.ip_address is None:
try:
ip_address = requests.get(
"https://checkip.amazonaws.com/", timeout=3
).text.strip()
except (requests.ConnectionError, requests.exceptions.ReadTimeout):
ip_address = "No internet connection"
Context.ip_address = ip_address
else:
ip_address = Context.ip_address
return ip_address
def initiated_analytics(data: dict[str, Any]) -> None:
if not analytics_enabled():
return
threading.Thread(
target=_do_analytics_request,
kwargs={
"url": f"{ANALYTICS_URL}gradio-initiated-analytics/",
"data": {**data, "ip_address": get_local_ip_address()},
},
).start()
def launched_analytics(blocks: gradio.Blocks, data: dict[str, Any]) -> None:
if not analytics_enabled():
return
blocks_telemetry, inputs_telemetry, outputs_telemetry, targets_telemetry = (
[],
[],
[],
[],
)
from gradio.blocks import BlockContext
for x in list(blocks.blocks.values()):
blocks_telemetry.append(x.get_block_name()) if isinstance(
x, BlockContext
) else blocks_telemetry.append(str(x))
for x in blocks.dependencies:
targets_telemetry = targets_telemetry + [
# Sometimes the target can be the Blocks object itself, so we need to check if its in blocks.blocks
str(blocks.blocks[y])
for y in x["targets"]
if y in blocks.blocks
]
inputs_telemetry = inputs_telemetry + [
str(blocks.blocks[y]) for y in x["inputs"]
]
outputs_telemetry = outputs_telemetry + [
str(blocks.blocks[y]) for y in x["outputs"]
]
additional_data = {
"version": GRADIO_VERSION,
"is_kaggle": blocks.is_kaggle,
"is_sagemaker": blocks.is_sagemaker,
"using_auth": blocks.auth is not None,
"dev_mode": blocks.dev_mode,
"show_api": blocks.show_api,
"show_error": blocks.show_error,
"title": blocks.title,
"inputs": blocks.input_components
if blocks.mode == "interface"
else inputs_telemetry,
"outputs": blocks.output_components
if blocks.mode == "interface"
else outputs_telemetry,
"targets": targets_telemetry,
"blocks": blocks_telemetry,
"events": [str(x["trigger"]) for x in blocks.dependencies],
}
data.update(additional_data)
data.update({"ip_address": get_local_ip_address()})
threading.Thread(
target=_do_analytics_request,
kwargs={
"url": f"{ANALYTICS_URL}gradio-launched-telemetry/",
"data": data,
},
).start()
def integration_analytics(data: dict[str, Any]) -> None:
if not analytics_enabled():
return
threading.Thread(
target=_do_analytics_request,
kwargs={
"url": f"{ANALYTICS_URL}gradio-integration-analytics/",
"data": {**data, "ip_address": get_local_ip_address()},
},
).start()
def error_analytics(message: str) -> None:
"""
Send error analytics if there is network
Parameters:
message: Details about error
"""
if not analytics_enabled():
return
data = {"ip_address": get_local_ip_address(), "error": message}
threading.Thread(
target=_do_analytics_request,
kwargs={
"url": f"{ANALYTICS_URL}gradio-error-analytics/",
"data": data,
},
).start()
|