Skip to content

Commit

Permalink
Remove modelcards dependency (huggingface#2050)
Browse files Browse the repository at this point in the history
* Switch to huggingface_hub.ModelCard

* Remove modelcards dependency in favor of Jinja2
  • Loading branch information
Wauplin authored Jan 20, 2023
1 parent 5ea4be8 commit bcb4767
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 43 deletions.
2 changes: 1 addition & 1 deletion docker/diffusers-flax-cpu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \
hf-doc-builder \
huggingface-hub \
Jinja2 \
librosa \
modelcards \
numpy \
scipy \
tensorboard \
Expand Down
2 changes: 1 addition & 1 deletion docker/diffusers-flax-tpu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \
hf-doc-builder \
huggingface-hub \
Jinja2 \
librosa \
modelcards \
numpy \
scipy \
tensorboard \
Expand Down
2 changes: 1 addition & 1 deletion docker/diffusers-onnxruntime-cpu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \
hf-doc-builder \
huggingface-hub \
Jinja2 \
librosa \
modelcards \
numpy \
scipy \
tensorboard \
Expand Down
2 changes: 1 addition & 1 deletion docker/diffusers-onnxruntime-cuda/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \
hf-doc-builder \
huggingface-hub \
Jinja2 \
librosa \
modelcards \
numpy \
scipy \
tensorboard \
Expand Down
2 changes: 1 addition & 1 deletion docker/diffusers-pytorch-cpu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \
hf-doc-builder \
huggingface-hub \
Jinja2 \
librosa \
modelcards \
numpy \
scipy \
tensorboard \
Expand Down
2 changes: 1 addition & 1 deletion docker/diffusers-pytorch-cuda/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
datasets \
hf-doc-builder \
huggingface-hub \
Jinja2 \
librosa \
modelcards \
numpy \
scipy \
tensorboard \
Expand Down
2 changes: 1 addition & 1 deletion examples/dreambooth/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ torchvision
transformers>=4.25.1
ftfy
tensorboard
modelcards
Jinja2
2 changes: 1 addition & 1 deletion examples/dreambooth/requirements_flax.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ torch
torchvision
ftfy
tensorboard
modelcards
Jinja2
2 changes: 1 addition & 1 deletion examples/research_projects/colossalai/requirement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ torch
torchvision
ftfy
tensorboard
modelcards
Jinja2
transformers
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ torchvision
transformers>=4.21.0
ftfy
tensorboard
modelcards
Jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ torchvision
transformers>=4.21.0
ftfy
tensorboard
modelcards
Jinja2
intel_extension_for_pytorch>=1.13
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ torchvision
transformers>=4.25.1
ftfy
tensorboard
modelcards
Jinja2
2 changes: 1 addition & 1 deletion examples/text_to_image/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ transformers>=4.25.1
datasets
ftfy
tensorboard
modelcards
Jinja2
2 changes: 1 addition & 1 deletion examples/text_to_image/requirements_flax.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ torch
torchvision
ftfy
tensorboard
modelcards
Jinja2
2 changes: 1 addition & 1 deletion examples/textual_inversion/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ torchvision
transformers>=4.25.1
ftfy
tensorboard
modelcards
Jinja2
2 changes: 1 addition & 1 deletion examples/textual_inversion/requirements_flax.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ torch
torchvision
ftfy
tensorboard
modelcards
Jinja2
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@
"isort>=5.5.4",
"jax>=0.2.8,!=0.3.2",
"jaxlib>=0.1.65",
"Jinja2",
"k-diffusion>=0.0.12",
"librosa",
"modelcards>=0.1.4",
"numpy",
"parameterized",
"pytest",
Expand Down Expand Up @@ -180,9 +180,10 @@ def run(self):
extras = {}
extras["quality"] = deps_list("black", "isort", "flake8", "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(
"datasets",
"Jinja2",
"k-diffusion",
"librosa",
"parameterized",
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/dependency_versions_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"isort": "isort>=5.5.4",
"jax": "jax>=0.2.8,!=0.3.2",
"jaxlib": "jaxlib>=0.1.65",
"Jinja2": "Jinja2",
"k-diffusion": "k-diffusion>=0.0.12",
"librosa": "librosa",
"modelcards": "modelcards>=0.1.4",
"numpy": "numpy",
"parameterized": "parameterized",
"pytest": "pytest",
Expand Down
1 change: 0 additions & 1 deletion src/diffusers/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
is_k_diffusion_available,
is_k_diffusion_version,
is_librosa_available,
is_modelcards_available,
is_onnx_available,
is_safetensors_available,
is_scipy_available,
Expand Down
19 changes: 8 additions & 11 deletions src/diffusers/utils/hub_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from typing import Dict, Optional, Union
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 .constants import HUGGINGFACE_CO_RESOLVE_ENDPOINT
Expand All @@ -31,21 +32,16 @@
_onnxruntime_version,
_torch_version,
is_flax_available,
is_modelcards_available,
is_onnx_available,
is_torch_available,
)
from .logging import get_logger


if is_modelcards_available():
from modelcards import CardData, ModelCard


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
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
Expand Down Expand Up @@ -87,10 +83,11 @@ def get_full_repo_name(model_id: str, organization: Optional[str] = None, token:


def create_model_card(args, model_name):
if not is_modelcards_available:
if not is_jinja_available():
raise ValueError(
"Please make sure to have `modelcards` installed when using the `create_model_card` function. You can"
" install the package with `pip install modelcards`."
"Modelcard rendering is based on Jinja templates."
" 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]:
Expand All @@ -100,7 +97,7 @@ def create_model_card(args, model_name):
repo_name = get_full_repo_name(model_name, token=hub_token)

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",
license="apache-2.0",
library_name="diffusers",
Expand Down
13 changes: 1 addition & 12 deletions src/diffusers/utils/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from collections import OrderedDict
from typing import Union

from huggingface_hub.utils import is_jinja_available # noqa: F401
from packaging import version
from packaging.version import Version, parse

Expand Down Expand Up @@ -146,14 +147,6 @@
_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"
_onnx_available = importlib.util.find_spec("onnxruntime") is not None
if _onnx_available:
Expand Down Expand Up @@ -253,10 +246,6 @@ def is_unidecode_available():
return _unidecode_available


def is_modelcards_available():
return _modelcards_available


def is_onnx_available():
return _onnx_available

Expand Down
51 changes: 51 additions & 0 deletions tests/test_hub_utils.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit bcb4767

Please sign in to comment.