# 在 Hugging Face 上使用 BERTopic

[BERTopic](https://github.com/MaartenGr/BERTopic) 是一个主题建模框架，它利用 🤗 transformers 和 c-TF-IDF 创建稠密聚类，从而生成易于解释的主题，同时在主题描述中保留重要词汇。

BERTopic 支持各种主题建模技术：

| [引导式](https://maartengr.github.io/BERTopic/getting_started/guided/guided.html) | [有监督](https://maartengr.github.io/BERTopic/getting_started/supervised/supervised.html) | [半监督](https://maartengr.github.io/BERTopic/getting_started/semisupervised/semisupervised.html) |
| --- | --- | --- |
| [手册](https://maartengr.github.io/BERTopic/getting_started/manual/manual.html) | [多主题分布](https://maartengr.github.io/BERTopic/getting_started/distribution/distribution.html) | [分层](https://maartengr.github.io/BERTopic/getting_started/hierarchicaltopics/hierarchicaltopics.html) |
| [基于类](https://maartengr.github.io/BERTopic/getting_started/topicsperclass/topicsperclass.html) | [动态](https://maartengr.github.io/BERTopic/getting_started/topicsovertime/topicsovertime.html) | [在线/增量](https://maartengr.github.io/BERTopic/getting_started/online/online.html) |
| [多模态](https://maartengr.github.io/BERTopic/getting_started/multimodal/multimodal.html) | [多方面](https://maartengr.github.io/BERTopic/getting_started/multiaspect/multiaspect.html) | [文本生成/LLM](https://maartengr.github.io/BERTopic/getting_started/representation/llm.html) |
| [零样本 **（新！）**](https://maartengr.github.io/BERTopic/getting_started/zeroshot/zeroshot.html) | [合并模型 **（新功能！）**](https://maartengr.github.io/BERTopic/getting_started/merge/merge.html) | [种子词 **（新功能！）**](https://maartengr.github.io/BERTopic/getting_started/seed_words/seed_words.html) |

## 在 Hub 上探索 BERTopic

您可以在[模型页面](https://huggingface.co/models?library=bertopic&sort=trending)左侧进行筛选，以查找 BERTopic 模型。

托管在 Hub 上的 BERTopic 模型配有模型卡片，其中包含有关这些模型的有用信息。得益于 BERTopic 与 Hugging Face Hub 的集成，您只需几行代码即可加载 BERTopic 模型。您还可以使用 [Inference Endpoints](https://huggingface.co/inference-endpoints) 部署这些模型。

## 安装

要开始使用，您可以遵循 [BERTopic 安装指南](https://github.com/MaartenGr/BERTopic#installation)。您也可以通过 pip 使用以下单行命令进行安装：

```bash
pip install bertopic
```

## 使用现有模型

所有 BERTopic 模型都可以轻松地从 Hub 加载：

```py
from bertopic import BERTopic
topic_model = BERTopic.load("MaartenGr/BERTopic_Wikipedia")
```

加载完成后，您可以使用 BERTopic 的功能来预测新实例的主题：

```py
topic, prob = topic_model.transform("This is an incredible movie!")
topic_model.topic_labels_[topic]
```

由此得出以下主题：

```text
64_rating_rated_cinematography_film
```

## 共享模型

创建 BERTopic 模型后，您可以通过 Hugging Face Hub 轻松与他人共享。为此，我们可以使用 `push_to_hf_hub` 函数，将模型直接推送到 Hugging Face Hub：

```python
from bertopic import BERTopic

# Train model
topic_model = BERTopic().fit(my_docs)

# Push to HuggingFace Hub
topic_model.push_to_hf_hub(
    repo_id="MaartenGr/BERTopic_ArXiv",
    save_ctfidf=True
)
```

请注意，保存的模型不包含降维和聚类算法。由于这些算法仅在训练模型和查找相关主题时是必需的，因此已被移除。推理过程通过主题嵌入和文档嵌入之间的简单余弦相似度来完成。这不仅加快了模型速度，还使我们能够拥有一个可以处理的小型 BERTopic 模型。

## 其他资源

* [BERTopic 仓库](https://github.com/MaartenGr/BERTopic)
* [BERTopic 文档](https://maartengr.github.io/BERTopic/)
* [Hub 中的 BERTopic 模型](https://huggingface.co/models?library=bertopic&sort=trending)
