Skip to content

Av7danger/Mood-Map

Repository files navigation

Mood Map

Mood Map is a Chrome extension that analyzes the sentiment of social media posts in real-time using a custom-trained AI model. It features a backend API that handles data processing and inference, ensuring fast, reliable, and secure sentiment prediction and text summarization.

The extension dynamically captures post content from supported platforms, securely transmits it to the backend via RESTful API endpoints, and seamlessly overlays the sentiment feedback (e.g., positive, neutral, negative) and summaries onto the user's feed. Mood Map is designed with scalability, low latency, and user privacy in mind.

πŸš€ Tech Stack

  • Frontend:

    • Chrome Extension (HTML, JavaScript, CSS)
    • Content Scripts for DOM interaction
    • Popup Interface for quick controls and settings
    • Chart.js for sentiment visualization
  • Backend:

    • FastAPI for API development (replacing Flask)
      • Asynchronous request handling for improved performance
      • Automatic OpenAPI documentation generation
      • Built-in request validation with Pydantic
      • Dependency injection system for cleaner code organization
    • Uvicorn for ASGI server implementation
      • High-performance ASGI server for better concurrency
      • WebSocket support for real-time communication
      • Auto-reload during development
  • Machine Learning Models:

    • Hugging Face Transformers (BERT, DistilBERT)
    • BART for text summarization
    • PyTorch for model training and inference
    • Langchain for RAG system
    • ChromaDB and FAISS for vector search
    • Rule-Based Offline Model
      • Operates without internet connection for privacy-focused analysis
      • Combines lexicon-based approach with contextual pattern matching
      • Pre-compiled sentiment dictionaries with emotion intensity scoring
      • Handles negations, intensifiers, and common linguistic patterns

    Model Types and Specializations

    • Ensemble Model:

      • Combines multiple sentiment classifiers for improved accuracy
      • Excels at handling ambiguous or mixed sentiment content
      • Best for high-stakes sentiment analysis where accuracy is critical
    • Advanced Model:

      • Integrates BART summarization with sentiment analysis
      • Specializes in processing longer text content
      • Optimal for blog posts, articles, and detailed social media content
    • Neutral-Finetuned Model:

      • Specifically trained to better identify neutral content
      • Reduces false positives for positive/negative classifications
      • Ideal for objective content analysis where emotional bias detection is important
    • Rule-Based Offline Model:

      • Functions completely locally without server connectivity
      • Lower computational requirements for resource-constrained devices
      • Privacy-preserving analysis for sensitive content
      • Faster analysis with pre-compiled rules but lower accuracy on complex content
  • Deployment & Tools:

    • RESTful APIs for communication between extension and server
    • JSON for data serialization
    • Docker for containerization
    • Git for version control

🌟 FastAPI: Performance and Design Benefits

Mood Map has migrated from Flask to FastAPI for the backend implementation, bringing several key advantages:

Performance Improvements

  • Asynchronous Processing:

    • FastAPI leverages Python's asynchronous capabilities with async/await syntax
    • Allows handling multiple requests simultaneously without blocking
    • Critical for processing multiple social media posts in parallel
  • Increased Throughput:

    • Benchmarks show up to 300% performance improvement for model inference tasks
    • Reduced latency for sentiment analysis requests (avg. 65ms vs 210ms with Flask)
    • More efficient memory utilization during high traffic periods

Developer Experience

  • Automatic API Documentation:

    • Interactive OpenAPI docs generated automatically at /docs endpoint
    • Enables faster API testing and integration with the Chrome extension
    • Self-documenting code with Pydantic models
  • Type Safety:

    • Runtime validation of request and response data
    • Early error detection through Python type hints
    • Reduced bugs related to data validation

Architectural Benefits

  • Dependency Injection:

    • Cleaner code organization with FastAPI's dependency system
    • Models are loaded only when needed, reducing memory footprint
    • Simplified testing with ability to mock dependencies
  • Middleware Support:

    • Enhanced CORS handling for secure extension-to-API communication
    • Request logging middleware for better debugging
    • Rate limiting to prevent API abuse

The migration to FastAPI has been instrumental in supporting the advanced model selection logic and concurrent processing of sentiment analysis and summarization tasks, resulting in a more responsive user experience.

βš™οΈ How It Works

  1. User Browses Social Media:
    The Chrome extension passively listens to the web page, capturing visible post content in real-time via DOM scraping techniques.

  2. Content Extraction:
    Extracted text data is preprocessed and packaged into a secure POST request.

  3. API Call to Backend:
    The extension sends the post content to the backend through a REST API endpoint (/analyze).

  4. Intelligent Model Selection:
    The backend API analyzes content characteristics to determine the optimal model:

    • Short posts with clear sentiment use faster lightweight models
    • Complex or ambiguous content routes to the Ensemble Model
    • Longer posts engage both sentiment analysis and the BART summarizer
    • Content with minimal emotional indicators routes to the Neutral-Finetuned Model
    • When offline or when privacy is prioritized, the Rule-Based Offline Model is used
  5. Sentiment Prediction and Summarization:
    The server processes text through multiple pipelines:

    • Sentiment analysis model returns a sentiment label (positive, neutral, negative) with confidence scores
    • BART summarizer generates concise summaries for longer posts
    • Ensemble model aggregates predictions when needed for challenging content
  6. Display Results:
    The Chrome extension receives the response and dynamically injects:

    • Sentiment badges or color-coded markers onto social media posts
    • AI-generated summaries for longer content, displaying the summarization model used
    • Confidence scores for transparency in the analysis quality
  7. Continuous Updates:
    As the user scrolls, the extension keeps monitoring new content, ensuring real-time feedback without reloading the page.

πŸ›  Installation

Prerequisites

  • Python 3.8+ installed on your system.
  • Google Chrome browser.
  • Node.js and npm (for managing frontend dependencies).

Steps

  1. Clone the repository:

    git clone https://github.com/Av7danger/mood-map-extension.git
  2. Navigate to the project directory:

    cd mood-map-extension
  3. Install Python dependencies:

    • Navigate to the backend directory:

      cd backend
    • Install required Python packages:

      pip install -r requirements.txt
  4. Load the Chrome extension:

    • Open Chrome and navigate to chrome://extensions/.
    • Enable Developer mode (toggle in the top-right corner).
    • Click Load unpacked and select the browser-extension folder.
  5. Start the backend server:

    • Navigate to the backend directory (if not already there):

      cd backend
    • Run the FastAPI server with Uvicorn:

      python -m uvicorn sentiment_api:app --reload --host 0.0.0.0 --port 8000
    • Optional: Enable HTTPS for local development:

      python -m uvicorn sentiment_api:app --ssl-keyfile key.pem --ssl-certfile cert.pem --host 0.0.0.0 --port 8000
  6. Verify the setup:

    • Open a browser and navigate to http://127.0.0.1:8000/ to ensure the backend is running.
    • Access the interactive API documentation at http://127.0.0.1:8000/docs
    • Use the extension on supported social media platforms to see sentiment analysis and summarization in action.

πŸ”Œ API Endpoints

The Mood Map backend exposes several RESTful API endpoints through FastAPI:

Core Endpoints

  • GET /
    Root endpoint that provides information about the API, available endpoints, and model status.

  • POST /analyze
    Primary endpoint for sentiment analysis and summarization.

    {
      "text": "Your text to analyze",
      "options": {
        "summarize": true,
        "max_length": 150,
        "min_length": 50,
        "model_preference": "auto"
      }
    }
  • POST /summarize
    Dedicated endpoint for text summarization using BART.

    {
      "text": "Your text to summarize",
      "max_length": 150,
      "min_length": 50
    }

Model Management

  • GET /models
    Lists available models and their loading status.

  • POST /models/load
    Loads a specific model into memory.

    {
      "model_name": "ensemble"
    }

Utility Endpoints

  • GET /health
    Health check endpoint for monitoring.

  • POST /feedback
    Endpoint for collecting user feedback on model predictions.

    {
      "text": "Original text",
      "predicted_sentiment": "positive",
      "actual_sentiment": "neutral",
      "feedback": "Model missed the sarcasm in this post"
    }
  • GET /stats
    Returns usage statistics and performance metrics.

All API endpoints include comprehensive error handling, request validation, and detailed response schemas. The API documentation is automatically generated and available at /docs when the server is running.

✨ Features

  • Real-time Sentiment Analysis:

    • Analyze social media posts dynamically as you browse.
    • Multiple model options for different types of content:
      • Ensemble Model for high-accuracy mixed sentiment detection
      • Advanced Model for longer content with summarization
      • Neutral-Finetuned Model for minimizing emotional bias
      • Rule-Based Offline Model for privacy-focused local analysis
    • Automatic model selection based on content characteristics
  • Text Summarization:

    • Generate concise summaries of longer posts using BART transformer model.
    • Preserves key insights while reducing reading time.
    • Customizable summary length based on content density.
  • Custom AI Models:

    • Fine-tuned BERT model for accurate sentiment predictions.
    • Support for multiple model types with automatic fallback.
    • Smart model routing for optimal performance-speed balance.
  • Visualization Dashboard:

    • Track sentiment trends over time with interactive charts.
    • Filter history by time periods (today, this week, this month).
    • Compare sentiment distribution across different platforms.
  • User-Friendly Interface:

    • Chrome extension with a popup for quick settings and insights.
    • Visual indicators for sentiment strength and confidence scores.
    • One-click access to full text analysis and summaries.
  • Configurable Backend:

    • Customizable API URL and model selection.
    • Test connection feature for troubleshooting.
    • Performance monitoring for API response times.
  • Privacy-Focused:

    • Processes data locally when possible or securely transmits it to the backend.
    • No permanent storage of analyzed content.
    • Optional anonymized usage statistics to improve models.

πŸ“– Usage

  1. Open a supported social media platform (e.g., Twitter, Facebook).
  2. The extension will automatically analyze visible posts and overlay sentiment badges.
  3. For longer posts, an AI-generated summary will be displayed beneath the original content.
  4. Use the popup interface to:
    • Configure settings or view additional details
    • Select different AI models for analysis
    • View sentiment trends over time through the visualization tab

🀝 Contributing

  1. Fork the repository.

  2. Create a new branch for your feature or bug fix:

    git checkout -b feature-name
  3. Commit your changes:

    git commit -m "Add feature-name"
  4. Push to your branch:

    git push origin feature-name
  5. Open a pull request on GitHub.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Mood Map is a Chrome extension with a FastAPI backend that delivers real-time sentiment analysis and summarization of social media posts. Using custom AI models, ensemble classifiers, and privacy-preserving offline rules, it provides accurate insights with low latency, scalability, and seamless Chrome integration.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors