This repository contains a Streamlit app that predicts the next word in a given sentence fragment using a Long Short-Term Memory (LSTM) neural network trained on Shakespeare's Hamlet from the Gutenberg corpus.
- Interactive Web App: A user-friendly Streamlit app to input a sentence and predict the next word.
- Trained LSTM Model: A deep learning model trained on the Hamlet text to generate contextually relevant predictions.
- Data Processing Notebook: A Jupyter notebook for data preprocessing, tokenization, model training, and evaluation.
Explore the live app here: LSTM Next Word Predictor
LSTM-Next-Word/
│
├── app.py # The Streamlit app script
├── processing.ipynb # Jupyter notebook for data processing and model training
├── requirements.txt # List of dependencies
├── next_word_lstm.h5 # Trained LSTM model
├── tokenizer.pkl # Tokenizer object for word-index mapping
-
Clone the repository:
git clone https://github.com/VanshajR/LSTM-Next-Word.git cd LSTM-Next-Word -
Create and activate a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Run the Streamlit app locally:
streamlit run app.py
The app uses the text of Hamlet by William Shakespeare, sourced from the Gutenberg corpus (nltk.corpus.gutenberg).
-
Data Preprocessing:
- The Hamlet text is tokenized, and sequences of words are generated.
- Input sequences are padded to uniform lengths, and labels are one-hot encoded.
-
Model Architecture:
- An embedding layer maps words to dense vectors.
- Two LSTM layers capture temporal dependencies in the text.
- A dense softmax layer outputs the probabilities of the next word.
-
Streamlit App:
- Users input a sentence fragment.
- The app tokenizes the input, pads it, and uses the LSTM model to predict the next word.
Input: "To be or not to be"
Output: "buried"Model training is detailed in processing.ipynb. It includes:
- Splitting data into training and test sets.
- Training the LSTM model for 80 epochs.
- Saving the trained model and tokenizer.
The project uses the following Python libraries:
tensorflownumpypandasscikit-learnnltkstreamlit
All dependencies are listed in requirements.txt.
This project is licensed under the MIT License. See the LICENSE file for details.