Skip to main content

python PyPI - Version Code style: black Ruff security: bandit Downloads

Text Emotion Classifier

Emotion Classifier Logo

A flexible emotion classifier package with support for multiple models, customizable preprocessing, visualization tools, fine-tuning capabilities, and more.

Overview

emotionclassifier is a Python package designed to classify emotions in text using various pre-trained models from Hugging Face's Transformers library. This package provides a user-friendly interface for emotion classification, along with tools for data preprocessing, visualization, fine-tuning, and integration with popular data platforms.

Features

  • Multiple Model Support: Easily switch between different pre-trained models.
  • Customizable Preprocessing: Clean and preprocess text data with customizable functions.
  • Visualization Tools: Visualize emotion distributions and trends over time.
  • Fine-tuning Capability: Fine-tune models on your own datasets.
  • User-friendly CLI: Command-line interface for quick emotion classification.
  • Integration with Data Platforms: Seamless integration with pandas DataFrames.
  • Extended Post-processing: Additional utilities for detailed emotion analysis.

Emotion Labels

  • 😠 Anger
  • 🤢 Disgust
  • 😨 Fear
  • 😊 Joy
  • 😢 Sadness
  • 😲 Surprise

Installation

You can install the package using pip:

pip install emotionclassifier

Usage

Basic Usage

Here's an example of how to use the EmotionClassifier to classify a single text:

from emotionclassifier import EmotionClassifier

# Initialize the classifier with the default model
classifier = EmotionClassifier()

# Classify a single text
text = "I am very happy today!"
result = classifier.predict(text)
print("Emotion:", result['label'])
print("Confidence:", result['confidence'])

Batch Processing

You can classify multiple texts at once using the predict_batch method:

texts = ["I am very happy today!", "I am so sad."]
results = classifier.predict_batch(texts)
print("Batch processing results:", results)

Visualization

To visualize the emotion distribution of a text:

from emotionclassifier import plot_emotion_distribution

result = classifier.predict("I am very happy today!")
plot_emotion_distribution(result['probabilities'], classifier.labels.values())

CLI Usage

You can also use the package from the command line:

emotionclassifier --model deberta-v3-small --text "I am very happy today!"

DataFrame Integration

Integrate with pandas DataFrames to classify text columns:

import pandas as pd
from emotionclassifier import DataFrameEmotionClassifier

df = pd.DataFrame({
    'text': ["I am very happy today!", "I am so sad."]
})

classifier = DataFrameEmotionClassifier()
df = classifier.classify_dataframe(df, 'text')
print(df)

Emotion Trends Over Time

Analyze and plot emotion trends over time:

from emotionclassifier import EmotionTrends

texts = ["I am very happy today!", "I am feeling okay.", "I am very sad."]
trends = EmotionTrends()
emotions = trends.analyze_trends(texts)
trends.plot_trends(emotions)

Fine-tuning

Fine-tune a pre-trained model on your own dataset:

from emotionclassifier.fine_tune import fine_tune_model

# Define your train and validation datasets
train_dataset = ...
val_dataset = ...

# Fine-tune the model
fine_tune_model(classifier.model, classifier.tokenizer, train_dataset, val_dataset, output_dir='fine_tuned_model')

Logging Configuration

By default, the sentimentpredictor package logs messages at the WARNING level and above. If you need more detailed logging (e.g., for debugging), you can set the logging level to INFO or DEBUG:

from sentimentpredictor.logger import set_logging_level

# Set logging level to INFO
set_logging_level('INFO')

# Set logging level to DEBUG
set_logging_level('DEBUG')

You can set the logging level to one of the following: DEBUG, INFO, WARNING, ERROR, CRITICAL.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

This package uses pre-trained models from the Hugging Face Transformers library.

Contributing

Contributions are welcome! Please see the CONTRIBUTING file for guidelines on how to contribute to this project.

Links

Metadata

Release files for emotionclassifier 0.1.4

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

Source distribution (sdist)

Source distribution for emotionclassifier 0.1.4
File Size Uploaded
emotionclassifier-0.1.4.tar.gz 9.8 kB Details

Built distribution (wheel)

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

Total release size: 22.0 kB

Release files / emotionclassifier-0.1.4.tar.gz

Download URL emotionclassifier-0.1.4.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6cae356781ac08ce91a12d5405f835714a3cb0942f667e7fbb107392f57769e8
BLAKE2b-256 checksum
How to use checksums
0fac87847f2635fae2372894b35ffce013e0100a6fe6c2464b5442cdad67db12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-41-generic

Release files / emotionclassifier-0.1.4-py3-none-any.whl

Download URL emotionclassifier-0.1.4-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c9a9337b33464513d9d2221e5f1aefc35264b85ee787f800cab709e8fa7b42a9
BLAKE2b-256 checksum
How to use checksums
8060e2e01ba6c92c16491aa05e35f6e821fe2b2c689fbae0d97d2618f48e438b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-41-generic

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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