File size: 9,695 Bytes
104a4ce
 
 
 
 
 
 
 
 
5db5b18
104a4ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f4bace
104a4ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f4bace
 
 
 
104a4ce
 
 
 
 
 
 
9f4bace
104a4ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f4bace
 
 
 
 
 
 
 
 
104a4ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b8ef86
104a4ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f4bace
dbb80f1
5db5b18
 
8afbdd4
c9bf28d
104a4ce
 
 
da7c78c
104a4ce
 
da7c78c
104a4ce
 
 
 
9f4bace
 
 
104a4ce
 
 
 
5db5b18
9f4bace
 
104a4ce
9f4bace
31639f7
 
 
9f4bace
31639f7
 
9f4bace
525c4ec
 
 
 
104a4ce
 
 
 
 
c9bf28d
 
 
9f4bace
4419eab
 
 
 
 
 
104a4ce
a6c5938
 
 
 
104a4ce
a6c5938
 
 
 
 
 
 
525c4ec
a6c5938
 
a3d216f
 
a6c5938
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import datetime
import os
from dataclasses import asdict, dataclass
from functools import lru_cache
from json import JSONDecodeError
from typing import List, Optional, Union

import gradio as gr
import requests
from diskcache import Cache
from huggingface_hub import (
    HfApi,
    ModelCard,
    hf_hub_url,
    list_repo_commits,
    logging,
    model_info,
)
from huggingface_hub.utils import EntryNotFoundError, disable_progress_bars

disable_progress_bars()

logging.set_verbosity_error()

token = os.getenv("HF_TOKEN")


def get_model_labels(model):
    try:
        url = hf_hub_url(repo_id=model, filename="config.json")
        return list(requests.get(url).json()["label2id"].keys())
    except (KeyError, JSONDecodeError, AttributeError):
        return None


@dataclass
class EngagementStats:
    likes: int
    downloads: int
    created_at: datetime.datetime


def _get_engagement_stats(hub_id):
    api = HfApi(token=token)
    repo = api.repo_info(hub_id)
    return EngagementStats(
        likes=repo.likes,
        downloads=repo.downloads,
        created_at=list_repo_commits(hub_id, repo_type="model")[-1].created_at,
    )


def _try_load_model_card(hub_id):
    try:
        card_text = ModelCard.load(hub_id, token=token).text
        length = len(card_text)
    except EntryNotFoundError:
        card_text = None
        length = None
    return card_text, length


def _try_parse_card_data(hub_id):
    data = {}
    keys = ["license", "language", "datasets", "tags"]
    for key in keys:
        try:
            value = model_info(hub_id, token=token).cardData[key]
            data[key] = value
        except (KeyError, AttributeError):
            data[key] = None
    return data


@dataclass
class ModelMetadata:
    hub_id: str
    tags: Optional[List[str]]
    license: Optional[str]
    library_name: Optional[str]
    datasets: Optional[List[str]]
    pipeline_tag: Optional[str]
    labels: Optional[List[str]]
    languages: Optional[Union[str, List[str]]]
    engagement_stats: Optional[EngagementStats] = None
    model_card_text: Optional[str] = None
    model_card_length: Optional[int] = None

    @classmethod
    @lru_cache()
    def from_hub(cls, hub_id):
        model = model_info(hub_id)
        card_text, length = _try_load_model_card(hub_id)
        data = _try_parse_card_data(hub_id)
        try:
            library_name = model.library_name
        except AttributeError:
            library_name = None
        # try:
        #     tags = model.tags
        # except AttributeError:
        #     tags = None
        try:
            pipeline_tag = model.pipeline_tag
        except AttributeError:
            pipeline_tag = None
        return ModelMetadata(
            hub_id=hub_id,
            languages=data["language"],
            tags=data["tags"],
            license=data["license"],
            library_name=library_name,
            datasets=data["datasets"],
            pipeline_tag=pipeline_tag,
            labels=get_model_labels(hub_id),
            engagement_stats=_get_engagement_stats(hub_id),
            model_card_text=card_text,
            model_card_length=length,
        )


COMMON_SCORES = {
    "license": {
        "required": True,
        "score": 2,
        "missing_recommendation": (
            "You have not added a license to your models metadata"
        ),
    },
    "datasets": {
        "required": False,
        "score": 1,
        "missing_recommendation": (
            "You have not added any datasets to your models metadata"
        ),
    },
    "model_card_text": {
        "required": True,
        "score": 3,
        "missing_recommendation": """You haven't created a model card for your model. It is strongly recommended to have a model card for your model. \nYou can create for your model by clicking [here](https://huggingface.co/HUB_ID/edit/main/README.md)""",
    },
    "tags": {
        "required": False,
        "score": 2,
        "missing_recommendation": (
            "You don't have any tags defined in your model metadata. Tags can help"
            " people find relevant models on the Hub. You can create for your model by"
            " clicking [here](https://huggingface.co/HUB_ID/edit/main/README.md)"
        ),
    },
}


TASK_TYPES_WITH_LANGUAGES = {
    "text-classification",
    "token-classification",
    "table-question-answering",
    "question-answering",
    "zero-shot-classification",
    "translation",
    "summarization",
    "text-generation",
    "text2text-generation",
    "fill-mask",
    "sentence-similarity",
    "text-to-speech",
    "automatic-speech-recognition",
    "text-to-image",
    "image-to-text",
    "visual-question-answering",
    "document-question-answering",
}

LABELS_REQUIRED_TASKS = {
    "text-classification",
    "token-classification",
    "object-detection",
    "audio-classification",
    "image-classification",
    "tabular-classification",
}
ALL_PIPELINES = {
    "audio-classification",
    "audio-to-audio",
    "automatic-speech-recognition",
    "conversational",
    "depth-estimation",
    "document-question-answering",
    "feature-extraction",
    "fill-mask",
    "graph-ml",
    "image-classification",
    "image-segmentation",
    "image-to-image",
    "image-to-text",
    "object-detection",
    "question-answering",
    "reinforcement-learning",
    "robotics",
    "sentence-similarity",
    "summarization",
    "table-question-answering",
    "tabular-classification",
    "tabular-regression",
    "text-classification",
    "text-generation",
    "text-to-image",
    "text-to-speech",
    "text-to-video",
    "text2text-generation",
    "token-classification",
    "translation",
    "unconditional-image-generation",
    "video-classification",
    "visual-question-answering",
    "voice-activity-detection",
    "zero-shot-classification",
    "zero-shot-image-classification",
}


@lru_cache(maxsize=None)
def generate_task_scores_dict():
    task_scores = {}
    for task in ALL_PIPELINES:
        task_dict = COMMON_SCORES.copy()
        if task in TASK_TYPES_WITH_LANGUAGES:
            task_dict = {
                **task_dict,
                **{
                    "languages": {
                        "required": True,
                        "score": 2,
                        "missing_recommendation": (
                            "You haven't defined any languages in your metadata. This"
                            f" is usually recommend for {task} task"
                        ),
                    }
                },
            }
        if task in LABELS_REQUIRED_TASKS:
            task_dict = {
                **task_dict,
                **{
                    "labels": {
                        "required": True,
                        "score": 2,
                        "missing_recommendation": (
                            "You haven't defined any labels in the config.json file"
                            f" these are usually recommended for {task}"
                        ),
                    }
                },
            }
        max_score = sum(value["score"] for value in task_dict.values())
        task_dict["_max_score"] = max_score
        task_scores[task] = task_dict
    return task_scores


SCORES = generate_task_scores_dict()


cache = Cache("/data/")


@cache.memoize(expire=60 * 60 * 24)  # expires after 24 hours
def _basic_check(hub_id):
    try:
        data = ModelMetadata.from_hub(hub_id)
        score = 0
        if task := data.pipeline_tag:
            task_scores = SCORES[task]
            to_fix = {}
            data_dict = asdict(data)
            for k, v in task_scores.items():
                if k.startswith("_"):
                    continue
                if data_dict[k] is None:
                    to_fix[k] = task_scores[k]["missing_recommendation"].replace(
                        "HUB_ID", hub_id
                    )
                if data_dict[k] is not None:
                    score += v["score"]
            max_score = task_scores["_max_score"]
            score = score / max_score
            (
                f"Your model's metadata score is {round(score*100)}% based on suggested"
                f" metadata for {task}. \n"
            )
            # recommendations = []
            if to_fix:
                recommendations = (
                    "Here are some suggestions to improve your model's metadata for"
                    f" {task}: \n"
                )
                for v in to_fix.values():
                    recommendations += f"\n- {v}"
            return score
            # return (
            #     score_summary + recommendations if recommendations else score_summary
            # )
    except Exception as e:
        print(e)
        return None


def basic_check(hub_id):
    return _basic_check(hub_id)


# print("caching models...")
# print("getting top 5,000 models")
# models = list_models(sort="downloads", direction=-1, limit=5_000)
# model_ids = [model.modelId for model in models]
# print("calculating metadata scores...")
# thread_map(basic_check, model_ids)

with gr.Blocks() as demo:
    gr.Markdown(
        """
# Model Metadata Checker

This app will check your model's metadata for a few common issues."""
    )
    with gr.Row():
        text = gr.Text(label="Model ID")
        button = gr.Button(label="Check", type="submit")
    with gr.Row():
        gr.Markdown("Results")
        markdown = gr.Number()
        button.click(_basic_check, text, markdown)

demo.queue(concurrency_count=8, max_size=5)
demo.launch()


# gr.Interface(fn=basic_check, inputs="text", outputs="markdown").launch(debug=True)