File size: 5,099 Bytes
549847f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a73d89
 
 
 
 
 
 
549847f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import requests
import streamlit as st
from modelcards import ModelCard


@st.cache
def get_cached_data():
    languages_df = pd.read_html("https://hf.co/languages")[0]
    languages_map = pd.Series(languages_df["Language"].values, index=languages_df["ISO code"]).to_dict()

    license_df = pd.read_html("https://huggingface.co/docs/hub/repositories-licenses")[0]
    license_map = pd.Series(
        license_df["License identifier (to use in model card)"].values, index=license_df.Fullname
    ).to_dict()

    available_metrics = [x['id'] for x in requests.get('https://huggingface.co/api/metrics').json()]

    return languages_map, license_map, available_metrics


languages_map, license_map, available_metrics = get_cached_data()

with st.sidebar:
    st.markdown('''
        <div align="center">
            <h1>Model Card Creator</h1>

        [![Github Badge](https://img.shields.io/github/stars/nateraw/modelcards?style=social)](https://github.com/nateraw/modelcards)
        </div>
    ''', unsafe_allow_html=True)
    st.markdown("This app lets you generate model cards for your 🤗 Hub model repo!")
    view = st.selectbox("View", ["Markdown", "Raw Text", "How to Programmatically Generate"])
    warning_placeholder = st.empty()
    placeholder = st.empty()
    st.markdown('---')

    model_name = st.text_input(
        "Model Name", "my-cool-model", help="The name of your model. (Ex. my-cool-model, bert-base-uncased, etc.)"
    )
    languages = (
        st.multiselect(
            "Language",
            languages_map.keys(),
            format_func=lambda x: languages_map[x],
            help="The language(s) associated with this model. If this is not a text-based model, you should specify whatever lanuage is used in the dataset. For instance, if the dataset's labels are in english, you should select English here.",
        )
        or None
    )
    license = st.selectbox("License", license_map.keys(), 33, help="The license associated with this model.")
    library_name = (
        st.text_input(
            "Library Name", help="The name of the library this model came from (Ex. pytorch, timm, spacy, keras, etc.)"
        )
        or None
    )
    tags = [
        x.strip()
        for x in st.text_input(
            "Tags (comma separated)",
            help="Additional tags to add which will be filterable on https://hf.co/models. (Ex. image-classification, vision, resnet)",
        ).split(',')
        if x.strip()
    ] or None
    dataset = (
        st.text_input(
            "Dataset", help="The dataset used to train this model. Use dataset id from https://hf.co/datasets"
        )
        or None
    )
    metrics = (
        st.multiselect(
            "Metrics",
            available_metrics,
            help="Metrics used in the training/evaluation of this model. Use metric id from https://hf.co/metrics.",
        )
        or None
    )
    model_description = st.text_area(
        "Model Description",
        "Describe your model here...",
        help="The model description provides basic details about the model. This includes the architecture, version, if it was introduced in a paper, if an original implementation is available, the author, and general information about the model. Any copyright should be attributed here. General information about training procedures, parameters, and important disclaimers can also be mentioned in this section.",
    )

    do_warn = False
    warning_msg = "Warning: The following fields are required but have not been filled in: "
    if not languages:
        warning_msg += "\n- Languages"
        do_warn = True
    if not license:
        warning_msg += "\n- License"
        do_warn = True

    if do_warn:
        warning_placeholder.warning(warning_msg)

    card = ModelCard.from_template(
        language=languages,
        license=license_map[license],
        library_name=library_name,
        tags=tags,
        datasets=dataset,
        metrics=metrics,
        model_id=model_name,
        model_description=model_description,
    )

    placeholder.download_button(
        label="Download Model Card", data=str(card), file_name='README.md', mime='text/plain', disabled=do_warn
    )

markdown_text = f"""
Card metadata...this should be at the beginning of your readme file.

```
---
{card.data.to_yaml()}
---
```

{card.text}
"""

creation_code = f'''
# Make sure you have modelcards installed!
# pip install modelcards==0.0.4

from modelcards import ModelCard

card = ModelCard.from_template(
    language={languages},
    license={"'" + license_map[license] + "'" if license else None},
    library_name={"'" + library_name + "'" if library_name else None},
    tags={tags},
    datasets={"'" + dataset + "'" if dataset else None},
    metrics={metrics},
    model_id={"'" + model_name + "'" if model_name else None},
    model_description={"'" + model_description + "'" if model_description else None},
)
'''

if view == 'Raw Text':
    st.text(str(card))
elif view == "Markdown":
    st.markdown(markdown_text)
else:
    st.code(creation_code)