Remove modelcards dependency (#2050)

* Switch to huggingface_hub.ModelCard

* Remove modelcards dependency in favor of Jinja2
This commit is contained in:
Lucain 2023-01-20 16:39:42 +01:00 committed by GitHub
parent 5ea4be86ab
commit bcb476797c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 80 additions and 43 deletions

View File

@ -34,8 +34,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \ datasets \
hf-doc-builder \ hf-doc-builder \
huggingface-hub \ huggingface-hub \
Jinja2 \
librosa \ librosa \
modelcards \
numpy \ numpy \
scipy \ scipy \
tensorboard \ tensorboard \

View File

@ -36,8 +36,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \ datasets \
hf-doc-builder \ hf-doc-builder \
huggingface-hub \ huggingface-hub \
Jinja2 \
librosa \ librosa \
modelcards \
numpy \ numpy \
scipy \ scipy \
tensorboard \ tensorboard \

View File

@ -34,8 +34,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \ datasets \
hf-doc-builder \ hf-doc-builder \
huggingface-hub \ huggingface-hub \
Jinja2 \
librosa \ librosa \
modelcards \
numpy \ numpy \
scipy \ scipy \
tensorboard \ tensorboard \

View File

@ -34,8 +34,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \ datasets \
hf-doc-builder \ hf-doc-builder \
huggingface-hub \ huggingface-hub \
Jinja2 \
librosa \ librosa \
modelcards \
numpy \ numpy \
scipy \ scipy \
tensorboard \ tensorboard \

View File

@ -33,8 +33,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \ datasets \
hf-doc-builder \ hf-doc-builder \
huggingface-hub \ huggingface-hub \
Jinja2 \
librosa \ librosa \
modelcards \
numpy \ numpy \
scipy \ scipy \
tensorboard \ tensorboard \

View File

@ -33,8 +33,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \ datasets \
hf-doc-builder \ hf-doc-builder \
huggingface-hub \ huggingface-hub \
Jinja2 \
librosa \ librosa \
modelcards \
numpy \ numpy \
scipy \ scipy \
tensorboard \ tensorboard \

View File

@ -3,4 +3,4 @@ torchvision
transformers>=4.25.1 transformers>=4.25.1
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2

View File

@ -5,4 +5,4 @@ torch
torchvision torchvision
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2

View File

@ -3,5 +3,5 @@ torch
torchvision torchvision
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2
transformers transformers

View File

@ -4,4 +4,4 @@ torchvision
transformers>=4.21.0 transformers>=4.21.0
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2

View File

@ -3,5 +3,5 @@ torchvision
transformers>=4.21.0 transformers>=4.21.0
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2
intel_extension_for_pytorch>=1.13 intel_extension_for_pytorch>=1.13

View File

@ -3,4 +3,4 @@ torchvision
transformers>=4.25.1 transformers>=4.25.1
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2

View File

@ -4,4 +4,4 @@ transformers>=4.25.1
datasets datasets
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2

View File

@ -6,4 +6,4 @@ torch
torchvision torchvision
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2

View File

@ -3,4 +3,4 @@ torchvision
transformers>=4.25.1 transformers>=4.25.1
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2

View File

@ -5,4 +5,4 @@ torch
torchvision torchvision
ftfy ftfy
tensorboard tensorboard
modelcards Jinja2

View File

@ -91,9 +91,9 @@ _deps = [
"isort>=5.5.4", "isort>=5.5.4",
"jax>=0.2.8,!=0.3.2", "jax>=0.2.8,!=0.3.2",
"jaxlib>=0.1.65", "jaxlib>=0.1.65",
"Jinja2",
"k-diffusion>=0.0.12", "k-diffusion>=0.0.12",
"librosa", "librosa",
"modelcards>=0.1.4",
"numpy", "numpy",
"parameterized", "parameterized",
"pytest", "pytest",
@ -180,9 +180,10 @@ extras = {}
extras = {} extras = {}
extras["quality"] = deps_list("black", "isort", "flake8", "hf-doc-builder") extras["quality"] = deps_list("black", "isort", "flake8", "hf-doc-builder")
extras["docs"] = deps_list("hf-doc-builder") extras["docs"] = deps_list("hf-doc-builder")
extras["training"] = deps_list("accelerate", "datasets", "tensorboard", "modelcards") extras["training"] = deps_list("accelerate", "datasets", "tensorboard", "Jinja2")
extras["test"] = deps_list( extras["test"] = deps_list(
"datasets", "datasets",
"Jinja2",
"k-diffusion", "k-diffusion",
"librosa", "librosa",
"parameterized", "parameterized",

View File

@ -15,9 +15,9 @@ deps = {
"isort": "isort>=5.5.4", "isort": "isort>=5.5.4",
"jax": "jax>=0.2.8,!=0.3.2", "jax": "jax>=0.2.8,!=0.3.2",
"jaxlib": "jaxlib>=0.1.65", "jaxlib": "jaxlib>=0.1.65",
"Jinja2": "Jinja2",
"k-diffusion": "k-diffusion>=0.0.12", "k-diffusion": "k-diffusion>=0.0.12",
"librosa": "librosa", "librosa": "librosa",
"modelcards": "modelcards>=0.1.4",
"numpy": "numpy", "numpy": "numpy",
"parameterized": "parameterized", "parameterized": "parameterized",
"pytest": "pytest", "pytest": "pytest",

View File

@ -48,7 +48,6 @@ from .import_utils import (
is_k_diffusion_available, is_k_diffusion_available,
is_k_diffusion_version, is_k_diffusion_version,
is_librosa_available, is_librosa_available,
is_modelcards_available,
is_onnx_available, is_onnx_available,
is_safetensors_available, is_safetensors_available,
is_scipy_available, is_scipy_available,

View File

@ -20,7 +20,8 @@ from pathlib import Path
from typing import Dict, Optional, Union from typing import Dict, Optional, Union
from uuid import uuid4 from uuid import uuid4
from huggingface_hub import HfFolder, whoami from huggingface_hub import HfFolder, ModelCard, ModelCardData, whoami
from huggingface_hub.utils import is_jinja_available
from .. import __version__ from .. import __version__
from .constants import HUGGINGFACE_CO_RESOLVE_ENDPOINT from .constants import HUGGINGFACE_CO_RESOLVE_ENDPOINT
@ -31,21 +32,16 @@ from .import_utils import (
_onnxruntime_version, _onnxruntime_version,
_torch_version, _torch_version,
is_flax_available, is_flax_available,
is_modelcards_available,
is_onnx_available, is_onnx_available,
is_torch_available, is_torch_available,
) )
from .logging import get_logger from .logging import get_logger
if is_modelcards_available():
from modelcards import CardData, ModelCard
logger = get_logger(__name__) logger = get_logger(__name__)
MODEL_CARD_TEMPLATE_PATH = Path(__file__).parent / "utils" / "model_card_template.md" MODEL_CARD_TEMPLATE_PATH = Path(__file__).parent / "model_card_template.md"
SESSION_ID = uuid4().hex SESSION_ID = uuid4().hex
HF_HUB_OFFLINE = os.getenv("HF_HUB_OFFLINE", "").upper() in ENV_VARS_TRUE_VALUES HF_HUB_OFFLINE = os.getenv("HF_HUB_OFFLINE", "").upper() in ENV_VARS_TRUE_VALUES
DISABLE_TELEMETRY = os.getenv("DISABLE_TELEMETRY", "").upper() in ENV_VARS_TRUE_VALUES DISABLE_TELEMETRY = os.getenv("DISABLE_TELEMETRY", "").upper() in ENV_VARS_TRUE_VALUES
@ -87,10 +83,11 @@ def get_full_repo_name(model_id: str, organization: Optional[str] = None, token:
def create_model_card(args, model_name): def create_model_card(args, model_name):
if not is_modelcards_available: if not is_jinja_available():
raise ValueError( raise ValueError(
"Please make sure to have `modelcards` installed when using the `create_model_card` function. You can" "Modelcard rendering is based on Jinja templates."
" install the package with `pip install modelcards`." " Please make sure to have `jinja` installed before using `create_model_card`."
" To install it, please run `pip install Jinja2`."
) )
if hasattr(args, "local_rank") and args.local_rank not in [-1, 0]: if hasattr(args, "local_rank") and args.local_rank not in [-1, 0]:
@ -100,7 +97,7 @@ def create_model_card(args, model_name):
repo_name = get_full_repo_name(model_name, token=hub_token) repo_name = get_full_repo_name(model_name, token=hub_token)
model_card = ModelCard.from_template( model_card = ModelCard.from_template(
card_data=CardData( # Card metadata object that will be converted to YAML block card_data=ModelCardData( # Card metadata object that will be converted to YAML block
language="en", language="en",
license="apache-2.0", license="apache-2.0",
library_name="diffusers", library_name="diffusers",

View File

@ -21,6 +21,7 @@ import sys
from collections import OrderedDict from collections import OrderedDict
from typing import Union from typing import Union
from huggingface_hub.utils import is_jinja_available # noqa: F401
from packaging import version from packaging import version
from packaging.version import Version, parse from packaging.version import Version, parse
@ -146,14 +147,6 @@ except importlib_metadata.PackageNotFoundError:
_unidecode_available = False _unidecode_available = False
_modelcards_available = importlib.util.find_spec("modelcards") is not None
try:
_modelcards_version = importlib_metadata.version("modelcards")
logger.debug(f"Successfully imported modelcards version {_modelcards_version}")
except importlib_metadata.PackageNotFoundError:
_modelcards_available = False
_onnxruntime_version = "N/A" _onnxruntime_version = "N/A"
_onnx_available = importlib.util.find_spec("onnxruntime") is not None _onnx_available = importlib.util.find_spec("onnxruntime") is not None
if _onnx_available: if _onnx_available:
@ -253,10 +246,6 @@ def is_unidecode_available():
return _unidecode_available return _unidecode_available
def is_modelcards_available():
return _modelcards_available
def is_onnx_available(): def is_onnx_available():
return _onnx_available return _onnx_available

51
tests/test_hub_utils.py Normal file
View File

@ -0,0 +1,51 @@
# coding=utf-8
# Copyright 2023 HuggingFace Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import Mock, patch
import diffusers.utils.hub_utils
class CreateModelCardTest(unittest.TestCase):
@patch("diffusers.utils.hub_utils.get_full_repo_name")
def test_create_model_card(self, repo_name_mock: Mock) -> None:
repo_name_mock.return_value = "full_repo_name"
with TemporaryDirectory() as tmpdir:
# Dummy args values
args = Mock()
args.output_dir = tmpdir
args.local_rank = 0
args.hub_token = "hub_token"
args.dataset_name = "dataset_name"
args.learning_rate = 0.01
args.train_batch_size = 100000
args.eval_batch_size = 10000
args.gradient_accumulation_steps = 0.01
args.adam_beta1 = 0.02
args.adam_beta2 = 0.03
args.adam_weight_decay = 0.0005
args.adam_epsilon = 0.000001
args.lr_scheduler = 1
args.lr_warmup_steps = 10
args.ema_inv_gamma = 0.001
args.ema_power = 0.1
args.ema_max_decay = 0.2
args.mixed_precision = True
# Model card mush be rendered and saved
diffusers.utils.hub_utils.create_model_card(args, model_name="model_name")
self.assertTrue((Path(tmpdir) / "README.md").is_file())