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

FIX fix a bug in _filter_emissions to accept numbers w/o decimal and dict emissions #753

Merged
merged 4 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
6 changes: 3 additions & 3 deletions src/huggingface_hub/utils/endpoint_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ def _filter_emissions(
if hasattr(model, "cardData"):
if isinstance(model.cardData, dict):
emission = model.cardData.get("co2_eq_emissions", None)
if isinstance(emissions, dict):
emission = emissions["emissions"]
if isinstance(emission, dict):
emission = emission["emissions"]
if emission:
emission = str(emission)
if any(char.isdigit() for char in emission):
emission = re.search("\d+\.\d+", str(emission)).group(0)
emission = re.search("\d+\.\d+|\d+", emission).group(0)
emissions.append((i, float(emission)))
filtered_results = []
for (idx, emission) in emissions:
Expand Down
14 changes: 13 additions & 1 deletion tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@
repo_type_and_id_from_hf_id,
)
from huggingface_hub.utils import logging
from huggingface_hub.utils.endpoint_helpers import DatasetFilter, ModelFilter
from huggingface_hub.utils.endpoint_helpers import (
DatasetFilter,
ModelFilter,
_filter_emissions,
)
from requests.exceptions import HTTPError

from .testing_constants import (
Expand Down Expand Up @@ -861,6 +865,14 @@ def test_filter_models_with_cardData(self):
models = _api.list_models("co2_eq_emissions")
self.assertTrue(all([not hasattr(model, "cardData") for model in models]))

def test_filter_emissions_dict(self):
# tests that dictionary is handled correctly as "emissions" and that
# 17g is accepted and parsed correctly as a value
# regression test for #753
model = ModelInfo(cardData={"co2_eq_emissions": {"emissions": "17g"}})
res = _filter_emissions([model], -1, 100)
assert len(res) == 1

@with_production_testing
def test_filter_emissions_with_max(self):
_api = HfApi()
Expand Down