Skip to content

Sehrish0508/Sentiment-Analysis-RNN

Repository files navigation

IMDB Movie Review Sentiment Analyzer (RNN)

Live demo: https://sehrish-sentiment-analyzer.streamlit.app/

A web UI where anyone can type a movie review and get a Positive / Negative sentiment prediction from a PyTorch RNN trained on the IMDB Dataset (50,000 reviews).

How it works

raw review text
      │
      ▼
clean_text()  (preprocessing.py)      lowercase → strip URLs/HTML/punctuation → stem
      │
      ▼
TF-IDF vectorizer (tfidf_vectorizer.pkl)   text → 5,000-dim numeric vector
      │
      ▼
RNN model (model.py + sentiment_rnn.pth)   vector → probability (sigmoid)
      │
      ▼
"Positive" if prob > 0.5 else "Negative"

Project structure

Sentiment-Analysis-RNN/
├── app.py                          # Streamlit UI (entry point)
├── model.py                        # RNN class definition (must match training)
├── preprocessing.py                # Text-cleaning pipeline used at inference time
├── requirements.txt
├── README.md
├── RNN_SentimentAnalysis_with_save.ipynb   # training notebook
├── sentiment_rnn.pth                # trained model weights
├── tfidf_vectorizer.pkl              # fitted TF-IDF vectorizer
└── model_config.json                 # model architecture config

sentiment_rnn.pth, tfidf_vectorizer.pkl, and model_config.json are already included in this repo, so the app runs immediately after cloning — no retraining needed. They're only regenerated if you open RNN_SentimentAnalysis_with_save.ipynb and run all cells yourself (e.g. to retrain on updated data or a different architecture).

Why 3 separate files instead of one .pkl?

A trained PyTorch neural network is different from a scikit-learn model. scikit-learn models are usually fine to pickle whole (joblib.dump(model, "model.pkl")) because they're plain Python objects. PyTorch models are not saved that way by convention — instead you save:

File What it is Why
sentiment_rnn.pth The model's learned weights only (state_dict) More portable and stable than pickling the whole object; recommended by PyTorch docs
model_config.json The architecture (input_size, hidden_size, num_layers) You need to rebuild the empty RNN() class before you can load weights into it
tfidf_vectorizer.pkl The fitted TF-IDF vectorizer (scikit-learn object) This one genuinely is a normal .pkl — it turns new raw text into the same numeric format the model was trained on

All three are required together. The .pth weights alone can't process raw text — only the vectorizer knows the vocabulary/feature mapping the model was trained on.

Setup

  1. Clone this repository

    git clone https://github.com/Sehrish0508/Sentiment-Analysis-RNN.git
    cd Sentiment-Analysis-RNN

    The trained model files (sentiment_rnn.pth, tfidf_vectorizer.pkl, model_config.json) are already included, so no training step is required to run the app.

  2. Install dependencies (Python 3.10+ recommended)

    python -m venv venv
    source venv/bin/activate        # Windows: venv\Scripts\activate
    pip install -r requirements.txt
  3. Run the app

    streamlit run app.py

    Streamlit will open the app in your browser at http://localhost:8501.

Model details

  • Architecture: single-layer RNN (hidden size 128) → fully connected layer → sigmoid
  • Input features: TF-IDF, top 5,000 terms
  • Training data: IMDB Dataset, 49,582 reviews after de-duplication (80/20 train/test split)
  • Test accuracy: ~87.3%

Known limitation worth knowing about

The TF-IDF vector for each review is fed to the RNN as a single timestep (x.unsqueeze(1)), not as a true sequence of word-by-word steps. This means the model isn't using the RNN's sequential/recurrent capability the way a word-embedding + sequence-of-tokens setup would — it's closer to a single feedforward pass through a recurrent cell. It still works reasonably well here (~87% accuracy) because TF-IDF already captures a lot of signal, but if you want to see the RNN's recurrence actually add value, a natural next step is to switch to a tokenizer + embedding layer + padded token sequences (and consider LSTM/GRU, which the notebook already flags as a fix for vanishing gradients).

Dataset

This project uses the IMDB Dataset of 50K Movie Reviews. The CSV is not included in this repo due to its size (~66 MB) — download it separately from the link above and place it alongside the notebook if you want to retrain the model.

Deploying / sharing

This app is deployed on Streamlit Community Cloud: https://sehrish-sentiment-analyzer.streamlit.app/

To deploy your own copy:

  1. Push this repo to GitHub (with sentiment_rnn.pth, tfidf_vectorizer.pkl, and model_config.json included — they're required at runtime and are small enough to commit directly).
  2. Go to Streamlit Community Cloud, sign in with GitHub, and create a new app pointing at this repository and app.py.

License / Data

The IMDB Dataset is provided for research/educational use; check the dataset's original license terms before any commercial use.

About

IMDB movie review sentiment analyzer using a PyTorch RNN + Streamlit

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors