forked from huggingface/datasets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub_fixtures.py
81 lines (65 loc) · 2.57 KB
/
hub_fixtures.py
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
import time
from unittest.mock import patch
import pytest
import requests
from huggingface_hub.hf_api import HfApi
USER = "__DUMMY_TRANSFORMERS_USER__"
FULL_NAME = "Dummy User"
PASS = "__DUMMY_TRANSFORMERS_PASS__"
ENDPOINT_STAGING = "https://moon-staging.huggingface.co"
ENDPOINT_STAGING_DATASETS_URL = ENDPOINT_STAGING + "/datasets/{path}/resolve/{revision}/{name}"
@pytest.fixture(scope="session")
def hf_api():
return HfApi(endpoint=ENDPOINT_STAGING)
@pytest.fixture(scope="session")
def hf_token(hf_api: HfApi):
hf_token = hf_api.login(username=USER, password=PASS)
yield hf_token
try:
hf_api.logout(hf_token)
except requests.exceptions.HTTPError:
pass
@pytest.fixture(scope="session")
def hf_private_dataset_repo_txt_data_(hf_api: HfApi, hf_token, text_file):
repo_name = "repo_txt_data-{}".format(int(time.time() * 10e3))
hf_api.create_repo(token=hf_token, name=repo_name, repo_type="dataset", private=True)
repo_id = f"{USER}/{repo_name}"
hf_api.upload_file(
token=hf_token,
path_or_fileobj=str(text_file),
path_in_repo="data.txt",
repo_id=repo_id,
repo_type="dataset",
)
yield repo_id
try:
hf_api.delete_repo(token=hf_token, name=repo_name, repo_type="dataset")
except requests.exceptions.HTTPError:
pass
@pytest.fixture()
def hf_private_dataset_repo_txt_data(hf_private_dataset_repo_txt_data_):
with patch("datasets.config.HF_ENDPOINT", ENDPOINT_STAGING):
with patch("datasets.config.HUB_DATASETS_URL", ENDPOINT_STAGING_DATASETS_URL):
yield hf_private_dataset_repo_txt_data_
@pytest.fixture(scope="session")
def hf_private_dataset_repo_zipped_txt_data_(hf_api: HfApi, hf_token, zip_csv_path):
repo_name = "repo_zipped_txt_data-{}".format(int(time.time() * 10e3))
hf_api.create_repo(token=hf_token, name=repo_name, repo_type="dataset", private=True)
repo_id = f"{USER}/{repo_name}"
hf_api.upload_file(
token=hf_token,
path_or_fileobj=str(zip_csv_path),
path_in_repo="data.zip",
repo_id=repo_id,
repo_type="dataset",
)
yield repo_id
try:
hf_api.delete_repo(token=hf_token, name=repo_name, repo_type="dataset")
except requests.exceptions.HTTPError:
pass
@pytest.fixture()
def hf_private_dataset_repo_zipped_txt_data(hf_private_dataset_repo_zipped_txt_data_):
with patch("datasets.config.HF_ENDPOINT", ENDPOINT_STAGING):
with patch("datasets.config.HUB_DATASETS_URL", ENDPOINT_STAGING_DATASETS_URL):
yield hf_private_dataset_repo_zipped_txt_data_