Hugging Face에서 BERTopic 사용하기

Hugging Face에서 BERTopic 사용하기

BERTopic은 🤗 transformers와 c-TF-IDF를 활용해 밀집 클러스터를 만들어, 토픽 설명에서 중요한 단어를 유지하면서도 쉽게 해석 가능한 토픽을 제공하는 토픽 모델링 프레임워크예요.

출처: 문서

본문

BERTopic은 모든 종류의 토픽 모델링 기법을 지원해요:

  • Guided, Supervised, Semi-supervised
  • Manual, Multi-topic distributions, Hierarchical
  • Class-based, Dynamic, Online/Incremental
  • Multimodal, Multi-aspect, Text Generation/LLM
  • Zero-shot (new!), Merge Models (new!), Seed Words (new!)

Hub에서 BERTopic 탐색하기

모델 페이지 왼쪽에서 필터링해 BERTopic 모델을 찾을 수 있어요.

Hub에 호스팅된 BERTopic 모델에는 모델에 대한 유용한 정보가 담긴 모델 카드가 있어요. BERTopic Hugging Face Hub 통합 덕분에 몇 줄의 코드로 BERTopic 모델을 불러올 수 있어요. Inference Endpoints로 이 모델들을 배포할 수도 있어요.

설치

BERTopic 설치 가이드를 따르거나 pip 한 줄로 설치할 수 있어요:

pip install bertopic

기존 모델 사용하기

모든 BERTopic 모델을 Hub에서 쉽게 불러올 수 있어요:

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

불러온 후 BERTopic의 기능으로 새 인스턴스의 토픽을 예측할 수 있어요:

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

다음 토픽을 주네요:

64_rating_rated_cinematography_film

모델 공유하기

BERTopic 모델을 만들었다면 Hugging Face Hub를 통해 쉽게 공유할 수 있어요. push_to_hf_hub 함수로 모델을 Hugging Face Hub에 직접 푸시할 수 있어요:

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 모델을 가질 수 있게 해줘요.

더 알아보기 (Learn more)