# shivammitra/lstm-and-gru-predicting-next-word architecture

> Architecture diagram of the GitHub repository shivammitra/lstm-and-gru-predicting-next-word, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shivammitra/lstm-and-gru-predicting-next-word
- Repository: https://github.com/shivammitra/lstm-and-gru-predicting-next-word

## Overview

#### Purpose and actual repository shape
This is a compact Python NLP experiment for word-level next-word prediction using recurrent neural networks. The checked-in tree differs from the README’s proposed notebook/module layout: the primary executable artifact is `app.py`, with one experimentation notebook, a Hamlet corpus, and a serialized tokenizer.

Paths: `app.py`, `experiemnts.ipynb`, `hamlet.txt`

#### Entry points and execution modes
- `app.py` is the application entry point and likely serves inference or an interactive prediction workflow using the trained/tokenized language-model assets.
- `experiemnts.ipynb` is the exploratory/training entry point, intended for iterative data preparation, LSTM/GRU experimentation, evaluation, and model comparison.
- Jupyter is a material runtime for reproducing experiments; Python dependencies are defined in `requirements.txt`.

Paths: `app.py`, `experiemnts.ipynb`, `requirements.txt`

#### Corpus ingestion and text preprocessing
The model’s source corpus is Shakespeare’s Hamlet in `hamlet.txt`. The preprocessing stage normalizes/tokenizes corpus text and transforms it into word-index sequences suitable for supervised language-model training. The persisted tokenizer defines the vocabulary-to-index contract and must remain compatible with any model consumed by the app.

Paths: `hamlet.txt`, `tokenizer.pickle`, `experiemnts.ipynb`

#### Sequence-generation boundary
Training converts tokenized text into fixed-context input sequences and a target next-word label. This is the key boundary between raw NLP data and numeric ML tensors: sequence length, vocabulary size, padding/encoding strategy, and label representation must be identical at training and prediction time.

Paths: `experiemnts.ipynb`, `tokenizer.pickle`

#### Recurrent modeling and comparison
The architecture centers on alternative recurrent language models: LSTM and GRU. Both consume the same encoded word-context inputs and produce a vocabulary-sized next-token prediction distribution. The notebook is the likely location for selecting embedding dimensions, recurrent units, optimization, epochs, and comparing loss/accuracy or qualitative generated outputs.

Paths: `experiemnts.ipynb`, `README.md`

#### Inference flow
Inference accepts prompt text, loads or uses `tokenizer.pickle`, converts the prompt to the trained index space, takes the relevant context window, obtains next-word scores from the model, and maps the selected index back to a word. The tokenizer is the essential persisted cross-process artifact; model artifact location is not present in the supplied tree and should be verified before deployment.

Paths: `app.py`, `tokenizer.pickle`

#### Runtime and external dependencies
The project runs locally in Python, with TensorFlow/Keras as the expected deep-learning runtime and Jupyter for experimentation. No deployed infrastructure, database, API, queue, or third-party online service is evidenced. Corpus and tokenizer are local files; model persistence/loading is an implicit requirement of `app.py` but no checked-in model file is shown.

Paths: `requirements.txt`, `app.py`, `hamlet.txt`

Main entry points: [`experiemnts.ipynb`](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb) (Text preprocessing), [`tokenizer.pickle`](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/tokenizer.pickle) (Persisted tokenizer), [`app.py`](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/app.py) (Prompt encoding), [`requirements.txt`](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/requirements.txt) (Python dependencies)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experiment["Training &amp; experiments"]
  node_corpus["Hamlet corpus<br/>text corpus<br/>[hamlet.txt]"]
  node_notebook["Experiment notebook<br/>Jupyter entry point<br/>[experiemnts.ipynb]"]
  node_preprocessing["Text preprocessing<br/>NLP stage<br/>[experiemnts.ipynb]"]
  node_sequence_builder["Sequence generation<br/>supervised-data stage<br/>[experiemnts.ipynb]"]
  node_encoded_tensors["Encoded sequences<br/>ML tensors<br/>[experiemnts.ipynb]"]
  node_lstm["LSTM model<br/>recurrent language model<br/>[experiemnts.ipynb]"]
  node_gru["GRU model<br/>recurrent language model<br/>[experiemnts.ipynb]"]
  node_evaluation["Model evaluation<br/>comparison stage<br/>[experiemnts.ipynb]"]
end

subgraph group_inference["Local inference"]
  node_app["Prediction app<br/>Python entry point<br/>[app.py]"]
  node_prompt_encoding["Prompt encoding<br/>inference stage<br/>[app.py]"]
  node_next_word["Next-word result<br/>prediction output<br/>[app.py]"]
end

node_deps["Python dependencies<br/>requirements<br/>[requirements.txt]"]
node_tokenizer["Persisted tokenizer<br/>vocabulary artifact<br/>[tokenizer.pickle]"]
node_model_artifact["Trained model artifact<br/>model persistence"]

node_deps -.->|"supports"| node_notebook
node_deps -.->|"supports"| node_app
node_corpus -->|"source text"| node_preprocessing
node_notebook -->|"runs"| node_preprocessing
node_preprocessing -->|"fits and persists"| node_tokenizer
node_preprocessing -->|"token indices"| node_sequence_builder
node_sequence_builder -->|"contexts and labels"| node_encoded_tensors
node_encoded_tensors -->|"trains"| node_lstm
node_encoded_tensors -->|"trains"| node_gru
node_lstm -->|"metrics and samples"| node_evaluation
node_gru -->|"metrics and samples"| node_evaluation
node_lstm -.->|"selected model saved"| node_model_artifact
node_gru -.->|"selected model saved"| node_model_artifact
node_app -->|"accepts prompt"| node_prompt_encoding
node_tokenizer -->|"encodes words"| node_prompt_encoding
node_prompt_encoding -->|"context indices"| node_model_artifact
node_model_artifact -->|"next-token scores"| node_next_word
node_tokenizer -->|"decodes index"| node_next_word

click node_deps "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/requirements.txt"
click node_corpus "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/hamlet.txt"
click node_notebook "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb"
click node_preprocessing "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb"
click node_sequence_builder "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb"
click node_encoded_tensors "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb"
click node_lstm "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb"
click node_gru "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb"
click node_evaluation "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb"
click node_tokenizer "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/tokenizer.pickle"
click node_app "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/app.py"
click node_prompt_encoding "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/app.py"
click node_next_word "https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/app.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_corpus,node_notebook,node_preprocessing,node_sequence_builder,node_encoded_tensors,node_lstm,node_gru,node_evaluation toneBlue
class node_app,node_prompt_encoding,node_next_word toneAmber
class node_deps,node_tokenizer,node_model_artifact toneNeutral
```

## Components

### Training & experiments

Notebook-driven corpus preparation, model comparison, and artifact generation.

- [Hamlet corpus](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/hamlet.txt): text corpus · Local Shakespeare source text for language-model training. · `hamlet.txt`
- [Experiment notebook](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb): Jupyter entry point · Prepares data, trains recurrent models, and evaluates alternatives. · `experiemnts.ipynb`
- [Text preprocessing](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb): NLP stage · Normalizes corpus text and converts words to indices. · `experiemnts.ipynb`
- [Sequence generation](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb): supervised-data stage · Builds fixed-context inputs and next-word labels. · `experiemnts.ipynb`
- [Encoded sequences](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb): ML tensors · Context and label representation shared by recurrent models. · `experiemnts.ipynb`
- [LSTM model](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb): recurrent language model · Predicts a vocabulary distribution from word context. · `experiemnts.ipynb`
- [GRU model](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb): recurrent language model · Alternative recurrent model evaluated on the same sequences. · `experiemnts.ipynb`
- [Model evaluation](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/experiemnts.ipynb): comparison stage · Compares loss, accuracy, and generated output quality. · `experiemnts.ipynb`

### Local inference

Python application predicts the next word from a prompt.

- [Prediction app](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/app.py): Python entry point · Loads language-model assets and runs local next-word prediction. · `app.py`
- [Prompt encoding](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/app.py): inference stage · Tokenizes the prompt and selects the trained context window. · `app.py`
- [Next-word result](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/app.py): prediction output · Maps selected token index back to a word. · `app.py`

### Other components

- [Python dependencies](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/requirements.txt): requirements · Defines local Python, TensorFlow/Keras, and Jupyter dependencies. · `requirements.txt`
- [Persisted tokenizer](https://github.com/shivammitra/lstm-and-gru-predicting-next-word/blob/main/tokenizer.pickle): vocabulary artifact · Vocabulary-to-index contract required by training and inference. · `tokenizer.pickle`
- **Trained model artifact**: model persistence · Required by inference, but no checked-in model location is evidenced.

## Connections

- Python dependencies → Experiment notebook: supports
- Python dependencies → Prediction app: supports
- Hamlet corpus → Text preprocessing: source text
- Experiment notebook → Text preprocessing: runs
- Text preprocessing → Persisted tokenizer: fits and persists
- Text preprocessing → Sequence generation: token indices
- Sequence generation → Encoded sequences: contexts and labels
- Encoded sequences → LSTM model: trains
- Encoded sequences → GRU model: trains
- LSTM model → Model evaluation: metrics and samples
- GRU model → Model evaluation: metrics and samples
- LSTM model → Trained model artifact: selected model saved
- GRU model → Trained model artifact: selected model saved
- Prediction app → Prompt encoding: accepts prompt
- Persisted tokenizer → Prompt encoding: encodes words
- Prompt encoding → Trained model artifact: context indices
- Trained model artifact → Next-word result: next-token scores
- Persisted tokenizer → Next-word result: decodes index
