To include a DatasetCard when pushing a dataset to the Hugging Face Hub using the datasets library, follow these steps:
1. Create or Modify the DatasetCard
A DatasetCard is a README.md file that provides metadata, descriptions, and other details about your dataset. You can create it manually or programmatically.
Option 1: Create a README.md Manually
- Create a
README.md file in your dataset directory with the required metadata in YAML format (at the top of the file) and additional descriptions.
Example README.md:
---
language: en
license: apache-2.0
task_categories:
- text-classification
pretty_name: My Dataset
---
# Dataset Description
This dataset contains...
## Usage
```python
from datasets import load_dataset
dataset = load_dataset("username/dataset_name")
#### **Option 2: Generate Programmatically**
You can use `DatasetCard` from `huggingface_hub` to generate it:
```python
from huggingface_hub import DatasetCard
data = {
"language": "en",
"license": "apache-2.0",
"task_categories": ["text-classification"],
"pretty_name": "My Dataset",
}
card = DatasetCard.from_template(
card_data=data,
template_path=None, # Use default template
dataset_name="my_dataset",
repo_url="https://huggingface.co/datasets/username/dataset_name",
)
# Save to README.md
with open("README.md", "w", encoding="utf-8") as f:
f.write(card.content)
2. Push Dataset with README.md
When pushing your dataset to the Hub, ensure the README.md is in the dataset directory.
Using push_to_hub
from datasets import Dataset
# Example dataset
dataset = Dataset.from_dict({"text": ["Hello", "World"], "label": [0, 1]})
# Push to Hub (README.md should be in the same directory)
dataset.push_to_hub("username/dataset_name")
Using upload_folder (Alternative)
If you have a local dataset directory with README.md:
from huggingface_hub import HfApi
api = HfApi()
api.upload_folder(
folder_path="path_to_dataset_dir",
repo_id="username/dataset_name",
repo_type="dataset",
)
3. Verify on the Hub
After pushing, check the dataset's README.md on the Hugging Face Hub (e.g., https://huggingface.co/datasets/username/dataset_name).
Key Notes
- The
README.md must be in the dataset directory before pushing.
- Use YAML front matter (between
---) for metadata.
- For more details, refer to the Dataset Card Guide.
This ensures your dataset has proper documentation when shared on the Hub! 🚀