Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
ab90353f
Commit
ab90353f
authored
Apr 30, 2020
by
Julien Chaumond
Browse files
[cli] {login, upload, s3} display more helpful error messages
parent
452dd0e4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
5 deletions
+31
-5
src/transformers/commands/user.py
src/transformers/commands/user.py
+22
-5
tests/test_hf_api.py
tests/test_hf_api.py
+9
-0
No files found.
src/transformers/commands/user.py
View file @
ab90353f
...
...
@@ -33,8 +33,10 @@ class UserCommands(BaseTransformersCLICommand):
rm_parser
.
add_argument
(
"--organization"
,
type
=
str
,
help
=
"Optional: organization namespace."
)
rm_parser
.
set_defaults
(
func
=
lambda
args
:
DeleteObjCommand
(
args
))
# upload
upload_parser
=
parser
.
add_parser
(
"upload"
)
upload_parser
.
add_argument
(
"path"
,
type
=
str
,
help
=
"Local path of the folder or individual file to 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 model folder or individual file to upload."
)
upload_parser
.
add_argument
(
"--organization"
,
type
=
str
,
help
=
"Optional: organization namespace."
)
upload_parser
.
add_argument
(
"--filename"
,
type
=
str
,
default
=
None
,
help
=
"Optional: override individual object filename on S3."
...
...
@@ -48,12 +50,17 @@ class ANSI:
"""
_bold
=
"
\u001b
[1m"
_red
=
"
\u001b
[31m"
_reset
=
"
\u001b
[0m"
@
classmethod
def
bold
(
cls
,
s
):
return
"{}{}{}"
.
format
(
cls
.
_bold
,
s
,
cls
.
_reset
)
@
classmethod
def
red
(
cls
,
s
):
return
"{}{}{}"
.
format
(
cls
.
_bold
+
cls
.
_red
,
s
,
cls
.
_reset
)
class
BaseUserCommand
:
def
__init__
(
self
,
args
):
...
...
@@ -80,6 +87,7 @@ class LoginCommand(BaseUserCommand):
except
HTTPError
as
e
:
# probably invalid credentials, display error message.
print
(
e
)
print
(
ANSI
.
red
(
e
.
response
.
text
))
exit
(
1
)
HfFolder
.
save_token
(
token
)
print
(
"Login successful"
)
...
...
@@ -100,6 +108,8 @@ class WhoamiCommand(BaseUserCommand):
print
(
ANSI
.
bold
(
"orgs: "
),
","
.
join
(
orgs
))
except
HTTPError
as
e
:
print
(
e
)
print
(
ANSI
.
red
(
e
.
response
.
text
))
exit
(
1
)
class
LogoutCommand
(
BaseUserCommand
):
...
...
@@ -138,6 +148,7 @@ class ListObjsCommand(BaseUserCommand):
objs
=
self
.
_api
.
list_objs
(
token
,
organization
=
self
.
args
.
organization
)
except
HTTPError
as
e
:
print
(
e
)
print
(
ANSI
.
red
(
e
.
response
.
text
))
exit
(
1
)
if
len
(
objs
)
==
0
:
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
)
except
HTTPError
as
e
:
print
(
e
)
print
(
ANSI
.
red
(
e
.
response
.
text
))
exit
(
1
)
print
(
"Done"
)
...
...
@@ -216,8 +228,13 @@ class UploadCommand(BaseUserCommand):
exit
()
print
(
ANSI
.
bold
(
"Uploading... This might take a while if files are large"
))
for
filepath
,
filename
in
files
:
access_url
=
self
.
_api
.
presign_and_upload
(
token
=
token
,
filename
=
filename
,
filepath
=
filepath
,
organization
=
self
.
args
.
organization
)
try
:
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
(
access_url
)
tests/test_hf_api.py
View file @
ab90353f
...
...
@@ -79,6 +79,15 @@ class HfApiEndpointsTest(HfApiCommonTest):
urls
=
self
.
_api
.
presign
(
token
=
self
.
_token
,
filename
=
"nested/valid_org.txt"
,
organization
=
"valid_org"
)
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
):
for
FILE_KEY
,
FILE_PATH
in
FILES
:
urls
=
self
.
_api
.
presign
(
token
=
self
.
_token
,
filename
=
FILE_KEY
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment