mirror of
https://github.com/huggingface/transformers.git
synced 2025-08-01 18:51:14 +06:00
[cli] {login, upload, s3} display more helpful error messages
This commit is contained in:
parent
452dd0e4d9
commit
ab90353f1a
@ -33,8 +33,10 @@ class UserCommands(BaseTransformersCLICommand):
|
|||||||
rm_parser.add_argument("--organization", type=str, help="Optional: organization namespace.")
|
rm_parser.add_argument("--organization", type=str, help="Optional: organization namespace.")
|
||||||
rm_parser.set_defaults(func=lambda args: DeleteObjCommand(args))
|
rm_parser.set_defaults(func=lambda args: DeleteObjCommand(args))
|
||||||
# upload
|
# upload
|
||||||
upload_parser = parser.add_parser("upload")
|
upload_parser = parser.add_parser("upload", help="Upload a model to S3.")
|
||||||
upload_parser.add_argument("path", type=str, help="Local path of the folder or individual file to upload.")
|
upload_parser.add_argument(
|
||||||
|
"path", type=str, help="Local path of the model folder or individual file to upload."
|
||||||
|
)
|
||||||
upload_parser.add_argument("--organization", type=str, help="Optional: organization namespace.")
|
upload_parser.add_argument("--organization", type=str, help="Optional: organization namespace.")
|
||||||
upload_parser.add_argument(
|
upload_parser.add_argument(
|
||||||
"--filename", type=str, default=None, help="Optional: override individual object filename on S3."
|
"--filename", type=str, default=None, help="Optional: override individual object filename on S3."
|
||||||
@ -48,12 +50,17 @@ class ANSI:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
_bold = "\u001b[1m"
|
_bold = "\u001b[1m"
|
||||||
|
_red = "\u001b[31m"
|
||||||
_reset = "\u001b[0m"
|
_reset = "\u001b[0m"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def bold(cls, s):
|
def bold(cls, s):
|
||||||
return "{}{}{}".format(cls._bold, s, cls._reset)
|
return "{}{}{}".format(cls._bold, s, cls._reset)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def red(cls, s):
|
||||||
|
return "{}{}{}".format(cls._bold + cls._red, s, cls._reset)
|
||||||
|
|
||||||
|
|
||||||
class BaseUserCommand:
|
class BaseUserCommand:
|
||||||
def __init__(self, args):
|
def __init__(self, args):
|
||||||
@ -80,6 +87,7 @@ class LoginCommand(BaseUserCommand):
|
|||||||
except HTTPError as e:
|
except HTTPError as e:
|
||||||
# probably invalid credentials, display error message.
|
# probably invalid credentials, display error message.
|
||||||
print(e)
|
print(e)
|
||||||
|
print(ANSI.red(e.response.text))
|
||||||
exit(1)
|
exit(1)
|
||||||
HfFolder.save_token(token)
|
HfFolder.save_token(token)
|
||||||
print("Login successful")
|
print("Login successful")
|
||||||
@ -100,6 +108,8 @@ class WhoamiCommand(BaseUserCommand):
|
|||||||
print(ANSI.bold("orgs: "), ",".join(orgs))
|
print(ANSI.bold("orgs: "), ",".join(orgs))
|
||||||
except HTTPError as e:
|
except HTTPError as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
print(ANSI.red(e.response.text))
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
class LogoutCommand(BaseUserCommand):
|
class LogoutCommand(BaseUserCommand):
|
||||||
@ -138,6 +148,7 @@ class ListObjsCommand(BaseUserCommand):
|
|||||||
objs = self._api.list_objs(token, organization=self.args.organization)
|
objs = self._api.list_objs(token, organization=self.args.organization)
|
||||||
except HTTPError as e:
|
except HTTPError as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
print(ANSI.red(e.response.text))
|
||||||
exit(1)
|
exit(1)
|
||||||
if len(objs) == 0:
|
if len(objs) == 0:
|
||||||
print("No shared file yet")
|
print("No shared file yet")
|
||||||
@ -156,6 +167,7 @@ class DeleteObjCommand(BaseUserCommand):
|
|||||||
self._api.delete_obj(token, filename=self.args.filename, organization=self.args.organization)
|
self._api.delete_obj(token, filename=self.args.filename, organization=self.args.organization)
|
||||||
except HTTPError as e:
|
except HTTPError as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
print(ANSI.red(e.response.text))
|
||||||
exit(1)
|
exit(1)
|
||||||
print("Done")
|
print("Done")
|
||||||
|
|
||||||
@ -216,8 +228,13 @@ class UploadCommand(BaseUserCommand):
|
|||||||
exit()
|
exit()
|
||||||
print(ANSI.bold("Uploading... This might take a while if files are large"))
|
print(ANSI.bold("Uploading... This might take a while if files are large"))
|
||||||
for filepath, filename in files:
|
for filepath, filename in files:
|
||||||
access_url = self._api.presign_and_upload(
|
try:
|
||||||
token=token, filename=filename, filepath=filepath, organization=self.args.organization
|
access_url = self._api.presign_and_upload(
|
||||||
)
|
token=token, filename=filename, filepath=filepath, organization=self.args.organization
|
||||||
|
)
|
||||||
|
except HTTPError as e:
|
||||||
|
print(e)
|
||||||
|
print(ANSI.red(e.response.text))
|
||||||
|
exit(1)
|
||||||
print("Your file now lives at:")
|
print("Your file now lives at:")
|
||||||
print(access_url)
|
print(access_url)
|
||||||
|
@ -79,6 +79,15 @@ class HfApiEndpointsTest(HfApiCommonTest):
|
|||||||
urls = self._api.presign(token=self._token, filename="nested/valid_org.txt", organization="valid_org")
|
urls = self._api.presign(token=self._token, filename="nested/valid_org.txt", organization="valid_org")
|
||||||
self.assertIsInstance(urls, PresignedUrl)
|
self.assertIsInstance(urls, PresignedUrl)
|
||||||
|
|
||||||
|
def test_presign_invalid(self):
|
||||||
|
try:
|
||||||
|
_ = self._api.presign(token=self._token, filename="non_nested.json")
|
||||||
|
except HTTPError as e:
|
||||||
|
self.assertIsNotNone(e.response.text)
|
||||||
|
self.assertTrue("Filename invalid" in e.response.text)
|
||||||
|
else:
|
||||||
|
self.fail("Expected an exception")
|
||||||
|
|
||||||
def test_presign(self):
|
def test_presign(self):
|
||||||
for FILE_KEY, FILE_PATH in FILES:
|
for FILE_KEY, FILE_PATH in FILES:
|
||||||
urls = self._api.presign(token=self._token, filename=FILE_KEY)
|
urls = self._api.presign(token=self._token, filename=FILE_KEY)
|
||||||
|
Loading…
Reference in New Issue
Block a user