A web application that analyzes the emotions in text. You write something, and the AI tells you what emotion it expresses!
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" โจ
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! ๐ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
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
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 |
You need to have:
- Python 3.8+ installed
- Node.js & npm installed (for the website)
- Install Python packages:
cd backend
pip install -r requirements.txtOr with the project's setup:
cd backend
pip install fastapi uvicorn torch transformers- Start the backend server:
cd backend
python app.pyThe server will run at: http://localhost:8000
- Install JavaScript packages:
cd frontend
npm install- Start the website:
cd frontend
npm run devThe website will open at: http://localhost:5173
If you want to use this API directly:
GET http://localhost:8000/
Response:
{
"status": "ok",
"message": "Emotion Analysis API is running",
"model_loaded": true
}GET http://localhost:8000/emotions
Response:
{
"available_emotions": ["admiration", "amusement", "anger", ...],
"count": 28
}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,
...
}
}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:
- You write text โ "I hate waiting in traffic"
- The AI reads each word: "hate" (negative), "traffic" (situation)
- It combines all the clues โ Predicts: Anger or Annoyance
- It also gives a confidence score: How sure it is
- FastAPI - Modern Python framework for building APIs
- PyTorch - AI/Machine Learning library
- Transformers - Pre-built AI models (BERT)
- Pydantic - Data validation
- React - JavaScript library for building user interfaces
- TypeScript - JavaScript with type checking
- Vite - Fast build tool
- Tailwind CSS - Styling
- BERT - Bidirectional Encoder Representations from Transformers
- 28 emotion classes - Trained on emotion detection
Solution: Make sure the emotion_model_final/ folder exists in the root directory with all model files.
Solution: Make sure backend is running on http://localhost:8000
Solution: Use a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt- Social Media Monitoring - Track sentiment in tweets
- Customer Support - Detect angry customers automatically
- Mental Health Apps - Analyze how users are feeling
- Content Moderation - Flag potentially toxic comments
- Market Research - Understand customer feedback emotions
This project is for educational and local development purposes.
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
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.
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! ๐ญ