Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add identical_ok option to HfApi.upload_file method #102

Merged
merged 1 commit into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def upload_file(
repo_id: str,
repo_type: Optional[str] = None,
revision: Optional[str] = None,
identical_ok: bool = True,
) -> str:
"""
Upload a local file (up to 5GB) to the given repo, tracking it with LFS if it's larger than 10MB
Expand All @@ -421,6 +422,10 @@ def upload_file(
revision (``str``, Optional):
The git revision to commit from. Defaults to the :obj:`"main"` branch.

identical_ok (``bool``, defaults to ``True``):
When set to false, will raise an HTTPError when the file you're trying to upload already exists on the hub
and its content did not change.

Returns:
``str``: The URL to visualize the uploaded file on the hub

Expand Down Expand Up @@ -501,7 +506,12 @@ def upload_file(
else:
r = requests.post(path, headers=headers, data=path_or_fileobj)

r.raise_for_status()
try:
r.raise_for_status()
except HTTPError as err:
if not (identical_ok and err.response.status_code == 409):
raise err

d = r.json()
return d["url"]

Expand Down
36 changes: 36 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,42 @@ def test_upload_file_bytesio(self):
finally:
self._api.delete_repo(token=self._token, name=REPO_NAME)

def test_upload_file_conflict(self):
self._api.create_repo(token=self._token, name=REPO_NAME)
try:
filecontent = BytesIO(b"File content, but in bytes IO")
self._api.upload_file(
path_or_fileobj=filecontent,
path_in_repo="temp/new_file.md",
repo_id=f"{USER}/{REPO_NAME}",
token=self._token,
identical_ok=True,
)

# No exception raised when identical_ok is True
self._api.upload_file(
path_or_fileobj=filecontent,
path_in_repo="temp/new_file.md",
repo_id=f"{USER}/{REPO_NAME}",
token=self._token,
identical_ok=True,
)

with self.assertRaises(HTTPError) as err_ctx:
self._api.upload_file(
path_or_fileobj=filecontent,
path_in_repo="temp/new_file.md",
repo_id=f"{USER}/{REPO_NAME}",
token=self._token,
identical_ok=False,
)
self.assertEqual(err_ctx.exception.response.status_code, 409)

except Exception as err:
self.fail(err)
finally:
self._api.delete_repo(token=self._token, name=REPO_NAME)


class HfApiPublicTest(unittest.TestCase):
def test_staging_list_models(self):
Expand Down