88import requests
99from huggingface_hub .hf_api import HfApi , RepositoryNotFoundError
1010from huggingface_hub .utils import hf_raise_for_status
11+ from huggingface_hub .utils ._headers import _http_user_agent
1112
1213
1314CI_HUB_USER = "__DUMMY_TRANSFORMERS_USER__"
1920CI_HFH_HUGGINGFACE_CO_URL_TEMPLATE = CI_HUB_ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}"
2021
2122
22- @pytest .fixture
23- def ci_hfh_hf_hub_url (monkeypatch ):
24- monkeypatch .setattr (
25- "huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE" , CI_HFH_HUGGINGFACE_CO_URL_TEMPLATE
26- )
27-
28-
2923@pytest .fixture
3024def ci_hub_config (monkeypatch ):
3125 monkeypatch .setattr ("datasets.config.HF_ENDPOINT" , CI_HUB_ENDPOINT )
3226 monkeypatch .setattr ("datasets.config.HUB_DATASETS_URL" , CI_HUB_DATASETS_URL )
27+ monkeypatch .setattr (
28+ "huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE" , CI_HFH_HUGGINGFACE_CO_URL_TEMPLATE
29+ )
30+ old_environ = dict (os .environ )
31+ os .environ ["HF_ENDPOINT" ] = CI_HUB_ENDPOINT
32+ yield
33+ os .environ .clear ()
34+ os .environ .update (old_environ )
3335
3436
3537@pytest .fixture
@@ -38,6 +40,22 @@ def set_ci_hub_access_token(ci_hub_config, monkeypatch):
3840 monkeypatch .setattr ("huggingface_hub.constants.HF_HUB_DISABLE_IMPLICIT_TOKEN" , False )
3941 old_environ = dict (os .environ )
4042 os .environ ["HF_TOKEN" ] = CI_HUB_USER_TOKEN
43+ os .environ ["HF_HUB_DISABLE_IMPLICIT_TOKEN" ] = "0"
44+ yield
45+ os .environ .clear ()
46+ os .environ .update (old_environ )
47+
48+
49+ def _http_ci_user_agent (* args , ** kwargs ):
50+ ua = _http_user_agent (* args , ** kwargs )
51+ return ua + os .environ .get ("CI_HEADERS" , "" )
52+
53+
54+ @pytest .fixture (autouse = True )
55+ def set_hf_ci_headers (monkeypatch ):
56+ old_environ = dict (os .environ )
57+ os .environ ["TRANSFORMERS_IS_CI" ] = "1"
58+ monkeypatch .setattr ("huggingface_hub.utils._headers._http_user_agent" , _http_ci_user_agent )
4159 yield
4260 os .environ .clear ()
4361 os .environ .update (old_environ )
@@ -105,7 +123,7 @@ def _hf_gated_dataset_repo_txt_data(hf_api: HfApi, hf_token, text_file_content):
105123
106124
107125@pytest .fixture ()
108- def hf_gated_dataset_repo_txt_data (_hf_gated_dataset_repo_txt_data , ci_hub_config , ci_hfh_hf_hub_url ):
126+ def hf_gated_dataset_repo_txt_data (_hf_gated_dataset_repo_txt_data , ci_hub_config ):
109127 return _hf_gated_dataset_repo_txt_data
110128
111129
@@ -129,7 +147,7 @@ def hf_private_dataset_repo_txt_data_(hf_api: HfApi, hf_token, text_file_content
129147
130148
131149@pytest .fixture ()
132- def hf_private_dataset_repo_txt_data (hf_private_dataset_repo_txt_data_ , ci_hub_config , ci_hfh_hf_hub_url ):
150+ def hf_private_dataset_repo_txt_data (hf_private_dataset_repo_txt_data_ , ci_hub_config ):
133151 return hf_private_dataset_repo_txt_data_
134152
135153
@@ -153,9 +171,7 @@ def hf_private_dataset_repo_zipped_txt_data_(hf_api: HfApi, hf_token, zip_csv_wi
153171
154172
155173@pytest .fixture ()
156- def hf_private_dataset_repo_zipped_txt_data (
157- hf_private_dataset_repo_zipped_txt_data_ , ci_hub_config , ci_hfh_hf_hub_url
158- ):
174+ def hf_private_dataset_repo_zipped_txt_data (hf_private_dataset_repo_zipped_txt_data_ , ci_hub_config ):
159175 return hf_private_dataset_repo_zipped_txt_data_
160176
161177
@@ -179,7 +195,5 @@ def hf_private_dataset_repo_zipped_img_data_(hf_api: HfApi, hf_token, zip_image_
179195
180196
181197@pytest .fixture ()
182- def hf_private_dataset_repo_zipped_img_data (
183- hf_private_dataset_repo_zipped_img_data_ , ci_hub_config , ci_hfh_hf_hub_url
184- ):
198+ def hf_private_dataset_repo_zipped_img_data (hf_private_dataset_repo_zipped_img_data_ , ci_hub_config ):
185199 return hf_private_dataset_repo_zipped_img_data_
0 commit comments