Skip to main content

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)]

Metadata

Release files for unlimited-classifier 0.0.3

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

Source distribution (sdist)

Source distribution for unlimited-classifier 0.0.3
File Size Uploaded
unlimited_classifier-0.0.3.tar.gz 13.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unlimited-classifier 0.0.3
File Interpreter ABI Platform
unlimited_classifier-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 27.1 kB

Release files / unlimited_classifier-0.0.3.tar.gz

Download URL unlimited_classifier-0.0.3.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
861c412c192bfde4dd2f1eb5c1a6504bc43351d0329558aef81aca479b3ed0dc
BLAKE2b-256 checksum
How to use checksums
c57fc1e81ffdf290ed32f33eeec229bb32fd9878c749e70b445818e85d77250e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / unlimited_classifier-0.0.3-py3-none-any.whl

Download URL unlimited_classifier-0.0.3-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8910d00cdbc3c007bde9a3aad9a1ae796dd55a8392f58438732bbb2b328c668f
BLAKE2b-256 checksum
How to use checksums
05ecacee9bb15e6ac8868f5894584090ea99885af33265c5be48a59fc91d39af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

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