Exemplo n.º 1
0
    def run(self):
        print(
            ANSI.red(
                "WARNING! `transformers-cli login` is deprecated and will be removed in v5. Please use "
                "`huggingface-cli login` instead."))
        print(  # docstyle-ignore
            """
        _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
        _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
        _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
        _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
        _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

        """)
        username = input("Username: "******"Login successful")
        print("Your token:", token, "\n")
        print("Your token has been saved to", HfFolder.path_token)
Exemplo n.º 2
0
def _login(hf_api, username=None, password=None, token=None):
    if token is None:
        try:
            token = hf_api.login(username, password)
        except HTTPError as e:
            # probably invalid credentials, display error message.
            print(e)
            print(ANSI.red(e.response.text))
            exit(1)
    elif not hf_api._is_valid_token(token):
        raise ValueError("Invalid token passed.")

    hf_api.set_access_token(token)
    HfFolder.save_token(token)
    print("Login successful")
    print("Your token has been saved to", HfFolder.path_token)
    helpers = currently_setup_credential_helpers()

    if "store" not in helpers:
        print(
            ANSI.red(
                "Authenticated through git-credential store but this isn't the helper defined on your machine.\nYou "
                "might have to re-authenticate when pushing to the Hugging Face Hub. Run the following command in your "
                "terminal in case you want to set this credential helper as the default\n\ngit config --global credential.helper store"
            ))
Exemplo n.º 3
0
    def setUpClass(cls):
        """
        Share this valid token in all tests below.
        """
        cls._hf_folder_patch = patch(
            "huggingface_hub.hf_api.HfFolder.path_token",
            TOKEN_PATH_STAGING,
        )
        cls._hf_folder_patch.start()

        cls._token = cls._api.login(username=USER, password=PASS)
        HfFolder.save_token(cls._token)
Exemplo n.º 4
0
 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)
     self.assertEqual(HfFolder.get_token(), token)
     HfFolder.delete_token()
     HfFolder.delete_token()
     # ^^ not an error, we test that the
     # second call does not fail.
     self.assertEqual(HfFolder.get_token(), None)
Exemplo n.º 5
0
 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)
     self.assertEqual(HfFolder.get_token(), token)
     HfFolder.delete_token()
     HfFolder.delete_token()
     # ^^ not an error, we test that the
     # second call does not fail.
     self.assertEqual(HfFolder.get_token(), None)
     # test TOKEN in env
     self.assertEqual(HfFolder.get_token(), None)
     with unittest.mock.patch.dict(os.environ,
                                   {"HUGGING_FACE_HUB_TOKEN": token}):
         self.assertEqual(HfFolder.get_token(), token)
Exemplo n.º 6
0
    def run(self):
        print(  # docstyle-ignore
            """
        _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
        _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
        _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
        _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
        _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

        """
        )
        username = input("Username: "******"Login successful")
        print("Your token:", token, "\n")
        print("Your token has been saved to", HfFolder.path_token)
Exemplo n.º 7
0
    def test_download_private_model(self):
        self._api.update_repo_visibility(
            token=self._token, repo_id=REPO_NAME, private=True
        )

        # Test download fails without token
        with tempfile.TemporaryDirectory() as tmpdirname:
            with self.assertRaisesRegex(
                requests.exceptions.HTTPError, "404 Client Error"
            ):
                _ = snapshot_download(
                    f"{USER}/{REPO_NAME}", revision="main", cache_dir=tmpdirname
                )

        # Test we can download with token from cache
        with tempfile.TemporaryDirectory() as tmpdirname:
            HfFolder.save_token(self._token)
            storage_folder = snapshot_download(
                f"{USER}/{REPO_NAME}",
                revision="main",
                cache_dir=tmpdirname,
                use_auth_token=True,
            )

            # folder contains the two files contributed and the .gitattributes
            folder_contents = os.listdir(storage_folder)
            self.assertEqual(len(folder_contents), 3)
            self.assertTrue("dummy_file.txt" in folder_contents)
            self.assertTrue("dummy_file_2.txt" in folder_contents)
            self.assertTrue(".gitattributes" in folder_contents)

            with open(os.path.join(storage_folder, "dummy_file.txt"), "r") as f:
                contents = f.read()
                self.assertEqual(contents, "v2")

            # folder name contains the revision's commit sha.
            self.assertTrue(self.second_commit_hash in storage_folder)

        # Test we can download with explicit token
        with tempfile.TemporaryDirectory() as tmpdirname:
            storage_folder = snapshot_download(
                f"{USER}/{REPO_NAME}",
                revision="main",
                cache_dir=tmpdirname,
                use_auth_token=self._token,
            )

            # folder contains the two files contributed and the .gitattributes
            folder_contents = os.listdir(storage_folder)
            self.assertEqual(len(folder_contents), 3)
            self.assertTrue("dummy_file.txt" in folder_contents)
            self.assertTrue("dummy_file_2.txt" in folder_contents)
            self.assertTrue(".gitattributes" in folder_contents)

            with open(os.path.join(storage_folder, "dummy_file.txt"), "r") as f:
                contents = f.read()
                self.assertEqual(contents, "v2")

            # folder name contains the revision's commit sha.
            self.assertTrue(self.second_commit_hash in storage_folder)

        self._api.update_repo_visibility(
            token=self._token, repo_id=REPO_NAME, private=False
        )
Exemplo n.º 8
0
 def test_create_repo_org_token_none_fail(self):
     REPO_NAME = repo_name("org")
     HfFolder.save_token("api_org_dummy_token")
     with pytest.raises(ValueError,
                        match="You must use your personal account token."):
         self._api.create_repo(repo_id=REPO_NAME)