test_model_card.py 3.37 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
thomwolf's avatar
thomwolf committed
19
20
import unittest

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

23
from .test_tokenization_common import TemporaryDirectory
thomwolf's avatar
thomwolf committed
24
25


26
class ModelCardTester(unittest.TestCase):
thomwolf's avatar
thomwolf committed
27
    def setUp(self):
28
29
30
31
32
33
34
35
36
        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": {
37
                "Datasets": {"BLEU": "My-great-dataset-v1", "ROUGE-1": "My-short-dataset-v2.1"},
38
39
40
41
42
43
                "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",
            },
44
            "quantitative_analyses": {"BLEU": 55.1, "ROUGE-1": 76},
45
        }
thomwolf's avatar
thomwolf committed
46
47

    def test_model_card_common_properties(self):
48
        modelcard = ModelCard.from_dict(self.inputs_dict)
49
50
51
52
53
54
55
56
57
        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
58
59

    def test_model_card_to_json_string(self):
60
61
        modelcard = ModelCard.from_dict(self.inputs_dict)
        obj = json.loads(modelcard.to_json_string())
thomwolf's avatar
thomwolf committed
62
63
64
65
66
67
68
        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)

        with TemporaryDirectory() as tmpdirname:
69
            filename = os.path.join(tmpdirname, "modelcard.json")
thomwolf's avatar
thomwolf committed
70
71
72
73
74
            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
75
76
77
78
79
80
81
82
    def test_model_card_from_and_save_pretrained(self):
        model_card_first = ModelCard.from_dict(self.inputs_dict)

        with TemporaryDirectory() as tmpdirname:
            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())