Intermediate

Model Sharing

The Hugging Face Hub is the central platform for sharing models, datasets, and applications. This lesson covers pushing models to the Hub, writing proper model cards, creating Gradio demos, and deploying to Spaces. Includes practice questions at the end.

Pushing Models to the Hub

# Pushing models to the Hugging Face Hub
from transformers import AutoModelForSequenceClassification, AutoTokenizer

# Method 1: Using push_to_hub() (recommended)
model = AutoModelForSequenceClassification.from_pretrained("./my-model")
tokenizer = AutoTokenizer.from_pretrained("./my-model")

# Push model and tokenizer
model.push_to_hub("my-username/my-sentiment-model")
tokenizer.push_to_hub("my-username/my-sentiment-model")

# Method 2: Using Trainer with push_to_hub=True
from transformers import TrainingArguments, Trainer

training_args = TrainingArguments(
    output_dir="./results",
    push_to_hub=True,
    hub_model_id="my-username/my-sentiment-model",
    hub_strategy="end",  # Push at end of training ("every_save", "checkpoint")
)

trainer = Trainer(model=model, args=training_args, ...)
trainer.train()
trainer.push_to_hub()  # Push final model

# Method 3: Using huggingface_hub library
from huggingface_hub import HfApi, login

# Login first (required for pushing)
login(token="hf_your_token_here")
# Or use: huggingface-cli login (from terminal)

api = HfApi()

# Create a new repository
api.create_repo(repo_id="my-username/my-model", repo_type="model")

# Upload files
api.upload_file(
    path_or_fileobj="./model.safetensors",
    path_in_repo="model.safetensors",
    repo_id="my-username/my-model"
)

# Upload entire folder
api.upload_folder(
    folder_path="./my-model",
    repo_id="my-username/my-model"
)

Model Cards

Model cards are documentation files (README.md) that describe your model. They are essential for responsible AI and required for models shared on the Hub.

# Model Card Template (README.md in your model repo)
model_card_template = """
---
language: en
license: mit
tags:
  - text-classification
  - sentiment-analysis
  - transformers
datasets:
  - imdb
metrics:
  - accuracy
  - f1
model-index:
  - name: my-sentiment-model
    results:
      - task:
          type: text-classification
          name: Sentiment Analysis
        dataset:
          name: IMDb
          type: imdb
        metrics:
          - name: Accuracy
            type: accuracy
            value: 0.93
          - name: F1
            type: f1
            value: 0.93
---

# My Sentiment Model

## Model Description
Fine-tuned DistilBERT for binary sentiment classification on the IMDb dataset.

## Intended Use
Classify English text as positive or negative sentiment.

## Training Data
IMDb movie review dataset (25K train, 25K test).

## Training Procedure
- Base model: distilbert-base-uncased
- Epochs: 3
- Learning rate: 2e-5
- Batch size: 16

## Evaluation Results
| Metric   | Value |
|----------|-------|
| Accuracy | 0.93  |
| F1       | 0.93  |

## Limitations
- English only
- Trained on movie reviews, may not generalize to other domains
- May exhibit biases present in the training data

## How to Use
```python
from transformers import pipeline
classifier = pipeline("text-classification", model="my-username/my-sentiment-model")
result = classifier("I loved this movie!")
```
"""

# Key model card sections for the exam:
required_sections = [
    "Model Description",      # What the model does
    "Intended Use",            # What it should be used for
    "Training Data",           # What data it was trained on
    "Training Procedure",      # How it was trained
    "Evaluation Results",      # Performance metrics
    "Limitations and Biases",  # Known issues
    "How to Use"               # Code example
]

Gradio Demos

# Building Gradio demos for your models
import gradio as gr
from transformers import pipeline

# Simple Interface
classifier = pipeline("text-classification",
                      model="distilbert-base-uncased-finetuned-sst-2-english")

def predict(text):
    result = classifier(text)[0]
    return {result["label"]: result["score"]}

demo = gr.Interface(
    fn=predict,
    inputs=gr.Textbox(label="Enter text", placeholder="Type something..."),
    outputs=gr.Label(label="Sentiment"),
    title="Sentiment Analysis",
    description="Classify text as positive or negative."
)
demo.launch()

# Advanced: Gradio Blocks for custom layouts
with gr.Blocks() as demo:
    gr.Markdown("# NLP Demo")

    with gr.Tab("Sentiment"):
        text_input = gr.Textbox(label="Text")
        sentiment_output = gr.Label(label="Result")
        btn = gr.Button("Analyze")
        btn.click(fn=predict, inputs=text_input, outputs=sentiment_output)

    with gr.Tab("NER"):
        ner_input = gr.Textbox(label="Text")
        ner_output = gr.HighlightedText(label="Entities")
        ner_btn = gr.Button("Extract Entities")
        ner_btn.click(fn=extract_entities, inputs=ner_input, outputs=ner_output)

demo.launch()

Hugging Face Spaces

# Deploying to Hugging Face Spaces
from huggingface_hub import HfApi

api = HfApi()

# Create a Space
api.create_repo(
    repo_id="my-username/my-demo",
    repo_type="space",
    space_sdk="gradio"    # Options: "gradio", "streamlit", "docker"
)

# Upload your app.py and requirements.txt
api.upload_file(
    path_or_fileobj="app.py",
    path_in_repo="app.py",
    repo_id="my-username/my-demo",
    repo_type="space"
)

api.upload_file(
    path_or_fileobj="requirements.txt",
    path_in_repo="requirements.txt",
    repo_id="my-username/my-demo",
    repo_type="space"
)

# Your Space will auto-build and deploy at:
# https://huggingface.co/spaces/my-username/my-demo

# Space configuration (in README.md YAML header):
space_config = """
---
title: My NLP Demo
emoji: 🚀
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.0.0
app_file: app.py
pinned: false
---
"""

# Space SDK options:
space_sdks = {
    "gradio": "Best for ML demos with inputs/outputs",
    "streamlit": "Best for data dashboards and exploration",
    "docker": "Full control, any framework",
    "static": "Simple HTML/CSS/JS pages"
}

Practice Questions

💡
Test your knowledge of model sharing:
Q1: What is the simplest way to push a fine-tuned model to the Hub?

Answer: Call model.push_to_hub("my-username/model-name") and tokenizer.push_to_hub("my-username/model-name"). You must be authenticated first with huggingface_hub.login() or by running huggingface-cli login in the terminal. Alternatively, set push_to_hub=True in TrainingArguments to push automatically after training.

Q2: What are the required sections of a model card?

Answer: A complete model card should include: (1) Model Description, (2) Intended Use, (3) Training Data, (4) Training Procedure, (5) Evaluation Results with metrics, (6) Limitations and Biases, and (7) How to Use with a code example. The YAML metadata header should specify language, license, tags, datasets, and metrics.

Q3: What is the difference between gr.Interface and gr.Blocks?

Answer: gr.Interface is a high-level API that creates a simple input-output demo with minimal code. gr.Blocks is a low-level API that gives full control over layout, multiple tabs, chained components, and complex interactions. Use Interface for quick demos and Blocks for production applications.

Q4: What SDK options are available for Hugging Face Spaces?

Answer: Four SDK options: (1) Gradio for ML demos with inputs/outputs, (2) Streamlit for data dashboards, (3) Docker for full control with any framework, and (4) Static for simple HTML/CSS/JS pages. Gradio is the most common choice for model demos.

Q5: Why are model cards important for responsible AI?

Answer: Model cards provide transparency about a model's capabilities, limitations, and potential biases. They help users understand: what data the model was trained on, what it should (and should not) be used for, known failure cases, and ethical considerations. Without model cards, users may misuse models or be unaware of biases, leading to harm.

Key Takeaways

💡
  • Use push_to_hub() or Trainer with push_to_hub=True to share models
  • Model cards are essential — include description, intended use, training details, metrics, and limitations
  • Gradio is the standard tool for building model demos — know both Interface and Blocks APIs
  • Spaces deploy Gradio/Streamlit/Docker apps for free on Hugging Face infrastructure
  • Always authenticate with huggingface-cli login before pushing models or creating Spaces