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

Logging fix for hf_api, logging documentation #748

Merged
merged 3 commits into from
Mar 10, 2022
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
36 changes: 35 additions & 1 deletion src/huggingface_hub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,38 @@ API.

```python
inference = InferenceApi("bert-base-uncased", task="feature-extraction", token=API_TOKEN)
```
```

## Controlling the logging of `huggingface_hub`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this information be in README? How are we going to create the documentation for this library? We probably need quite a few user guides and I think this should go in one of those, which probably should be located in the /doc folder.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm putting this here for now, but this should definitely be in a separate guide as outlined in https://github.com/huggingface/huggingface_hub/issues/754


The `huggingface_hub` package exposes a `logging` utility to control the logging level of the package itself.
You can import it as such:

```py
from huggingface_hub import logging
```

Then, you may define the verbosity in order to update the amount of logs you'll see:

```python
from huggingface_hub import logging

logging.set_verbosity_error()
logging.set_verbosity_warning()
logging.set_verbosity_info()
logging.set_verbosity_debug()

logging.set_verbosity(...)
```

The levels should be understood as follows:

- `error`: this will only show critical logs about usage which may result in an error or unexpected behavior.
- `warning`: this will show logs which aren't critical, about usage which may result in unintended behavior.
Additionally, important informative logs may be shown.
- `info`: this will show most logs, including some verbose logging regarding what is happening under the hood.
If something is behaving in an unexpected manner, we recommend switching the verbosity level to this in order
to get more information.
- `debug`: this shows all logs, including some internal logs which may be used to track exactly what's happening
under the hood.

16 changes: 11 additions & 5 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
import subprocess
import sys
Expand All @@ -31,6 +30,7 @@
REPO_TYPES_URL_PREFIXES,
SPACES_SDK_TYPES,
)
from .utils import logging
from .utils.endpoint_helpers import (
AttributeDictionary,
DatasetFilter,
Expand All @@ -49,6 +49,8 @@

USERNAME_PLACEHOLDER = "hf_user"

logger = logging.get_logger(__name__)


def repo_type_and_id_from_hf_id(hf_id: str):
"""
Expand Down Expand Up @@ -422,8 +424,9 @@ def login(self, username: str, password: str) -> str:

Throws: requests.exceptions.HTTPError if credentials are invalid
"""
logging.error(
"HfApi.login: This method is deprecated in favor of `set_access_token`."
warnings.warn(
"HfApi.login: This method is deprecated in favor of `set_access_token` and will be removed in v0.7.",
FutureWarning,
)
path = f"{self.endpoint}/api/login"
r = requests.post(path, json={"username": username, "password": password})
Expand Down Expand Up @@ -492,7 +495,10 @@ def logout(self, token: Optional[str] = None) -> None:
token (``str``, `optional`):
Hugging Face token. Will default to the locally saved token if not provided.
"""
logging.error("This method is deprecated in favor of `unset_access_token`.")
warnings.warn(
LysandreJik marked this conversation as resolved.
Show resolved Hide resolved
"HfApi.logout: This method is deprecated in favor of `unset_access_token` and will be removed in v0.7.",
FutureWarning,
)
if token is None:
token = HfFolder.get_token()
if token is None:
Expand Down Expand Up @@ -1265,7 +1271,7 @@ def move_repo(
)
else:
raise e
logging.info(
logger.info(
"Accepted transfer request. You will get an email once this is successfully completed."
)

Expand Down
4 changes: 2 additions & 2 deletions src/huggingface_hub/keras_mixin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import logging
import os
from pathlib import Path
from shutil import copytree, rmtree
Expand All @@ -16,9 +15,10 @@
from .constants import CONFIG_NAME
from .hf_api import HfApi, HfFolder
from .repository import Repository
from .utils import logging


logger = logging.getLogger(__name__)
logger = logging.get_logger(__name__)

if is_tf_available():
import tensorflow as tf
Expand Down
19 changes: 19 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ def test_login_cli(self):
read_from_credential_store(USERNAME_PLACEHOLDER), (None, None)
)

def test_login_deprecation_error(self):
with pytest.warns(
FutureWarning,
match=r"HfApi.login: This method is deprecated in favor of "
r"`set_access_token` and will be removed in v0.7.",
Comment on lines +145 to +146
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not black formatted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it is! I've split it in two to respect the 88 line char limit, black seems okay with it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black unindented it, the second line was previously indented at the same level as the | in match=|

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

):
self._api.login(username=USER, password=PASS)

def test_logout_deprecation_error(self):
with pytest.warns(
FutureWarning,
match=r"HfApi.logout: This method is deprecated in favor of "
r"`unset_access_token` and will be removed in v0.7.",
):
try:
self._api.logout()
except HTTPError:
pass


class HfApiCommonTestWithLogin(HfApiCommonTest):
@classmethod
Expand Down