A small Keras LSTM text generator trained on public-domain romantic literature.
1import tensorflow as tf
2model = tf.keras.models.load_model("romantic_gpt.keras")
1pip install tensorflow numpy
2python generate.py --prompt "she looked into his eyes" --words 50 --temperature 0.8
1# 1. Download public-domain texts into data/
2cd data
3curl -o pride_and_prejudice.txt https://www.gutenberg.org/cache/epub/1342/pg1342.txt
4curl -o sense_and_sensibility.txt https://www.gutenberg.org/cache/epub/161/pg161.txt
5curl -o jane_eyre.txt https://www.gutenberg.org/cache/epub/1260/pg1260.txt
6cd ..
7
8# 2. Train
9python train.py
10
11# 3. Generate
12python generate.py --prompt "her heart beat faster"
1import json
2import tensorflow as tf
3import numpy as np
4from tensorflow.keras.preprocessing.sequence import pad_sequences
5
6model = tf.keras.models.load_model("romantic_gpt.keras")
7
8with open("tokenizer_config.json") as f:
9 config = json.load(f)
10
11word_index = config["word_index"]
12index_word = config["index_word"]
13seq_length = config["max_seq_length"]
14
15prompt = "she looked into his eyes"
16words = prompt.lower().split()
17
18for _ in range(50):
19 token_ids = [word_index.get(w, 1) for w in words]
20 padded = pad_sequences([token_ids], maxlen=seq_length, padding="pre")
21 probs = model.predict(padded, verbose=0)[0]
22 next_id = int(np.argmax(probs))
23 next_word = index_word.get(str(next_id), "")
24 if next_word:
25 words.append(next_word)
26
27print(" ".join(words))
All training texts are sourced from
Project Gutenberg
and are in the
public domain in the United States. See
data/README.md for
download instructions and recommended titles.
The code in this repository is provided as-is for educational purposes.
Training data is public domain (Project Gutenberg).