Skip to main content

Slimmer version of BERTopic for transforming new data with an existing, trained model.

Project description

Lightopic

This package addresses the specific use case of deploying a BERTopic model that you've trained, and now want to use for transforming new data, e.g. via an API.

This came up for me because I wanted to deploy such a model API but wanted to make the deployment smaller and faster. The BERTopic package is broad, which brings with it a load of dependencies (e.g. torch, a bunch of cuda libraries). So I wrote this as a way to do the transform step only, with a virtual environment that's about 95% smaller than one with the actual BERTopic package.

The main prerequisite is that you need to have trained a BERTopic model separately and have serialised it in a way that's compatible with lightopic. The lightopic package also offers you a way to do that: guidance on how is below. From that point you can instantiate a Lightopic object and use its transform method on new data.

Training and serialising your LightBERTopic model

This is a necessary step: you can't instantiate a Lightopic object without first having trained and serialised your model. To make this part easier the LightBERTopic class is available: this is a child class of bertopic.BERTopic, only with a method added to save_lightopic.

from lightopic.lightbertopic import LightBERTopic
docs = fetch_20newsgroups(subset='all',  remove=('headers', 'footers', 'quotes'))['data']

topic_model = LightBERTopic()
topics, probs = topic_model.fit_transform(docs)
topic_model.save_lightopic("model_directory")

NB. for this to work you must have bertopic installed, which you can do with pip install lightopic[bertopic].

NOTE: this package is still under development, so this required format may (and probably will) change!

Using a Lightopic model

Now the serialised model is ready to use.

from lightopic import Lightopic
topic_model = Lightopic()
topic_model.load("model_directory")
topic_model.transform(embeddings)

This transform step does not rely on BERTopic at all, so it can use the smaller installation you get from pip install lightopic.

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

lightopic-0.0.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

lightopic-0.0.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file lightopic-0.0.2.tar.gz.

File metadata

  • Download URL: lightopic-0.0.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for lightopic-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7db698a0f0147c485d837122271407bcca7c339ad641064ee14f45bc1562e3df
MD5 ea101787fed4e9b4399afefde448b244
BLAKE2b-256 e8995530b4e4a01f164a0782bba801c479e069a3f5e136ee0f010530d6c3d391

See more details on using hashes here.

Provenance

The following attestation bundles were made for lightopic-0.0.2.tar.gz:

Publisher: publish-to-pypi.yml on hamedbh/lightopic

Attestations:

File details

Details for the file lightopic-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: lightopic-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for lightopic-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3a7e96108d27c90fe7aedc5dbd825b4a511d2cd79df9b299a8c8c069ea5decb7
MD5 defe4a2f387278a70142f6c264bab91e
BLAKE2b-256 02dabe9fe41666a9a24e9b81868d51903b644ecdac63bcd456ddbd87829da290

See more details on using hashes here.

Provenance

The following attestation bundles were made for lightopic-0.0.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on hamedbh/lightopic

Attestations:

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