This project performs sentiment analysis on the IMDB movie reviews dataset using Recurrent Neural Networks (RNN) and Long Short-Term Memory (LSTM) networks. It includes a production-ready Flask web application with a TailwindCSS frontend to predict whether movie reviews are positive or negative, complete with a user-friendly interface and search history feature.
Given a movie review (text), predict whether the review is positive or negative. This is a binary classification problem in Natural Language Processing (NLP).
Dataset Link: IMDB Dataset of 50K Movie Reviews
Kaggle Notebook Link: Movie Sentiment Analysis with LSTM-RNN
- Model: Pre-trained LSTM model for accurate sentiment prediction on movie reviews.
- API: Flask-based RESTful API to serve predictions, supporting single and batch review inputs.
- Frontend: Responsive web interface built with TailwindCSS and Chart.js, featuring:
- Input textarea for single or multiple reviews.
- Sentiment results with confidence scores and color-coded badges (green for positive, red for negative).
- Pie chart for batch review sentiment distribution.
- Search history to track all predictions within a session, with a clear button (resets on refresh).
- Tech Stack:
- Backend: Flask, TensorFlow, NLTK, NumPy
- Frontend: HTML, TailwindCSS, Chart.js, JavaScript
- Raw text is converted into numeric vectors using word embeddings.
- Embeddings map words into a continuous vector space, preserving semantic relationships.
- Example: king → [0.25, -0.63, 0.41, ...]
- Words with similar meanings have closer embeddings.
- Implemented using Keras
Embeddinglayer.
- RNNs process sequential data like text, capturing dependencies between words.
- Example: In "The movie was not good," the word "not" influences the sentiment.
- Limitation: Suffers from vanishing gradients, struggling with long-range dependencies.
- An advanced RNN variant with gates (input, forget, output) to control memory flow.
- Benefits:
- Remembers important context over long sequences.
- Ignores irrelevant words.
- Mitigates vanishing gradient issues.
- For sentiment analysis, LSTM captures context like "not" affecting "good" in long reviews.
- Flask API: Serves the pre-trained LSTM model, handling text preprocessing and predictions.
- Frontend: Responsive UI with TailwindCSS for styling, Chart.js for visualizations, and JavaScript for interactivity.
- History: Client-side storage of search history (resets on refresh) with a clear button.
-
Data Preparation
- Loaded IMDB dataset (50,000 reviews).
- Tokenized text and padded sequences to a fixed length (maxlen=250).
- Split into train/test sets.
-
Model Building
- Built RNN and LSTM models with an Embedding layer.
- Used early stopping to prevent overfitting.
- Saved the trained LSTM model (
model.h5) and tokenizer (tokenizer.pickle).
-
API Development
- Created Flask endpoints:
/: Serves the main web interface./predict: Processes single or batch reviews, returns sentiment and confidence scores.
- Preprocessing includes tokenization, stopword removal, stemming, and padding.
- Created Flask endpoints:
-
Frontend Development
- Designed a responsive UI with TailwindCSS.
- Added interactive elements: input form, results display, batch summary chart, and search history.
- Styled with Poppins font and vibrant colors (indigo, green, red).
-
Training
- Optimizer: Adam
- Loss: Binary Crossentropy
- Metrics: Accuracy
Simple RNN:
model = Sequential()
model.add(Embedding(10000, 128))
model.add(SimpleRNN(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))LSTM (Improved):
model = Sequential()
model.add(Embedding(10000, 128))
model.add(LSTM(128))
model.add(Dense(1, activation='sigmoid'))-
Simple RNN:
- Test Accuracy: 0.86
- Test Loss: 0.32
- Struggled with long reviews due to vanishing gradients.
-
LSTM:
- Test Accuracy: 0.88
- Test Loss: 0.28
- Improved performance by capturing long-term dependencies.
-
Web App:
- Successfully predicts sentiments for single or batch reviews.
- Example output:
Review: "This movie was amazing with great acting and story!" Sentiment: Positive, Confidence: 0.87 Review: "The moview is utter shit. It is not upto the mark." Sentiment: Negative, Confidence: 0.12
- Python: 3.8 or higher
- Files:
app.py: Flask applicationtemplates/index.html: Frontend templatelstm_model.h5: Pre-trained LSTM modeltokenizer.pickle: Saved tokenizer
- Internet: Required for TailwindCSS and Chart.js CDNs (or host locally for offline use)
project_directory/
├── app.py
├── models/
└── lstm_model.h5
└── tokenizer.pickle
├── templates/
│ └── index.html
If lstm_model.h5 is missing do run the Notebook and save the model
-
Clone the Repository:
git clone https://github.com/BenGJ10/Movie-Sentiment-Analysis.git cd Movie-Sentiment-Analysis -
Create a Virtual Environment:
python -m venv venv
Activate it:
- Windows:
venv\Scripts\activate
- macOS/Linux:
source venv/bin/activate
- Windows:
-
Install Dependencies:
pip install -r requirements.txt
-
Run the App:
python app.py
- Access at
http://127.0.0.1:5000in your browser. - The app will load the LSTM model and tokenizer at startup.
- Access at
-
Usage:
- Enter one or multiple reviews (one per line) in the textarea.
- Click
Analyze Sentimentto see results with confidence scores. - View batch sentiment distribution (pie chart) for multiple reviews.
- Check the
Search Historysection for past searches. - Click
Clear Historyto reset results and history (resets automatically on refresh).
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Developed by Ben Gregory John

