Skip to main content

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 Colab Example

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

  • 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 access token

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=topic_modeling_keywords, keywords_examples=keywords_examples, labels_examples=labels_examples, model_id=model_id, access_token=hf_access_token))

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

Documentation

ConTextMining.get_topic_labels(*, topic_modeling_keywords, labels_examples,keywords_examples, model_id, access_token)
  • topic_modeling_keywords (str, required): keywords stemming from the outputs of topic models (keywords representing each cluster) for ConTextMining to label.

  • keywords_examples (list, required): list-of-string(s) containing topic modeling keywords which serves as training examples for few-shot learning.

  • labels_examples (list, required): list-of-string(s) containing the labels CORRESPONDING TO THE INDEX of the keywords of keyword_examples above.

  • model_id (str, required): huggingface model_id of choice. For now, its a choice between "microsoft/Phi-3-mini-4k-instruct", "meta-llama/Meta-Llama-3.1-8B-Instruct", or "google/gemma-2-2b-it". Defaults to "google/gemma-2-2b-it".

  • access_token (str, required): Huggingface access token. To learn how to obtain one, refer to huggingface.co/docs/hub/en/security-tokens. Defaults to None

Citation

C Alba "ConText Mining: Complementing topic models with few-shot in-context learning to generate interpretable topics" Forthcoming at IEEE Symposium Series on Computational Intelligence.

Questions?

Contact me at alba@wustl.edu

Metadata

Release files for ConTextMining 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ConTextMining 0.0.6
File Size Uploaded Provenance
contextmining-0.0.6.tar.gz 6.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ConTextMining 0.0.6
File Interpreter ABI Platform
ConTextMining-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 12.1 kB

Release files / contextmining-0.0.6.tar.gz

Download URL contextmining-0.0.6.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e7551e8c372779428e1c80dd71aa129fcdbc51c86bac11d23e850e445567cc46
BLAKE2b-256 checksum
How to use checksums
a053b45f241377fb520f51fa3b96fb524efa7a708535703ee6436462a98cd32d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.5

Release files / ConTextMining-0.0.6-py3-none-any.whl

Download URL ConTextMining-0.0.6-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb66f82f4085d75ee4ef34e708b4c43c86128808fcbf392e41f0cf850305a4b9
BLAKE2b-256 checksum
How to use checksums
422437ff90658082c24c985818fb0b16865f295c891087c0a2bbb59d1d683ce8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.5

Security for ConTextMining 0.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page