test_hf_api.py 8.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# coding=utf-8
# Copyright 2019-present, the HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Aymeric Augustin's avatar
Aymeric Augustin committed
15

16
17

import os
18
19
import shutil
import subprocess
20
21
22
import time
import unittest

23
import requests
24
from requests.exceptions import HTTPError
Julien Chaumond's avatar
Julien Chaumond committed
25
from transformers.hf_api import HfApi, HfFolder, ModelInfo, PresignedUrl, RepoObj, S3Obj
26
from transformers.testing_utils import require_git_lfs
27

Aymeric Augustin's avatar
Aymeric Augustin committed
28

29
30
USER = "__DUMMY_TRANSFORMERS_USER__"
PASS = "__DUMMY_TRANSFORMERS_PASS__"
31
32
FILES = [
    (
33
        "nested/Test-{}.txt".format(int(time.time())),
34
        os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures/input.txt"),
35
36
    ),
    (
37
        "nested/yoyo {}.txt".format(int(time.time())),  # space is intentional
38
        os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures/empty.txt"),
39
40
    ),
]
41
ENDPOINT_STAGING = "https://moon-staging.huggingface.co"
42
43
44
45
46
47
48
ENDPOINT_STAGING_BASIC_AUTH = f"https://{USER}:{PASS}@moon-staging.huggingface.co"

REPO_NAME = "my-model-{}".format(int(time.time()))
REPO_NAME_LARGE_FILE = "my-model-largefiles-{}".format(int(time.time()))
WORKING_REPO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures/working_repo")
LARGE_FILE_14MB = "https://cdn-media.huggingface.co/lfs-largefiles/progit.epub"
LARGE_FILE_18MB = "https://cdn-media.huggingface.co/lfs-largefiles/progit.pdf"
49
50
51


class HfApiCommonTest(unittest.TestCase):
52
    _api = HfApi(endpoint=ENDPOINT_STAGING)
53
54
55
56
57
58
59
60
61


class HfApiLoginTest(HfApiCommonTest):
    def test_login_invalid(self):
        with self.assertRaises(HTTPError):
            self._api.login(username=USER, password="fake")

    def test_login_valid(self):
        token = self._api.login(username=USER, password=PASS)
Aymeric Augustin's avatar
Aymeric Augustin committed
62
        self.assertIsInstance(token, str)
63
64
65
66
67
68
69
70
71
72


class HfApiEndpointsTest(HfApiCommonTest):
    @classmethod
    def setUpClass(cls):
        """
        Share this valid token in all tests below.
        """
        cls._token = cls._api.login(username=USER, password=PASS)

73
74
75
    @classmethod
    def tearDownClass(cls):
        for FILE_KEY, FILE_PATH in FILES:
76
            cls._api.delete_obj(token=cls._token, filetype="datasets", filename=FILE_KEY)
77

78
    def test_whoami(self):
79
        user, orgs = self._api.whoami(token=self._token)
80
        self.assertEqual(user, USER)
81
82
83
84
        self.assertIsInstance(orgs, list)

    def test_presign_invalid_org(self):
        with self.assertRaises(HTTPError):
85
86
87
            _ = self._api.presign(
                token=self._token, filetype="datasets", filename="nested/fake_org.txt", organization="fake"
            )
88
89

    def test_presign_valid_org(self):
90
91
92
        urls = self._api.presign(
            token=self._token, filetype="datasets", filename="nested/valid_org.txt", organization="valid_org"
        )
93
        self.assertIsInstance(urls, PresignedUrl)
94
95

    def test_presign(self):
96
        for FILE_KEY, FILE_PATH in FILES:
97
            urls = self._api.presign(token=self._token, filetype="datasets", filename=FILE_KEY)
98
99
            self.assertIsInstance(urls, PresignedUrl)
            self.assertEqual(urls.type, "text/plain")
100
101

    def test_presign_and_upload(self):
102
        for FILE_KEY, FILE_PATH in FILES:
103
104
105
            access_url = self._api.presign_and_upload(
                token=self._token, filetype="datasets", filename=FILE_KEY, filepath=FILE_PATH
            )
Aymeric Augustin's avatar
Aymeric Augustin committed
106
            self.assertIsInstance(access_url, str)
107
            with open(FILE_PATH, "r") as f:
108
109
110
                body = f.read()
            r = requests.get(access_url)
            self.assertEqual(r.text, body)
111
112

    def test_list_objs(self):
113
        objs = self._api.list_objs(token=self._token, filetype="datasets")
114
115
116
117
        self.assertIsInstance(objs, list)
        if len(objs) > 0:
            o = objs[-1]
            self.assertIsInstance(o, S3Obj)
118

Julien Chaumond's avatar
Julien Chaumond committed
119
120
121
122
123
124
125
126
127
128
129
    def test_list_repos_objs(self):
        objs = self._api.list_repos_objs(token=self._token)
        self.assertIsInstance(objs, list)
        if len(objs) > 0:
            o = objs[-1]
            self.assertIsInstance(o, RepoObj)

    def test_create_and_delete_repo(self):
        self._api.create_repo(token=self._token, name=REPO_NAME)
        self._api.delete_repo(token=self._token, name=REPO_NAME)

130

131
132
133
134
135
136
137
138
139
140
141
142
class HfApiPublicTest(unittest.TestCase):
    def test_staging_model_list(self):
        _api = HfApi(endpoint=ENDPOINT_STAGING)
        _ = _api.model_list()

    def test_model_list(self):
        _api = HfApi()
        models = _api.model_list()
        self.assertGreater(len(models), 100)
        self.assertIsInstance(models[0], ModelInfo)


143
144
145
146
147
148
149
150
class HfFolderTest(unittest.TestCase):
    def test_token_workflow(self):
        """
        Test the whole token save/get/delete workflow,
        with the desired behavior with respect to non-existent tokens.
        """
        token = "token-{}".format(int(time.time()))
        HfFolder.save_token(token)
151
        self.assertEqual(HfFolder.get_token(), token)
152
153
154
155
        HfFolder.delete_token()
        HfFolder.delete_token()
        # ^^ not an error, we test that the
        # second call does not fail.
156
        self.assertEqual(HfFolder.get_token(), None)
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228


@require_git_lfs
class HfLargefilesTest(HfApiCommonTest):
    @classmethod
    def setUpClass(cls):
        """
        Share this valid token in all tests below.
        """
        cls._token = cls._api.login(username=USER, password=PASS)

    def setUp(self):
        try:
            shutil.rmtree(WORKING_REPO_DIR)
        except FileNotFoundError:
            pass

    def tearDown(self):
        self._api.delete_repo(token=self._token, name=REPO_NAME_LARGE_FILE)

    def setup_local_clone(self, REMOTE_URL):
        REMOTE_URL_AUTH = REMOTE_URL.replace(ENDPOINT_STAGING, ENDPOINT_STAGING_BASIC_AUTH)
        subprocess.run(["git", "clone", REMOTE_URL_AUTH, WORKING_REPO_DIR], check=True, capture_output=True)
        subprocess.run(["git", "lfs", "track", "*.pdf"], check=True, cwd=WORKING_REPO_DIR)
        subprocess.run(["git", "lfs", "track", "*.epub"], check=True, cwd=WORKING_REPO_DIR)

    def test_end_to_end_thresh_6M(self):
        REMOTE_URL = self._api.create_repo(
            token=self._token, name=REPO_NAME_LARGE_FILE, lfsmultipartthresh=6 * 10 ** 6
        )
        self.setup_local_clone(REMOTE_URL)

        subprocess.run(["wget", LARGE_FILE_18MB], check=True, capture_output=True, cwd=WORKING_REPO_DIR)
        subprocess.run(["git", "add", "*"], check=True, cwd=WORKING_REPO_DIR)
        subprocess.run(["git", "commit", "-m", "commit message"], check=True, cwd=WORKING_REPO_DIR)

        # This will fail as we haven't set up our custom transfer agent yet.
        failed_process = subprocess.run(["git", "push"], capture_output=True, cwd=WORKING_REPO_DIR)
        self.assertEqual(failed_process.returncode, 1)
        self.assertIn("transformers-cli lfs-enable-largefiles", failed_process.stderr.decode())
        # ^ Instructions on how to fix this are included in the error message.

        subprocess.run(["transformers-cli", "lfs-enable-largefiles", WORKING_REPO_DIR], check=True)

        start_time = time.time()
        subprocess.run(["git", "push"], check=True, cwd=WORKING_REPO_DIR)
        print("took", time.time() - start_time)

        # To be 100% sure, let's download the resolved file
        pdf_url = f"{REMOTE_URL}/resolve/main/progit.pdf"
        DEST_FILENAME = "uploaded.pdf"
        subprocess.run(["wget", pdf_url, "-O", DEST_FILENAME], check=True, capture_output=True, cwd=WORKING_REPO_DIR)
        dest_filesize = os.stat(os.path.join(WORKING_REPO_DIR, DEST_FILENAME)).st_size
        self.assertEqual(dest_filesize, 18685041)

    def test_end_to_end_thresh_16M(self):
        # Here we'll push one multipart and one non-multipart file in the same commit, and see what happens
        REMOTE_URL = self._api.create_repo(
            token=self._token, name=REPO_NAME_LARGE_FILE, lfsmultipartthresh=16 * 10 ** 6
        )
        self.setup_local_clone(REMOTE_URL)

        subprocess.run(["wget", LARGE_FILE_18MB], check=True, capture_output=True, cwd=WORKING_REPO_DIR)
        subprocess.run(["wget", LARGE_FILE_14MB], check=True, capture_output=True, cwd=WORKING_REPO_DIR)
        subprocess.run(["git", "add", "*"], check=True, cwd=WORKING_REPO_DIR)
        subprocess.run(["git", "commit", "-m", "both files in same commit"], check=True, cwd=WORKING_REPO_DIR)

        subprocess.run(["transformers-cli", "lfs-enable-largefiles", WORKING_REPO_DIR], check=True)

        start_time = time.time()
        subprocess.run(["git", "push"], check=True, cwd=WORKING_REPO_DIR)
        print("took", time.time() - start_time)