Skip to content

Media

Show each topic's media beside its name: its pictures, and a montage to play.

A topic's media is summarised once per modality when it is represented: its images tiled into a collage, the middle frame of each video tiled into a frame sheet, and the opening seconds of each clip joined into a montage. This lays those summaries out one row per topic, so a topic of photographs, recordings and clips is seen and heard at once.

Plotly has no mark for sound, so the page is plain HTML with every picture and montage embedded in it. Showing it needs IPython, which every notebook has.

Parameters:

Name Type Description Default
topic_model BERTopic

A fitted BERTopic instance.

required
topics list[int] | None

A selection of topics to show.

None
top_n_topics int | None

Only show the top n most frequent topics.

None
height int

The height of each picture on the page, in pixels.

200

Returns:

Type Description
HTML

An IPython.display.HTML, which a notebook shows inline. Its data is the page

HTML

itself, which any browser opens once it is written to a file.

Examples: To see the media of a model fitted on images and their captions:

from bertopic import BERTopic
from bertopic.representation import MultiModalRepresentation

topic_model = BERTopic(representation_model={"Media": MultiModalRepresentation()})
topic_model.fit(captions, images=images)
topic_model.visualize_media()

Or to save the ten largest topics as a page:

page = topic_model.visualize_media(top_n_topics=10)
with open("media.html", "w", encoding="utf-8") as file:
    file.write(page.data)
Source code in bertopic\plotting\_media.py
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def visualize_media(
    topic_model: BERTopic,
    topics: list[int] | None = None,
    top_n_topics: int | None = None,
    height: int = 200,
) -> HTML:
    """Show each topic's media beside its name: its pictures, and a montage to play.

    A topic's media is summarised once per modality when it is represented: its images
    tiled into a collage, the middle frame of each video tiled into a frame sheet, and the
    opening seconds of each clip joined into a montage. This lays those summaries out one
    row per topic, so a topic of photographs, recordings and clips is seen and heard at once.

    Plotly has no mark for sound, so the page is plain HTML with every picture and montage
    embedded in it. Showing it needs IPython, which every notebook has.

    Arguments:
        topic_model: A fitted BERTopic instance.
        topics: A selection of topics to show.
        top_n_topics: Only show the top n most frequent topics.
        height: The height of each picture on the page, in pixels.

    Returns:
        An `IPython.display.HTML`, which a notebook shows inline. Its `data` is the page
        itself, which any browser opens once it is written to a file.

    Examples:
    To see the media of a model fitted on images and their captions:

    ```python
    from bertopic import BERTopic
    from bertopic.representation import MultiModalRepresentation

    topic_model = BERTopic(representation_model={"Media": MultiModalRepresentation()})
    topic_model.fit(captions, images=images)
    topic_model.visualize_media()
    ```

    Or to save the ten largest topics as a page:

    ```python
    page = topic_model.visualize_media(top_n_topics=10)
    with open("media.html", "w", encoding="utf-8") as file:
        file.write(page.data)
    ```
    """
    rows = []
    for topic_id in select_topics(topic_model, topics, top_n_topics):
        topic = topic_model._topics[topic_id]
        summaries = topic.media.summaries if topic.media is not None else {}

        # One figure per summary, captioned with what it is, so a collage and a frame sheet are told apart
        figures = [
            f"<figure>{_embed(summaries[modality], height)}<figcaption>{name}</figcaption></figure>"
            for modality, name in SUMMARIES.items()
            if modality in summaries
        ]
        rows.append(
            f'<tr><td class="number">{topic.id}</td><td class="number">{topic.nr_documents}</td>'
            f"<td>{escape(topic.name)}</td><td>{''.join(figures)}</td></tr>"
        )

    # Imported here because IPython comes with every notebook, but BERTopic does not depend on it
    from IPython.display import HTML

    return HTML(PAGE.format(rows="\n".join(rows)))