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
b6d4284b
Commit
b6d4284b
authored
Dec 13, 2019
by
Julien Chaumond
Browse files
[cli] Uploads: fix + test edge case
parent
7bd11dda
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
14 deletions
+33
-14
transformers/hf_api.py
transformers/hf_api.py
+2
-1
transformers/tests/fixtures/empty.txt
transformers/tests/fixtures/empty.txt
+0
-0
transformers/tests/hf_api_test.py
transformers/tests/hf_api_test.py
+31
-13
No files found.
transformers/hf_api.py
View file @
b6d4284b
...
...
@@ -131,8 +131,9 @@ class HfApi:
# the client still has to specify it when uploading the file.
with
open
(
filepath
,
"rb"
)
as
f
:
pf
=
TqdmProgressFileReader
(
f
)
data
=
f
if
pf
.
total_size
>
0
else
""
r
=
requests
.
put
(
urls
.
write
,
data
=
f
,
headers
=
{
r
=
requests
.
put
(
urls
.
write
,
data
=
data
,
headers
=
{
"content-type"
:
urls
.
type
,
})
r
.
raise_for_status
()
...
...
transformers/tests/fixtures/empty.txt
0 → 100644
View file @
b6d4284b
transformers/tests/hf_api_test.py
View file @
b6d4284b
...
...
@@ -15,18 +15,30 @@
from
__future__
import
absolute_import
,
division
,
print_function
import
os
import
six
import
time
import
unittest
from
transformers.hf_api
import
HfApi
,
S3Obj
,
PresignedUrl
,
HfFolder
,
HTTPError
import
requests
import
six
from
transformers.hf_api
import
HfApi
,
HfFolder
,
HTTPError
,
PresignedUrl
,
S3Obj
USER
=
"__DUMMY_TRANSFORMERS_USER__"
PASS
=
"__DUMMY_TRANSFORMERS_PASS__"
FILE_KEY
=
"Test-{}.txt"
.
format
(
int
(
time
.
time
()))
FILE_PATH
=
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)),
"fixtures/input.txt"
)
FILES
=
[
(
"Test-{}.txt"
.
format
(
int
(
time
.
time
())),
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)),
"fixtures/input.txt"
)
),
(
"yoyo {}.txt"
.
format
(
int
(
time
.
time
())),
# space is intentional
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)),
"fixtures/empty.txt"
)
),
]
...
...
@@ -57,15 +69,21 @@ class HfApiEndpointsTest(HfApiCommonTest):
self
.
assertEqual
(
user
,
USER
)
def
test_presign
(
self
):
urls
=
self
.
_api
.
presign
(
token
=
self
.
_token
,
filename
=
FILE_KEY
)
self
.
assertIsInstance
(
urls
,
PresignedUrl
)
self
.
assertEqual
(
urls
.
type
,
"text/plain"
)
for
FILE_KEY
,
FILE_PATH
in
FILES
:
urls
=
self
.
_api
.
presign
(
token
=
self
.
_token
,
filename
=
FILE_KEY
)
self
.
assertIsInstance
(
urls
,
PresignedUrl
)
self
.
assertEqual
(
urls
.
type
,
"text/plain"
)
def
test_presign_and_upload
(
self
):
access_url
=
self
.
_api
.
presign_and_upload
(
token
=
self
.
_token
,
filename
=
FILE_KEY
,
filepath
=
FILE_PATH
)
self
.
assertIsInstance
(
access_url
,
six
.
string_types
)
for
FILE_KEY
,
FILE_PATH
in
FILES
:
access_url
=
self
.
_api
.
presign_and_upload
(
token
=
self
.
_token
,
filename
=
FILE_KEY
,
filepath
=
FILE_PATH
)
self
.
assertIsInstance
(
access_url
,
six
.
string_types
)
with
open
(
FILE_PATH
,
'r'
)
as
f
:
body
=
f
.
read
()
r
=
requests
.
get
(
access_url
)
self
.
assertEqual
(
r
.
text
,
body
)
def
test_list_objs
(
self
):
objs
=
self
.
_api
.
list_objs
(
token
=
self
.
_token
)
...
...
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