utils.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. import os
  3. import platform
  4. import random
  5. import threading
  6. import time
  7. from pathlib import Path
  8. import requests
  9. from ultralytics.utils import (
  10. ARGV,
  11. ENVIRONMENT,
  12. IS_COLAB,
  13. IS_GIT_DIR,
  14. IS_PIP_PACKAGE,
  15. LOGGER,
  16. ONLINE,
  17. RANK,
  18. SETTINGS,
  19. TESTS_RUNNING,
  20. TQDM,
  21. TryExcept,
  22. __version__,
  23. colorstr,
  24. get_git_origin_url,
  25. )
  26. from ultralytics.utils.downloads import GITHUB_ASSETS_NAMES
  27. HUB_API_ROOT = os.environ.get("ULTRALYTICS_HUB_API", "https://api.ultralytics.com")
  28. HUB_WEB_ROOT = os.environ.get("ULTRALYTICS_HUB_WEB", "https://hub.ultralytics.com")
  29. PREFIX = colorstr("Ultralytics HUB: ")
  30. HELP_MSG = "If this issue persists please visit https://github.com/ultralytics/hub/issues for assistance."
  31. def request_with_credentials(url: str) -> any:
  32. """
  33. Make an AJAX request with cookies attached in a Google Colab environment.
  34. Args:
  35. url (str): The URL to make the request to.
  36. Returns:
  37. (any): The response data from the AJAX request.
  38. Raises:
  39. OSError: If the function is not run in a Google Colab environment.
  40. """
  41. if not IS_COLAB:
  42. raise OSError("request_with_credentials() must run in a Colab environment")
  43. from google.colab import output # noqa
  44. from IPython import display # noqa
  45. display.display(
  46. display.Javascript(
  47. f"""
  48. window._hub_tmp = new Promise((resolve, reject) => {{
  49. const timeout = setTimeout(() => reject("Failed authenticating existing browser session"), 5000)
  50. fetch("{url}", {{
  51. method: 'POST',
  52. credentials: 'include'
  53. }})
  54. .then((response) => resolve(response.json()))
  55. .then((json) => {{
  56. clearTimeout(timeout);
  57. }}).catch((err) => {{
  58. clearTimeout(timeout);
  59. reject(err);
  60. }});
  61. }});
  62. """
  63. )
  64. )
  65. return output.eval_js("_hub_tmp")
  66. def requests_with_progress(method, url, **kwargs):
  67. """
  68. Make an HTTP request using the specified method and URL, with an optional progress bar.
  69. Args:
  70. method (str): The HTTP method to use (e.g. 'GET', 'POST').
  71. url (str): The URL to send the request to.
  72. **kwargs (any): Additional keyword arguments to pass to the underlying `requests.request` function.
  73. Returns:
  74. (requests.Response): The response object from the HTTP request.
  75. Note:
  76. - If 'progress' is set to True, the progress bar will display the download progress for responses with a known
  77. content length.
  78. - If 'progress' is a number then progress bar will display assuming content length = progress.
  79. """
  80. progress = kwargs.pop("progress", False)
  81. if not progress:
  82. return requests.request(method, url, **kwargs)
  83. response = requests.request(method, url, stream=True, **kwargs)
  84. total = int(response.headers.get("content-length", 0) if isinstance(progress, bool) else progress) # total size
  85. try:
  86. pbar = TQDM(total=total, unit="B", unit_scale=True, unit_divisor=1024)
  87. for data in response.iter_content(chunk_size=1024):
  88. pbar.update(len(data))
  89. pbar.close()
  90. except requests.exceptions.ChunkedEncodingError: # avoid 'Connection broken: IncompleteRead' warnings
  91. response.close()
  92. return response
  93. def smart_request(method, url, retry=3, timeout=30, thread=True, code=-1, verbose=True, progress=False, **kwargs):
  94. """
  95. Makes an HTTP request using the 'requests' library, with exponential backoff retries up to a specified timeout.
  96. Args:
  97. method (str): The HTTP method to use for the request. Choices are 'post' and 'get'.
  98. url (str): The URL to make the request to.
  99. retry (int, optional): Number of retries to attempt before giving up. Default is 3.
  100. timeout (int, optional): Timeout in seconds after which the function will give up retrying. Default is 30.
  101. thread (bool, optional): Whether to execute the request in a separate daemon thread. Default is True.
  102. code (int, optional): An identifier for the request, used for logging purposes. Default is -1.
  103. verbose (bool, optional): A flag to determine whether to print out to console or not. Default is True.
  104. progress (bool, optional): Whether to show a progress bar during the request. Default is False.
  105. **kwargs (any): Keyword arguments to be passed to the requests function specified in method.
  106. Returns:
  107. (requests.Response): The HTTP response object. If the request is executed in a separate thread, returns None.
  108. """
  109. retry_codes = (408, 500) # retry only these codes
  110. @TryExcept(verbose=verbose)
  111. def func(func_method, func_url, **func_kwargs):
  112. """Make HTTP requests with retries and timeouts, with optional progress tracking."""
  113. r = None # response
  114. t0 = time.time() # initial time for timer
  115. for i in range(retry + 1):
  116. if (time.time() - t0) > timeout:
  117. break
  118. r = requests_with_progress(func_method, func_url, **func_kwargs) # i.e. get(url, data, json, files)
  119. if r.status_code < 300: # return codes in the 2xx range are generally considered "good" or "successful"
  120. break
  121. try:
  122. m = r.json().get("message", "No JSON message.")
  123. except AttributeError:
  124. m = "Unable to read JSON."
  125. if i == 0:
  126. if r.status_code in retry_codes:
  127. m += f" Retrying {retry}x for {timeout}s." if retry else ""
  128. elif r.status_code == 429: # rate limit
  129. h = r.headers # response headers
  130. m = (
  131. f"Rate limit reached ({h['X-RateLimit-Remaining']}/{h['X-RateLimit-Limit']}). "
  132. f"Please retry after {h['Retry-After']}s."
  133. )
  134. if verbose:
  135. LOGGER.warning(f"{PREFIX}{m} {HELP_MSG} ({r.status_code} #{code})")
  136. if r.status_code not in retry_codes:
  137. return r
  138. time.sleep(2**i) # exponential standoff
  139. return r
  140. args = method, url
  141. kwargs["progress"] = progress
  142. if thread:
  143. threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True).start()
  144. else:
  145. return func(*args, **kwargs)
  146. class Events:
  147. """
  148. A class for collecting anonymous event analytics. Event analytics are enabled when sync=True in settings and
  149. disabled when sync=False. Run 'yolo settings' to see and update settings.
  150. Attributes:
  151. url (str): The URL to send anonymous events.
  152. rate_limit (float): The rate limit in seconds for sending events.
  153. metadata (dict): A dictionary containing metadata about the environment.
  154. enabled (bool): A flag to enable or disable Events based on certain conditions.
  155. """
  156. url = "https://www.google-analytics.com/mp/collect?measurement_id=G-X8NCJYTQXM&api_secret=QLQrATrNSwGRFRLE-cbHJw"
  157. def __init__(self):
  158. """Initializes the Events object with default values for events, rate_limit, and metadata."""
  159. self.events = [] # events list
  160. self.rate_limit = 30.0 # rate limit (seconds)
  161. self.t = 0.0 # rate limit timer (seconds)
  162. self.metadata = {
  163. "cli": Path(ARGV[0]).name == "yolo",
  164. "install": "git" if IS_GIT_DIR else "pip" if IS_PIP_PACKAGE else "other",
  165. "python": ".".join(platform.python_version_tuple()[:2]), # i.e. 3.10
  166. "version": __version__,
  167. "env": ENVIRONMENT,
  168. "session_id": round(random.random() * 1e15),
  169. "engagement_time_msec": 1000,
  170. }
  171. self.enabled = (
  172. SETTINGS["sync"]
  173. and RANK in {-1, 0}
  174. and not TESTS_RUNNING
  175. and ONLINE
  176. and (IS_PIP_PACKAGE or get_git_origin_url() == "https://github.com/ultralytics/ultralytics.git")
  177. )
  178. def __call__(self, cfg):
  179. """
  180. Attempts to add a new event to the events list and send events if the rate limit is reached.
  181. Args:
  182. cfg (IterableSimpleNamespace): The configuration object containing mode and task information.
  183. """
  184. if not self.enabled:
  185. # Events disabled, do nothing
  186. return
  187. # Attempt to add to events
  188. if len(self.events) < 25: # Events list limited to 25 events (drop any events past this)
  189. params = {
  190. **self.metadata,
  191. "task": cfg.task,
  192. "model": cfg.model if cfg.model in GITHUB_ASSETS_NAMES else "custom",
  193. }
  194. if cfg.mode == "export":
  195. params["format"] = cfg.format
  196. self.events.append({"name": cfg.mode, "params": params})
  197. # Check rate limit
  198. t = time.time()
  199. if (t - self.t) < self.rate_limit:
  200. # Time is under rate limiter, wait to send
  201. return
  202. # Time is over rate limiter, send now
  203. data = {"client_id": SETTINGS["uuid"], "events": self.events} # SHA-256 anonymized UUID hash and events list
  204. # POST equivalent to requests.post(self.url, json=data)
  205. smart_request("post", self.url, json=data, retry=0, verbose=False)
  206. # Reset events and rate limit timer
  207. self.events = []
  208. self.t = t
  209. # Run below code on hub/utils init -------------------------------------------------------------------------------------
  210. events = Events()