A machine learning web application that detects human emotions from text using Natural Language Processing. Built with TF-IDF vectorization and Logistic Regression, wrapped in an interactive Streamlit UI.
π Live Demo: emotion-detector-pranjal-pandey.streamlit.app
- Overview
- Demo
- Emotions Detected
- Project Structure
- NLP Pipeline
- Model Comparison
- Tech Stack
- Installation & Usage
- Dataset
- Results
- Author
This project builds an end-to-end NLP pipeline that classifies text into one of 6 human emotions. The pipeline includes full text preprocessing, feature extraction using TF-IDF and Bag of Words, and classification using multiple ML models. The best model is served through a Streamlit web app with a clean, interactive UI.
Run locally with:
python -m streamlit run emotion_app.pyFeatures of the UI:
- Upload your own training data
- Type any text and get instant emotion prediction
- View confidence scores for all 6 emotions as a bar chart
- See cleaned text after the full preprocessing pipeline
- View training data emotion distribution chart
| Emotion | Label | Emoji |
|---|---|---|
| Sadness | 0 | π’ |
| Anger | 1 | π |
| Love | 2 | β€οΈ |
| Surprise | 3 | π² |
| Fear | 4 | π¨ |
| Joy | 5 | π |
emotion-detector-nlp/
β
βββ emotion_app.py # Streamlit web application
βββ NLP-emotions.ipynb # Full model training notebook
βββ train.txt # Training dataset (text;emotion format)
βββ requirements.txt # Python dependencies
βββ README.md # Project documentation
Every input text goes through the following preprocessing steps before prediction:
Raw Text
β
βΌ
1. Lowercase conversion β "I AM HAPPY" β "i am happy"
β
βΌ
2. Remove punctuation β "hello!!!" β "hello"
β
βΌ
3. Remove numbers β "room 404" β "room "
β
βΌ
4. Remove emojis β keeps only ASCII characters
β
βΌ
5. Remove URLs β strips http/https/www links
β
βΌ
6. Remove stopwords (NLTK) β removes "is", "the", "and" etc. (198 words)
β
βΌ
Cleaned Text β TF-IDF Vectorizer β Logistic Regression β Predicted Emotion
Three models were trained and evaluated. Here are the results:
| Model | Vectorizer | Test Accuracy | Notes |
|---|---|---|---|
| Multinomial Naive Bayes | Bag of Words (CountVectorizer) | ~77% | Fast, but lower accuracy |
| Multinomial Naive Bayes | TF-IDF | ~74% | Worse than BoW for this dataset |
| Logistic Regression | TF-IDF | ~86.2% | β Best model β selected for deployment |
- Handles high-dimensional sparse TF-IDF features well
- Outputs calibrated probabilities for all classes
max_iter=1000allowed full convergence of the optimizer- Significantly outperformed Naive Bayes on this 6-class problem
- TF-IDF penalizes very common words that appear across all documents
- Gives higher weight to words that are more unique to specific emotions
- Reduces noise from frequently occurring but uninformative words
| Tool | Purpose |
|---|---|
| Python 3.8+ | Core programming language |
| Pandas | Data loading and manipulation |
| NLTK | Stopwords removal and tokenization |
| Scikit-learn | TF-IDF, CountVectorizer, Logistic Regression, Naive Bayes |
| Matplotlib | Probability bar charts and distribution plots |
| Streamlit | Interactive web UI |
| NumPy | Numerical operations |
git clone https://github.com/venom312004/emotion-detector-nlp.git
cd emotion-detector-nlppip install -r requirements.txtpython -m streamlit run emotion_app.py- Upload your
train.txtfile via the sidebar - Wait a few seconds for the model to train
- Type any text in the input box
- Click Predict Emotion and see the result!
The dataset is a plain text file (train.txt) with semicolon-separated values:
i feel really happy today;joy
i miss you so much;sadness
this makes me so angry;anger
i love spending time with you;love
i had no idea this would happen;surprise
i am terrified of the dark;fear
- Format:
text;emotion - Classes: 6 (sadness, anger, love, surprise, fear, joy)
- Split: 80% training / 20% testing
- Random state: 42 (reproducible results)
Best Model : Logistic Regression + TF-IDF
Test Accuracy: ~86.2%
Classes : 6
Max Features : 20,000 (TF-IDF vocabulary)
Max Iter : 1000
Solver : lbfgs
The model performs particularly well on joy, sadness, and anger β emotions with strong distinctive vocabulary. Surprise and fear sometimes overlap due to similar language patterns.
Pranjal Pandey
- π B.Tech in Artificial Intelligence
- πΌ Email: pranjalpandey0301@gmail.com
- π GitHub: @venom312004
This project is licensed under the MIT License β feel free to use, modify, and distribute.
Made with β€οΈ using Python & Streamlit