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.
- β 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
The project consists of the following modules:
- FastAPI route registration
- REST API server initialization
- Configuration loading from
.env - Classification request handling
- Machine learning model loading and training
- Text preprocessing
- Classification function with confidence score
- SQLite database initialization
- Classification results storage
- Statistics retrieval
- Tokenization and lemmatization using spaCy
- Keyword extraction
- Part-of-speech filtering
- Text cleaning and normalization functions
- Time-related helper functions
- Logging and validation
- Centralized settings storage
- Parameter reading from
.envfile
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)
git clone <repository-url>
cd Emotion-Classifierpip install -r requirements.txtpython -m spacy download uk_core_news_smEdit the .env file as needed:
HOST=127.0.0.1
PORT=8000
DEBUG=true
LOG_LEVEL=INFOpython start.pyThis will automatically:
- Configure the system
- Train the model (if needed)
- Start the server
python start.py --setuppython start.py --trainpython start.py --testpython start.py --init-dbpython start.py --serverAfter starting the server, documentation is available at:
- Swagger UI: http://localhost:26000/docs
- ReDoc: http://localhost:26000/redoc
{
"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"
}{
"start_date": "2025-06-01T00:00:00Z",
"end_date": "2025-06-08T23:59:59Z"
}Checks the status of the server, database, and model.
The system automatically collects statistics:
- Number of classifications
- Distribution by emotions
- Average model confidence
- Request processing time
# 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.logpython start.py --test# 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"ΠΠ/
βββ 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
- spaCy model: If the Ukrainian model is not installed, the system uses basic text processing
- Text size: Works optimally with texts up to 512 characters
- Language: The system is optimized for the Ukrainian language
- Fork the project
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Create a Pull Request
This project was created for educational purposes.