A real-time Twitter sentiment analysis web application built with FastAPI, WebSockets, and modern web technologies. Search for tweets, analyze sentiment in real-time, and visualize results with interactive charts and word clouds.
- Search tweets by keywords, hashtags, or phrases
- Real-time sentiment analysis using TextBlob and Transformers
- Support for multiple languages (English, Spanish, French, German)
- Configurable result limits (20-100 tweets)
- Pie Charts - Sentiment distribution breakdown
- Timeline Charts - Sentiment trends over time
- Word Clouds - Most frequent words visualization
- Live Statistics - Real-time sentiment counters
- WebSocket Streaming - Live tweet updates
- Auto-refresh - Continuous sentiment monitoring
- Live Dashboard - Real-time connection status
- Background Processing - Non-blocking data analysis
- Responsive Bootstrap 5 design
- Dark/Light mode support
- Mobile-friendly interface
- Smooth animations and transitions
- Toast notifications and loading states
- Python 3.9 or higher
- Twitter Developer Account with Bearer Token
- Conda or pip package manager
git clone https://github.com/BukuBukuChagma/CodeAlpha_Sentiment_Analysis_On_Twitter.git
cd CodeAlpha_Sentiment_Analysis_On_Twitter# Create conda environment
conda create -n twitter-sentiment python=3.11 -y
conda activate twitter-sentiment
# Or use pip venv
python -m venv venv
source venv/bin/activate # Linux/Mac
# venv\Scripts\activate # Windowspip install -r requirements.txtCreate and .env file in the backend directory and add your twitter bearer token there.
# Edit .env file and add your Twitter Bearer Token
TWITTER_BEARER_TOKEN=your_actual_bearer_token_herecd backend
uvicorn main:app --reload --host 0.0.0.0 --port 8000- Main App: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Alternative API Docs: http://localhost:8000/redoc
- Enter keywords or hashtags in the search box
- Select number of results and language
- Click "Search Tweets"
- View results with automatic sentiment analysis
- Enter your search query
- Click "Start Stream" to begin live monitoring
- Watch real-time tweets appear with sentiment analysis
- Click "Stop Stream" to end monitoring
- Auto-generated: Pie chart appears after search
- Manual Generation: Click visualization buttons
- Word Cloud: Generate from current tweet data
- Timeline: View sentiment trends over time
- Click on any tweet text to analyze individual sentiment
- Use the modal for custom text analysis
- View detailed sentiment scores and confidence levels
POST /api/search- Search tweets with sentiment analysisGET /api/tweets- Retrieve stored tweets with filteringDELETE /api/tweets- Clear all stored dataGET /api/stats- Get sentiment statistics
POST /api/analyze- Analyze custom text sentimentGET /api/distribution- Get sentiment distribution dataGET /api/timeline- Get sentiment timeline data
GET /api/wordcloud- Generate word cloud imageGET /api/chart/distribution- Generate pie chartGET /api/chart/timeline- Generate timeline chart
WebSocket /ws/stream- Real-time tweet streamingGET /ws/status- WebSocket connection status
# Twitter API
TWITTER_BEARER_TOKEN=your_bearer_token
# Database
DATABASE_URL=sqlite:///./twitter_sentiment.db
# Sentiment Analysis
SENTIMENT_MODEL=textblob # or "transformers"
# API Limits
MAX_TWEETS_PER_SEARCH=50
DEFAULT_LANGUAGE=en
REQUESTS_PER_MINUTE=100- Visit Twitter Developer Portal
- Create a new app or use existing one
- Generate Bearer Token from "Keys and tokens"
- Add token to
.envfile
twitter-sentiment-analysis/
โโโ backend/
| โโโ .env # Environment variables
โ โโโ main.py # FastAPI application entry point
โ โโโ database.py # Database configuration
โ โโโ models.py # SQLAlchemy models
โ โโโ twitter_client.py # Twitter API integration
โ โโโ sentiment_analyzer.py # Sentiment analysis logic
โ โโโ routes/
โ โโโ tweets.py # Tweet-related endpoints
โ โโโ sentiment.py # Sentiment analysis endpoints
โ โโโ websocket.py # WebSocket streaming
โโโ static/
โ โโโ css/style.css # Custom styling
โ โโโ js/app.js # Frontend JavaScript
โโโ templates/
โ โโโ index.html # Main web interface
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
- FastAPI - Modern Python web framework
- SQLAlchemy - Database ORM
- SQLite - Lightweight database
- Tweepy - Twitter API client
- TextBlob - Natural language processing
- Transformers - Advanced NLP models
- Matplotlib - Chart generation
- WordCloud - Word cloud visualization
- HTML5/CSS3 - Modern web standards
- Bootstrap 5 - Responsive UI framework
- JavaScript ES6+ - Interactive functionality
- WebSockets - Real-time communication
- Font Awesome - Icon library
- TextBlob (Default) - Fast, lightweight analysis
- Transformers - Advanced transformer models
- Custom Models - Extensible for custom implementations
WebSocket Connection Failed
# Check if server is running on correct port
curl http://localhost:8000/healthTwitter API Errors
- Verify Bearer Token is correct
- Check API rate limits
- Ensure proper Twitter API access level
Dependencies Issues
# Update pip and reinstall
pip install --upgrade pip
pip install -r requirements.txt --force-reinstallDatabase Issues
# Reset database
rm backend/twitter_sentiment.db
# Restart application to recreate tables- Use TextBlob for faster sentiment analysis
- Limit tweet search results for better performance
- Enable caching for repeated queries
- Use background tasks for data processing
This project is licensed under the MIT License - see the LICENSE file for details.
- Twitter API v2 for tweet data
- FastAPI for the amazing web framework
- TextBlob for sentiment analysis
- Bootstrap for UI components
โญ Star this repository if you found it helpful!
