Skip to main content

Universal text classifier for generative models

Project description

Ultimited classifier

Tree

This is a universal text classifier that uses text generation with constraints for large-scale zero-shot text categorization. We represent labels as a tree of tokens and limit the possible variants of generation to provided labels. Such approach allows to make classification of text having millions of labels.

Install

pip install -U unlimited-classifier

How to use

Initialize TextClassifier with labels for classification and transformers model and tokenizer (names or actual model/tokenizer).

You can choose any appropriate model on HuggingFace 🤗 including encoder-decoders and just decoders ones.

from unlimited_classifier import TextClassifier

classifier = TextClassifier(
    labels=[
        '''label for classification 1''',
        '''label for classification 2'''    
    ],
    model='''model name''',
    tokenizer='''tokenizer name''',
)

To update labels trie run the following method:

classifier.initialize_labels_trie(labels)

Call method that suits you best:

  • invoke: for single text;
  • ainvoke: asynchronous for single text;
  • invoke_batch: for multiple texts;
  • ainvoke_batch: asynchronous for multiple texts;
text = '''text for classification'''
texts = [
    '''text for classification 1''',
    '''text for classification 2'''
]

output = classifier.invoke(text)

OR

output = classifier.ainvoke(text)

OR

output = classifier.invoke_batch(texts)

OR

output = classifier.ainvoke_batch(texts)

Scorer:

Without a scorer, the results yield beam scores that are difficult to interpret.

You can utilize the Scorer class to interpret scores within the range of 0 to 1.

Initialization with scorer:

classifier = TextClassifier(
    labels=[
        '''label for classification 1''',
        '''label for classification 2'''    
    ],
    model='''model name''',
    tokenizer='''tokenizer name''',
    scorer=Scorer()
)

Scorere initialized with "knowledgator/comprehend_it-base" model.

Examples:

classifier = TextClassifier(
    labels=["positive", "negative", "neutral"],
    model="t5-base",
    tokenizer="t5-base",
)

text = "Characterize movie review: I rented I AM CURIOUS-YELLOW from my video store because of all the controversy that surrounded it when it was first released in 1967. I also heard that at first it was seized by U.S. customs if it ever tried to enter this country, therefore being a fan of films considered controversial I really had to see this for myself.<br /><br />The plot is centered around a young Swedish drama student named Lena who wants to learn everything she can about life. In particular she wants to focus her attentions to making some sort of documentary on what the average Swede thought about certain political issues such as the Vietnam War and race issues in the United States. In between asking politicians and ordinary denizens of Stockholm about their opinions on politics, she has sex with her drama teacher, classmates, and married men. What kills me about I AM CURIOUS-YELLOW is that 40 years ago, this was considered pornographic. Really, the sex and nudity scenes are few and far between, even then it\"s not shot like some cheaply made porno. While my countrymen mind find it shocking, in reality sex and nudity are a major staple in Swedish cinema. Even Ingmar Bergman, arguably their answer to good old boy John Ford, had sex scenes in his films. I do commend the filmmakers for the fact that any sex shown in the film is shown for artistic purposes rather than just to shock people and make money to be shown in pornographic theaters in America. I AM CURIOUS-YELLOW is a good film for anyone wanting to study the meat and potatoes (no pun intended) of Swedish cinema. But really, this film doesn\"t have much of a plot."

output = classifier.invoke(text)
# [('negative', 0.0007813576376065612), ('positive', 0.0006674602045677602), ('neutral', 0.00023184997553471476)]

With scorer:

from unlimited_classifier.scorer import Scorer

classifier = TextClassifier(
    labels=["positive", "negative", "neutral"],
    model="t5-base",
    tokenizer="t5-base",
    scorer=Scorer()
)

output = classifier.invoke(text)
# [('positive', 0.9700802564620972), ('negative', 0.9582770466804504), ('neutral', 0.6801289319992065)]

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

unlimited_classifier-0.0.3.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

unlimited_classifier-0.0.3-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file unlimited_classifier-0.0.3.tar.gz.

File metadata

  • Download URL: unlimited_classifier-0.0.3.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for unlimited_classifier-0.0.3.tar.gz
Algorithm Hash digest
SHA256 861c412c192bfde4dd2f1eb5c1a6504bc43351d0329558aef81aca479b3ed0dc
MD5 02891c626eda5b63eecdde7004b3ce7c
BLAKE2b-256 c57fc1e81ffdf290ed32f33eeec229bb32fd9878c749e70b445818e85d77250e

See more details on using hashes here.

File details

Details for the file unlimited_classifier-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for unlimited_classifier-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8910d00cdbc3c007bde9a3aad9a1ae796dd55a8392f58438732bbb2b328c668f
MD5 bec2cf10e405d3dd993abdb352d85ef7
BLAKE2b-256 05ecacee9bb15e6ac8868f5894584090ea99885af33265c5be48a59fc91d39af

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