Skip to content

Commit a7f3151

Browse files
feat: Adds a new environment variable HF_HUB_USER_AGENT_ORIGIN to set origin of calls in user-agent (#2869)
* feat: Adds a new environment variable HF_HUB_USER_AGENT_ORIGIN to set origin of calls in user-agent. This can be used to track calls for partners for collaborations. --------- Co-authored-by: Célina <[email protected]>
1 parent ace6668 commit a7f3151

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/huggingface_hub/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ def _as_int(value: Optional[str]) -> Optional[int]:
194194
# Used to override the get request timeout on a system level
195195
HF_HUB_DOWNLOAD_TIMEOUT: int = _as_int(os.environ.get("HF_HUB_DOWNLOAD_TIMEOUT")) or DEFAULT_DOWNLOAD_TIMEOUT
196196

197+
# Allows to add information about the requester in the user-agent (eg. partner name)
198+
HF_HUB_USER_AGENT_ORIGIN: Optional[str] = os.environ.get("HF_HUB_USER_AGENT_ORIGIN")
199+
197200
# List frameworks that are handled by the InferenceAPI service. Useful to scan endpoints and check which models are
198201
# deployed and running. Since 95% of the models are using the top 4 frameworks listed below, we scan only those by
199202
# default. We still keep the full list of supported frameworks in case we want to scan all of them.

src/huggingface_hub/utils/_headers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ def _http_user_agent(
213213
elif isinstance(user_agent, str):
214214
ua += "; " + user_agent
215215

216+
# Retrieve user-agent origin headers from environment variable
217+
origin = constants.HF_HUB_USER_AGENT_ORIGIN
218+
if origin is not None:
219+
ua += "; origin/" + origin
220+
216221
return _deduplicate_user_agent(ua)
217222

218223

tests/test_utils_headers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,17 @@ def test_user_agent_deduplicate(self) -> None:
141141
# 4. order is preserved
142142
"python/3.7; python/3.8; hf_hub/0.12; transformers/None; diffusers/0.12.1",
143143
)
144+
145+
@patch("huggingface_hub.utils._telemetry.constants.HF_HUB_USER_AGENT_ORIGIN", "custom-origin")
146+
def test_user_agent_with_origin(self) -> None:
147+
self.assertTrue(self._get_user_agent().endswith("origin/custom-origin"))
148+
149+
@patch("huggingface_hub.utils._telemetry.constants.HF_HUB_USER_AGENT_ORIGIN", "custom-origin")
150+
def test_user_agent_with_origin_and_user_agent(self) -> None:
151+
self.assertTrue(
152+
self._get_user_agent(user_agent={"a": "b", "c": "d"}).endswith("a/b; c/d; origin/custom-origin")
153+
)
154+
155+
@patch("huggingface_hub.utils._telemetry.constants.HF_HUB_USER_AGENT_ORIGIN", "custom-origin")
156+
def test_user_agent_with_origin_and_user_agent_str(self) -> None:
157+
self.assertTrue(self._get_user_agent(user_agent="a/b;c/d").endswith("a/b; c/d; origin/custom-origin"))

0 commit comments

Comments
 (0)