Skip to content

Professional full-stack sentiment analysis application using DistilBERT transformer model with FastAPI backend and modular React frontend. Features real-time text analysis, GPU acceleration, environment-aware configuration, and professional component architecture.

Notifications You must be signed in to change notification settings

khaledjamal/fastapi-react-sentiment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

DistilBERT Sentiment Analysis Web Application

A professional full-stack sentiment analysis application powered by DistilBERT transformer model with FastAPI backend and modular React frontend.

πŸš€ Features

  • Real-time sentiment analysis using DistilBERT model
  • Modular React frontend with component separation
  • GPU acceleration support (NVIDIA CUDA)
  • Analysis history tracking with 10-item limit
  • Example text suggestions for quick testing
  • Environment-aware configuration (dev/prod modes)
  • Professional error handling and user feedback
  • Responsive design for desktop and mobile
  • Character encoding properly handled
  • Development mode indicator for environment awareness

πŸ› οΈ Tech Stack

Backend

  • FastAPI - Modern Python web framework
  • Transformers (Hugging Face) - ML model integration
  • PyTorch - Deep learning framework
  • DistilBERT - Pre-trained sentiment analysis model
  • CUDA - GPU acceleration support

Frontend

  • React - Component-based UI library
  • Modern JavaScript - ES6+ features
  • Modular Architecture - Separated components and utilities
  • Environment Configuration - Dev/prod aware setup
  • CSS3 - Professional styling with animations

πŸ“ Project Structure

.
β”œβ”€β”€ README.md
β”œβ”€β”€ .gitignore
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ webapp/
β”‚   β”‚   β”œβ”€β”€ sentiment-api-basic.py   # Basic sentiment analysis FastAPI server
β”‚   β”‚   β”œβ”€β”€ sentiment-api-metrics.py # Sentiment API with inference time metrics
β”‚   β”‚   └── sentiment-api-client.py  # Comprehensive test suite and API client
β”‚   β”œβ”€β”€ requirements.txt             # Python dependencies
β”‚   └── venv/                        # Virtual environment (not in Git)
└── frontend/
    β”œβ”€β”€ components/
    β”‚   β”œβ”€β”€ Icons.js                 # SVG icon components
    β”‚   └── AnalysisResult.js        # Result display component
    β”œβ”€β”€ utils/
    β”‚   β”œβ”€β”€ config.js                # Environment configuration
    β”‚   β”œβ”€β”€ api.js                   # API communication
    β”‚   └── helpers.js               # Utility functions
    β”œβ”€β”€ data/
    β”‚   └── constants.js             # Application constants
    β”œβ”€β”€ sentiment-analyzer.html      # Main HTML file
    β”œβ”€β”€ app.js                       # Main React component
    └── styles.css                   # Professional styling

🚦 Getting Started

Prerequisites

  • Python 3.8+
  • NVIDIA GPU (optional, for faster inference)
  • Modern web browser (Chrome, Firefox, Safari, Edge)

Installation

  1. Clone this repository

    git clone <repository-url>
    cd distilbert-sentiment-analyzer
  2. Set up the backend

    cd backend
    python -m venv venv
    venv\Scripts\activate  # Windows
    source venv/bin/activate  # Linux/Mac
    pip install -r requirements.txt
  3. Start the FastAPI server

    python webapp/main-distbert.py
  4. Open the frontend

    • Navigate to the frontend directory
    • Open sentiment-analyzer.html in your web browser
    • Or serve via a local HTTP server for best results

Running with Local Server (Recommended)

cd frontend
python -m http.server 9000
# Then open: http://localhost:9000/sentiment-analyzer.html

🎯 Usage Examples

Quick Testing

  • Positive: "I love this course!" β†’ POSITIVE (99.9% confidence)
  • Negative: "This is terrible!" β†’ NEGATIVE (99.8% confidence)
  • Mixed: "The weather is okay today" β†’ POSITIVE (68.4% confidence)

Interactive Features

  • Click example buttons for instant text input
  • Use Ctrl+Enter for quick analysis
  • View analysis history in the sidebar
  • Clear history with one click
  • Real-time character feedback

πŸ”§ API Endpoints

Endpoint Method Description
/ GET API information and examples
/health GET Health check and system status
/analyze POST Sentiment analysis endpoint

API Request Example

POST /analyze
{
  "text": "I love this application!"
}

API Response Example

{
  "text": "I love this application!",
  "sentiment": "POSITIVE",
  "confidence": 0.999
}

πŸ“Š Model Performance

  • Model: DistilBERT (66M parameters)
  • Speed: ~60% faster than BERT-base
  • Accuracy: ~97% of BERT's performance
  • Memory Usage: ~250MB GPU memory
  • Inference Time: <100ms per request (GPU)

πŸ—οΈ Architecture Highlights

Professional Code Organization

  • Component Separation: Modular React components
  • Utility Functions: Reusable helper functions
  • Environment Configuration: Dev/prod environment handling
  • Constant Management: Centralized configuration
  • Error Handling: Comprehensive error management

Performance Optimizations

  • Model Caching: Pre-loaded models for instant inference
  • GPU Acceleration: CUDA support for faster processing
  • Component Efficiency: Optimized React rendering
  • Request Optimization: Efficient API communication

πŸ§ͺ Testing

Automated Testing

cd backend
venv\Scripts\activate  # Ensure virtual environment is active
python webapp/test-distbert.py

Test Coverage

  • Health checks - API availability verification
  • Single analysis - Individual text processing
  • Batch processing - Multiple text analysis
  • Edge cases - Empty text, long text, special characters
  • Performance testing - Response time measurement

🌍 Environment Configuration

The application automatically detects the environment:

  • Development Mode: localhost β†’ Shows "(Development Mode)" indicator
  • Production Mode: Deployed domains β†’ Clean production interface

Configuration managed in frontend/utils/config.js:

  • API endpoints
  • Timeout settings
  • Debug flags
  • Environment-specific features

🀝 Contributing

This project demonstrates professional development practices:

Technical Skills Showcased

  • Full-stack development with modern technologies
  • Machine learning integration using state-of-the-art models
  • API design following RESTful principles
  • Frontend architecture with component-based design
  • Environment management for deployment flexibility
  • Error handling and user experience optimization
  • Code organization and maintainability practices

Development Workflow

  • Git version control with proper .gitignore
  • Virtual environment management
  • Dependency tracking with requirements.txt
  • Modular architecture for scalability
  • Professional documentation and code comments

πŸ“„ License

This project is open source and available under the MIT License.

πŸ“ž Support

For questions about implementation or deployment:

  1. Check the comprehensive test suite in test-distbert.py
  2. Review the API documentation at http://localhost:8000/docs (when server is running)
  3. Examine the modular frontend code for customization examples

Built with ❀️ using DistilBERT, FastAPI, and React

About

Professional full-stack sentiment analysis application using DistilBERT transformer model with FastAPI backend and modular React frontend. Features real-time text analysis, GPU acceleration, environment-aware configuration, and professional component architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published