A comprehensive sentiment analysis system using PyTorch,
TorchText,
FastAPI,
Streamlit,
MLflow, and
Airflow.
The system uses a TextCNN model trained on the NTC-SCV dataset to classify Vietnamese text as positive or negative. Quick demo here
- Sentiment Classification: Analyze Vietnamese text as positive or negative
- Text Processing: Teencode normalization, emoji handling, URL removal
- Interactive UI: Streamlit frontend for user interaction and feedback
- API Backend: FastAPI service for model inference and data handling
- MLOps Pipeline:
- MLflow for experiment tracking and model registry
- Airflow for scheduling regular model retraining
- Containerization: Docker for easy deployment and environment consistency
- Feedback Loop: User feedback collection to improve model performance
The system consists of several interconnected components:
-
Frontend (Streamlit)
- Interactive web interface for text input
- Visualization of sentiment analysis results
- Feedback collection mechanism
-
Backend (FastAPI)
- RESTful API endpoints for sentiment prediction
- Feedback data collection and storage
- Model serving and integration
-
Model Training (PyTorch)
- TextCNN architecture implementation
- TorchText for vocabulary building and text processing
- Trained on NTC-SCV Vietnamese sentiment dataset
-
MLOps Pipeline
- MLflow tracks experiments, parameters, and metrics
- Airflow orchestrates regular model retraining
- Automated model evaluation and deployment
-
Containerization (Docker)
- Individual services containerized
- Docker Compose for multi-container management
- Consistent environments across development and production
The TextCNN model consists of:
- Embedding Layer: Converts Vietnamese words into dense vectors
- Multiple Convolutional Layers: With different kernel sizes (2, 3, 4) to capture various n-gram features
- Max Pooling: Extracts the most important features from each convolutional output
- Fully Connected Layer: Maps extracted features to sentiment classification
Current performance metrics:
- Test Accuracy: 88.97%
- Test Loss: 0.2777
- Data Extraction: Load raw data from NTC-SCV dataset
- Preprocessing:
- Language filtering (Vietnamese only)
- Teencode normalization
- URL and HTML removal
- Emoji handling
- Vocabulary Building: Create word vocabulary using TorchText
- Model Training: Train TextCNN model with PyTorch
- Evaluation: Assess model on validation and test sets
- Deployment: Register and deploy successful models via MLflow
- User Input: Text submitted via Streamlit UI
- API Processing: FastAPI endpoint receives and preprocesses text
- Model Inference: Processed text fed to deployed model
- Result Visualization: Sentiment predictions displayed in UI
- Feedback Collection: User can provide feedback on prediction accuracy
The automated training pipeline is orchestrated by Apache Airflow, running on a weekly schedule:
π¦ sentiment_analysis_training DAG
β
βββ π₯ extract_data
β βββ Fetches raw data from NTC-SCV dataset
β
βββ π transform_data
β βββ Filters non-Vietnamese content and preprocesses text
β
βββ π§ train
β βββ Builds vocabulary and trains TextCNN model
β
βββ π evaluate
β βββ Tests model on hold-out data and logs metrics
β
βββ π deploy_model
βββ Promotes model to production if metrics improve
Key benefits:
- β±οΈ Automated Scheduling: Regular weekly retraining
- π Data Validation: Ensures quality input data
- π Performance Tracking: Monitors metrics across versions
- π‘οΈ Deployment Safeguards: Only deploys if performance improves
- π Comprehensive Logging: Full experiment tracking with MLflow
-
Backend:
-
Frontend:
-
MLOps:
-
Infrastructure:
-
Data Processing:
- Docker and Docker Compose
- Git
-
Clone the repository:
git clone https://github.com/edithh81/vietnamese-sentiment-analysis.git cd vietnamese-sentiment-analysis -
Start the services:
docker-compose up -d
-
Access the components:
- Streamlit UI: http://localhost:8501
- FastAPI Docs: http://localhost:8000/docs
- MLflow UI: http://localhost:5000
- Airflow UI: http://localhost:8080
This project uses a virtual environment to manage dependencies. Instead of committing the full environment, follow these steps to recreate it:
# Create a virtual environment
python -m venv myenv
# Activate the environment
# On Windows
myenv\Scripts\activate
# On macOS/Linux
source myenv/bin/activate
# Install dependencies
pip install -r requirements.txt# Build the containers
docker-compose build
# Start services
docker-compose up -dFor detailed setup instructions, refer to the Setup and Installation section.
sentiment-analysis-app/
βββ airflow/ # Airflow DAGs and configurations
βββ app/
β βββ backend/ # FastAPI service
β βββ frontend/ # Streamlit UI
βββ config/ # Configuration files
βββ data/ # Dataset storage
βββ docker/ # Docker configurations
βββ mlflow_/ # MLflow utilities
βββ models/ # Saved models
βββ notebooks/ # Jupyter notebooks for exploration and development
βββ src/ # Core source code
β βββ model/ # Model architecture
β βββ preprocessing/ # Text preprocessing utilities
β βββ train/ # Training logic
βββ tests/ # Unit and integration tests
βββ .dockerignore
βββ .gitignore
βββ docker-compose.yml # Multi-container Docker configuration
βββ README.md
βββ requirements.txt # Python dependencies
The system processes Vietnamese text using multiple steps:
- Tokenization: Using
pyvifor Vietnamese word segmentation - Teencode Normalization: Maps informal Vietnamese (e.g., "ko" β "khΓ΄ng")
- URL and HTML Removal: Removes web links and HTML tags
- Emoji Handling: Converts emojis to textual representations
- Language Detection: Filters out non-Vietnamese content
Note: For improved Vietnamese text processing, consider using underthesea library instead of pyvi if available on your system.
- Implement more advanced model architectures (BERT, PhoBERT)
- Add support for aspect-based sentiment analysis
- Integrate active learning from user feedback
- Expand language support to handle code-switching (Vietnamese-English)
- Enhance visualization with sentiment trend analysis
Current model metrics with 20 epochs and batch size of 128:
- Test Accuracy: 88.97%
- Test Loss: 0.2777
The model performs well on the test dataset, though real-world performance through the Streamlit interface may vary. User feedback is being collected to improve future versions.


