Skip to content

Krish-Om/emotion_analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

11 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽญ Emotion Analysis Project

A web application that analyzes the emotions in text. You write something, and the AI tells you what emotion it expresses!


๐Ÿ“‹ What is This Project?

Imagine you have a text message and want to know if it sounds happy, angry, sad, or something else. This project uses Artificial Intelligence to automatically detect the emotion in any text you give it.

Example:

  • You type: "I'm so excited for my vacation!"
  • AI responds: "Emotion: Excitement" โœจ

๐Ÿ—๏ธ How Does It Work?

Here's a simple diagram showing how the application flows:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     USER TYPES TEXT                         โ”‚
โ”‚              "I just got great news!"                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              FRONTEND (Website/App)                          โ”‚
โ”‚     (React - This is what you see on your screen)           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚ Sends text to the server
                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              BACKEND (FastAPI Server)                        โ”‚
โ”‚     (This is the brain that processes your request)         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           AI MODEL (BERT - The Smart AI)                    โ”‚
โ”‚     (A trained AI that understands emotions)                โ”‚
โ”‚              โ†“                                               โ”‚
โ”‚     Analyzes the text and predicts emotion                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              RESULT SENT BACK                               โ”‚
โ”‚    "Emotion: Joy (Confidence: 89%)"                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚       FRONTEND DISPLAYS THE RESULT TO USER                  โ”‚
โ”‚              User sees the answer!  ๐Ÿ˜Š                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ Project Structure

ritik/
โ”œโ”€โ”€ backend/                    # Server code (the brain)
โ”‚   โ”œโ”€โ”€ app.py                 # Main API application
โ”‚   โ”œโ”€โ”€ model.py               # AI model utilities
โ”‚   โ”œโ”€โ”€ emotion_model_final/   # The trained AI model files
โ”‚   โ””โ”€โ”€ pyproject.toml         # Python dependencies
โ”‚
โ”œโ”€โ”€ frontend/                   # Website code (what you see)
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ App.tsx            # Main app component
โ”‚   โ”‚   โ”œโ”€โ”€ components/        # UI components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ TextInput.tsx  # Text input box
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ EmotionResults.tsx  # Result display
โ”‚   โ”‚   โ””โ”€โ”€ utils/
โ”‚   โ”‚       โ””โ”€โ”€ emotionAnalyzer.ts  # Talks to backend
โ”‚   โ””โ”€โ”€ package.json           # JavaScript dependencies
โ”‚
โ””โ”€โ”€ emotion_model_final/        # Pre-trained AI model
    โ”œโ”€โ”€ model.safetensors      # The actual AI brain
    โ”œโ”€โ”€ config.json            # Model settings
    โ””โ”€โ”€ tokenizer files        # Helps convert text to numbers

๐ŸŽฏ What Emotions Can It Detect?

The AI can recognize 28 different emotions:

Emotions
๐Ÿ˜Š Admiration ๐Ÿ˜‚ Amusement ๐Ÿ˜  Anger ๐Ÿ˜ค Annoyance
๐Ÿ‘ Approval ๐Ÿ’• Caring ๐Ÿค” Confusion ๐Ÿคจ Curiosity
๐Ÿ˜ Desire ๐Ÿ˜ž Disappointment ๐Ÿ‘Ž Disapproval ๐Ÿคฎ Disgust
๐Ÿ˜ณ Embarrassment ๐ŸŽ‰ Excitement ๐Ÿ˜จ Fear ๐Ÿ™ Gratitude
๐Ÿ˜ข Grief ๐Ÿ˜„ Joy ๐Ÿ’‘ Love ๐Ÿ˜ฐ Nervousness
๐ŸŒŸ Optimism ๐Ÿ† Pride ๐Ÿ’ก Realization ๐Ÿ˜Œ Relief
๐Ÿ˜” Remorse ๐Ÿ˜ž Sadness ๐Ÿ˜ฒ Surprise ๐Ÿ˜‘ Neutral

๐Ÿš€ Getting Started

Prerequisites

You need to have:

  • Python 3.8+ installed
  • Node.js & npm installed (for the website)

Backend Setup

  1. Install Python packages:
cd backend
pip install -r requirements.txt

Or with the project's setup:

cd backend
pip install fastapi uvicorn torch transformers
  1. Start the backend server:
cd backend
python app.py

The server will run at: http://localhost:8000

Frontend Setup

  1. Install JavaScript packages:
cd frontend
npm install
  1. Start the website:
cd frontend
npm run dev

The website will open at: http://localhost:5173


๐Ÿ“ก API Endpoints (For Developers)

If you want to use this API directly:

1. Health Check โœ…

GET http://localhost:8000/

Response:

{
  "status": "ok",
  "message": "Emotion Analysis API is running",
  "model_loaded": true
}

2. Get Available Emotions ๐Ÿ“‹

GET http://localhost:8000/emotions

Response:

{
  "available_emotions": ["admiration", "amusement", "anger", ...],
  "count": 28
}

3. Analyze Emotion ๐Ÿ”

POST http://localhost:8000/analyze
Content-Type: application/json

{
  "text": "I'm so happy right now!"
}

Response:

{
  "text": "I'm so happy right now!",
  "emotion": "joy",
  "confidence": 0.95,
  "scores": {
    "joy": 0.95,
    "excitement": 0.03,
    "surprise": 0.02,
    ...
  }
}

๐Ÿง  How Does the AI Work? (Simple Explanation)

The project uses BERT, which is a powerful AI model trained on billions of text examples.

Think of it like this:

  • You show a human millions of texts with labeled emotions
  • After seeing so many examples, they learn to recognize emotions in new texts
  • That's exactly what BERT does, but as a computer!

The Process:

  1. You write text โ†’ "I hate waiting in traffic"
  2. The AI reads each word: "hate" (negative), "traffic" (situation)
  3. It combines all the clues โ†’ Predicts: Anger or Annoyance
  4. It also gives a confidence score: How sure it is

โš™๏ธ Tech Stack

Backend

  • FastAPI - Modern Python framework for building APIs
  • PyTorch - AI/Machine Learning library
  • Transformers - Pre-built AI models (BERT)
  • Pydantic - Data validation

Frontend

  • React - JavaScript library for building user interfaces
  • TypeScript - JavaScript with type checking
  • Vite - Fast build tool
  • Tailwind CSS - Styling

AI Model

  • BERT - Bidirectional Encoder Representations from Transformers
  • 28 emotion classes - Trained on emotion detection

๐Ÿ”ง Troubleshooting

Issue: "Model not loaded"

Solution: Make sure the emotion_model_final/ folder exists in the root directory with all model files.

Issue: "Connection refused" on frontend

Solution: Make sure backend is running on http://localhost:8000

Issue: Python dependencies not installing

Solution: Use a virtual environment:

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -r requirements.txt

๐Ÿ“ Example Use Cases

  1. Social Media Monitoring - Track sentiment in tweets
  2. Customer Support - Detect angry customers automatically
  3. Mental Health Apps - Analyze how users are feeling
  4. Content Moderation - Flag potentially toxic comments
  5. Market Research - Understand customer feedback emotions

๐ŸŽ“ Learning Resources


๐Ÿ“„ License

This project is for educational and local development purposes.


๐Ÿค Contributing

Feel free to modify and improve this project!

Ideas to enhance it:

  • Add support for multiple languages
  • Create a dashboard to track emotions over time
  • Add user authentication
  • Store results in a database
  • Deploy to production

โ“ FAQ

Q: Can this detect sarcasm? A: Not perfectly. Sarcasm is tricky even for humans sometimes!

Q: Is my text sent to external servers? A: No! Everything runs locally on your computer. Your data is private.

Q: Can I use this for real-world applications? A: This version is for learning/development. For production, you'd want more testing and error handling.

Q: What's the accuracy? A: Usually around 80-90% depending on the text length and clarity.


๐ŸŽ‰ Have Fun!

Try analyzing different texts and see what emotions the AI detects. You might be surprised!

"I can't wait for Friday!" โ†’ Excitement โœจ
"This coffee is cold again..." โ†’ Annoyance ๐Ÿ˜ค
"Thank you so much!" โ†’ Gratitude ๐Ÿ™
"I just got a promotion!" โ†’ Joy ๐Ÿ˜„

Happy emotion detecting! ๐ŸŽญ

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors