Skip to content

shivareddy2002/Emotion-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

44 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

A professional deep learning web application that detects human emotions from text in real time using a Bidirectional LSTM (BiLSTM) neural network. Built with TensorFlow, Keras, Flask, and Bootstrap, this project transforms raw text into meaningful emotional insights.


๐Ÿงญ Project Elaboration (Quick Walkthrough)

Emotion-AI is a production-oriented sentiment understanding app that combines an NLP model with a Flask UI. In practice, the system works as a pipeline:

  1. User enters free-form text in the web page (templates/index.html).
  2. Backend sanitizes text (lowercasing + regex cleanup) before inference (app.py).
  3. Inference strategy is selected at runtime:
    • Local model mode (TensorFlow assets loaded from disk) for full deep-learning predictions.
    • Remote inference mode (API forwarding) for lightweight cloud deployment such as Vercel limits.
    • Lexicon fallback mode for resilient predictions when ML dependencies are unavailable.
  4. Emotion + confidence are returned as JSON and rendered instantly in the UI.

Why this architecture matters

  • Reliability: graceful fallback prevents total outage if model/runtime is unavailable.
  • Deployability: remote inference keeps serverless package size small.
  • Maintainability: model assets and app logic are separated (model_loader.py vs app.py).

Core repository layout

  • app.py โ†’ Flask routes, runtime mode switching, normalization, and fallback logic.
  • model_loader.py โ†’ loading tokenizer/encoder/model artifacts.
  • templates/ + static/ โ†’ web experience (UI, styles, interactions).
  • requirements*.txt โ†’ dependency sets for local vs deployment scenarios.

๐Ÿš€ Features

  • ๐Ÿ”ฎ Real-time emotion prediction

  • ๐Ÿง  BiLSTM-powered deep learning model

  • ๐ŸŽญ Supports 7 emotions:
    ๐Ÿ˜Š Joy | ๐Ÿ˜ข Sadness | ๐Ÿ˜ก Anger | ๐Ÿ˜จ Fear | ๐Ÿ’™ Love | ๐Ÿ˜ฒ Surprise | ๐Ÿ˜ Neutral

  • ๐ŸŒ Interactive Flask web interface


๐Ÿ—๏ธ Technical Architecture

1๏ธโƒฃ Data Loading & Exploration
Dataset is ingested and split into 80% training / 20% testing.

2๏ธโƒฃ Text Cleaning & Tokenization
Regex cleaning โ†’ Tokenization โ†’ Padding to fixed sequence length.

3๏ธโƒฃ Model Architecture
Embedding โ†’ Bidirectional LSTM โ†’ Dropout โ†’ Softmax Output.

4๏ธโƒฃ Training & Validation
Categorical Cross-Entropy + Adam Optimizer with Early Stopping & Checkpointing.

5๏ธโƒฃ Evaluation & Optimization
Accuracy, Precision, Recall, Confusion Matrix & Hyperparameter tuning.

6๏ธโƒฃ Deployment & Inference
Trained model (emotion_model.h5) integrated into Flask for real-time prediction.

๐Ÿ–ฅ๏ธ Web Interface

  • Input text via a modern UI
  • One-click โ€œTry a Sampleโ€ emotion buttons
  • Live prediction display with confidence bar
  • Fully responsive design

๐Ÿ› ๏ธ Tech Stack

Layer Technology
Frontend HTML, CSS, JS, Bootstrap
Backend Flask (Python)
ML / DL TensorFlow, Keras
NLP Tokenizer, Padding, Regex
Deployment Flask Web App

โš™๏ธ Workflow & Steps

1๏ธโƒฃ Data Loading & Preprocessing

  • Dataset loaded from .csv / .txt file
  • Text tokenized using Keras Tokenizer
  • Sequences padded for uniform input length

2๏ธโƒฃ Label Encoding

  • Emotion labels converted to numeric form using LabelEncoder
  • One-hot encoding applied for multi-class classification

3๏ธโƒฃ Model Architecture

Built using Keras Sequential API:

  • ๐Ÿ”น Embedding Layer โ€“ Converts words into dense vectors
  • ๐Ÿ”น Flatten Layer โ€“ Converts embeddings into 1D vector
  • ๐Ÿ”น Dense Layer โ€“ Learns complex patterns
  • ๐Ÿ”น Output Layer โ€“ Softmax activation for multi-class prediction

Compiled with:

  • Optimizer: Adam
  • Loss Function: categorical_crossentropy

4๏ธโƒฃ Model Training

  • Dataset split using train_test_split
  • Trained for 10 epochs
  • Batch size: 32
  • Validation data used to monitor performance

5๏ธโƒฃ Prediction & Testing

  • Model predicts emotions for unseen text
  • Example:

    Input: "I am feeling very nostalgic"
    Output: "Love"


๐Ÿ–ผ๏ธ Visual Workflow

flowchart LR
    subgraph DP[๐Ÿ“‚ Data Preparation]
        A["๐Ÿ“ฆ Import Libraries"]
        B["๐Ÿ“š Load Dataset"]
        C["โœ‚๏ธ Preprocessing"]
    end

    subgraph LP[๐Ÿท๏ธ Label Encoding]
        D["๐Ÿ”ข Encode Emotions"]
        E["๐Ÿ“Š One-Hot Encoding"]
    end

    subgraph MT[๐Ÿค– Modeling & Training]
        F["๐Ÿ—๏ธ Build Model"]
        G["โšก Train Model"]
    end

    subgraph PR[๐Ÿ”ฎ Prediction]
        H["๐Ÿ“ User Input"]
        I["๐Ÿ” Tokenize & Pad"]
        J["๐ŸŽฏ Predict Emotion"]
    end

    A --> B --> C --> D --> E --> F --> G --> H --> I --> J
    %% --- Styles ---
    style A fill:#FFD54F,stroke:#F57F17,stroke-width:2px,color:#000;
    style B fill:#4FC3F7,stroke:#0277BD,stroke-width:2px,color:#fff;
    style C fill:#AED581,stroke:#33691E,stroke-width:2px,color:#000;
    style D fill:#FFCC80,stroke:#EF6C00,stroke-width:2px,color:#000;
    style E fill:#FFE082,stroke:#F9A825,stroke-width:2px,color:#000;
    style F fill:#BA68C8,stroke:#4A148C,stroke-width:2px,color:#fff;
    style G fill:#FF8A65,stroke:#BF360C,stroke-width:2px,color:#fff;
    style H fill:#81D4FA,stroke:#01579B,stroke-width:2px,color:#000;
    style I fill:#B3E5FC,stroke:#0288D1,stroke-width:2px,color:#000;
    style J fill:#90CAF9,stroke:#0D47A1,stroke-width:2px,color:#000;
Loading

๐Ÿš€ Run Locally

# Clone the repository
git clone https://github.com/shivareddy2002/emotion-ai.git
cd emotion-ai

# Install dependencies
pip install -r requirements.txt

# Run the app
python app.py

โœจ Key Features

  • Text Tokenization & Padding
  • Multi-class Emotion Classification
  • Deep Learning with Embeddings
  • One-Hot Encoded Labels
  • Validation-Based Training

๐Ÿ› ๏ธ Technologies Used

  • Python
  • TensorFlow / Keras
  • NumPy, Pandas
  • Scikit-learn
  • Jupyter Notebook

๐Ÿš€ Applications

  • ๐Ÿ“ฑ Social Media Sentiment Analysis
  • ๐Ÿ›’ Customer Feedback Classification
  • ๐Ÿ›ก๏ธ Content Moderation
  • ๐Ÿง  Mental Health Monitoring
  • ๐Ÿ’ฌ Chatbots & Virtual Assistants

๐Ÿงฉ Conclusion

This project demonstrates how Neural Networks can effectively understand and classify human emotions from text.
It highlights the power of NLP in real-world applications such as customer service, analytics, and well-being platforms.


๐Ÿ‘จโ€๐Ÿ’ป Author

Lomada Siva Gangi Reddy

  • ๐ŸŽ“ B.Tech CSE (Data Science), RGMCET (2021โ€“2025)
  • ๐Ÿ’ก Interests: Python | Machine Learning | Deep Learning | Data Science
  • ๐Ÿ“ Open to Internships & Job Offers

Contact Me:


โ˜๏ธ Deploy on Vercel Without "Project Size Exceeded"

Vercel serverless functions have strict bundle limits, and tensorflow-cpu is usually too large.

This repository now supports two modes:

  1. Local mode: uses TensorFlow model files directly.
  2. Vercel mode (default on Vercel): skips TensorFlow and forwards inference requests to a remote API.

Steps

  1. Deploy a small Python API for model inference on a platform that supports heavy ML dependencies (Render/Railway/EC2).
  2. In Vercel project settings, add:
    • USE_LOCAL_MODEL=false
    • REMOTE_INFERENCE_URL=https://your-ml-api.example.com/predict
  3. Deploy this repo to Vercel. It installs only requirements-vercel.txt, which avoids large ML packages.

Local development

Use local dependencies when running model inference in the same process:

pip install -r requirements-local.txt
python app.py

Troubleshooting deployed crashes

If Vercel shows FUNCTION_INVOCATION_FAILED:

  1. Open https://<your-domain>/health and verify:
    • use_local_model is false on Vercel
    • remote_inference_configured is true
  2. In Vercel Project Settings โ†’ Environment Variables, set:
    • USE_LOCAL_MODEL=false
    • REMOTE_INFERENCE_URL=https://your-ml-api.example.com/predict
  3. Redeploy after saving env vars.

Analyze button shows no results?

If /predict cannot reach your remote inference API, the app now returns a lightweight fallback prediction so the UI still responds. For production-quality predictions, configure REMOTE_INFERENCE_URL to your ML service and keep USE_LOCAL_MODEL=false on Vercel.

Remote API payload should include either {emotion, confidence} or {label, score}. If payload is invalid, the app automatically falls back to lightweight local keyword prediction.

Get best prediction quality

For highest quality predictions in production:

  1. Prefer a dedicated remote inference service with your trained TensorFlow model.
  2. Set REMOTE_INFERENCE_URL in Vercel to that endpoint.
  3. Keep USE_LOCAL_MODEL=false on Vercel to avoid serverless crashes and size limits.
  4. Use /health to verify runtime status (remote_inference_configured=true).

If remote service is unavailable, the app will safely fall back to the built-in lite model.

If Vercel still shows FUNCTION_INVOCATION_FAILED

  1. Ensure vercel.json includes templates/** and static/** in includeFiles for @vercel/python.
  2. Keep USE_LOCAL_MODEL=false on Vercel unless TensorFlow and model files are actually available.
  3. Check https://<your-domain>/health after deploy; it should return JSON (not crash page).
  4. If root route fails, verify templates are deployed (missing templates cause serverless 500s).

Why preview branch works but main fails on Vercel

A common root cause is different Vercel environment configuration between Preview and Production after merge:

  • Preview deployment may have REMOTE_INFERENCE_URL configured while Production does not.
  • Production may still have stale USE_LOCAL_MODEL=true from older deploys.
  • Build packaging can differ if vercel.json changes were not merged cleanly.

After merging to main, verify Production env values in Vercel Project Settings and redeploy Production.

About

Emotion detection web app using BiLSTM + Flask with Vercel-optimized serverless deployment and remote inference fallback.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors