File tree Expand file tree Collapse file tree 3 files changed +22
-0
lines changed Expand file tree Collapse file tree 3 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -194,6 +194,9 @@ def _as_int(value: Optional[str]) -> Optional[int]:
194
194
# Used to override the get request timeout on a system level
195
195
HF_HUB_DOWNLOAD_TIMEOUT : int = _as_int (os .environ .get ("HF_HUB_DOWNLOAD_TIMEOUT" )) or DEFAULT_DOWNLOAD_TIMEOUT
196
196
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
+
197
200
# List frameworks that are handled by the InferenceAPI service. Useful to scan endpoints and check which models are
198
201
# deployed and running. Since 95% of the models are using the top 4 frameworks listed below, we scan only those by
199
202
# default. We still keep the full list of supported frameworks in case we want to scan all of them.
Original file line number Diff line number Diff line change @@ -213,6 +213,11 @@ def _http_user_agent(
213
213
elif isinstance (user_agent , str ):
214
214
ua += "; " + user_agent
215
215
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
+
216
221
return _deduplicate_user_agent (ua )
217
222
218
223
Original file line number Diff line number Diff line change @@ -141,3 +141,17 @@ def test_user_agent_deduplicate(self) -> None:
141
141
# 4. order is preserved
142
142
"python/3.7; python/3.8; hf_hub/0.12; transformers/None; diffusers/0.12.1" ,
143
143
)
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" ))
You can’t perform that action at this time.
0 commit comments