Model Cards
Last updated
Last updated
The boincai_hub
library provides a Python interface to create, share, and update Model Cards. Visit for a deeper view of what Model Cards on the Hub are, and how they work under the hood.
To load an existing card from the Hub, you can use the function. Here, we’ll load the card from .
Copied
This card has some helpful attributes that you may want to access/leverage:
card.data
: Returns a instance with the model card’s metadata. Call .to_dict()
on this instance to get the representation as a dictionary.
card.text
: Returns the text of the card, excluding the metadata header.
card.content
: Returns the text content of the card, including the metadata header.
To initialize a Model Card from text, just pass the text content of the card to the ModelCard
on init.
Copied
Another way you might want to do this is with f-strings. In the following example, we:
Show how you might use a template variable via Python f-strings.
Copied
The above example would leave us with a card that looks like this:
Copied
If you have Jinja2
installed, you can create Model Cards from a jinja template file. Let’s see a basic example:
Copied
The resulting card’s markdown looks like this:
Copied
If you update any card.data, it’ll reflect in the card itself.
Copied
Now, as you can see, the metadata header has been updated:
Copied
Note that you will have to have Jinja2 installed to use from_template
. You can do so with pip install Jinja2
.
Copied
First, we’ll create a new repo called ‘hf-hub-modelcards-pr-test’ under the authenticated user’s namespace:
Copied
Then, we’ll create a card from the default template (same as the one defined in the section above):
Copied
Finally, we’ll push that up to the hub
Copied
If you instead wanted to push a card as a pull request, you can just say create_pr=True
when calling push_to_hub
:
Copied
In this section we will see what metadata are in repo cards and how to update them.
Let’s start with a first example:
Copied
With these two lines of code you will update the metadata to set a new pipeline_tag
.
By default, you cannot update a key that is already existing on the card. If you want to do so, you must pass overwrite=True
explicitly:
Copied
It often happen that you want to suggest some changes to a repository on which you don’t have write permission. You can do that by creating a PR on that repo which will allow the owners to review and merge your suggestions.
Copied
Copied
The resulting card.data
should look like this:
Copied
If you have more than one evaluation result you’d like to share, just pass a list of EvalResult
:
Copied
Which should leave you with the following card.data
:
Copied
Use to convert metadata we defined to YAML so we can use it to insert the YAML block in the model card.
As you update the card data, you can validate the card is still valid against the Hub by calling . This ensures that the card passes any validation rules set up on the BOINC AI Hub.
Instead of using your own template, you can also use the , which is a fully featured model card with tons of sections you may want to fill out. Under the hood, it uses to fill out a template file.
If you’re authenticated with the BOINC AI Hub (either by using boincai-cli login
or ), you can push cards to the Hub by simply calling . Let’s take a look at how to do that…
You can check out the resulting card .
A resulting PR created from this command can be seen .
metadata
refers to a hash map (or key value) context that provides some high-level information about a model, dataset or Space. That information can include details such as the model’s pipeline type
, model_id
or model_description
. For more detail you can take a look to these guides: , and . Now lets see some examples on how to update those metadata.
To include evaluation results in the metadata model-index
, you can pass an or a list of EvalResult
with your associated evaluation results. Under the hood it’ll create the model-index
when you call card.data.to_dict()
. For more information on how this works, you can check out .
Note that using this function requires you to include the model_name
attribute in .