test_model_card.py 3.35 KB
Newer Older
thomwolf's avatar
thomwolf committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# coding=utf-8
# Copyright 2019 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.
Aymeric Augustin's avatar
Aymeric Augustin committed
15

thomwolf's avatar
thomwolf committed
16
17

import json
Aymeric Augustin's avatar
Aymeric Augustin committed
18
import os
19
import tempfile
thomwolf's avatar
thomwolf committed
20
21
import unittest

22
from transformers.modelcard import ModelCard
Aymeric Augustin's avatar
Aymeric Augustin committed
23

thomwolf's avatar
thomwolf committed
24

25
class ModelCardTester(unittest.TestCase):
thomwolf's avatar
thomwolf committed
26
    def setUp(self):
27
28
29
30
31
32
33
34
35
        self.inputs_dict = {
            "model_details": {
                "Organization": "testing",
                "Model date": "today",
                "Model version": "v2.1, Developed by Test Corp in 2019.",
                "Architecture": "Convolutional Neural Network.",
            },
            "metrics": "BLEU and ROUGE-1",
            "evaluation_data": {
36
                "Datasets": {"BLEU": "My-great-dataset-v1", "ROUGE-1": "My-short-dataset-v2.1"},
37
38
39
40
41
42
                "Preprocessing": "See details on https://arxiv.org/pdf/1810.03993.pdf",
            },
            "training_data": {
                "Dataset": "English Wikipedia dump dated 2018-12-01",
                "Preprocessing": "Using SentencePiece vocabulary of size 52k tokens. See details on https://arxiv.org/pdf/1810.03993.pdf",
            },
43
            "quantitative_analyses": {"BLEU": 55.1, "ROUGE-1": 76},
44
        }
thomwolf's avatar
thomwolf committed
45
46

    def test_model_card_common_properties(self):
47
        modelcard = ModelCard.from_dict(self.inputs_dict)
48
49
50
51
52
53
54
55
56
        self.assertTrue(hasattr(modelcard, "model_details"))
        self.assertTrue(hasattr(modelcard, "intended_use"))
        self.assertTrue(hasattr(modelcard, "factors"))
        self.assertTrue(hasattr(modelcard, "metrics"))
        self.assertTrue(hasattr(modelcard, "evaluation_data"))
        self.assertTrue(hasattr(modelcard, "training_data"))
        self.assertTrue(hasattr(modelcard, "quantitative_analyses"))
        self.assertTrue(hasattr(modelcard, "ethical_considerations"))
        self.assertTrue(hasattr(modelcard, "caveats_and_recommendations"))
thomwolf's avatar
thomwolf committed
57
58

    def test_model_card_to_json_string(self):
59
60
        modelcard = ModelCard.from_dict(self.inputs_dict)
        obj = json.loads(modelcard.to_json_string())
thomwolf's avatar
thomwolf committed
61
62
63
64
65
66
        for key, value in self.inputs_dict.items():
            self.assertEqual(obj[key], value)

    def test_model_card_to_json_file(self):
        model_card_first = ModelCard.from_dict(self.inputs_dict)

67
        with tempfile.TemporaryDirectory() as tmpdirname:
68
            filename = os.path.join(tmpdirname, "modelcard.json")
thomwolf's avatar
thomwolf committed
69
70
71
72
73
            model_card_first.to_json_file(filename)
            model_card_second = ModelCard.from_json_file(filename)

        self.assertEqual(model_card_second.to_dict(), model_card_first.to_dict())

thomwolf's avatar
thomwolf committed
74
75
76
    def test_model_card_from_and_save_pretrained(self):
        model_card_first = ModelCard.from_dict(self.inputs_dict)

77
        with tempfile.TemporaryDirectory() as tmpdirname:
thomwolf's avatar
thomwolf committed
78
79
80
81
            model_card_first.save_pretrained(tmpdirname)
            model_card_second = ModelCard.from_pretrained(tmpdirname)

        self.assertEqual(model_card_second.to_dict(), model_card_first.to_dict())