Create skops-model-card-creator space
Browse files- README.md +315 -4
- __init__.py +0 -0
- app.py +53 -0
- cat.png +0 -0
- create.py +131 -0
- edit.py +421 -0
- gethelp.py +329 -0
- make-data.py +30 -0
- packages.txt +1 -0
- requirements.txt +7 -0
- start.py +252 -0
- tasks.py +298 -0
- utils.py +107 -0
README.md
CHANGED
@@ -1,12 +1,323 @@
|
|
1 |
---
|
2 |
-
title: Skops Model Card Creator
|
3 |
-
emoji:
|
4 |
colorFrom: indigo
|
5 |
colorTo: blue
|
6 |
sdk: streamlit
|
7 |
-
sdk_version: 1.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Skops Model Card Creator
|
3 |
+
emoji: 🐨
|
4 |
colorFrom: indigo
|
5 |
colorTo: blue
|
6 |
sdk: streamlit
|
7 |
+
sdk_version: 1.17.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: bsd-3-clause
|
11 |
+
tags:
|
12 |
+
- sklearn
|
13 |
+
- skops
|
14 |
+
- model card
|
15 |
---
|
16 |
|
17 |
+
# Create a Hugging Face model repository for scikit learn models
|
18 |
+
|
19 |
+
This page aims to provide a simple interface to use the
|
20 |
+
[`skops`](https://skops.readthedocs.io/) model card and HF Hub creation
|
21 |
+
utilities.
|
22 |
+
|
23 |
+
Below, we will explain the steps involved to create your own model repository to
|
24 |
+
host your scikit-learn model:
|
25 |
+
|
26 |
+
1. Prepare the model repository
|
27 |
+
2. Edit the model card
|
28 |
+
3. Create the model repository on Hugging Face Hub
|
29 |
+
|
30 |
+
## Step 1: Prepare the model repository
|
31 |
+
|
32 |
+
In this step, you do the necessary preparation work to create a [model
|
33 |
+
repository on Hugging Face Hub](https://huggingface.co/docs/hub/models).
|
34 |
+
|
35 |
+
### Upload a model
|
36 |
+
|
37 |
+
Here you should upload the sklearn model we want to present in the model
|
38 |
+
repository. The model should be stored either as a ``pickle`` file or it should
|
39 |
+
use the [secure skops persistence
|
40 |
+
format](https://skops.readthedocs.io/en/stable/persistence.html). Later, this
|
41 |
+
model will be uploaded to the model repository so that you can share it with
|
42 |
+
others.
|
43 |
+
|
44 |
+
The uploaded model should be a scikit-learn model or a model that is compatible
|
45 |
+
with the sklearn API, e.g. using [XGBoost sklearn
|
46 |
+
wrapper](https://xgboost.readthedocs.io/en/stable/python/python_api.html#module-xgboost.sklearn)
|
47 |
+
when it's an XGBoost model.
|
48 |
+
|
49 |
+
If you just want to test out the application and don't want to upload a model, a
|
50 |
+
dummy model will be used instead.
|
51 |
+
|
52 |
+
### Upload input data
|
53 |
+
|
54 |
+
It's possible to upload input data as a csv file. If that is done, the first few
|
55 |
+
rows of the input data will be used as sample data for the model, e.g. when
|
56 |
+
trying out the [inference API](https://huggingface.co/inference-api).
|
57 |
+
|
58 |
+
|
59 |
+
### Choose the task type
|
60 |
+
|
61 |
+
Choose the type of task that the model is intended to solve. It can be either
|
62 |
+
classification or regression, with input data being either tabular in nature or
|
63 |
+
text.
|
64 |
+
|
65 |
+
### Requirements
|
66 |
+
|
67 |
+
This is the list of Python requirements needed to run the model.
|
68 |
+
|
69 |
+
### Choose the model card template
|
70 |
+
|
71 |
+
This is the final step and choosing one of the options will bring you to the
|
72 |
+
editing step.
|
73 |
+
|
74 |
+
#### Create a new skops model card
|
75 |
+
|
76 |
+
This is the recommended way of getting started. The skops model card template
|
77 |
+
prefills the model card with some [useful
|
78 |
+
contents](https://skops.readthedocs.io/en/stable/model_card.html#model-card-content)
|
79 |
+
that you probably want to have in most model cards. Don't worry: If you don't
|
80 |
+
like part of the content, you can always edit or delete it later.
|
81 |
+
|
82 |
+
#### Create a new empty model card
|
83 |
+
|
84 |
+
If you want to start the model card completely from scratch, that's also
|
85 |
+
possible by choosing this option. It will generate a completely empty model card
|
86 |
+
for you that you can fashion to your liking.
|
87 |
+
|
88 |
+
#### Load existing model card from HF Hub
|
89 |
+
|
90 |
+
If you want to use an existing model card and edit it, that's also possible.
|
91 |
+
Please enter the Hugging Face Hub repository ID here and the corresponding model
|
92 |
+
card will be loaded. The repo ID is typically someting like `username/reponame`,
|
93 |
+
e.g. `openai/whisper-small`. Some models also omit the user name, e.g. `gpt2`.
|
94 |
+
|
95 |
+
Note that when you choose an existing model card, a couple of files will be
|
96 |
+
downloaded, because they may be required to render the model card (e.g. images).
|
97 |
+
Therefore, depending on the repository, this step may take a bit.
|
98 |
+
|
99 |
+
If you notice any problems when rendering the existing model card, please let us
|
100 |
+
know by [creating an issue](https://github.com/skops-dev/skops/issues).
|
101 |
+
|
102 |
+
## Step 2: Edit the model card
|
103 |
+
|
104 |
+
Before creating the model repository, it is crucial to ensure that the [model
|
105 |
+
card](https://huggingface.co/blog/model-cards) is edited to best represent the
|
106 |
+
model you're working on. This can be achieved in the editing step, which is
|
107 |
+
described in more detail below.
|
108 |
+
|
109 |
+
### Editing sidbar
|
110 |
+
|
111 |
+
In the left sidebar, you will be able to edit the model card, whereas the main
|
112 |
+
screen is reserved for rendering the model card so that you see what you will
|
113 |
+
get. We will start by describing the editing sidebar.
|
114 |
+
|
115 |
+
Tip: You should increase the width of the side bar if it is too narrow for your
|
116 |
+
taste.
|
117 |
+
|
118 |
+
#### Undo, redo & reset
|
119 |
+
|
120 |
+
On top of the side bar, you have the option to undo, redo, and reset the last
|
121 |
+
operation you did. Say, you accidentally made a change, just press the `Undo`
|
122 |
+
button to undo this change. Similarly, if you want to undo your undo operation,
|
123 |
+
press the `Redo` button. Finally, if you press `Reset`, all your operations will
|
124 |
+
be undone (but don't worry if you click the button accidentally, you can redo
|
125 |
+
all of them if you want).
|
126 |
+
|
127 |
+
#### Save, create repo & delete
|
128 |
+
|
129 |
+
These buttons are intended for when you finished editing the model card. When
|
130 |
+
you click on `Save`, you will get the option to download the model card as a
|
131 |
+
markdown file.
|
132 |
+
|
133 |
+
When clicking the `Create Repo` button, you will be taken to the next screen,
|
134 |
+
which offers you to create a model repository on Hugging Face Hub. This step
|
135 |
+
will be explained in more detail further below.
|
136 |
+
|
137 |
+
Finally, you can click on `Delete` to completely discard all the changes you
|
138 |
+
made and be taken back to the start screen of the app. Be careful, any change
|
139 |
+
you made will be lost. It is thus advised to first save the model card before
|
140 |
+
pressing `Delete`.
|
141 |
+
|
142 |
+
#### Edit a section
|
143 |
+
|
144 |
+
Each section has its own form field, which allows you to make edits. Change the
|
145 |
+
name of the section or change the content (or both), then click `Update` to see
|
146 |
+
a preview of your change. As with all other operations, you can undo the change
|
147 |
+
by clicking on `Undo`.
|
148 |
+
|
149 |
+
#### Delete a section
|
150 |
+
|
151 |
+
Below the form field for editing the section, you will find a `Delete` button
|
152 |
+
(including the name of the section to make it clear which section it refers to).
|
153 |
+
If you click that button, the whole section, _including its subsections_, will
|
154 |
+
be deleted. Again, click on `Undo` if you accidentally deleted something that
|
155 |
+
you want to keep.
|
156 |
+
|
157 |
+
#### Add section below
|
158 |
+
|
159 |
+
If you click on this button, a new subsection wil be created under the current
|
160 |
+
section. This will create a section with a dummy title and dummy content, which
|
161 |
+
you can then edit.
|
162 |
+
|
163 |
+
Note that this will create a new _subsection_. If there are already existing
|
164 |
+
subsections in the current section, the new subsection will be created _below_
|
165 |
+
those existing subsections. So the new subsection you create might not appear
|
166 |
+
exactly where you expect it to appear. To illustrate this, assume that we have
|
167 |
+
the following sections and subsections:
|
168 |
+
|
169 |
+
- Section A
|
170 |
+
- Subsection A.1
|
171 |
+
- Subsection A.2
|
172 |
+
- Section B
|
173 |
+
|
174 |
+
If you create a new section below "Section A", it will be created on the same
|
175 |
+
level, and below of, "Subsection A.2", resulting in the following structure:
|
176 |
+
|
177 |
+
- Section A
|
178 |
+
- Subsection A.1
|
179 |
+
- Subsection A.2
|
180 |
+
- NEW SUBSECTION
|
181 |
+
- Section B
|
182 |
+
|
183 |
+
If you create a new section below the "Subsection A.1", you will actually create
|
184 |
+
a sub-subsection, resulting in the following structure instead:
|
185 |
+
|
186 |
+
- Section A
|
187 |
+
- Subsection A.1
|
188 |
+
- NEW SUB-SUBSECTION
|
189 |
+
- Subsection A.2
|
190 |
+
- Section B
|
191 |
+
|
192 |
+
Hopefully, this clarifies things. Unfortunately, there is no possibility (yet)
|
193 |
+
to re-order sections.
|
194 |
+
|
195 |
+
#### Add figure below
|
196 |
+
|
197 |
+
This button works quite similarly to adding a new section. The main difference
|
198 |
+
is that instead of having a text area to enter content, you will be asked to
|
199 |
+
upload an image file. By default, a dummy image will be shown in the preview.
|
200 |
+
|
201 |
+
#### Add metrics (only skops template)
|
202 |
+
|
203 |
+
If you have chosen the skops template, you will see an additional field called
|
204 |
+
`Add metrics` near the top of the side bar. Here you can choose metrics you want
|
205 |
+
to be shown in the model card, e.g. the accuracy the model achieved on a
|
206 |
+
specific dataset. Please enter one metric per line, with the metric name on the
|
207 |
+
left, then an `=` sign, and the value on the right, e.g. `accuracy on test set =
|
208 |
+
0.85`.
|
209 |
+
|
210 |
+
After pressing `update`, the metrics will be shown in a table in the section
|
211 |
+
`Model description/Evaluation Results`. You can always add or remove metrics
|
212 |
+
from this field later. If you want to delete this section completely, look for
|
213 |
+
its edit form further below and press `Delete`. There, you can also edit the
|
214 |
+
table in a more fine grained way, e.g. by changing the alignment.
|
215 |
+
|
216 |
+
If you don't use the skops template and still want to add a table, it is
|
217 |
+
possible to do that, but it's requires a bit more work. Add a new section as
|
218 |
+
described above, then, in the text area, create a table using the [markdown
|
219 |
+
table
|
220 |
+
syntax](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables#creating-a-table).
|
221 |
+
|
222 |
+
### Model card visualization
|
223 |
+
|
224 |
+
The main part of the page will show you what the final model card will look
|
225 |
+
like.
|
226 |
+
|
227 |
+
#### Metadata
|
228 |
+
|
229 |
+
On the very top, you can see the metadata of the model card (it is collapsed by
|
230 |
+
default). The metadata can be very useful for features on the HF Hub, e.g.
|
231 |
+
allowing other users to find your model by a given tag.
|
232 |
+
|
233 |
+
Right now, it is not possible to edit the metadata directly from here. But don't
|
234 |
+
worry, once you have created the model card repository, you can easily edit the
|
235 |
+
metadata there.
|
236 |
+
|
237 |
+
#### Table of Contents
|
238 |
+
|
239 |
+
For your convenience, a table of contents is also shown at the top (collapsed by
|
240 |
+
default). This is useful if you have a bigger model card and want to see the
|
241 |
+
overview of all its contents.
|
242 |
+
|
243 |
+
#### Markdown preview
|
244 |
+
|
245 |
+
Finally, the model card itself is shown. This is how the model card will look
|
246 |
+
like once it is saved as markdown and then rendered.
|
247 |
+
|
248 |
+
## Step 3: Creating a model repository
|
249 |
+
|
250 |
+
After you have finished editing the model card, it is time to create a model
|
251 |
+
repository on Hugging Face Hub. Click on `Create Repo` and you will be taken to
|
252 |
+
the final step of the process.
|
253 |
+
|
254 |
+
### Back & Delete
|
255 |
+
|
256 |
+
If you find yourself wanting to make more edits to the model card, just click on
|
257 |
+
the `Back` button and you'll be brought back to the editing step.
|
258 |
+
|
259 |
+
You can also click `Delete`, which will discard all your changes and bring you
|
260 |
+
back to the start page. Be careful: This step cannot be undone and all your
|
261 |
+
progress will be lost.
|
262 |
+
|
263 |
+
### Files included in the repository
|
264 |
+
|
265 |
+
For your convenience, this will show a preview of all the files included in the
|
266 |
+
repository, as well as their sizes. Don't create a repository if you see files
|
267 |
+
there that you don't want to be uploaded.
|
268 |
+
|
269 |
+
### Privacy settings
|
270 |
+
|
271 |
+
By default, a private repository will be created. If you untick this box, it
|
272 |
+
will be public instead. More information on what that implies can be found in
|
273 |
+
the [docs on repository
|
274 |
+
settings](https://huggingface.co/docs/hub/repositories-settings).
|
275 |
+
|
276 |
+
### Name of the repository
|
277 |
+
|
278 |
+
Here you have to enter the name of the repository. Typically, that's something
|
279 |
+
like `username/reponame` or `organame/reponame`. This field is mandatory and you
|
280 |
+
should ensure that the corresponding repository ID does not exist yet.
|
281 |
+
|
282 |
+
### Enter your Hugging Face token
|
283 |
+
|
284 |
+
Here you need to paste your Hugging Face token, which is used for
|
285 |
+
authentication. The token can be found [here](https://hf.co/settings/token) and
|
286 |
+
it always starts with "hf_". Entering a token is necessary to create a
|
287 |
+
repository.
|
288 |
+
|
289 |
+
Note that if you don't already have an account on Hugging Face, you need to
|
290 |
+
create one to get a token. It's free.
|
291 |
+
|
292 |
+
### Create a new repository
|
293 |
+
|
294 |
+
Once all the required fields are filled, click on this button to create the
|
295 |
+
repository. Depending on the size, it may take a couple of seconds to finish.
|
296 |
+
Once it is created, you will see a success notification that includes the link
|
297 |
+
to the repository. Congratulations, you're done!
|
298 |
+
|
299 |
+
## Troubleshooting
|
300 |
+
|
301 |
+
### Not all skops features available
|
302 |
+
|
303 |
+
This app is based on the [skops model card
|
304 |
+
feature](https://skops.readthedocs.io/en/stable/model_card.html#model-card-content).
|
305 |
+
However, it does not support all the options that are available there. If you
|
306 |
+
want to use all those options in a programmatic fashion, please follow the link
|
307 |
+
and read up on what it takes to create a model card with skops. The full power
|
308 |
+
of the `Card` class is documented
|
309 |
+
[here](https://skops.readthedocs.io/en/stable/modules/classes.html#skops.card.Card).
|
310 |
+
|
311 |
+
### Strange behavior
|
312 |
+
|
313 |
+
If the app behaves strangely, shows error messages, or renders incorrectly, it
|
314 |
+
may be necessary to refresh the browser tab. This will take you back to the
|
315 |
+
start page, with all progress being lost. If you can reproduce that behavior,
|
316 |
+
please [creating an issue](https://github.com/skops-dev/skops/issues) and let us
|
317 |
+
know.
|
318 |
+
|
319 |
+
### Contact
|
320 |
+
|
321 |
+
If you want to contact us, you can join our discord channel. To do that, follow
|
322 |
+
[these
|
323 |
+
instructions](https://skops.readthedocs.io/en/stable/community.html#discord).
|
__init__.py
ADDED
File without changes
|
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""The app.py used with streamlit
|
2 |
+
|
3 |
+
This ties together the different parts of the app.
|
4 |
+
|
5 |
+
"""
|
6 |
+
|
7 |
+
import os
|
8 |
+
import shutil
|
9 |
+
from pathlib import Path
|
10 |
+
from tempfile import mkdtemp
|
11 |
+
from typing import Literal
|
12 |
+
|
13 |
+
import streamlit as st
|
14 |
+
from create import create_repo_input_form
|
15 |
+
from edit import edit_input_form
|
16 |
+
from gethelp import help_page
|
17 |
+
from start import start_input_form
|
18 |
+
|
19 |
+
# Change cwd to a temporary path
|
20 |
+
if "work_dir" not in st.session_state:
|
21 |
+
work_dir = Path(mkdtemp(prefix="skops-"))
|
22 |
+
shutil.copy2("cat.png", work_dir / "cat.png")
|
23 |
+
os.chdir(work_dir)
|
24 |
+
st.session_state.work_dir = work_dir
|
25 |
+
|
26 |
+
# Create a hf_path, which is where the repo will be created locally. When the
|
27 |
+
# session is created, copy the dummy cat.png file there and make it the cwd
|
28 |
+
if "hf_path" not in st.session_state:
|
29 |
+
hf_path = Path(mkdtemp(prefix="skops-"))
|
30 |
+
st.session_state.hf_path = hf_path
|
31 |
+
|
32 |
+
|
33 |
+
st.header("Skops model card creator")
|
34 |
+
|
35 |
+
|
36 |
+
class Screen:
|
37 |
+
state: Literal["start", "edit", "create_repo"] = "start"
|
38 |
+
|
39 |
+
|
40 |
+
if "screen" not in st.session_state:
|
41 |
+
st.session_state.screen = Screen()
|
42 |
+
|
43 |
+
|
44 |
+
if st.session_state.screen.state == "start":
|
45 |
+
start_input_form()
|
46 |
+
elif st.session_state.screen.state == "help":
|
47 |
+
help_page()
|
48 |
+
elif st.session_state.screen.state == "edit":
|
49 |
+
edit_input_form()
|
50 |
+
elif st.session_state.screen.state == "create_repo":
|
51 |
+
create_repo_input_form()
|
52 |
+
else:
|
53 |
+
st.write("Something went wrong, please open an issue")
|
cat.png
ADDED
![]() |
create.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
from utils import get_rendered_model_card
|
6 |
+
|
7 |
+
from skops import hub_utils
|
8 |
+
|
9 |
+
|
10 |
+
def _add_back_button():
|
11 |
+
def fn():
|
12 |
+
st.session_state.screen.state = "edit"
|
13 |
+
|
14 |
+
st.button("Back", help="continue editing the model card", on_click=fn)
|
15 |
+
|
16 |
+
|
17 |
+
def _add_delete_button():
|
18 |
+
def fn():
|
19 |
+
if "hf_path" in st.session_state:
|
20 |
+
del st.session_state["hf_path"]
|
21 |
+
if "model_card" in st.session_state:
|
22 |
+
del st.session_state["model_card"]
|
23 |
+
if "task_state" in st.session_state:
|
24 |
+
st.session_state.task_state.reset()
|
25 |
+
if "create_repo_name" in st.session_state:
|
26 |
+
del st.session_state["create_repo_name"]
|
27 |
+
if "hf_token" in st.session_state:
|
28 |
+
del st.session_state["hf_token"]
|
29 |
+
st.session_state.screen.state = "start"
|
30 |
+
|
31 |
+
st.button("Delete", on_click=fn, help="Start over from scratch (lose all progress)")
|
32 |
+
|
33 |
+
|
34 |
+
def _save_model_card(path: Path) -> None:
|
35 |
+
model_card = st.session_state.get("model_card")
|
36 |
+
if model_card:
|
37 |
+
# do not use model_card.save, see doc of get_rendered_model_card
|
38 |
+
rendered = get_rendered_model_card(
|
39 |
+
model_card, hf_path=str(st.session_state.hf_path)
|
40 |
+
)
|
41 |
+
with open(path / "README.md", "w") as f:
|
42 |
+
f.write(rendered)
|
43 |
+
|
44 |
+
|
45 |
+
def _display_repo_overview(path: Path) -> None:
|
46 |
+
text = "Files included in the repository:\n"
|
47 |
+
for file in os.listdir(path):
|
48 |
+
size = os.path.getsize(path / file)
|
49 |
+
text += f"- `{file} ({size:,} bytes)`\n"
|
50 |
+
st.markdown(text)
|
51 |
+
|
52 |
+
|
53 |
+
def _display_private_box():
|
54 |
+
tip = (
|
55 |
+
"Private repositories can only seen by you or members of the same "
|
56 |
+
"organization, see https://huggingface.co/docs/hub/repositories-settings"
|
57 |
+
)
|
58 |
+
st.checkbox(
|
59 |
+
"Make repository private", value=True, help=tip, key="create_repo_private"
|
60 |
+
)
|
61 |
+
|
62 |
+
|
63 |
+
def _repo_id_field():
|
64 |
+
st.text_input("Name of the repository (e.g. 'User/MyRepo')", key="create_repo_name")
|
65 |
+
|
66 |
+
|
67 |
+
def _hf_token_field():
|
68 |
+
tip = "The Hugging Face token can be found at https://hf.co/settings/token"
|
69 |
+
st.text_input("Enter your Hugging Face token ('hf_***')", key="hf_token", help=tip)
|
70 |
+
|
71 |
+
|
72 |
+
def _create_hf_repo(path, repo_name, hf_token, private):
|
73 |
+
try:
|
74 |
+
hub_utils.push(
|
75 |
+
repo_id=repo_name,
|
76 |
+
source=path,
|
77 |
+
token=hf_token,
|
78 |
+
private=private,
|
79 |
+
create_remote=True,
|
80 |
+
)
|
81 |
+
except Exception as exc:
|
82 |
+
st.error(
|
83 |
+
"Oops, something went wrong, please create an issue. "
|
84 |
+
f"The error message is:\n\n{exc}"
|
85 |
+
)
|
86 |
+
return
|
87 |
+
|
88 |
+
st.success(f"Successfully created the repo 'https://huggingface.co/{repo_name}'")
|
89 |
+
|
90 |
+
|
91 |
+
def _add_create_repo_button():
|
92 |
+
private = bool(st.session_state.get("create_repo_private"))
|
93 |
+
repo_name = st.session_state.get("create_repo_name")
|
94 |
+
hf_token = st.session_state.get("hf_token")
|
95 |
+
disabled = (not repo_name) or (not hf_token)
|
96 |
+
|
97 |
+
button_text = "Create a new repository"
|
98 |
+
tip = "Creating a repo requires a name and a token"
|
99 |
+
path = st.session_state.get("hf_path")
|
100 |
+
st.button(
|
101 |
+
button_text,
|
102 |
+
help=tip,
|
103 |
+
disabled=disabled,
|
104 |
+
on_click=_create_hf_repo,
|
105 |
+
args=(path, repo_name, hf_token, private),
|
106 |
+
)
|
107 |
+
|
108 |
+
if not repo_name:
|
109 |
+
st.info("Repository name is required")
|
110 |
+
if not hf_token:
|
111 |
+
st.info("Token is required")
|
112 |
+
|
113 |
+
|
114 |
+
def create_repo_input_form():
|
115 |
+
if not st.session_state.screen.state == "create_repo":
|
116 |
+
return
|
117 |
+
|
118 |
+
col_0, col_1, *_ = st.columns([2, 2, 2, 2])
|
119 |
+
with col_0:
|
120 |
+
_add_back_button()
|
121 |
+
with col_1:
|
122 |
+
_add_delete_button()
|
123 |
+
|
124 |
+
hf_path = st.session_state.hf_path
|
125 |
+
_save_model_card(hf_path)
|
126 |
+
_display_repo_overview(hf_path)
|
127 |
+
_display_private_box()
|
128 |
+
st.markdown("---")
|
129 |
+
_repo_id_field()
|
130 |
+
_hf_token_field()
|
131 |
+
_add_create_repo_button()
|
edit.py
ADDED
@@ -0,0 +1,421 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""The editing page of the app
|
2 |
+
|
3 |
+
This is the meat of the application. On the sidebar, the content of the model
|
4 |
+
card is displayed in the form of editable fields. On the right side, the
|
5 |
+
rendered model card is shown.
|
6 |
+
|
7 |
+
In the side bar, users can:
|
8 |
+
|
9 |
+
- edit the title and content of existing sections
|
10 |
+
- delete sections
|
11 |
+
- add new sections below the current section
|
12 |
+
- add new figures below the current section
|
13 |
+
|
14 |
+
Moreover, each action results in a "task" that is tracked in the task state. A
|
15 |
+
task has a "do" and an "undo" method. This allows us to provide "undo" and
|
16 |
+
"redo" features to the app, making it easier for users to experiment and deal
|
17 |
+
with errors. The "reset" button undoes all the tasks, leading back to the
|
18 |
+
initial model card.
|
19 |
+
|
20 |
+
When the user is finished, there is a "save" button that downloads the model
|
21 |
+
card. They can also click "delete" to start over again, leading them to the
|
22 |
+
start page.
|
23 |
+
|
24 |
+
"""
|
25 |
+
|
26 |
+
|
27 |
+
from __future__ import annotations
|
28 |
+
|
29 |
+
import reprlib
|
30 |
+
from pathlib import Path
|
31 |
+
from tempfile import mkdtemp
|
32 |
+
|
33 |
+
import streamlit as st
|
34 |
+
from huggingface_hub import hf_hub_download
|
35 |
+
from tasks import (
|
36 |
+
AddFigureTask,
|
37 |
+
AddMetricsTask,
|
38 |
+
AddSectionTask,
|
39 |
+
DeleteSectionTask,
|
40 |
+
TaskState,
|
41 |
+
UpdateFigureTask,
|
42 |
+
UpdateFigureTitleTask,
|
43 |
+
UpdateSectionTask,
|
44 |
+
)
|
45 |
+
from utils import (
|
46 |
+
get_rendered_model_card,
|
47 |
+
iterate_key_section_content,
|
48 |
+
process_card_for_rendering,
|
49 |
+
)
|
50 |
+
|
51 |
+
from skops import card
|
52 |
+
from skops.card._model_card import PlotSection, split_subsection_names
|
53 |
+
|
54 |
+
arepr = reprlib.Repr()
|
55 |
+
arepr.maxstring = 24
|
56 |
+
tmp_path = Path(mkdtemp(prefix="skops-")) # temporary files
|
57 |
+
|
58 |
+
|
59 |
+
def load_model_card_from_repo(repo_id: str) -> card.Card:
|
60 |
+
print("downloading model card")
|
61 |
+
path = hf_hub_download(repo_id, "README.md")
|
62 |
+
model_card = card.parse_modelcard(path)
|
63 |
+
return model_card
|
64 |
+
|
65 |
+
|
66 |
+
def _update_model_card(
|
67 |
+
model_card: card.Card,
|
68 |
+
key: str,
|
69 |
+
section_name: str,
|
70 |
+
content: str,
|
71 |
+
) -> None:
|
72 |
+
# This is a very roundabout way to update the model card but it's necessary
|
73 |
+
# because of how streamlit handles session state. Basically, there have to
|
74 |
+
# be "key" arguments, which have to be retrieved from the session_state, as
|
75 |
+
# they are up-to-date. Just getting the Python variables is not enough, as
|
76 |
+
# they can be out of date.
|
77 |
+
|
78 |
+
# key names must match with those used in form
|
79 |
+
new_title = st.session_state[f"{key}.title"]
|
80 |
+
new_content = st.session_state[f"{key}.content"]
|
81 |
+
|
82 |
+
# determine if title is the same
|
83 |
+
old_title_split = split_subsection_names(section_name)
|
84 |
+
new_title_split = old_title_split[:-1] + [new_title]
|
85 |
+
is_title_same = old_title_split == new_title_split
|
86 |
+
|
87 |
+
# determine if content is the same
|
88 |
+
is_content_same = (content == new_content) or (not content and not new_content)
|
89 |
+
if is_title_same and is_content_same:
|
90 |
+
return
|
91 |
+
|
92 |
+
section = model_card.select(key)
|
93 |
+
if not isinstance(section, PlotSection):
|
94 |
+
# a normal section
|
95 |
+
task = UpdateSectionTask(
|
96 |
+
model_card,
|
97 |
+
key=key,
|
98 |
+
old_name=section_name,
|
99 |
+
new_name=new_title,
|
100 |
+
old_content=content,
|
101 |
+
new_content=new_content,
|
102 |
+
)
|
103 |
+
else:
|
104 |
+
# a plot sectoin
|
105 |
+
if not new_content: # only title changed
|
106 |
+
task = UpdateFigureTitleTask(
|
107 |
+
model_card, key=key, old_name=section_name, new_name=new_title
|
108 |
+
)
|
109 |
+
else: # new figure uploaded
|
110 |
+
fname = new_content.name.replace(" ", "_")
|
111 |
+
fpath = st.session_state.hf_path / fname
|
112 |
+
old_path = fpath.parent / Path(section.path).name
|
113 |
+
task = UpdateFigureTask(
|
114 |
+
model_card,
|
115 |
+
key=key,
|
116 |
+
old_name=section_name,
|
117 |
+
new_name=new_title,
|
118 |
+
data=new_content,
|
119 |
+
new_path=fpath,
|
120 |
+
old_path=old_path,
|
121 |
+
)
|
122 |
+
st.session_state.task_state.add(task)
|
123 |
+
|
124 |
+
|
125 |
+
def _add_section(model_card: card.Card, key: str) -> None:
|
126 |
+
section_name = f"{key}/Untitled"
|
127 |
+
task = AddSectionTask(
|
128 |
+
model_card, title=section_name, content="[More Information Needed]"
|
129 |
+
)
|
130 |
+
st.session_state.task_state.add(task)
|
131 |
+
|
132 |
+
|
133 |
+
def _add_figure(model_card: card.Card, key: str) -> None:
|
134 |
+
section_name = f"{key}/Untitled"
|
135 |
+
hf_path = st.session_state.hf_path
|
136 |
+
task = AddFigureTask(
|
137 |
+
model_card, path=hf_path, title=section_name, content="cat.png"
|
138 |
+
)
|
139 |
+
st.session_state.task_state.add(task)
|
140 |
+
|
141 |
+
|
142 |
+
def _delete_section(model_card: card.Card, key: str, path: Path) -> None:
|
143 |
+
task = DeleteSectionTask(model_card, key=key, path=path)
|
144 |
+
st.session_state.task_state.add(task)
|
145 |
+
|
146 |
+
|
147 |
+
def _add_section_form(
|
148 |
+
model_card: card.Card, key: str, section_name: str, old_title: str, content: str
|
149 |
+
) -> None:
|
150 |
+
with st.form(key, clear_on_submit=False):
|
151 |
+
st.header(section_name)
|
152 |
+
# setting the 'key' argument below to update the session_state
|
153 |
+
st.text_input("Section name", value=old_title, key=f"{key}.title")
|
154 |
+
st.text_area("Content", value=content, key=f"{key}.content")
|
155 |
+
st.form_submit_button(
|
156 |
+
"Update",
|
157 |
+
on_click=_update_model_card,
|
158 |
+
args=(model_card, key, section_name, content),
|
159 |
+
)
|
160 |
+
|
161 |
+
|
162 |
+
def _add_fig_form(
|
163 |
+
model_card: card.Card, key: str, section_name: str, old_title: str, content: str
|
164 |
+
) -> None:
|
165 |
+
with st.form(key, clear_on_submit=False):
|
166 |
+
st.header(section_name)
|
167 |
+
# setting the 'key' argument below to update the session_state
|
168 |
+
st.text_input("Section name", value=old_title, key=f"{key}.title")
|
169 |
+
st.file_uploader("Upload image", key=f"{key}.content")
|
170 |
+
st.form_submit_button(
|
171 |
+
"Update",
|
172 |
+
on_click=_update_model_card,
|
173 |
+
args=(model_card, key, section_name, content),
|
174 |
+
)
|
175 |
+
|
176 |
+
|
177 |
+
def create_form_from_section(
|
178 |
+
model_card: card.Card,
|
179 |
+
key: str,
|
180 |
+
section_name: str,
|
181 |
+
) -> None:
|
182 |
+
# Code for creating a single section, plot or text
|
183 |
+
section = model_card.select(key)
|
184 |
+
content = section.content
|
185 |
+
split_sections = split_subsection_names(section_name)
|
186 |
+
old_title = split_sections[-1]
|
187 |
+
|
188 |
+
if isinstance(section, PlotSection):
|
189 |
+
_add_fig_form(
|
190 |
+
model_card=model_card,
|
191 |
+
key=key,
|
192 |
+
section_name=section_name,
|
193 |
+
old_title=old_title,
|
194 |
+
content=content,
|
195 |
+
)
|
196 |
+
path = st.session_state.hf_path / Path(section.path).name
|
197 |
+
else:
|
198 |
+
_add_section_form(
|
199 |
+
model_card=model_card,
|
200 |
+
key=key,
|
201 |
+
section_name=section_name,
|
202 |
+
old_title=old_title,
|
203 |
+
content=content,
|
204 |
+
)
|
205 |
+
path = None
|
206 |
+
|
207 |
+
col_0, col_1, col_2 = st.columns([4, 2, 2])
|
208 |
+
with col_0:
|
209 |
+
st.button(
|
210 |
+
f"Delete '{arepr.repr(old_title)}'",
|
211 |
+
on_click=_delete_section,
|
212 |
+
args=(model_card, key, path),
|
213 |
+
key=f"{key}.delete",
|
214 |
+
help="Delete this section, including all its subsections",
|
215 |
+
)
|
216 |
+
with col_1:
|
217 |
+
st.button(
|
218 |
+
"add section below",
|
219 |
+
on_click=_add_section,
|
220 |
+
args=(model_card, key),
|
221 |
+
key=f"{key}.add",
|
222 |
+
help="Add a new subsection below this section",
|
223 |
+
)
|
224 |
+
with col_2:
|
225 |
+
st.button(
|
226 |
+
"add figure below",
|
227 |
+
on_click=_add_figure,
|
228 |
+
args=(model_card, key),
|
229 |
+
key=f"{key}.fig",
|
230 |
+
help="Add a new figure below this section",
|
231 |
+
)
|
232 |
+
|
233 |
+
|
234 |
+
def display_sections(model_card: card.Card) -> None:
|
235 |
+
# display all sections, looping through them recursively
|
236 |
+
for key, title in iterate_key_section_content(model_card._data):
|
237 |
+
create_form_from_section(model_card, key=key, section_name=title)
|
238 |
+
|
239 |
+
|
240 |
+
def display_toc(model_card: card.Card) -> None:
|
241 |
+
toc = model_card.get_toc()
|
242 |
+
st.markdown(toc)
|
243 |
+
|
244 |
+
|
245 |
+
def display_model_card(model_card: card.Card) -> None:
|
246 |
+
rendered = model_card.render()
|
247 |
+
metadata, rendered = process_card_for_rendering(rendered)
|
248 |
+
|
249 |
+
# strip metadata
|
250 |
+
with st.expander("show metadata"):
|
251 |
+
st.text(metadata)
|
252 |
+
|
253 |
+
with st.expander("Table of Contents"):
|
254 |
+
display_toc(model_card)
|
255 |
+
|
256 |
+
st.markdown(rendered, unsafe_allow_html=True)
|
257 |
+
|
258 |
+
|
259 |
+
def reset_model_card() -> None:
|
260 |
+
if "task_state" not in st.session_state:
|
261 |
+
return
|
262 |
+
if "model_card" not in st.session_state:
|
263 |
+
del st.session_state["model_card"]
|
264 |
+
|
265 |
+
while st.session_state.task_state.done_list:
|
266 |
+
st.session_state.task_state.undo()
|
267 |
+
|
268 |
+
|
269 |
+
def delete_model_card() -> None:
|
270 |
+
if "hf_path" in st.session_state:
|
271 |
+
del st.session_state["hf_path"]
|
272 |
+
if "model_card" in st.session_state:
|
273 |
+
del st.session_state["model_card"]
|
274 |
+
if "task_state" in st.session_state:
|
275 |
+
st.session_state.task_state.reset()
|
276 |
+
st.session_state.screen.state = "start"
|
277 |
+
|
278 |
+
|
279 |
+
def undo_last():
|
280 |
+
st.session_state.task_state.undo()
|
281 |
+
display_model_card(st.session_state.model_card)
|
282 |
+
|
283 |
+
|
284 |
+
def redo_last():
|
285 |
+
st.session_state.task_state.redo()
|
286 |
+
display_model_card(st.session_state.model_card)
|
287 |
+
|
288 |
+
|
289 |
+
def add_download_model_card_button():
|
290 |
+
model_card = st.session_state.model_card
|
291 |
+
data = get_rendered_model_card(model_card, hf_path=str(st.session_state.hf_path))
|
292 |
+
tip = "Download the generated model card as markdown file"
|
293 |
+
st.download_button(
|
294 |
+
"Save (md)",
|
295 |
+
data=data,
|
296 |
+
help=tip,
|
297 |
+
file_name="README.md",
|
298 |
+
)
|
299 |
+
|
300 |
+
|
301 |
+
def add_create_repo_button():
|
302 |
+
def fn():
|
303 |
+
st.session_state.screen.state = "create_repo"
|
304 |
+
|
305 |
+
button_disabled = not bool(st.session_state.get("model_card"))
|
306 |
+
st.button(
|
307 |
+
"Create Repo",
|
308 |
+
help="Create a model repository on Hugging Face Hub",
|
309 |
+
on_click=fn,
|
310 |
+
disabled=button_disabled,
|
311 |
+
)
|
312 |
+
|
313 |
+
|
314 |
+
def display_edit_buttons():
|
315 |
+
# first row: undo + redo + reset
|
316 |
+
col_0, col_1, col_2, *_ = st.columns([2, 2, 2, 2])
|
317 |
+
undo_disabled = not bool(st.session_state.task_state.done_list)
|
318 |
+
redo_disabled = not bool(st.session_state.task_state.undone_list)
|
319 |
+
with col_0:
|
320 |
+
name = f"UNDO ({len(st.session_state.task_state.done_list)})"
|
321 |
+
tip = "Undo the last edit"
|
322 |
+
st.button(name, on_click=undo_last, disabled=undo_disabled, help=tip)
|
323 |
+
with col_1:
|
324 |
+
name = f"REDO ({len(st.session_state.task_state.undone_list)})"
|
325 |
+
tip = "Redo the last undone edit"
|
326 |
+
st.button(name, on_click=redo_last, disabled=redo_disabled, help=tip)
|
327 |
+
with col_2:
|
328 |
+
tip = "Undo all edits"
|
329 |
+
st.button("Reset", on_click=reset_model_card, help=tip)
|
330 |
+
|
331 |
+
# second row: download + create repo + delete
|
332 |
+
col_0, col_1, col_2, *_ = st.columns([2, 2, 2, 2])
|
333 |
+
with col_0:
|
334 |
+
add_download_model_card_button()
|
335 |
+
with col_1:
|
336 |
+
add_create_repo_button()
|
337 |
+
with col_2:
|
338 |
+
tip = "Start over from scratch (lose all progress)"
|
339 |
+
st.button("Delete", on_click=delete_model_card, help=tip)
|
340 |
+
|
341 |
+
|
342 |
+
def _update_model_diagram():
|
343 |
+
val = st.session_state.get("special_model_diagram", True)
|
344 |
+
model_card = st.session_state.model_card
|
345 |
+
model_card.model_diagram = val
|
346 |
+
|
347 |
+
# TODO: this may no longer be necesssary once this issue is solved:
|
348 |
+
# https://github.com/skops-dev/skops/issues/292
|
349 |
+
if val:
|
350 |
+
model_card.add_model_plot()
|
351 |
+
else:
|
352 |
+
model_card.delete("Model description/Training Procedure/Model Plot")
|
353 |
+
|
354 |
+
|
355 |
+
def _parse_metrics(metrics: str) -> dict[str, str | float]:
|
356 |
+
# parse metrics from text area, one per line, into a dict
|
357 |
+
metrics_table = {}
|
358 |
+
for line in metrics.splitlines():
|
359 |
+
line = line.strip()
|
360 |
+
val: str | float
|
361 |
+
name, _, val = line.partition("=")
|
362 |
+
try:
|
363 |
+
# try to coerce to float but don't error if it fails
|
364 |
+
val = float(val.strip())
|
365 |
+
except ValueError:
|
366 |
+
pass
|
367 |
+
metrics_table[name.strip()] = val
|
368 |
+
return metrics_table
|
369 |
+
|
370 |
+
|
371 |
+
def _update_metrics():
|
372 |
+
metrics = st.session_state.get("special_metrics_text", {})
|
373 |
+
model_card = st.session_state.model_card
|
374 |
+
metrics_table = _parse_metrics(metrics)
|
375 |
+
|
376 |
+
# check if any change
|
377 |
+
if metrics_table == model_card._metrics:
|
378 |
+
return
|
379 |
+
|
380 |
+
task = AddMetricsTask(model_card, metrics_table)
|
381 |
+
st.session_state.task_state.add(task)
|
382 |
+
|
383 |
+
|
384 |
+
def display_skops_special_fields():
|
385 |
+
st.checkbox(
|
386 |
+
"Show model diagram",
|
387 |
+
value=True,
|
388 |
+
on_change=_update_model_diagram,
|
389 |
+
key="special_model_diagram",
|
390 |
+
)
|
391 |
+
|
392 |
+
with st.expander("Add metrics"):
|
393 |
+
with st.form("special_metrics", clear_on_submit=False):
|
394 |
+
st.text_area(
|
395 |
+
"Add one metric per line, e.g. 'accuracy = 0.9'",
|
396 |
+
key="special_metrics_text",
|
397 |
+
)
|
398 |
+
st.form_submit_button(
|
399 |
+
"Update",
|
400 |
+
on_click=_update_metrics,
|
401 |
+
)
|
402 |
+
|
403 |
+
|
404 |
+
def edit_input_form():
|
405 |
+
if "task_state" not in st.session_state:
|
406 |
+
st.session_state.task_state = TaskState()
|
407 |
+
|
408 |
+
with st.sidebar:
|
409 |
+
# TOP ROW BUTTONS
|
410 |
+
display_edit_buttons()
|
411 |
+
|
412 |
+
# SHOW SPECIAL FIELDS IF SKOPS TEMPLATE WAS USED
|
413 |
+
if st.session_state.get("model_card_type", "") == "skops":
|
414 |
+
display_skops_special_fields()
|
415 |
+
|
416 |
+
# SHOW EDITABLE SECTIONS
|
417 |
+
if "model_card" in st.session_state:
|
418 |
+
display_sections(st.session_state.model_card)
|
419 |
+
|
420 |
+
if "model_card" in st.session_state:
|
421 |
+
display_model_card(st.session_state.model_card)
|
gethelp.py
ADDED
@@ -0,0 +1,329 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
|
4 |
+
def add_back_button(key):
|
5 |
+
def fn():
|
6 |
+
st.session_state.screen.state = "start"
|
7 |
+
|
8 |
+
st.button("Back", help="Get back to the start screen", on_click=fn, key=key)
|
9 |
+
|
10 |
+
|
11 |
+
help_md = """# Create a Hugging Face model repository for scikit learn models
|
12 |
+
|
13 |
+
This page aims to provide a simple interface to use the
|
14 |
+
[`skops`](https://skops.readthedocs.io/) model card and HF Hub creation
|
15 |
+
utilities.
|
16 |
+
|
17 |
+
Below, we will explain the steps involved to create your own model repository to
|
18 |
+
host your scikit-learn model:
|
19 |
+
|
20 |
+
1. Prepare the model repository
|
21 |
+
2. Edit the model card
|
22 |
+
3. Create the model repository on Hugging Face Hub
|
23 |
+
|
24 |
+
## Step 1: Prepare the model repository
|
25 |
+
|
26 |
+
In this step, you do the necessary preparation work to create a [model
|
27 |
+
repository on Hugging Face Hub](https://huggingface.co/docs/hub/models).
|
28 |
+
|
29 |
+
### Upload a model
|
30 |
+
|
31 |
+
Here you should upload the sklearn model we want to present in the model
|
32 |
+
repository. The model should be stored either as a ``pickle`` file or it should
|
33 |
+
use the [secure skops persistence
|
34 |
+
format](https://skops.readthedocs.io/en/stable/persistence.html). Later, this
|
35 |
+
model will be uploaded to the model repository so that you can share it with
|
36 |
+
others.
|
37 |
+
|
38 |
+
The uploaded model should be a scikit-learn model or a model that is compatible
|
39 |
+
with the sklearn API, e.g. using [XGBoost sklearn
|
40 |
+
wrapper](https://xgboost.readthedocs.io/en/stable/python/python_api.html#module-xgboost.sklearn)
|
41 |
+
when it's an XGBoost model.
|
42 |
+
|
43 |
+
If you just want to test out the application and don't want to upload a model, a
|
44 |
+
dummy model will be used instead.
|
45 |
+
|
46 |
+
### Upload input data
|
47 |
+
|
48 |
+
It's possible to upload input data as a csv file. If that is done, the first few
|
49 |
+
rows of the input data will be used as sample data for the model, e.g. when
|
50 |
+
trying out the [inference API](https://huggingface.co/inference-api).
|
51 |
+
|
52 |
+
|
53 |
+
### Choose the task type
|
54 |
+
|
55 |
+
Choose the type of task that the model is intended to solve. It can be either
|
56 |
+
classification or regression, with input data being either tabular in nature or
|
57 |
+
text.
|
58 |
+
|
59 |
+
### Requirements
|
60 |
+
|
61 |
+
This is the list of Python requirements needed to run the model.
|
62 |
+
|
63 |
+
### Choose the model card template
|
64 |
+
|
65 |
+
This is the final step and choosing one of the options will bring you to the
|
66 |
+
editing step.
|
67 |
+
|
68 |
+
#### Create a new skops model card
|
69 |
+
|
70 |
+
This is the recommended way of getting started. The skops model card template
|
71 |
+
prefills the model card with some [useful
|
72 |
+
contents](https://skops.readthedocs.io/en/stable/model_card.html#model-card-content)
|
73 |
+
that you probably want to have in most model cards. Don't worry: If you don't
|
74 |
+
like part of the content, you can always edit or delete it later.
|
75 |
+
|
76 |
+
#### Create a new empty model card
|
77 |
+
|
78 |
+
If you want to start the model card completely from scratch, that's also
|
79 |
+
possible by choosing this option. It will generate a completely empty model card
|
80 |
+
for you that you can fashion to your liking.
|
81 |
+
|
82 |
+
#### Load existing model card from HF Hub
|
83 |
+
|
84 |
+
If you want to use an existing model card and edit it, that's also possible.
|
85 |
+
Please enter the Hugging Face Hub repository ID here and the corresponding model
|
86 |
+
card will be loaded. The repo ID is typically someting like `username/reponame`,
|
87 |
+
e.g. `openai/whisper-small`. Some models also omit the user name, e.g. `gpt2`.
|
88 |
+
|
89 |
+
Note that when you choose an existing model card, a couple of files will be
|
90 |
+
downloaded, because they may be required to render the model card (e.g. images).
|
91 |
+
Therefore, depending on the repository, this step may take a bit.
|
92 |
+
|
93 |
+
If you notice any problems when rendering the existing model card, please let us
|
94 |
+
know by [creating an issue](https://github.com/skops-dev/skops/issues).
|
95 |
+
|
96 |
+
## Step 2: Edit the model card
|
97 |
+
|
98 |
+
Before creating the model repository, it is crucial to ensure that the [model
|
99 |
+
card](https://huggingface.co/blog/model-cards) is edited to best represent the
|
100 |
+
model you're working on. This can be achieved in the editing step, which is
|
101 |
+
described in more detail below.
|
102 |
+
|
103 |
+
### Editing sidbar
|
104 |
+
|
105 |
+
In the left sidebar, you will be able to edit the model card, whereas the main
|
106 |
+
screen is reserved for rendering the model card so that you see what you will
|
107 |
+
get. We will start by describing the editing sidebar.
|
108 |
+
|
109 |
+
Tip: You should increase the width of the side bar if it is too narrow for your
|
110 |
+
taste.
|
111 |
+
|
112 |
+
#### Undo, redo & reset
|
113 |
+
|
114 |
+
On top of the side bar, you have the option to undo, redo, and reset the last
|
115 |
+
operation you did. Say, you accidentally made a change, just press the `Undo`
|
116 |
+
button to undo this change. Similarly, if you want to undo your undo operation,
|
117 |
+
press the `Redo` button. Finally, if you press `Reset`, all your operations will
|
118 |
+
be undone (but don't worry if you click the button accidentally, you can redo
|
119 |
+
all of them if you want).
|
120 |
+
|
121 |
+
#### Save, create repo & delete
|
122 |
+
|
123 |
+
These buttons are intended for when you finished editing the model card. When
|
124 |
+
you click on `Save`, you will get the option to download the model card as a
|
125 |
+
markdown file.
|
126 |
+
|
127 |
+
When clicking the `Create Repo` button, you will be taken to the next screen,
|
128 |
+
which offers you to create a model repository on Hugging Face Hub. This step
|
129 |
+
will be explained in more detail further below.
|
130 |
+
|
131 |
+
Finally, you can click on `Delete` to completely discard all the changes you
|
132 |
+
made and be taken back to the start screen of the app. Be careful, any change
|
133 |
+
you made will be lost. It is thus advised to first save the model card before
|
134 |
+
pressing `Delete`.
|
135 |
+
|
136 |
+
#### Edit a section
|
137 |
+
|
138 |
+
Each section has its own form field, which allows you to make edits. Change the
|
139 |
+
name of the section or change the content (or both), then click `Update` to see
|
140 |
+
a preview of your change. As with all other operations, you can undo the change
|
141 |
+
by clicking on `Undo`.
|
142 |
+
|
143 |
+
#### Delete a section
|
144 |
+
|
145 |
+
Below the form field for editing the section, you will find a `Delete` button
|
146 |
+
(including the name of the section to make it clear which section it refers to).
|
147 |
+
If you click that button, the whole section, _including its subsections_, will
|
148 |
+
be deleted. Again, click on `Undo` if you accidentally deleted something that
|
149 |
+
you want to keep.
|
150 |
+
|
151 |
+
#### Add section below
|
152 |
+
|
153 |
+
If you click on this button, a new subsection wil be created under the current
|
154 |
+
section. This will create a section with a dummy title and dummy content, which
|
155 |
+
you can then edit.
|
156 |
+
|
157 |
+
Note that this will create a new _subsection_. If there are already existing
|
158 |
+
subsections in the current section, the new subsection will be created _below_
|
159 |
+
those existing subsections. So the new subsection you create might not appear
|
160 |
+
exactly where you expect it to appear. To illustrate this, assume that we have
|
161 |
+
the following sections and subsections:
|
162 |
+
|
163 |
+
- Section A
|
164 |
+
- Subsection A.1
|
165 |
+
- Subsection A.2
|
166 |
+
- Section B
|
167 |
+
|
168 |
+
If you create a new section below "Section A", it will be created on the same
|
169 |
+
level, and below of, "Subsection A.2", resulting in the following structure:
|
170 |
+
|
171 |
+
- Section A
|
172 |
+
- Subsection A.1
|
173 |
+
- Subsection A.2
|
174 |
+
- NEW SUBSECTION
|
175 |
+
- Section B
|
176 |
+
|
177 |
+
If you create a new section below the "Subsection A.1", you will actually create
|
178 |
+
a sub-subsection, resulting in the following structure instead:
|
179 |
+
|
180 |
+
- Section A
|
181 |
+
- Subsection A.1
|
182 |
+
- NEW SUB-SUBSECTION
|
183 |
+
- Subsection A.2
|
184 |
+
- Section B
|
185 |
+
|
186 |
+
Hopefully, this clarifies things. Unfortunately, there is no possibility (yet)
|
187 |
+
to re-order sections.
|
188 |
+
|
189 |
+
#### Add figure below
|
190 |
+
|
191 |
+
This button works quite similarly to adding a new section. The main difference
|
192 |
+
is that instead of having a text area to enter content, you will be asked to
|
193 |
+
upload an image file. By default, a dummy image will be shown in the preview.
|
194 |
+
|
195 |
+
#### Add metrics (only skops template)
|
196 |
+
|
197 |
+
If you have chosen the skops template, you will see an additional field called
|
198 |
+
`Add metrics` near the top of the side bar. Here you can choose metrics you want
|
199 |
+
to be shown in the model card, e.g. the accuracy the model achieved on a
|
200 |
+
specific dataset. Please enter one metric per line, with the metric name on the
|
201 |
+
left, then an `=` sign, and the value on the right, e.g. `accuracy on test set =
|
202 |
+
0.85`.
|
203 |
+
|
204 |
+
After pressing `update`, the metrics will be shown in a table in the section
|
205 |
+
`Model description/Evaluation Results`. You can always add or remove metrics
|
206 |
+
from this field later. If you want to delete this section completely, look for
|
207 |
+
its edit form further below and press `Delete`. There, you can also edit the
|
208 |
+
table in a more fine grained way, e.g. by changing the alignment.
|
209 |
+
|
210 |
+
If you don't use the skops template and still want to add a table, it is
|
211 |
+
possible to do that, but it's requires a bit more work. Add a new section as
|
212 |
+
described above, then, in the text area, create a table using the [markdown
|
213 |
+
table
|
214 |
+
syntax](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables#creating-a-table).
|
215 |
+
|
216 |
+
### Model card visualization
|
217 |
+
|
218 |
+
The main part of the page will show you what the final model card will look
|
219 |
+
like.
|
220 |
+
|
221 |
+
#### Metadata
|
222 |
+
|
223 |
+
On the very top, you can see the metadata of the model card (it is collapsed by
|
224 |
+
default). The metadata can be very useful for features on the HF Hub, e.g.
|
225 |
+
allowing other users to find your model by a given tag.
|
226 |
+
|
227 |
+
Right now, it is not possible to edit the metadata directly from here. But don't
|
228 |
+
worry, once you have created the model card repository, you can easily edit the
|
229 |
+
metadata there.
|
230 |
+
|
231 |
+
#### Table of Contents
|
232 |
+
|
233 |
+
For your convenience, a table of contents is also shown at the top (collapsed by
|
234 |
+
default). This is useful if you have a bigger model card and want to see the
|
235 |
+
overview of all its contents.
|
236 |
+
|
237 |
+
#### Markdown preview
|
238 |
+
|
239 |
+
Finally, the model card itself is shown. This is how the model card will look
|
240 |
+
like once it is saved as markdown and then rendered.
|
241 |
+
|
242 |
+
## Step 3: Creating a model repository
|
243 |
+
|
244 |
+
After you have finished editing the model card, it is time to create a model
|
245 |
+
repository on Hugging Face Hub. Click on `Create Repo` and you will be taken to
|
246 |
+
the final step of the process.
|
247 |
+
|
248 |
+
### Back & Delete
|
249 |
+
|
250 |
+
If you find yourself wanting to make more edits to the model card, just click on
|
251 |
+
the `Back` button and you'll be brought back to the editing step.
|
252 |
+
|
253 |
+
You can also click `Delete`, which will discard all your changes and bring you
|
254 |
+
back to the start page. Be careful: This step cannot be undone and all your
|
255 |
+
progress will be lost.
|
256 |
+
|
257 |
+
### Files included in the repository
|
258 |
+
|
259 |
+
For your convenience, this will show a preview of all the files included in the
|
260 |
+
repository, as well as their sizes. Don't create a repository if you see files
|
261 |
+
there that you don't want to be uploaded.
|
262 |
+
|
263 |
+
### Privacy settings
|
264 |
+
|
265 |
+
By default, a private repository will be created. If you untick this box, it
|
266 |
+
will be public instead. More information on what that implies can be found in
|
267 |
+
the [docs on repository
|
268 |
+
settings](https://huggingface.co/docs/hub/repositories-settings).
|
269 |
+
|
270 |
+
### Name of the repository
|
271 |
+
|
272 |
+
Here you have to enter the name of the repository. Typically, that's something
|
273 |
+
like `username/reponame` or `organame/reponame`. This field is mandatory and you
|
274 |
+
should ensure that the corresponding repository ID does not exist yet.
|
275 |
+
|
276 |
+
### Enter your Hugging Face token
|
277 |
+
|
278 |
+
Here you need to paste your Hugging Face token, which is used for
|
279 |
+
authentication. The token can be found [here](https://hf.co/settings/token) and
|
280 |
+
it always starts with "hf_". Entering a token is necessary to create a
|
281 |
+
repository.
|
282 |
+
|
283 |
+
Note that if you don't already have an account on Hugging Face, you need to
|
284 |
+
create one to get a token. It's free.
|
285 |
+
|
286 |
+
### Create a new repository
|
287 |
+
|
288 |
+
Once all the required fields are filled, click on this button to create the
|
289 |
+
repository. Depending on the size, it may take a couple of seconds to finish.
|
290 |
+
Once it is created, you will see a success notification that includes the link
|
291 |
+
to the repository. Congratulations, you're done!
|
292 |
+
|
293 |
+
## Troubleshooting
|
294 |
+
|
295 |
+
### Not all skops features available
|
296 |
+
|
297 |
+
This app is based on the [skops model card
|
298 |
+
feature](https://skops.readthedocs.io/en/stable/model_card.html#model-card-content).
|
299 |
+
However, it does not support all the options that are available there. If you
|
300 |
+
want to use all those options in a programmatic fashion, please follow the link
|
301 |
+
and read up on what it takes to create a model card with skops. The full power
|
302 |
+
of the `Card` class is documented
|
303 |
+
[here](https://skops.readthedocs.io/en/stable/modules/classes.html#skops.card.Card).
|
304 |
+
|
305 |
+
### Strange behavior
|
306 |
+
|
307 |
+
If the app behaves strangely, shows error messages, or renders incorrectly, it
|
308 |
+
may be necessary to refresh the browser tab. This will take you back to the
|
309 |
+
start page, with all progress being lost. If you can reproduce that behavior,
|
310 |
+
please [creating an issue](https://github.com/skops-dev/skops/issues) and let us
|
311 |
+
know.
|
312 |
+
|
313 |
+
### Contact
|
314 |
+
|
315 |
+
If you want to contact us, you can join our discord channel. To do that, follow
|
316 |
+
[these
|
317 |
+
instructions](https://skops.readthedocs.io/en/stable/community.html#discord).
|
318 |
+
"""
|
319 |
+
|
320 |
+
|
321 |
+
def add_help_content():
|
322 |
+
# This is the exact same text as in the README.md of this space
|
323 |
+
st.markdown(help_md)
|
324 |
+
|
325 |
+
|
326 |
+
def help_page():
|
327 |
+
add_back_button(key="help_get_back")
|
328 |
+
add_help_content()
|
329 |
+
add_back_button(key="help_get_back2") # names must be unique
|
make-data.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# companion script to the space creator
|
2 |
+
# generates the logreg.pkl and logreg.skops file, as well as data.csv
|
3 |
+
|
4 |
+
import pickle
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
from sklearn.datasets import make_classification
|
8 |
+
from sklearn.linear_model import LogisticRegression
|
9 |
+
from sklearn.pipeline import Pipeline
|
10 |
+
from sklearn.preprocessing import StandardScaler
|
11 |
+
|
12 |
+
import skops.io as sio
|
13 |
+
|
14 |
+
X, y = make_classification()
|
15 |
+
df = pd.DataFrame(X)
|
16 |
+
|
17 |
+
clf = Pipeline(
|
18 |
+
[
|
19 |
+
("scale", StandardScaler()),
|
20 |
+
("clf", LogisticRegression(random_state=0)),
|
21 |
+
]
|
22 |
+
)
|
23 |
+
clf.fit(X, y)
|
24 |
+
|
25 |
+
with open("logreg.pkl", "wb") as f:
|
26 |
+
pickle.dump(clf, f)
|
27 |
+
sio.dump(clf, "logreg.skops")
|
28 |
+
|
29 |
+
|
30 |
+
df.to_csv("data.csv", index=False)
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
pandoc
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
catboost
|
2 |
+
huggingface_hub
|
3 |
+
lightgbm
|
4 |
+
pandas
|
5 |
+
scikit-learn
|
6 |
+
xgboost
|
7 |
+
git+https://github.com/skops-dev/skops.git
|
start.py
ADDED
@@ -0,0 +1,252 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Start page of the app
|
2 |
+
|
3 |
+
This page is used to initialize a model card that is either:
|
4 |
+
|
5 |
+
1. based on the skops template
|
6 |
+
2. empty
|
7 |
+
3. loads an existing model card
|
8 |
+
|
9 |
+
Optionally, users can add a model file, data, requirements, and choose a task.
|
10 |
+
|
11 |
+
"""
|
12 |
+
|
13 |
+
import glob
|
14 |
+
import io
|
15 |
+
import os
|
16 |
+
import pickle
|
17 |
+
import shutil
|
18 |
+
from pathlib import Path
|
19 |
+
from tempfile import mkdtemp
|
20 |
+
|
21 |
+
import pandas as pd
|
22 |
+
import sklearn
|
23 |
+
import streamlit as st
|
24 |
+
from huggingface_hub import snapshot_download
|
25 |
+
from huggingface_hub.utils import HFValidationError, RepositoryNotFoundError
|
26 |
+
from sklearn.base import BaseEstimator
|
27 |
+
from sklearn.dummy import DummyClassifier
|
28 |
+
|
29 |
+
import skops.io as sio
|
30 |
+
from skops import card, hub_utils
|
31 |
+
from skops.io import get_untrusted_types
|
32 |
+
|
33 |
+
tmp_path = Path(mkdtemp(prefix="skops-")) # temporary files
|
34 |
+
description = """Create a Hugging Face model repository for scikit learn models
|
35 |
+
|
36 |
+
This page aims to provide a simple interface to use the
|
37 |
+
[`skops`](https://skops.readthedocs.io/) model card and HF Hub creation
|
38 |
+
utilities.
|
39 |
+
|
40 |
+
"""
|
41 |
+
|
42 |
+
|
43 |
+
def load_model() -> None:
|
44 |
+
if st.session_state.get("model_file") is None:
|
45 |
+
st.session_state.model = DummyClassifier()
|
46 |
+
return
|
47 |
+
|
48 |
+
bytes_data = st.session_state.model_file.getvalue()
|
49 |
+
if st.session_state.model_file.name.endswith("skops"):
|
50 |
+
model = sio.loads(bytes_data, trusted=get_untrusted_types(data=bytes_data))
|
51 |
+
else:
|
52 |
+
model = pickle.loads(bytes_data)
|
53 |
+
assert isinstance(model, BaseEstimator), "model must be an sklearn model"
|
54 |
+
|
55 |
+
st.session_state.model = model
|
56 |
+
|
57 |
+
|
58 |
+
def load_data() -> None:
|
59 |
+
if st.session_state.get("data_file"):
|
60 |
+
bytes_data = io.BytesIO(st.session_state.data_file.getvalue())
|
61 |
+
df = pd.read_csv(bytes_data)
|
62 |
+
else:
|
63 |
+
df = pd.DataFrame([])
|
64 |
+
|
65 |
+
st.session_state.data = df
|
66 |
+
|
67 |
+
|
68 |
+
def _clear_repo(path: str) -> None:
|
69 |
+
for file_path in glob.glob(str(Path(path) / "*")):
|
70 |
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
71 |
+
os.unlink(file_path)
|
72 |
+
elif os.path.isdir(file_path):
|
73 |
+
shutil.rmtree(file_path)
|
74 |
+
|
75 |
+
|
76 |
+
def init_repo() -> None:
|
77 |
+
path = st.session_state.hf_path
|
78 |
+
_clear_repo(path)
|
79 |
+
requirements = []
|
80 |
+
task = "tabular-classification"
|
81 |
+
data = pd.DataFrame([])
|
82 |
+
|
83 |
+
if "requirements" in st.session_state:
|
84 |
+
requirements = st.session_state.requirements.splitlines()
|
85 |
+
if "task" in st.session_state:
|
86 |
+
task = st.session_state.task
|
87 |
+
if "data_file" in st.session_state:
|
88 |
+
load_data()
|
89 |
+
data = st.session_state.data
|
90 |
+
|
91 |
+
if task.startswith("text") and isinstance(data, pd.DataFrame):
|
92 |
+
data = data.values.tolist()
|
93 |
+
|
94 |
+
try:
|
95 |
+
file_name = tmp_path / "model.skops"
|
96 |
+
sio.dump(st.session_state.model, file_name)
|
97 |
+
|
98 |
+
hub_utils.init(
|
99 |
+
model=file_name,
|
100 |
+
dst=path,
|
101 |
+
task=task,
|
102 |
+
data=data,
|
103 |
+
requirements=requirements,
|
104 |
+
)
|
105 |
+
except Exception as exc:
|
106 |
+
print("Uh oh, something went wrong when initializing the repo:", exc)
|
107 |
+
|
108 |
+
|
109 |
+
def create_skops_model_card() -> None:
|
110 |
+
init_repo()
|
111 |
+
metadata = card.metadata_from_config(st.session_state.hf_path)
|
112 |
+
model_card = card.Card(model=st.session_state.model, metadata=metadata)
|
113 |
+
st.session_state.model_card = model_card
|
114 |
+
st.session_state.model_card_type = "skops"
|
115 |
+
st.session_state.screen.state = "edit"
|
116 |
+
|
117 |
+
|
118 |
+
def create_empty_model_card() -> None:
|
119 |
+
init_repo()
|
120 |
+
metadata = card.metadata_from_config(st.session_state.hf_path)
|
121 |
+
model_card = card.Card(
|
122 |
+
model=st.session_state.model, metadata=metadata, template=None
|
123 |
+
)
|
124 |
+
model_card.add(**{"Untitled": "[More Information Needed]"})
|
125 |
+
st.session_state.model_card = model_card
|
126 |
+
st.session_state.model_card_type = "empty"
|
127 |
+
st.session_state.screen.state = "edit"
|
128 |
+
|
129 |
+
|
130 |
+
def create_hf_model_card() -> None:
|
131 |
+
repo_id = st.session_state.get("hf_repo_id", "").strip().strip("'").strip('"')
|
132 |
+
if not repo_id:
|
133 |
+
return
|
134 |
+
|
135 |
+
try:
|
136 |
+
allow_patterns = [
|
137 |
+
"*.md",
|
138 |
+
".txt",
|
139 |
+
"*.png",
|
140 |
+
"*.gif",
|
141 |
+
"*.jpg",
|
142 |
+
"*.jpeg",
|
143 |
+
"*.bmp",
|
144 |
+
"*.webp",
|
145 |
+
]
|
146 |
+
path = snapshot_download(repo_id, allow_patterns=allow_patterns)
|
147 |
+
except (HFValidationError, RepositoryNotFoundError):
|
148 |
+
st.error(
|
149 |
+
f"Repository '{repo_id}' could not be found on HF Hub, "
|
150 |
+
"please check that the repo ID is correct."
|
151 |
+
)
|
152 |
+
return
|
153 |
+
|
154 |
+
# move everything to the hf_path and working dir
|
155 |
+
hf_path = st.session_state.hf_path
|
156 |
+
shutil.copytree(path, hf_path, dirs_exist_ok=True)
|
157 |
+
shutil.copytree(path, ".", dirs_exist_ok=True)
|
158 |
+
|
159 |
+
model_card = card.parse_modelcard(hf_path / "README.md")
|
160 |
+
st.session_state.model_card = model_card
|
161 |
+
st.session_state.model_card_type = "loaded"
|
162 |
+
st.session_state.screen.state = "edit"
|
163 |
+
|
164 |
+
|
165 |
+
def add_help_button():
|
166 |
+
def fn():
|
167 |
+
st.session_state.screen.state = "help"
|
168 |
+
|
169 |
+
st.button(
|
170 |
+
"Instructions",
|
171 |
+
on_click=fn,
|
172 |
+
help="Detailed explanation of this space",
|
173 |
+
key="get_help",
|
174 |
+
)
|
175 |
+
|
176 |
+
|
177 |
+
def start_input_form():
|
178 |
+
if "model" not in st.session_state:
|
179 |
+
st.session_state.model = DummyClassifier()
|
180 |
+
|
181 |
+
if "data" not in st.session_state:
|
182 |
+
st.session_state.data = pd.DataFrame([])
|
183 |
+
|
184 |
+
if "model_card" not in st.session_state:
|
185 |
+
st.session_state.model_card = None
|
186 |
+
|
187 |
+
st.markdown(description)
|
188 |
+
|
189 |
+
add_help_button()
|
190 |
+
|
191 |
+
st.markdown("---")
|
192 |
+
|
193 |
+
st.text(
|
194 |
+
"Upload an sklearn model (strongly recommended)\n"
|
195 |
+
"The model can be used to automatically populate fields in the model card."
|
196 |
+
)
|
197 |
+
|
198 |
+
if not st.session_state.get("model_file"):
|
199 |
+
st.file_uploader(
|
200 |
+
"Upload an sklearn model (pickle or skops format)",
|
201 |
+
on_change=load_model,
|
202 |
+
key="model_file",
|
203 |
+
)
|
204 |
+
|
205 |
+
st.markdown("---")
|
206 |
+
|
207 |
+
st.text(
|
208 |
+
"Upload samples from your data (in csv format)\n"
|
209 |
+
"This sample data can be attached to the metadata of the model card"
|
210 |
+
)
|
211 |
+
st.file_uploader(
|
212 |
+
"Upload input data (csv)", type=["csv"], on_change=load_data, key="data_file"
|
213 |
+
)
|
214 |
+
st.markdown("---")
|
215 |
+
|
216 |
+
st.selectbox(
|
217 |
+
label="Choose the task type",
|
218 |
+
options=[
|
219 |
+
"tabular-classification",
|
220 |
+
"tabular-regression",
|
221 |
+
"text-classification",
|
222 |
+
"text-regression",
|
223 |
+
],
|
224 |
+
key="task",
|
225 |
+
on_change=init_repo,
|
226 |
+
)
|
227 |
+
st.markdown("---")
|
228 |
+
|
229 |
+
st.text_area(
|
230 |
+
label="Requirements",
|
231 |
+
value=f"scikit-learn=={sklearn.__version__}\n",
|
232 |
+
key="requirements",
|
233 |
+
on_change=init_repo,
|
234 |
+
)
|
235 |
+
st.markdown("---")
|
236 |
+
|
237 |
+
st.markdown("Choose one of the options below to get started:")
|
238 |
+
col_0, col_1, col_2 = st.columns([2, 2, 2])
|
239 |
+
with col_0:
|
240 |
+
st.button("Create a new skops model card", on_click=create_skops_model_card)
|
241 |
+
|
242 |
+
with col_1:
|
243 |
+
st.button("Create a new empty model card", on_click=create_empty_model_card)
|
244 |
+
|
245 |
+
with col_2:
|
246 |
+
with st.form("Load existing model card from HF Hub", clear_on_submit=False):
|
247 |
+
st.markdown("Load existing model card from HF Hub")
|
248 |
+
st.text_input("Repo name (e.g. 'gpt2')", key="hf_repo_id")
|
249 |
+
st.form_submit_button("Load", on_click=create_hf_model_card)
|
250 |
+
|
251 |
+
|
252 |
+
start_input_form()
|
tasks.py
ADDED
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Functionality around tasks
|
2 |
+
|
3 |
+
Tasks are used to implement "undo" and "redo" functionality.
|
4 |
+
|
5 |
+
"""
|
6 |
+
from __future__ import annotations
|
7 |
+
|
8 |
+
import shutil
|
9 |
+
from pathlib import Path
|
10 |
+
from tempfile import mkdtemp
|
11 |
+
from uuid import uuid4
|
12 |
+
|
13 |
+
from streamlit.runtime.uploaded_file_manager import UploadedFile
|
14 |
+
|
15 |
+
from skops import card
|
16 |
+
from skops.card._model_card import PlotSection, split_subsection_names
|
17 |
+
|
18 |
+
|
19 |
+
class Task:
|
20 |
+
"""(Abstract) base class for tasks"""
|
21 |
+
|
22 |
+
def do(self) -> None:
|
23 |
+
raise NotImplementedError
|
24 |
+
|
25 |
+
def undo(self) -> None:
|
26 |
+
raise NotImplementedError
|
27 |
+
|
28 |
+
|
29 |
+
class TaskState:
|
30 |
+
"""Tracking the state of tasks"""
|
31 |
+
|
32 |
+
def __init__(self) -> None:
|
33 |
+
self.done_list: list[Task] = []
|
34 |
+
self.undone_list: list[Task] = []
|
35 |
+
|
36 |
+
def undo(self) -> None:
|
37 |
+
if not self.done_list:
|
38 |
+
return
|
39 |
+
|
40 |
+
task = self.done_list.pop(-1)
|
41 |
+
task.undo()
|
42 |
+
self.undone_list.append(task)
|
43 |
+
|
44 |
+
def redo(self) -> None:
|
45 |
+
if not self.undone_list:
|
46 |
+
return
|
47 |
+
|
48 |
+
task = self.undone_list.pop(-1)
|
49 |
+
task.do()
|
50 |
+
self.done_list.append(task)
|
51 |
+
|
52 |
+
def add(self, task: Task) -> None:
|
53 |
+
task.do()
|
54 |
+
self.done_list.append(task)
|
55 |
+
self.undone_list.clear()
|
56 |
+
|
57 |
+
def reset(self) -> None:
|
58 |
+
self.done_list.clear()
|
59 |
+
self.undone_list.clear()
|
60 |
+
|
61 |
+
|
62 |
+
class AddSectionTask(Task):
|
63 |
+
"""Add a new text section"""
|
64 |
+
|
65 |
+
def __init__(
|
66 |
+
self,
|
67 |
+
model_card: card.Card,
|
68 |
+
title: str,
|
69 |
+
content: str,
|
70 |
+
) -> None:
|
71 |
+
self.model_card = model_card
|
72 |
+
self.title = title
|
73 |
+
self.key = title + " " + str(uuid4())[:6]
|
74 |
+
self.content = content
|
75 |
+
|
76 |
+
def do(self) -> None:
|
77 |
+
self.model_card.add(**{self.key: self.content})
|
78 |
+
section = self.model_card.select(self.key)
|
79 |
+
section.title = split_subsection_names(self.title)[-1]
|
80 |
+
|
81 |
+
def undo(self) -> None:
|
82 |
+
self.model_card.delete(self.key)
|
83 |
+
|
84 |
+
|
85 |
+
class AddFigureTask(Task):
|
86 |
+
"""Add a new figure section
|
87 |
+
|
88 |
+
Figure always starts out with dummy image cat.png.
|
89 |
+
|
90 |
+
"""
|
91 |
+
|
92 |
+
def __init__(
|
93 |
+
self,
|
94 |
+
model_card: card.Card,
|
95 |
+
path: Path,
|
96 |
+
title: str,
|
97 |
+
content: str,
|
98 |
+
) -> None:
|
99 |
+
self.model_card = model_card
|
100 |
+
self.title = title
|
101 |
+
|
102 |
+
# Create a unique file name, since the same image can exist more than
|
103 |
+
# once per model card.
|
104 |
+
fname = Path(content)
|
105 |
+
stem = fname.stem
|
106 |
+
suffix = fname.suffix
|
107 |
+
uniq = str(uuid4())[:6]
|
108 |
+
new_fname = str(path / stem) + "_" + uniq + suffix
|
109 |
+
|
110 |
+
self.key = title + " " + uniq
|
111 |
+
self.content = Path(new_fname)
|
112 |
+
|
113 |
+
def do(self) -> None:
|
114 |
+
shutil.copy("cat.png", self.content)
|
115 |
+
self.model_card.add_plot(**{self.key: self.content})
|
116 |
+
section = self.model_card.select(self.key)
|
117 |
+
section.title = split_subsection_names(self.title)[-1]
|
118 |
+
|
119 |
+
def undo(self) -> None:
|
120 |
+
self.content.unlink(missing_ok=True)
|
121 |
+
self.model_card.delete(self.key)
|
122 |
+
|
123 |
+
|
124 |
+
class DeleteSectionTask(Task):
|
125 |
+
"""Delete a section
|
126 |
+
|
127 |
+
The section is not completely removed from the underlying data structure,
|
128 |
+
but only turned invisible.
|
129 |
+
|
130 |
+
"""
|
131 |
+
|
132 |
+
def __init__(
|
133 |
+
self,
|
134 |
+
model_card: card.Card,
|
135 |
+
key: str,
|
136 |
+
path: Path | None,
|
137 |
+
) -> None:
|
138 |
+
self.model_card = model_card
|
139 |
+
self.key = key
|
140 |
+
# when 'deleting' a file, move it to a temp file
|
141 |
+
self.path = path
|
142 |
+
self.tmp_path = Path(mkdtemp(prefix="skops-")) / str(uuid4())
|
143 |
+
|
144 |
+
def do(self) -> None:
|
145 |
+
self.model_card.select(self.key).visible = False
|
146 |
+
if self.path:
|
147 |
+
shutil.move(self.path, self.tmp_path)
|
148 |
+
|
149 |
+
def undo(self) -> None:
|
150 |
+
self.model_card.select(self.key).visible = True
|
151 |
+
if self.path:
|
152 |
+
shutil.move(self.tmp_path, self.path)
|
153 |
+
|
154 |
+
|
155 |
+
class UpdateSectionTask(Task):
|
156 |
+
"""Change the title or content of a text section"""
|
157 |
+
|
158 |
+
def __init__(
|
159 |
+
self,
|
160 |
+
model_card: card.Card,
|
161 |
+
key: str,
|
162 |
+
old_name: str,
|
163 |
+
new_name: str,
|
164 |
+
old_content: str,
|
165 |
+
new_content: str,
|
166 |
+
) -> None:
|
167 |
+
self.model_card = model_card
|
168 |
+
self.key = key
|
169 |
+
self.old_name = old_name
|
170 |
+
self.new_name = new_name
|
171 |
+
self.old_content = old_content
|
172 |
+
self.new_content = new_content
|
173 |
+
|
174 |
+
def do(self) -> None:
|
175 |
+
section = self.model_card.select(self.key)
|
176 |
+
new_title = split_subsection_names(self.new_name)[-1]
|
177 |
+
section.title = new_title
|
178 |
+
section.content = self.new_content
|
179 |
+
|
180 |
+
def undo(self) -> None:
|
181 |
+
section = self.model_card.select(self.key)
|
182 |
+
old_title = split_subsection_names(self.old_name)[-1]
|
183 |
+
section.title = old_title
|
184 |
+
section.content = self.old_content
|
185 |
+
|
186 |
+
|
187 |
+
class UpdateFigureTitleTask(Task):
|
188 |
+
"""Change the title a plot section
|
189 |
+
|
190 |
+
Changing the title is easy, just replace it and be done.
|
191 |
+
|
192 |
+
"""
|
193 |
+
|
194 |
+
def __init__(
|
195 |
+
self,
|
196 |
+
model_card: card.Card,
|
197 |
+
key: str,
|
198 |
+
old_name: str,
|
199 |
+
new_name: str,
|
200 |
+
) -> None:
|
201 |
+
self.model_card = model_card
|
202 |
+
self.key = key
|
203 |
+
self.old_name = old_name
|
204 |
+
self.new_name = new_name
|
205 |
+
|
206 |
+
def do(self) -> None:
|
207 |
+
section = self.model_card.select(self.key)
|
208 |
+
new_title = split_subsection_names(self.new_name)[-1]
|
209 |
+
section.title = self.title = new_title
|
210 |
+
|
211 |
+
def undo(self) -> None:
|
212 |
+
section = self.model_card.select(self.key)
|
213 |
+
old_title = split_subsection_names(self.old_name)[-1]
|
214 |
+
section.title = old_title
|
215 |
+
|
216 |
+
|
217 |
+
class UpdateFigureTask(Task):
|
218 |
+
"""Change the title or image of a figure section
|
219 |
+
|
220 |
+
Changing the title is easy, just replace it and be done.
|
221 |
+
|
222 |
+
Changing the figure is a bit more tricky. The old figure is in the hf_path
|
223 |
+
under its old name. The new figure is an UploadFile object. For the DO
|
224 |
+
operation, move the old figure to a temporary file and store the UploadFile
|
225 |
+
content to a new file (which may have a different name).
|
226 |
+
|
227 |
+
For the UNDO operation, delete the new figure (its content is still stored
|
228 |
+
in the UploadFile) and move back the old figure from its temporary file to
|
229 |
+
the original location (with its original name).
|
230 |
+
|
231 |
+
"""
|
232 |
+
|
233 |
+
def __init__(
|
234 |
+
self,
|
235 |
+
model_card: card.Card,
|
236 |
+
key: str,
|
237 |
+
old_name: str,
|
238 |
+
new_name: str,
|
239 |
+
data: UploadedFile,
|
240 |
+
new_path: Path,
|
241 |
+
old_path: Path,
|
242 |
+
) -> None:
|
243 |
+
self.model_card = model_card
|
244 |
+
self.key = key
|
245 |
+
self.old_name = old_name
|
246 |
+
self.new_name = new_name
|
247 |
+
self.new_path = new_path
|
248 |
+
self.old_path = old_path
|
249 |
+
self.new_data = data
|
250 |
+
# when 'deleting' the old image, move to temp path
|
251 |
+
self.tmp_path = Path(mkdtemp(prefix="skops-")) / str(uuid4())
|
252 |
+
|
253 |
+
def do(self) -> None:
|
254 |
+
section = self.model_card.select(self.key)
|
255 |
+
assert isinstance(section, PlotSection), "has to be a PlotSection"
|
256 |
+
new_title = split_subsection_names(self.new_name)[-1]
|
257 |
+
section.title = self.title = new_title
|
258 |
+
|
259 |
+
# write figure
|
260 |
+
# note: this can still be the same image if the image is a file, there
|
261 |
+
# is no test to check, e.g., the hash of the image
|
262 |
+
shutil.move(self.old_path, self.tmp_path)
|
263 |
+
|
264 |
+
with open(self.new_path, "wb") as f:
|
265 |
+
f.write(self.new_data.getvalue())
|
266 |
+
|
267 |
+
section.path = self.new_path
|
268 |
+
|
269 |
+
def undo(self) -> None:
|
270 |
+
section = self.model_card.select(self.key)
|
271 |
+
assert isinstance(section, PlotSection), "has to be a PlotSection"
|
272 |
+
old_title = split_subsection_names(self.old_name)[-1]
|
273 |
+
section.title = old_title
|
274 |
+
|
275 |
+
self.new_path.unlink(missing_ok=True)
|
276 |
+
shutil.move(self.tmp_path, self.old_path)
|
277 |
+
section.path = self.old_path
|
278 |
+
|
279 |
+
|
280 |
+
class AddMetricsTask(Task):
|
281 |
+
"""Add new metrics"""
|
282 |
+
|
283 |
+
def __init__(
|
284 |
+
self,
|
285 |
+
model_card: card.Card,
|
286 |
+
metrics: dict[str, str | int | float],
|
287 |
+
) -> None:
|
288 |
+
self.model_card = model_card
|
289 |
+
self.old_metrics = model_card._metrics.copy()
|
290 |
+
self.new_metrics = metrics
|
291 |
+
|
292 |
+
def do(self) -> None:
|
293 |
+
self.model_card._metrics.clear()
|
294 |
+
self.model_card.add_metrics(**self.new_metrics)
|
295 |
+
|
296 |
+
def undo(self) -> None:
|
297 |
+
self.model_card._metrics.clear()
|
298 |
+
self.model_card.add_metrics(**self.old_metrics)
|
utils.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Utility functions for the app"""
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
+
import base64
|
6 |
+
import os
|
7 |
+
import re
|
8 |
+
from pathlib import Path
|
9 |
+
from typing import Iterator
|
10 |
+
|
11 |
+
from skops import card
|
12 |
+
from skops.card._model_card import Section
|
13 |
+
|
14 |
+
PAT_MD_IMG = re.compile(
|
15 |
+
r'(!\[(?P<image_title>[^\]]+)\]\((?P<image_path>[^\)"\s]+)\s*([^\)]*)\))'
|
16 |
+
)
|
17 |
+
|
18 |
+
|
19 |
+
def get_rendered_model_card(model_card: card.Card, hf_path: str) -> str:
|
20 |
+
# This is a bit hacky:
|
21 |
+
# As a space, the model card is created in a temporary hf_path directory,
|
22 |
+
# which is where all the files are put. So e.g. if a figure is added, it is
|
23 |
+
# found at /tmp/skops-jtyqdgk3/fig.png. However, when the model card is is
|
24 |
+
# actually used, we don't want that, since there, the files will be in the
|
25 |
+
# cwd. Therefore, we remove the tmp directory everywhere we find it in the
|
26 |
+
# file.
|
27 |
+
if not hf_path.endswith(os.path.sep):
|
28 |
+
hf_path += os.path.sep
|
29 |
+
|
30 |
+
rendered = model_card.render()
|
31 |
+
rendered = rendered.replace(hf_path, "")
|
32 |
+
return rendered
|
33 |
+
|
34 |
+
|
35 |
+
def process_card_for_rendering(rendered: str) -> tuple[str, str]:
|
36 |
+
idx = rendered[1:].index("\n---") + 1
|
37 |
+
metadata = rendered[3:idx]
|
38 |
+
rendered = rendered[idx + 4 :] # noqa: E203
|
39 |
+
|
40 |
+
# below is a hack to display the images in streamlit
|
41 |
+
# https://discuss.streamlit.io/t/image-in-markdown/13274/10 The problem is
|
42 |
+
|
43 |
+
# that streamlit does not display images in markdown, so we need to replace
|
44 |
+
# them with html. However, we only want that in the rendered markdown, not
|
45 |
+
# in the card that is produced for the hub
|
46 |
+
def markdown_images(markdown):
|
47 |
+
# example image markdown:
|
48 |
+
# 
|
49 |
+
images = PAT_MD_IMG.findall(markdown)
|
50 |
+
return images
|
51 |
+
|
52 |
+
def img_to_bytes(img_path):
|
53 |
+
img_bytes = Path(img_path).read_bytes()
|
54 |
+
encoded = base64.b64encode(img_bytes).decode()
|
55 |
+
return encoded
|
56 |
+
|
57 |
+
def img_to_html(img_path, img_alt):
|
58 |
+
img_format = img_path.split(".")[-1]
|
59 |
+
img_html = (
|
60 |
+
f'<img src="data:image/{img_format.lower()};'
|
61 |
+
f'base64,{img_to_bytes(img_path)}" '
|
62 |
+
f'alt="{img_alt}" '
|
63 |
+
'style="max-width: 100%;">'
|
64 |
+
)
|
65 |
+
return img_html
|
66 |
+
|
67 |
+
def markdown_insert_images(markdown):
|
68 |
+
images = markdown_images(markdown)
|
69 |
+
|
70 |
+
for image in images:
|
71 |
+
image_markdown = image[0]
|
72 |
+
image_alt = image[1]
|
73 |
+
image_path = image[2]
|
74 |
+
markdown = markdown.replace(
|
75 |
+
image_markdown, img_to_html(image_path, image_alt)
|
76 |
+
)
|
77 |
+
return markdown
|
78 |
+
|
79 |
+
rendered_with_img = markdown_insert_images(rendered)
|
80 |
+
return metadata, rendered_with_img
|
81 |
+
|
82 |
+
|
83 |
+
def iterate_key_section_content(
|
84 |
+
data: dict[str, Section],
|
85 |
+
parent_section: str = "",
|
86 |
+
parent_keys: list[str] | None = None,
|
87 |
+
) -> Iterator[tuple[str, str]]:
|
88 |
+
parent_keys = parent_keys or []
|
89 |
+
|
90 |
+
for key, val in data.items():
|
91 |
+
if not val.visible:
|
92 |
+
continue
|
93 |
+
|
94 |
+
if parent_section:
|
95 |
+
title = "/".join((parent_section, val.title))
|
96 |
+
else:
|
97 |
+
title = val.title
|
98 |
+
|
99 |
+
return_key = key if not parent_keys else "/".join(parent_keys + [key])
|
100 |
+
yield return_key, title
|
101 |
+
|
102 |
+
if val.subsections:
|
103 |
+
yield from iterate_key_section_content(
|
104 |
+
val.subsections,
|
105 |
+
parent_section=title,
|
106 |
+
parent_keys=parent_keys + [key],
|
107 |
+
)
|