Skip to content

nestorchurin/Emotion-Classifier

Repository files navigation

Ukrainian Text Emotion Classification System

Course Project
West Ukrainian National University (WUNU)
Faculty of Computer Information Technologies (FCIT)
Specialization: Computer Science and Artificial Intelligence (CSAI)

A REST API system for automatic emotion classification in Ukrainian text. The system uses machine learning to identify one of 6 emotions: Joy, Fear, Anger, Sadness, Disgust, Surprise.

🌟 Features

  • βœ… FastAPI-based REST API
  • βœ… Classification of 6 emotions in Ukrainian text
  • βœ… Results storage in SQLite database
  • βœ… Statistics and analytics
  • βœ… Automatic model training
  • βœ… Text processing with spaCy
  • βœ… Interactive API documentation

πŸ—οΈ Architecture

The project consists of the following modules:

1. main.py - Main Module

  • FastAPI route registration
  • REST API server initialization
  • Configuration loading from .env
  • Classification request handling

2. emotion_classifier.py - Emotion Classification Module

  • Machine learning model loading and training
  • Text preprocessing
  • Classification function with confidence score

3. database.py - Database Module

  • SQLite database initialization
  • Classification results storage
  • Statistics retrieval

4. text_analyzer.py - Text Processing Module

  • Tokenization and lemmatization using spaCy
  • Keyword extraction
  • Part-of-speech filtering

5. utils.py - Utility Module

  • Text cleaning and normalization functions
  • Time-related helper functions
  • Logging and validation

6. config.py - Configuration Module

  • Centralized settings storage
  • Parameter reading from .env file

πŸ“Š Dataset

The system uses the Ukrainian emotions dataset ukr-detect/ukr-emotions-binary with the following characteristics:

  • Total number of examples: 4,949
  • Emotions: Joy, Fear, Anger, Sadness, Disgust, Surprise
  • Distribution: Train (2,466), Validation (249), Test (2,234)

βš™οΈ Installation

1. Clone the project

git clone <repository-url>
cd Emotion-Classifier

2. Install dependencies

pip install -r requirements.txt

3. Install spaCy model (optional)

python -m spacy download uk_core_news_sm

4. Configure settings

Edit the .env file as needed:

HOST=127.0.0.1
PORT=8000
DEBUG=true
LOG_LEVEL=INFO

πŸš€ Launch

Automatic launch (recommended)

python start.py

This will automatically:

  • Configure the system
  • Train the model (if needed)
  • Start the server

Step-by-step setup

1. System setup

python start.py --setup

2. Model training

python start.py --train

3. Testing

python start.py --test

4. Database initialization

python start.py --init-db

5. Server launch

python start.py --server

πŸ“š API Documentation

After starting the server, documentation is available at:

Main endpoints:

POST /classify - Text Classification

{
  "text": "Π― Π΄ΡƒΠΆΠ΅ щасливий ΡΡŒΠΎΠ³ΠΎΠ΄Π½Ρ–!",
  "save_to_db": true
}

Response:

{
  "text": "Π― Π΄ΡƒΠΆΠ΅ щасливий ΡΡŒΠΎΠ³ΠΎΠ΄Π½Ρ–!",
  "predicted_emotion": "Joy",
  "confidence": 0.892,
  "all_emotions": {    "Joy": 0.892,
    "Fear": 0.034,
    "Anger": 0.028,
    "Sadness": 0.025,
    "Disgust": 0.012,
    "Surprise": 0.009
  },
  "timestamp": "2025-06-08T15:30:45.123Z"
}

POST /stats - Statistics

{
  "start_date": "2025-06-01T00:00:00Z",
  "end_date": "2025-06-08T23:59:59Z"
}

GET /health - System Status

Checks the status of the server, database, and model.

πŸ“ˆ Monitoring and Statistics

The system automatically collects statistics:

  • Number of classifications
  • Distribution by emotions
  • Average model confidence
  • Request processing time

πŸ”§ Configuration

Main parameters in .env:

# Server
HOST=127.0.0.1
PORT=8000
DEBUG=true

# Model
MODEL_TYPE=sklearn
MAX_TEXT_LENGTH=512
MIN_TEXT_LENGTH=5

# Database
DATABASE_PATH=emotions.db

# Logging
LOG_LEVEL=INFO
LOG_FILE=logs/app.log

πŸ§ͺ Testing

Manual testing

python start.py --test

API testing with curl

# Text classification
curl -X POST "http://localhost:8000/classify" \
  -H "Content-Type: application/json" \
  -d '{"text": "Π¦Π΅ Ρ‡ΡƒΠ΄ΠΎΠ²ΠΈΠΉ дСнь!", "save_to_db": true}'

# Health check
curl -X GET "http://localhost:8000/health"

πŸ“ Project Structure

КП/
β”œβ”€β”€ main.py                    # Main FastAPI server
β”œβ”€β”€ config.py                  # System configuration
β”œβ”€β”€ emotion_classifier.py      # Emotion classifier
β”œβ”€β”€ database.py               # Database operations
β”œβ”€β”€ text_analyzer.py          # Text processing
β”œβ”€β”€ utils.py                  # Utility functions
β”œβ”€β”€ start.py                  # Launch script
β”œβ”€β”€ requirements.txt          # Dependencies
β”œβ”€β”€ .env                      # Configuration
β”œβ”€β”€ README.md                 # Documentation
β”œβ”€β”€ models/                   # Trained models
β”œβ”€β”€ logs/                     # System logs
└── ukr_emotions_dataset/     # Dataset
    β”œβ”€β”€ all_data.csv
    β”œβ”€β”€ train.csv
    β”œβ”€β”€ validation.csv
    β”œβ”€β”€ test.csv
    └── dataset_info.txt

πŸ› Known Limitations

  1. spaCy model: If the Ukrainian model is not installed, the system uses basic text processing
  2. Text size: Works optimally with texts up to 512 characters
  3. Language: The system is optimized for the Ukrainian language

🀝 Contribution

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Create a Pull Request

πŸ“„ License

This project was created for educational purposes.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages