-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* repocard utilities + tests * mix those methods on Repository * handle case of no README.md inside repo * suggestion from @osanseviero Co-authored-by: Omar Sanseviero <[email protected]> * tiny doc add * `MODELCARD_NAME` => more general `REPOCARD_NAME` * Additional test Co-authored-by: Omar Sanseviero <[email protected]> Co-authored-by: Lysandre <[email protected]>
- Loading branch information
1 parent
259a4ce
commit e665982
Showing
5 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import re | ||
from pathlib import Path | ||
from typing import Dict, Optional, Union | ||
|
||
import yaml | ||
|
||
|
||
# exact same regex as in the Hub server. Please keep in sync. | ||
REGEX_YAML_BLOCK = re.compile(r"---[\n\r]+([\S\s]*?)[\n\r]+---[\n\r]") | ||
|
||
|
||
def metadata_load(local_path: Union[str, Path]) -> Optional[Dict]: | ||
content = Path(local_path).read_text() | ||
match = REGEX_YAML_BLOCK.search(content) | ||
if match: | ||
yaml_block = match.group(1) | ||
data = yaml.safe_load(yaml_block) | ||
if isinstance(data, dict): | ||
return data | ||
else: | ||
raise ValueError("repo card metadata block should be a dict") | ||
else: | ||
return None | ||
|
||
|
||
def metadata_save(local_path: Union[str, Path], data: Dict) -> None: | ||
data_yaml = yaml.dump(data, sort_keys=False) | ||
# sort_keys: keep dict order | ||
content = Path(local_path).read_text() if Path(local_path).is_file() else "" | ||
match = REGEX_YAML_BLOCK.search(content) | ||
if match: | ||
output = ( | ||
content[: match.start()] + f"---\n{data_yaml}---\n" + content[match.end() :] | ||
) | ||
else: | ||
output = f"---\n{data_yaml}---\n{content}" | ||
|
||
Path(local_path).write_text(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright 2021 The HuggingFace Team. All rights reserved. | ||
# | ||
# 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 os | ||
import shutil | ||
import unittest | ||
from pathlib import Path | ||
|
||
from huggingface_hub.constants import REPOCARD_NAME | ||
from huggingface_hub.repocard import metadata_load, metadata_save | ||
|
||
from .testing_utils import set_write_permission_and_retry | ||
|
||
|
||
DUMMY_MODELCARD = """ | ||
Hi | ||
--- | ||
license: mit | ||
datasets: | ||
- foo | ||
- bar | ||
--- | ||
Hello | ||
""" | ||
|
||
DUMMY_MODELCARD_TARGET = """ | ||
Hi | ||
--- | ||
meaning_of_life: 42 | ||
--- | ||
Hello | ||
""" | ||
|
||
DUMMY_MODELCARD_TARGET_NO_YAML = """--- | ||
meaning_of_life: 42 | ||
--- | ||
Hello | ||
""" | ||
|
||
DUMMY_MODELCARD_TARGET_NO_TAGS = """ | ||
Hello | ||
""" | ||
|
||
REPOCARD_DIR = os.path.join( | ||
os.path.dirname(os.path.abspath(__file__)), "fixtures/repocard" | ||
) | ||
|
||
|
||
class RepocardTest(unittest.TestCase): | ||
def setUp(self): | ||
os.makedirs(REPOCARD_DIR, exist_ok=True) | ||
|
||
def tearDown(self) -> None: | ||
try: | ||
shutil.rmtree(REPOCARD_DIR, onerror=set_write_permission_and_retry) | ||
except FileNotFoundError: | ||
pass | ||
|
||
def test_metadata_load(self): | ||
filepath = Path(REPOCARD_DIR) / REPOCARD_NAME | ||
filepath.write_text(DUMMY_MODELCARD) | ||
data = metadata_load(filepath) | ||
self.assertDictEqual(data, {"license": "mit", "datasets": ["foo", "bar"]}) | ||
|
||
def test_metadata_save(self): | ||
filename = "dummy_target.md" | ||
filepath = Path(REPOCARD_DIR) / filename | ||
filepath.write_text(DUMMY_MODELCARD) | ||
metadata_save(filepath, {"meaning_of_life": 42}) | ||
content = filepath.read_text() | ||
self.assertEqual(content, DUMMY_MODELCARD_TARGET) | ||
|
||
def test_metadata_save_from_file_no_yaml(self): | ||
filename = "dummy_target_2.md" | ||
filepath = Path(REPOCARD_DIR) / filename | ||
filepath.write_text("Hello\n") | ||
metadata_save(filepath, {"meaning_of_life": 42}) | ||
content = filepath.read_text() | ||
self.assertEqual(content, DUMMY_MODELCARD_TARGET_NO_YAML) | ||
|
||
def test_no_metadata_returns_none(self): | ||
filename = "dummy_target_3.md" | ||
filepath = Path(REPOCARD_DIR) / filename | ||
filepath.write_text(DUMMY_MODELCARD_TARGET_NO_TAGS) | ||
data = metadata_load(filepath) | ||
self.assertEqual(data, None) |