From 7f635253bb966833b234c4663b1cab68928cdb2c Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Wed, 2 Feb 2022 19:25:07 +0300 Subject: [PATCH 01/16] tensorboard in keras_mixin --- src/huggingface_hub/keras_mixin.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 9b6fc30694..94192151c1 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -2,6 +2,7 @@ import logging import os from pathlib import Path +from shutil import copy from typing import Any, Dict, Optional, Union from huggingface_hub import ModelHubMixin @@ -71,6 +72,7 @@ def push_to_hub_keras( model, repo_path_or_name: Optional[str] = None, repo_url: Optional[str] = None, + tensorboard_dir: Optional[str] = None, commit_message: Optional[str] = "Add model", organization: Optional[str] = None, private: Optional[bool] = None, @@ -97,6 +99,9 @@ def push_to_hub_keras( Specify this in case you want to push to an existing repository in the hub. If unspecified, a new repository will be created in your namespace (unless you specify an :obj:`organization`) with :obj:`repo_name`. + tensorboard_dir (:obj:`str`, `optional`): + TensorBoard logging directory. Specify this in case you have used TensorBoard callback and would like + to host TensorBoard on your model repository. commit_message (:obj:`str`, `optional`): Message to commit while pushing. Will default to :obj:`"add model"`. organization (:obj:`str`, `optional`): @@ -174,6 +179,7 @@ def push_to_hub_keras( **model_save_kwargs, ) + copy(tensorboard_dir, repo_path_or_name) # Commit and push! repo.git_add(auto_lfs_track=True) repo.git_commit(commit_message) From daf596c445b083abd0541384b6c316453c0a8516 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 12:53:30 +0300 Subject: [PATCH 02/16] fix --- src/huggingface_hub/keras_mixin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 94192151c1..fb78ac3439 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -2,7 +2,7 @@ import logging import os from pathlib import Path -from shutil import copy +from shutil import copytree from typing import Any, Dict, Optional, Union from huggingface_hub import ModelHubMixin @@ -179,7 +179,7 @@ def push_to_hub_keras( **model_save_kwargs, ) - copy(tensorboard_dir, repo_path_or_name) + copytree(tensorboard_dir, repo_path_or_name/logs) # Commit and push! repo.git_add(auto_lfs_track=True) repo.git_commit(commit_message) From a6857224db9ab56fc14dac3251d044ae81e064d9 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 13:20:17 +0300 Subject: [PATCH 03/16] directory fix --- src/huggingface_hub/keras_mixin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index fb78ac3439..5e6d254826 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -179,7 +179,7 @@ def push_to_hub_keras( **model_save_kwargs, ) - copytree(tensorboard_dir, repo_path_or_name/logs) + copytree(tensorboard_dir, f"{repo_path_or_name}/logs") # Commit and push! repo.git_add(auto_lfs_track=True) repo.git_commit(commit_message) From ed0af7db835fc1a46f482a3e3481bb659bf7ae42 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 13:54:43 +0300 Subject: [PATCH 04/16] wrote tests --- tests/test_keras_integration.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index d5ccbbe32c..c49e52d9dc 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -292,6 +292,36 @@ def test_push_to_hub(self): self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) + def test_push_to_hub_tensorboard(self): + os.mkdir("./log_dir") + with open("./log_dir/tensorboard.txt", "w") as fp: + fp.write("Keras FTW") + REPO_NAME = repo_name("PUSH_TO_HUB") + model = self.model_init() + model.build((None, 2)) + push_to_hub_keras( + model, + repo_path_or_name=f"{WORKING_REPO_DIR}/{REPO_NAME}", + tensorboard_dir="./log_dir", + api_endpoint=ENDPOINT_STAGING, + use_auth_token=self._token, + git_user="ci", + git_email="ci@dummy.com", + config={"num": 7, "act": "gelu_fast"}, + include_optimizer=False, + ) + self.assertEqual( + os.path.exists(f"{WORKING_REPO_DIR}/{REPO_NAME}/logs/tensorboard.txt"), True + ) + + model_info = HfApi(endpoint=ENDPOINT_STAGING).model_info( + f"{USER}/{REPO_NAME}", + ) + + self.assertEqual(model_info.modelId, f"{USER}/{REPO_NAME}") + + self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) + def test_push_to_hub_model_kwargs(self): REPO_NAME = repo_name("PUSH_TO_HUB") model = self.model_init() From ebc159aa3c79ce307894def8c9475d1d92abe4ad Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 14:03:15 +0300 Subject: [PATCH 05/16] test directory name fix --- tests/test_keras_integration.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index c49e52d9dc..73c8a2751a 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -293,8 +293,8 @@ def test_push_to_hub(self): self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) def test_push_to_hub_tensorboard(self): - os.mkdir("./log_dir") - with open("./log_dir/tensorboard.txt", "w") as fp: + os.mkdir("./tb_log_dir") + with open("./tb_log_dir/tensorboard.txt", "w") as fp: fp.write("Keras FTW") REPO_NAME = repo_name("PUSH_TO_HUB") model = self.model_init() @@ -302,7 +302,7 @@ def test_push_to_hub_tensorboard(self): push_to_hub_keras( model, repo_path_or_name=f"{WORKING_REPO_DIR}/{REPO_NAME}", - tensorboard_dir="./log_dir", + tensorboard_dir="./tb_log_dir", api_endpoint=ENDPOINT_STAGING, use_auth_token=self._token, git_user="ci", From f3260b210b1be4a821dae6575dea2b849fd239d1 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 14:10:50 +0300 Subject: [PATCH 06/16] changed the directory to dummy repo dir --- tests/test_keras_integration.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 73c8a2751a..bd503046d2 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -293,8 +293,8 @@ def test_push_to_hub(self): self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) def test_push_to_hub_tensorboard(self): - os.mkdir("./tb_log_dir") - with open("./tb_log_dir/tensorboard.txt", "w") as fp: + os.mkdir("{WORKING_REPO_DIR}/tb_log_dir") + with open("{WORKING_REPO_DIR}/tb_log_dir/tensorboard.txt", "w") as fp: fp.write("Keras FTW") REPO_NAME = repo_name("PUSH_TO_HUB") model = self.model_init() @@ -302,7 +302,7 @@ def test_push_to_hub_tensorboard(self): push_to_hub_keras( model, repo_path_or_name=f"{WORKING_REPO_DIR}/{REPO_NAME}", - tensorboard_dir="./tb_log_dir", + tensorboard_dir="{WORKING_REPO_DIR}/tb_log_dir", api_endpoint=ENDPOINT_STAGING, use_auth_token=self._token, git_user="ci", From 7a7a4aaf5d2c41a59847cf249a5f3a4db40c8522 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 14:22:13 +0300 Subject: [PATCH 07/16] changed the directory to dummy repo dir --- tests/test_keras_integration.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index bd503046d2..4f658d6d8b 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -293,8 +293,8 @@ def test_push_to_hub(self): self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) def test_push_to_hub_tensorboard(self): - os.mkdir("{WORKING_REPO_DIR}/tb_log_dir") - with open("{WORKING_REPO_DIR}/tb_log_dir/tensorboard.txt", "w") as fp: + os.mkdir("f{WORKING_REPO_DIR}/tb_log_dir") + with open("f{WORKING_REPO_DIR}/tb_log_dir/tensorboard.txt", "w") as fp: fp.write("Keras FTW") REPO_NAME = repo_name("PUSH_TO_HUB") model = self.model_init() @@ -302,7 +302,7 @@ def test_push_to_hub_tensorboard(self): push_to_hub_keras( model, repo_path_or_name=f"{WORKING_REPO_DIR}/{REPO_NAME}", - tensorboard_dir="{WORKING_REPO_DIR}/tb_log_dir", + tensorboard_dir="f{WORKING_REPO_DIR}/tb_log_dir", api_endpoint=ENDPOINT_STAGING, use_auth_token=self._token, git_user="ci", From 573bd8d025889e9815aa06b410e473d84060f841 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 14:26:35 +0300 Subject: [PATCH 08/16] fixed fstring --- tests/test_keras_integration.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 4f658d6d8b..1e28839cd5 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -293,8 +293,8 @@ def test_push_to_hub(self): self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) def test_push_to_hub_tensorboard(self): - os.mkdir("f{WORKING_REPO_DIR}/tb_log_dir") - with open("f{WORKING_REPO_DIR}/tb_log_dir/tensorboard.txt", "w") as fp: + os.mkdir(f"{WORKING_REPO_DIR}/tb_log_dir") + with open(f"{WORKING_REPO_DIR}/tb_log_dir/tensorboard.txt", "w") as fp: fp.write("Keras FTW") REPO_NAME = repo_name("PUSH_TO_HUB") model = self.model_init() @@ -302,7 +302,7 @@ def test_push_to_hub_tensorboard(self): push_to_hub_keras( model, repo_path_or_name=f"{WORKING_REPO_DIR}/{REPO_NAME}", - tensorboard_dir="f{WORKING_REPO_DIR}/tb_log_dir", + tensorboard_dir=f"{WORKING_REPO_DIR}/tb_log_dir", api_endpoint=ENDPOINT_STAGING, use_auth_token=self._token, git_user="ci", From 9009823c54d003b8e9ca3281fc9b075507eb07be Mon Sep 17 00:00:00 2001 From: Merve Noyan Date: Fri, 4 Feb 2022 16:36:27 +0300 Subject: [PATCH 09/16] Update src/huggingface_hub/keras_mixin.py Co-authored-by: Omar Sanseviero --- src/huggingface_hub/keras_mixin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index 5e6d254826..ff6363b6d8 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -100,8 +100,8 @@ def push_to_hub_keras( repository will be created in your namespace (unless you specify an :obj:`organization`) with :obj:`repo_name`. tensorboard_dir (:obj:`str`, `optional`): - TensorBoard logging directory. Specify this in case you have used TensorBoard callback and would like - to host TensorBoard on your model repository. + TensorBoard logging directory to be pushed. The Hub automatically hosts + and displays a TensorBoard instance if log files are included in the repository. commit_message (:obj:`str`, `optional`): Message to commit while pushing. Will default to :obj:`"add model"`. organization (:obj:`str`, `optional`): From 87a318eb450baeb8fb4b6a22e9aaf0ac5224751c Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 16:49:17 +0300 Subject: [PATCH 10/16] committed suggestions --- src/huggingface_hub/keras_mixin.py | 10 +++++----- tests/test_keras_integration.py | 16 ++++------------ 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/huggingface_hub/keras_mixin.py b/src/huggingface_hub/keras_mixin.py index ff6363b6d8..b615f95254 100644 --- a/src/huggingface_hub/keras_mixin.py +++ b/src/huggingface_hub/keras_mixin.py @@ -72,7 +72,7 @@ def push_to_hub_keras( model, repo_path_or_name: Optional[str] = None, repo_url: Optional[str] = None, - tensorboard_dir: Optional[str] = None, + log_dir: Optional[str] = None, commit_message: Optional[str] = "Add model", organization: Optional[str] = None, private: Optional[bool] = None, @@ -99,8 +99,8 @@ def push_to_hub_keras( Specify this in case you want to push to an existing repository in the hub. If unspecified, a new repository will be created in your namespace (unless you specify an :obj:`organization`) with :obj:`repo_name`. - tensorboard_dir (:obj:`str`, `optional`): - TensorBoard logging directory to be pushed. The Hub automatically hosts + log_dir (:obj:`str`, `optional`): + TensorBoard logging directory to be pushed. The Hub automatically hosts and displays a TensorBoard instance if log files are included in the repository. commit_message (:obj:`str`, `optional`): Message to commit while pushing. Will default to :obj:`"add model"`. @@ -178,8 +178,8 @@ def push_to_hub_keras( include_optimizer=include_optimizer, **model_save_kwargs, ) - - copytree(tensorboard_dir, f"{repo_path_or_name}/logs") + if log_dir is not None: + copytree(log_dir, f"{repo_path_or_name}/logs") # Commit and push! repo.git_add(auto_lfs_track=True) repo.git_commit(commit_message) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 1e28839cd5..caa866b868 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -293,7 +293,7 @@ def test_push_to_hub(self): self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) def test_push_to_hub_tensorboard(self): - os.mkdir(f"{WORKING_REPO_DIR}/tb_log_dir") + os.mkdirs(f"{WORKING_REPO_DIR}/tb_log_dir") with open(f"{WORKING_REPO_DIR}/tb_log_dir/tensorboard.txt", "w") as fp: fp.write("Keras FTW") REPO_NAME = repo_name("PUSH_TO_HUB") @@ -302,24 +302,16 @@ def test_push_to_hub_tensorboard(self): push_to_hub_keras( model, repo_path_or_name=f"{WORKING_REPO_DIR}/{REPO_NAME}", - tensorboard_dir=f"{WORKING_REPO_DIR}/tb_log_dir", + log_dir=f"{WORKING_REPO_DIR}/tb_log_dir", api_endpoint=ENDPOINT_STAGING, use_auth_token=self._token, git_user="ci", git_email="ci@dummy.com", - config={"num": 7, "act": "gelu_fast"}, - include_optimizer=False, ) - self.assertEqual( - os.path.exists(f"{WORKING_REPO_DIR}/{REPO_NAME}/logs/tensorboard.txt"), True - ) - - model_info = HfApi(endpoint=ENDPOINT_STAGING).model_info( - f"{USER}/{REPO_NAME}", + self.assertTrue( + os.path.exists(f"{WORKING_REPO_DIR}/{REPO_NAME}/logs/tensorboard.txt") ) - self.assertEqual(model_info.modelId, f"{USER}/{REPO_NAME}") - self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) def test_push_to_hub_model_kwargs(self): From cf3be216039ab0605a6600c915dd13c6bb2346b1 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 16:52:37 +0300 Subject: [PATCH 11/16] committed suggestions --- tests/test_keras_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index caa866b868..00c63153cd 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -293,7 +293,7 @@ def test_push_to_hub(self): self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) def test_push_to_hub_tensorboard(self): - os.mkdirs(f"{WORKING_REPO_DIR}/tb_log_dir") + os.makedirs(f"{WORKING_REPO_DIR}/tb_log_dir") with open(f"{WORKING_REPO_DIR}/tb_log_dir/tensorboard.txt", "w") as fp: fp.write("Keras FTW") REPO_NAME = repo_name("PUSH_TO_HUB") From 4f790b3f8fc19bc26ba10e7e26dae0ef87ff6980 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 18:19:34 +0300 Subject: [PATCH 12/16] test that checks if file is in remote --- tests/test_keras_integration.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 00c63153cd..15bd4987a4 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -308,9 +308,11 @@ def test_push_to_hub_tensorboard(self): git_user="ci", git_email="ci@dummy.com", ) - self.assertTrue( - os.path.exists(f"{WORKING_REPO_DIR}/{REPO_NAME}/logs/tensorboard.txt") + model_info = HfApi(endpoint=ENDPOINT_STAGING).model_info( + f"{USER}/{REPO_NAME}", ) + model_files = [f.rfilename for f in model_info.siblings] + self.assertTrue("tensorboard.txt" in model_files) self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) From 975291cdd082fea8a22400a5ba5a20d80244023e Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 18:31:42 +0300 Subject: [PATCH 13/16] test that checks if file is in remote --- tests/test_keras_integration.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 15bd4987a4..1fd17dea98 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -312,7 +312,8 @@ def test_push_to_hub_tensorboard(self): f"{USER}/{REPO_NAME}", ) model_files = [f.rfilename for f in model_info.siblings] - self.assertTrue("tensorboard.txt" in model_files) + self.assertTrue("logs/tensorboard.txt" in model_files) + self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) From 954b4264dcb5444724c86c021c269ea8c2a697e2 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 18:35:16 +0300 Subject: [PATCH 14/16] make style --- tests/test_keras_integration.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 1fd17dea98..c5129bc7c2 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -313,7 +313,6 @@ def test_push_to_hub_tensorboard(self): ) model_files = [f.rfilename for f in model_info.siblings] self.assertTrue("logs/tensorboard.txt" in model_files) - self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) From 6c3c6a753d4f690844adda7f2a0e7f0425213f86 Mon Sep 17 00:00:00 2001 From: Merve Noyan Date: Fri, 4 Feb 2022 19:01:57 +0300 Subject: [PATCH 15/16] Update tests/test_keras_integration.py Co-authored-by: Omar Sanseviero --- tests/test_keras_integration.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index c5129bc7c2..179fe616f1 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -311,8 +311,7 @@ def test_push_to_hub_tensorboard(self): model_info = HfApi(endpoint=ENDPOINT_STAGING).model_info( f"{USER}/{REPO_NAME}", ) - model_files = [f.rfilename for f in model_info.siblings] - self.assertTrue("logs/tensorboard.txt" in model_files) + self.assertTrue("logs/tensorboard.txt" in model_info.siblings) self._api.delete_repo(name=f"{REPO_NAME}", token=self._token) From ccd364edbd27346f660b493a37e593ca2da01549 Mon Sep 17 00:00:00 2001 From: merveenoyan Date: Fri, 4 Feb 2022 19:09:13 +0300 Subject: [PATCH 16/16] revert --- tests/test_keras_integration.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_keras_integration.py b/tests/test_keras_integration.py index 179fe616f1..1b887ee22d 100644 --- a/tests/test_keras_integration.py +++ b/tests/test_keras_integration.py @@ -311,7 +311,10 @@ def test_push_to_hub_tensorboard(self): model_info = HfApi(endpoint=ENDPOINT_STAGING).model_info( f"{USER}/{REPO_NAME}", ) - self.assertTrue("logs/tensorboard.txt" in model_info.siblings) + + self.assertTrue( + "logs/tensorboard.txt" in [f.rfilename for f in model_info.siblings] + ) self._api.delete_repo(name=f"{REPO_NAME}", token=self._token)