Skip to main content

Complementing topic models with few-shot in-context learning to generate interpretable topics

Project description

ConTextMining is a package generate interpretable topics labels from the keywords of topic models (e.g, LDA, BERTopic) through few-shot in-context learning.

pypi package GitHub Source Code

Requirements

Required packages

The following packages are required for ConTextMining.

  • torch (to learn how to install, please refer to pytorch.org/)

  • transformers

  • tokenizers

  • huggingface-hub

  • flash_attn

  • accelerate

To install these packages, you can do the following:

pip install torch transformers tokenizers huggingface-hub flash_attn accelerate

GPU requirements

You require at least one GPU to use ConTextMining.

VRAM requirements depend on factors like number of keywords or topics used to topic labels you wish to generate.

However, at least 8GB of VRAM is recommended

huggingface API key

You will need a huggingface access token. To obtain one:

  1. you'd first need to create a huggingface account if you do not have one.

  2. Create and store a new access token. To learn more, please refer to huggingface.co/docs/hub/en/security-tokens.

  3. Note: Some pre-trained large language models (LLMs) may require permissions. For more information, please refer to huggingface.co/docs/hub/en/models-gated.

Installation

To install in python, simply do the following:

pip install ConTextMining

Quick Start

Here we provide a quick example on how you can execute ConTextMining to conveniently generate interpretable topics labels from the keywords of topic models.

from ConTextMining import get_topic_labels



# specify your huggingface access token. To learn how to obtain one, refer to huggingface.co/docs/hub/en/security-tokens

hf_access_token="<your huggingface access token>" 



# specify the huggingface model id. Choose between "microsoft/Phi-3-mini-4k-instruct", "meta-llama/Meta-Llama-3.1-8B-Instruct" or "google/gemma-2-2b-it"

model_id="meta-llama/Meta-Llama-3.1-8B-Instruct"



# specify the keywords for the few-shot learning examples

keywords_examples = [

    "olympic, year, said, games, team",

    "mr, bush, president, white, house",

    "sadi, report, evidence, findings, defense",

    "french, union, germany, workers, paris",

    "japanese, year, tokyo, matsui, said"

]



# specify the labels CORRESPONDING TO THE INDEX of the keywords of 'keyword_examples' above. 

labels_examples = [

    "sports",

    "politics",

    "research",

    "france",

    "japan"

]



# specify your topic modeling keywords of wish to generate coherently topic labels. 

topic_modeling_keywords ='''Topic 1: [amazing, really, place, phenomenon, pleasant],

Topic 2: [loud, awful, sunday, like, slow],

Topic 3: [spinach, carrots, green, salad, dressing],

Topic 4: [mango, strawberry, vanilla, banana, peanut],

Topic 5: [fish, roll, salmon, fresh, good]'''





print(get_topic_labels(topic_modeling_keywords, keywords_examples, labels_examples, model_id, hf_access_token))

You will now get the interpretable topic model labels for all 5 topics!

Citation

C Alba "ConText Mining: Complementing topic models with few-shot in-context learning to generate interpretable topics" Working paper.

Questions?

Contact me at alba@wustl.edu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contextmining-0.0.1.tar.gz (5.7 kB view details)

Uploaded Source

File details

Details for the file contextmining-0.0.1.tar.gz.

File metadata

  • Download URL: contextmining-0.0.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for contextmining-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a1812e8584366af2d76cf8b08c174cf5ad396af8e0a7e6b19c62500532d2c012
MD5 195ddcdb0896a1d100b31388ed021112
BLAKE2b-256 bfd4fbbf3ee3eb4ad0a7d43a66dc47624de6679053f47ad62df0c942f45373a9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page