nateraw commited on
Commit
549847f
·
1 Parent(s): 6f41b42

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import requests
3
+ import streamlit as st
4
+ from modelcards import ModelCard
5
+
6
+
7
+ @st.cache
8
+ def get_cached_data():
9
+ languages_df = pd.read_html("https://hf.co/languages")[0]
10
+ languages_map = pd.Series(languages_df["Language"].values, index=languages_df["ISO code"]).to_dict()
11
+
12
+ license_df = pd.read_html("https://huggingface.co/docs/hub/repositories-licenses")[0]
13
+ license_map = pd.Series(
14
+ license_df["License identifier (to use in model card)"].values, index=license_df.Fullname
15
+ ).to_dict()
16
+
17
+ available_metrics = [x['id'] for x in requests.get('https://huggingface.co/api/metrics').json()]
18
+
19
+ return languages_map, license_map, available_metrics
20
+
21
+
22
+ languages_map, license_map, available_metrics = get_cached_data()
23
+
24
+ with st.sidebar:
25
+ st.title("Model Card Creator")
26
+ st.markdown("This app lets you generate model cards for your 🤗 Hub model repo!")
27
+ view = st.selectbox("View", ["Markdown", "Raw Text", "How to Programmatically Generate"])
28
+ warning_placeholder = st.empty()
29
+ placeholder = st.empty()
30
+ st.markdown('---')
31
+
32
+ model_name = st.text_input(
33
+ "Model Name", "my-cool-model", help="The name of your model. (Ex. my-cool-model, bert-base-uncased, etc.)"
34
+ )
35
+ languages = (
36
+ st.multiselect(
37
+ "Language",
38
+ languages_map.keys(),
39
+ format_func=lambda x: languages_map[x],
40
+ 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.",
41
+ )
42
+ or None
43
+ )
44
+ license = st.selectbox("License", license_map.keys(), 33, help="The license associated with this model.")
45
+ library_name = (
46
+ st.text_input(
47
+ "Library Name", help="The name of the library this model came from (Ex. pytorch, timm, spacy, keras, etc.)"
48
+ )
49
+ or None
50
+ )
51
+ tags = [
52
+ x.strip()
53
+ for x in st.text_input(
54
+ "Tags (comma separated)",
55
+ help="Additional tags to add which will be filterable on https://hf.co/models. (Ex. image-classification, vision, resnet)",
56
+ ).split(',')
57
+ if x.strip()
58
+ ] or None
59
+ dataset = (
60
+ st.text_input(
61
+ "Dataset", help="The dataset used to train this model. Use dataset id from https://hf.co/datasets"
62
+ )
63
+ or None
64
+ )
65
+ metrics = (
66
+ st.multiselect(
67
+ "Metrics",
68
+ available_metrics,
69
+ help="Metrics used in the training/evaluation of this model. Use metric id from https://hf.co/metrics.",
70
+ )
71
+ or None
72
+ )
73
+ model_description = st.text_area(
74
+ "Model Description",
75
+ "Describe your model here...",
76
+ 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.",
77
+ )
78
+
79
+ do_warn = False
80
+ warning_msg = "Warning: The following fields are required but have not been filled in: "
81
+ if not languages:
82
+ warning_msg += "\n- Languages"
83
+ do_warn = True
84
+ if not license:
85
+ warning_msg += "\n- License"
86
+ do_warn = True
87
+
88
+ if do_warn:
89
+ warning_placeholder.warning(warning_msg)
90
+
91
+ card = ModelCard.from_template(
92
+ language=languages,
93
+ license=license_map[license],
94
+ library_name=library_name,
95
+ tags=tags,
96
+ datasets=dataset,
97
+ metrics=metrics,
98
+ model_id=model_name,
99
+ model_description=model_description,
100
+ )
101
+
102
+ placeholder.download_button(
103
+ label="Download Model Card", data=str(card), file_name='README.md', mime='text/plain', disabled=do_warn
104
+ )
105
+
106
+ markdown_text = f"""
107
+ Card metadata...this should be at the beginning of your readme file.
108
+
109
+ ```
110
+ ---
111
+ {card.data.to_yaml()}
112
+ ---
113
+ ```
114
+
115
+ {card.text}
116
+ """
117
+
118
+ creation_code = f'''
119
+ # Make sure you have modelcards installed!
120
+ # pip install modelcards==0.0.4
121
+
122
+ from modelcards import ModelCard
123
+
124
+ card = ModelCard.from_template(
125
+ language={languages},
126
+ license={"'" + license_map[license] + "'" if license else None},
127
+ library_name={"'" + library_name + "'" if library_name else None},
128
+ tags={tags},
129
+ datasets={"'" + dataset + "'" if dataset else None},
130
+ metrics={metrics},
131
+ model_id={"'" + model_name + "'" if model_name else None},
132
+ model_description={"'" + model_description + "'" if model_description else None},
133
+ )
134
+ '''
135
+
136
+ if view == 'Raw Text':
137
+ st.text(str(card))
138
+ elif view == "Markdown":
139
+ st.markdown(markdown_text)
140
+ else:
141
+ st.code(creation_code)