A Deep Learning project that predicts the next word in a sequence of text, trained on Shakespeare's "Hamlet". This project utilizes Long Short-Term Memory (LSTM) networks for sequence modeling and is deployed as an interactive web application using Streamlit.
The goal of this project is to develop a language model capable of understanding context and generating text in the style of Shakespeare. By analyzing sequences of words from Hamlet, the model learns to predict the most probable next word, demonstrating the power of RNNs in Natural Language Processing (NLP).
LIVE DEMO - https://nextword-predictionlstm-p62xzk3lsy7rv3lhkfalak.streamlit.app/
- Deep Learning Model: Stacked LSTM architecture (Embedding -> LSTM -> Dropout -> Dense).
- Dataset: Complete text of Shakespeare's Hamlet.
- Visualization: TensorBoard integration for tracking training loss and accuracy.
- Web App: Interactive UI built with Streamlit for real-time predictions.
- Early Stopping: Implemented to prevent overfitting during training.
Here is the organization of the repository:
| File Name | Description |
|---|---|
app.py |
The main Streamlit application file for deployment. |
experiemnt s.ipynb |
Jupyter Notebook containing data preprocessing, model training, and evaluation code. |
hamlet.txt |
The raw text data source used for training. |
next_word_lstm.h5 |
The trained Keras model saved in HDF5 format. |
tokenizer.pickle |
The tokenizer object used to convert text to sequences (saved for consistent inference). |
requiremen ts.txt |
List of Python dependencies required to run the project. |
-
Clone the repository:
git clone [https://github.com/your-username/next-word-prediction-lstm.git](https://github.com/your-username/next-word-prediction-lstm.git) cd next-word-prediction-lstm -
Create a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install -r "requirements.txt"(Note: Ensure the filename matches exactly as it appears in the repo)
To launch the interactive Next Word Predictor:
streamlit run app.py
This will open the application in your default web browser (usually at http://localhost:8501).
If you want to retrain the model or experiment with hyperparameters:
- Open
experiemnt s.ipynbin Jupyter Notebook or Google Colab. - Run the cells sequentially to preprocess data, train the LSTM, and save the new model files.
- Monitor training progress using TensorBoard (if configured in the notebook).
The model is built using TensorFlow/Keras with the following layers:
- Embedding Layer: Converts word indices into dense vectors of fixed size.
- LSTM Layer 1: 150 units, returns sequences.
- Dropout Layer: 20% dropout to prevent overfitting.
- LSTM Layer 2: 100 units.
- Dense Output Layer: Softmax activation to predict the probability of the next word among all vocabulary words.