An end-to-end Machine Learning and Streamlit application that classifies the underlying emotion of any input English text into one of six categories: Sadness (0), Joy (1), Love (2), Anger (3), Fear (4), and Surprise (5).
This project leverages a large dataset containing over 416,000 text samples to compare classical Machine Learning approaches (Logistic Regression, Linear Support Vector Machines, Random Forests) to achieve a considerably decent accuracy and a deployable solution.
The application is designed to run via a Streamlit dashboard.
You can view this on know-your-emotions.streamlit.app and test it. Thanks, Streamlit, for helping us deploy the app!
The model is trained on the popular Emotions Dataset by Nelgiriyewithana via Kaggle. It contains highly unbalanced categories:
- Joy (~141k samples)
- Sadness (~121k samples)
- Anger (~57k samples)
- Fear (~47k samples)
- Love (~34k samples)
- Surprise (~15k samples)
We benchmarked multiple representation techniques and classifiers:
- TF-IDF + Logistic Regression
- Unigrams vs Bigrams with
sublinear_tfand custom class weighting. - Accuracy: ~90-91%
- Unigrams vs Bigrams with
- TF-IDF + Linear SVC (Best Performing Classical Model)
- Achieved solid macro performance with balanced weights.
- Accuracy: ~90% (with exceptional speed and small disk footprint)
- Sentence Transformers (all-MiniLM-L6-v2) + Logistic Regression
- Evaluated dense semantic embeddings.
- Accuracy: ~65%
- TF-IDF + Random Forest Classifier
- Constrained to prevent unconstrained tree size.
- Accuracy: ~81%
Our chosen production pipeline employs the Linear Support Vector Classifier (LinearSVC) paired with a tuned Bigram TF-IDF Vectorizer due to its outstanding generalization and lightweight inference footprint.
├── app.py # Streamlit web application script
├── emotion_model.pkl # Saved trained LinearSVC classifier
├── tfidf_vectorizer.pkl # Saved fitted TF-IDF Bigram Vectorizer
├── requirements.txt # Package dependencies
└── README.md # Project documentation
Follow these steps to run the Streamlit app on your local machine:
git clone https://github.com/githubsneethi/Text-Emotion-Classifier.git
cd Text-Emotion-Classifierpython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activateEnsure you have python installed, then run:
pip install streamlit scikit-learn numpy pandas joblibstreamlit run app.pyYour browser should automatically pop open a new window to http://localhost:8501 showcasing the interface.
- Text input area: Write any phrase or paragraph of your choice.
- Dynamic Emotion prediction: Uses the cached serialized model pipeline instantly.
- Decision-function Softmax: Outputs confidence probabilities for each class (even with LinearSVC).
- Clean Bar Charts: Sorted breakdowns of the exact classification confidence percentages across all emotions.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page or open a Pull Request.
This project is licensed under the MIT License.