A comprehensive Reddit sentiment analysis application that fetches posts and comments from Reddit, analyzes their sentiment using Hugging Face transformers, and presents results through a modern web dashboard.
- Reddit Integration: Fetch posts and comments from specific subreddits or search by keywords
- Advanced Sentiment Analysis: Uses DistilBERT model for accurate sentiment classification
- Real-time Dashboard: Interactive charts and tables showing sentiment trends
- Background Processing: Asynchronous analysis of large datasets
- Modern UI: Built with React and TailwindCSS for a beautiful user experience
- Docker Support: Easy deployment with Docker Compose
- API: RESTful API with FastAPI
- Database: SQLite with SQLAlchemy ORM
- Reddit Integration: PRAW (Python Reddit API Wrapper)
- Sentiment Analysis: Hugging Face Transformers (DistilBERT)
- Background Tasks: FastAPI background tasks for async processing
- Framework: React 18 with modern hooks
- Styling: TailwindCSS for responsive design
- Charts: Recharts for data visualization
- HTTP Client: Axios for API communication
- Notifications: React Hot Toast for user feedback
- Docker and Docker Compose
- Reddit API credentials (Client ID and Secret)
- Python 3.11+ (for local development)
- Node.js 18+ (for local development)
git clone <repository-url>
cd reddit_nlp- Go to Reddit App Preferences
- Click "Create App" or "Create Another App"
- Choose "script" as the app type
- Note down your Client ID and Secret
Copy the example environment file and configure your settings:
cp env.example .envEdit .env with your Reddit API credentials:
# Reddit API Configuration
REDDIT_CLIENT_ID=your_reddit_client_id_here
REDDIT_CLIENT_SECRET=your_reddit_client_secret_here
REDDIT_USER_AGENT=RedditSentimentBot/1.0
# Database Configuration
DATABASE_URL=sqlite:///./reddit_sentiment.db
# Redis Configuration (for background tasks)
REDIS_URL=redis://localhost:6379
# Application Configuration
DEBUG=True
SECRET_KEY=your-secret-key-hereStart the application with Docker Compose:
docker-compose up --buildThis will start:
- Backend API: http://localhost:8000
- Frontend Dashboard: http://localhost:3000
- Redis: For background task queue
- Dashboard: http://localhost:3000
- API Documentation: http://localhost:8000/docs
- API Health Check: http://localhost:8000/health
-
Overview Dashboard
- View sentiment statistics
- Interactive pie charts and trend graphs
- Recent posts table with sentiment labels
-
Analysis Page
- Analyze posts from specific subreddits
- Search posts by keywords
- Quick text sentiment analysis
- Background processing for large datasets
POST /api/reddit/fetch-subreddit/{subreddit_name}- Fetch posts from subredditPOST /api/reddit/search-keyword- Search posts by keywordGET /api/reddit/posts- Get analyzed postsGET /api/reddit/comments- Get analyzed comments
GET /api/sentiment/stats- Get sentiment statisticsGET /api/sentiment/trend- Get sentiment trends over timePOST /api/sentiment/analyze-text- Analyze single text
GET /api/dashboard/data- Get comprehensive dashboard dataGET /api/dashboard/subreddits- Get list of analyzed subreddits
cd backend
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
uvicorn main:app --reloadcd frontend
npm install
npm startreddit_nlp/
βββ backend/
β βββ app/
β β βββ models.py # Database models
β β βββ schemas.py # Pydantic schemas
β β βββ database.py # Database configuration
β β βββ routers/ # API route handlers
β β βββ services/ # Business logic
β β βββ utils/ # Utility functions
β βββ main.py # FastAPI application
β βββ requirements.txt # Python dependencies
β βββ Dockerfile
βββ frontend/
β βββ src/
β β βββ components/ # React components
β β βββ pages/ # Page components
β β βββ services/ # API services
β β βββ utils/ # Utility functions
β βββ package.json # Node.js dependencies
β βββ Dockerfile
βββ data/ # Database storage
βββ docker-compose.yml # Docker orchestration
βββ env.example # Environment template
βββ README.md
The application uses DistilBERT-base-uncased-finetuned-sst-2-english from Hugging Face:
- Model: DistilBERT (distilled version of BERT)
- Task: Sentiment analysis (positive/negative/neutral)
- Accuracy: High accuracy on English text
- Performance: Fast inference with good accuracy trade-off
- Positive: Optimistic, happy, or favorable sentiment
- Negative: Pessimistic, sad, or unfavorable sentiment
- Neutral: Objective or balanced sentiment
The application uses FastAPI background tasks for:
- Analyzing large batches of Reddit posts
- Processing comments from multiple posts
- Updating analysis sessions
- Handling long-running sentiment analysis jobs
- backend: FastAPI application
- frontend: React development server
- redis: Background task queue
./data: Persistent database storageredis_data: Redis data persistence
-
Reddit API Rate Limits
- Reddit has rate limits (60 requests per minute)
- The app handles this gracefully with retries
-
Model Loading Issues
- Ensure sufficient memory (2GB+ recommended)
- Model downloads automatically on first run
-
Database Connection
- SQLite database is created automatically
- Check file permissions in
./datadirectory
View application logs:
docker-compose logs -f backend
docker-compose logs -f frontend- Batch Processing: Sentiment analysis is batched for efficiency
- Caching: Redis is used for background task management
- Database Indexing: Optimized queries with proper indexes
- Model Optimization: DistilBERT provides good speed/accuracy balance
- Environment variables for sensitive data
- CORS configuration for frontend-backend communication
- Input validation and sanitization
- SQL injection protection via SQLAlchemy ORM
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Reddit for the API
- Hugging Face for the sentiment analysis model
- FastAPI for the backend framework
- React for the frontend framework
- TailwindCSS for styling
- Recharts for data visualization
For issues and questions:
- Create an issue in the repository
- Check the troubleshooting section
- Review the API documentation at
/docs
Happy Analyzing! π