--- language: - en license: mit tags: - sentiment-analysis - roberta - transformers - pytorch - fastapi - multilingual - docker - kafka - nlp pipeline_tag: text-classification library_name: transformers datasets: - Reddit metrics: - accuracy - f1 model-index: - name: sentiment-roberta results: - task: type: text-classification name: Sentiment Analysis dataset: name: Reddit Sentiment Dataset type: custom metrics: - type: accuracy value: 0.8709 name: Accuracy - type: f1 value: 0.8715 name: Weighted F1 Score --- ## ๐ Project Philosophy This project intentionally preserves a controlled amount of real-world noise inside the final training dataset instead of aggressively sanitizing every sample. The objective was to train the sentiment classifier under realistic social-media conditions, where user-generated content naturally includes: - repetitive text - malformed sentences - slang and informal grammar - emotionally chaotic writing - duplicated phrases - inconsistent punctuation - low-quality Reddit comments - partially incoherent text - noisy conversational patterns Examples of preserved noise include: - repeated phrases such as: "Avoid being judgmental." - incomplete or poorly structured sentences - emotionally disorganized long-form Reddit posts - imperfect GPT-generated synthetic samples - informal internet writing styles Rather than building a perfectly clean academic benchmark, the pipeline focuses on creating a model capable of handling imperfect real-world inputs commonly found on social media platforms. The preprocessing pipeline still performs: - invalid text filtering - semantic validation - augmentation quality control - synthetic sample filtering but intentionally avoids over-cleaning the dataset in order to preserve natural language variability. This strategy improves: - robustness to noisy inputs - real-world generalization - inference stability - tolerance to imperfect user text - production-oriented behavior The final model was designed to operate under realistic NLP conditions rather than idealized datasets. โ ๏ธ Note: Even though the model achieved strong performance under noisy conditions, cleaner datasets and more aggressive manual curation could likely produce even higher evaluation metrics and better class separation. For optimal training performance, GPU acceleration is strongly recommended. A GPU with at least **8 GB of VRAM** is suggested for fine-tuning RoBERTa efficiently, especially when using: - mixed precision training - gradient accumulation - SWA optimization - larger batch sizes - transformer-based augmentation pipelines ## ๐ Project Overview The pipeline consists of the following stages: 1. **Reddit Data Extraction** 2. **Real-Time Streaming with Apache Kafka** 3. **Data Storage with PostgreSQL / MySQL** 4. **Text Cleaning, Data Augmentation & Dataset Balancing** 5. **Fine-Tuning the RoBERTa Sentiment Classifier** 6. **Model Evaluation, Testing & Inference** 7. **Inference API & Web Interface** 8. **Full Docker Support** 9. **Installation & Environment Setup** ## ๐ฅ 1. Reddit Data Extraction - Data is collected from the Reddit API using multiple subreddits grouped into **five categories**. - The number of posts retrieved is customizable through the `TOTAL_LIMIT` variable. - The project supports environment-based Reddit authentication using: ```env REDDIT_CLIENT_ID REDDIT_CLIENT_SECRET REDDIT_USER_AGENT REDDIT_USERNAME REDDIT_PASSWORD ## โก 2. Real-Time Streaming with Apache Kafka The pipeline leverages Apache Kafka to enable real-time data ingestion and processing. This stage is composed of two main components: ๐ค Kafka Producer - Fetches Reddit posts dynamically using the Reddit API - Streams each post as a message into a Kafka topic (reddit_posts) - Controls the ingestion rate to avoid overwhelming the system - Ensures scalability by decoupling data ingestion from downstream processing Key responsibilities: - Data ingestion from Reddit - Message serialization (JSON) - Publishing messages to Kafka topics ๐ฅ Kafka Consumer - Subscribes to the reddit_posts topic - Consumes messages in batches for efficiency - Stores raw data into the database (reddit_posts table) - Applies preprocessing and data augmentation - Publishes cleaned data into a new Kafka topic (cleaned_data) Key responsibilities: - Batch processing of streaming data - Data persistence (raw + processed) - Data cleaning and transformation - Forwarding processed data for downstream tasks ๐ Streaming Flow Reddit API โ Kafka Producer โ Kafka Topic (reddit_posts) โ Kafka Consumer โ Database (raw data) โ Data Cleaning & Augmentation โ Kafka Topic (cleaned_data) โ๏ธ Kafka Configuration The pipeline supports both local execution and Docker-based environments. Kafka broker address - Use "localhost:9092" for local execution - Use "kafka:9092" when running with Docker KAFKA_BROKER=localhost:9092 or kafka:9092 Input topic (raw Reddit data) KAFKA_TOPIC=reddit_posts Consumer group identifier (for scalability and fault tolerance) KAFKA_CONSUMER_GROUP=reddit_consumer_group Output topic (processed/cleaned data) TOPIC_OUTPUT=cleaned_data Consumer group ID (used internally by Kafka) GROUP_ID=reddit_consumer_group ๐ง Design Considerations - Decoupled Architecture: Producers and consumers operate independently - Scalability: Multiple consumers can be added using the same GROUP_ID - Fault Tolerance: Kafka ensures message durability and replayability - Batch Processing: Improves performance and reduces database overhead - Streaming + Processing Hybrid: Combines real-time ingestion with batch transformations ๐ Why Kafka? Using Apache Kafka allows this pipeline to: Handle large-scale data ingestion Enable real-time sentiment analysis pipelines Support future extensions (e.g., monitoring, alerting, dashboards) Integrate easily with distributed systems (Spark, Flink, etc.) ## ๐๏ธ 3. Data Storage with PostgreSQL / MySQL The pipeline includes a flexible and scalable storage layer built on top of relational databases such as PostgreSQL and MySQL. Database interactions are managed using SQLAlchemy, enabling seamless switching between database engines without modifying the core logic. โ๏ธ Database Configuration The system is fully configurable via environment variables: # Supported values: "postgres" or "mysql" DB_TYPE= DB_HOST= DB_PORT= DB_NAME= DB_USER= DB_PASSWORD= ๐ Connection Management A centralized connection handler dynamically builds the database URL Uses SQLAlchemy engine for efficient connection pooling Ensures a single reusable connection instance across the pipeline Supports both local and Docker-based environments ๐งฑ Data Model Overview The pipeline stores data across multiple structured tables: ๐ฅ reddit_posts (Raw Data) Stores original Reddit data ingested from Kafka. Fields: - id (Primary Key) - title - text - score - num_comments - created_utc - url - label (auto-generated) ๐ Labeling Strategy: Rule-based labeling using subreddit categories Fallback to sentiment analysis using VADER Hybrid approach improves labeling robustness ๐งน cleaned_data (Processed Data) Stores cleaned and normalized text ready for training. Fields: - id - text - label ๐ relabeled_data (Hybrid Relabeling) Stores dataset after applying model-based relabeling Used to reduce noise and improve data quality before training Hybrid relabeling uses a pretrained external model (cardiffnlp/twitter-roberta-base-sentiment) before the final fine-tuning stage. โ๏ธ balanced_* (Balanced Datasets) Stores class-balanced datasets Created using undersampling or other balancing strategies ๐งช synthetic_* (Augmented Data) Stores synthetically generated samples Used to improve minority class representation ๐ validation_data Stores validation split (20% of dataset) Includes text and numeric labels used for evaluation ๐ validation_results Stores model predictions for validation data Automatically resets before inserting new results ๐ง Data Processing Logic - Raw messages from Kafka are stored immediately in reddit_posts - Data validation filters: - Missing fields (id, text) - Non-informative text - Hybrid labeling system: - Subreddit-based labeling - VADER sentiment fallback - Cleaned data is stored in cleaned_data - Additional transformations generate: - Relabeled datasets - Balanced datasets - Synthetic datasets Kafka (reddit_posts) โ Database (reddit_posts - raw) โ Validation + Labeling (Hybrid) โ Database (cleaned_data) โ (Others) โ relabeled_data: โ balanced_data: Balance dataset distribution through oversampling, downsampling or both โ combined: Combine relabeled (raw data) and cleaned data โ synthetic_data: Data generated by GPT-2 for oversampling The dataset used to train the model consists of a mix of balanced and combined data. Which is called balanced combined ## ๐งน 4. Text Cleaning, Data Augmentation & Dataset Balancing A critical stage of the pipeline focuses on improving data quality, dataset diversity, and class balance before model training. This step combines: - Text normalization and preprocessing - Invalid / noisy text filtering - Traditional NLP-based data augmentation - Hybrid dataset balancing using downsampling + oversampling - Synthetic text generation using GPT-2-based controlled generation This significantly improves training stability and model generalization. ๐งฝ Text Cleaning The clean_text() function standardizes the input text before training. - Applied preprocessing steps: - Convert text to lowercase - Remove special characters - Remove extra spaces - Remove emojis and Unicode symbols - Remove English stopwords using NLTK - Normalize sentence structure This helps reduce noise and improves semantic consistency across samples. ๐ซ Invalid Text Detection The is_valid_text() function filters low-quality or meaningless content such as: - [deleted] - [removed] - placeholder text - extremely short texts - non-informative content - malformed generated samples This prevents noisy samples from contaminating the training dataset. ๐ Traditional Data Augmentation To increase dataset diversity, the pipeline applies lightweight augmentation techniques: ๐น Synonym Replacement Uses WordNet through NLTK to replace words with semantically similar synonyms. Example: Original: I feel very happy today Augmented: I feel very glad today ๐น Word Dropout Randomly removes non-critical words while preserving semantic meaning. Special care is taken to preserve: negations (not, never, no) short but important words Example: Original: I do not like this product at all Augmented: I not like product ๐ง Semantic Validation Not every augmentation is useful. The pipeline uses Sentence Transformers (all-MiniLM-L6-v2) to validate semantic similarity between: - original text - augmented text using cosine similarity. This prevents augmentation from changing the original sentiment meaning. โ๏ธ Dataset Balancing Strategy The original Reddit dataset was highly imbalanced across sentiment classes: - Positive - Neutral - Negative To prevent model bias toward majority classes, a hybrid balancing strategy was implemented. ๐ฝ Downsampling (Majority Class) If severe imbalance is detected: the majority class is partially reduced the reduction ratio is automatically adjusted based on imbalance severity This avoids over-representation without losing too much valuable information. - Adaptive strategy: - High imbalance โ stronger downsampling - Moderate imbalance โ softer downsampling This is more robust than fixed-ratio downsampling. ๐ผ Oversampling (Minority Classes) Minority classes are expanded using synthetic text generation with GPT-2. This improves representation for underrepresented sentiment classes. The system automatically: - detects minority classes - calculates missing samples - generates only the required amount This avoids unnecessary synthetic noise. ๐งช Synthetic Data Generation with GPT-2 Instead of naive duplication, the project generates new realistic Reddit-style text samples using: GPT-2 + prompt-based controlled generation Each sentiment class uses specialized prompts Example prompts: Negative โ dissatisfaction / frustration Neutral โ factual, emotion-free statements Positive โ appreciation / satisfaction Example: Write a short emotionally positive Reddit comment showing appreciation and satisfaction. Generation uses: - nucleus sampling (top_p) - temperature control - filtering and validation - post-cleaning of generated outputs This produces higher-quality synthetic samples for oversampling. ๐งผ Synthetic Data Filtering Generated samples are additionally filtered using: - minimum word count - punctuation checks - invalid symbol detection - spam/noise detection - malformed sentence removal - prompt leakage removal This ensures only high-quality synthetic samples are retained. ๐ Preprocessing Metrics The system tracks preprocessing quality using shared metrics: - empty_text_count - invalid_text_count This helps monitor: - dataset quality - cleaning effectiveness - augmentation quality and improves debugging across the full pipeline. ๐ Full Preprocessing Flow Raw Reddit Data โ Text Cleaning โ Invalid Text Filtering โ Traditional Augmentation โ synonym replacement โ word dropout โ Dataset Balancing โ downsampling โ oversampling (GPT-2) โ Synthetic Data Validation โ Final Training Dataset ๐ Why This Matters This stage improves: - Model generalization - Class balance - Training stability - Minority class performance - Label quality - Real-world robustness Without this step, the model would suffer from: - overfitting - class bias - poor minority recall - noisy training signals ## ๐ค 5. Fine-Tuning the RoBERTa Sentiment Classifier The training stage focuses on fine-tuning RoBERTa for multi-class sentiment classification: - Negative - Neutral - Positive The model is trained using a production-oriented strategy designed to improve: - generalization - minority class performance - training stability - robustness against overfitting This goes far beyond standard fine-tuning. ๐ง Model Architecture The project uses: roberta-base via Hugging Face Transformers. Configuration includes: - num_labels = 3 - reduced dropout tuning - attention dropout optimization - progressive layer unfreezing - mixed precision training - SWA optimization This setup improves performance while maintaining efficient training. ๐ฆ Flexible Dataset Selection Training supports multiple dataset sources: Available sources: - raw โ original Reddit data - relabeled โ hybrid relabeled dataset - cleaned โ cleaned + augmented dataset - combined โ 50% relabeled + 50% cleaned Available training modes: - unbalanced - balanced - synthetic This allows controlled experimentation and comparison between data strategies. Example: train_model( data_source="combined", dataset_type="balanced" ) โ๏ธ Loss Function Strategy Because sentiment classes remain naturally imbalanced, the training pipeline uses advanced loss handling. ๐ฅ Focal Loss (Primary) Custom Focal Loss is used to improve minority class learning. Benefits: - reduces dominance of easy samples - focuses on hard examples - improves minority recall - reduces majority class bias Additional improvements: - dynamic alpha calculation - class-aware weighting - special boost for the Neutral class This significantly improved F1 performance. ๐น Weighted CrossEntropy (Alternative) Also implemented as a fallback baseline using: - class weights - balanced label distribution This allows direct comparison against Focal Loss. ๐ง Progressive Layer Freezing / Unfreezing Instead of fine-tuning the full model immediately Initial strategy: - first 6 RoBERTa layers are frozen Progressive unfreezing: - deeper layers are gradually unfrozen every few epochs Benefits: - prevents catastrophic forgetting - stabilizes early training - improves transfer learning efficiency - reduces overfitting risk This behaves similarly to advanced techniques like Layer-wise Learning Rate Decay (LLRD). ๐ Learning Rate Optimization The project uses: OneCycleLR Scheduler instead of static learning rates. This provides: - warm-up phase - cosine annealing - smoother convergence - better final generalization Additional learning rate reductions are applied when new layers are unfrozen. โก Mixed Precision + Gradient Accumulation Training includes: - Mixed Precision Training Using: - autocast + GradScaler Benefits: - lower VRAM consumption - faster training - improved GPU efficiency Gradient Accumulation Configuration: - batch_size = 24 - accumulation_steps = 3 This simulates larger effective batch sizes without GPU memory issues. ๐ง Stochastic Weight Averaging (SWA) The final training epochs use: SWA (Stochastic Weight Averaging) This technique: - averages model weights - improves generalization - produces flatter minima - reduces validation instability The final saved model is the SWA-optimized version, not the raw last epoch checkpoint. ๐ Early Stopping To avoid overfitting: Early stopping monitors: - weighted F1-score - validation loss Training stops automatically if no improvement is detected. This prevents unnecessary training and preserves the best-performing checkpoint. ๐ Validation Strategy Dataset split: - 80% Training - 20% Validation using stratified sampling. Validation data is also stored in the database for: - reproducibility - inference testing - model evaluation consistency ๐ Evaluation Metrics Each epoch tracks: - Validation Accuracy - Weighted F1 Score - Training Loss - Validation Loss - Full Classification Report Special focus is placed on: Weighted F1 Score because it better reflects performance on imbalanced datasets. ๐พ Model Saving The final model is saved as: Roberta_sentiment_model/ including: - model weights (model.safetensors) - tokenizer - config files - tokenizer metadata This allows immediate deployment with: - AutoTokenizer.from_pretrained(...) - AutoModelForSequenceClassification.from_pretrained(...) and direct upload to Hugging Face. ๐ Training Flow Balanced Dataset โ Train / Validation Split โ RoBERTa Tokenization โ Progressive Fine-Tuning โ Focal Loss โ OneCycleLR โ Mixed Precision โ SWA โ Early Stopping โ Validation Monitoring โ Best Model Selection โ Final SWA Model Saved This is a production-grade training strategy for robust NLP classification with: - advanced optimization - imbalance handling - model stability improvements - reproducible experimentation - deployable output ## ๐งช 6. Model Evaluation, Testing & Inference After training, the fine-tuned RoBERTa model is evaluated using a dedicated validation pipeline designed to measure both global performance and per-class robustness. This stage ensures the model is reliable before deployment and provides detailed diagnostics for sentiment classification quality. ๐ฆ Validation Dataset The validation dataset is automatically created during training using an 80/20 stratified split: - 80% โ Training set - 20% โ Validation set The validation split is stored in the database inside: validation_data Fields: - id - text - label This guarantees reproducibility and allows evaluation to be performed independently after training. ๐ค Inference on Validation Data The saved model from Roberta_sentiment_model is loaded for evaluation. Each validation sample is processed using: - RoBERTaTokenizer - RoBERTaForSequenceClassification - Softmax probability scoring - Argmax class prediction Predicted labels are then stored in: validation_results Fields: - id - text - predicted_label This separation between ground truth and predictions enables robust post-training analysis. ๐ Evaluation Metrics The project uses multiple evaluation metrics to avoid relying only on accuracy. - Global Metrics - Accuracy - Weighted F1-score - Balanced Accuracy - Matthews Correlation Coefficient (MCC) These metrics provide a better understanding of performance, especially under class imbalance. ๐ Per-Class Detailed Metrics For each sentiment class: - Negative - Neutral - Positive the system computes: - TP (True Positives) - TN (True Negatives) - FP (False Positives) - FN (False Negatives) - Precision - Recall - F1-score - Balanced Accuracy - MCC - Support This helps identify which classes are harder to predict (typically the Neutral class). ๐ Confusion Matrix Analysis Two confusion matrices are generated: - Standard Confusion Matrix Shows absolute prediction counts across classes.