Skip to content

Commit b4988ee

Browse files
committed
add python code generator in machine-learning/nlp/text-generator
1 parent d8c41dd commit b4988ee

File tree

5 files changed

+78
-56
lines changed

5 files changed

+78
-56
lines changed
+28-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
# [How to Build a Text Generator using Keras in Python](https://www.thepythoncode.com/article/text-generation-keras-python)
22
To run this:
33
- `pip3 install -r requirements.txt`
4-
- To use pre-trained text generator model that was trained on Alice's wonderland text book:
4+
- To use pre-trained text generator model that was trained on Alice's wonderland text book or Python Code:
55
```
66
python generate.py --help
77
```
8-
**Output:**
8+
This will prompt you with the choice, seed and number of characters you want!
9+
Here is an example on Alice's wonderland with 200 characters:
910
```
10-
usage: generate.py [-h] [-n N_CHARS] seed
11-
12-
Text generator that was trained on Alice's Adventures in the Wonderland book.
13-
14-
positional arguments:
15-
seed Seed text to start with, can be any english text, but
16-
it's preferable you take from the book itself.
17-
18-
optional arguments:
19-
-h, --help show this help message and exit
20-
-n N_CHARS, --n-chars N_CHARS
21-
Number of characters to generate, default is 200.
22-
```
23-
Generating 200 characters with that seed:
24-
```
25-
python generate.py "down down down there was nothing else to do so alice soon began talking again " -n 200
11+
Generated text:
12+
the duchess asked to the dormouse she wanted about for the world all her life i dont know what to think that it was so much sort of mine for the world a little like a stalking and was going to the mou
2613
```
27-
**Output:**
14+
Another example:
2815
```
29-
Generating text: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 200/200 [00:40<00:00, 5.02it/s]
16+
Please choose which model you want to generate text with:
17+
1 - Alice's wonderland
18+
2 - Python Code
19+
2
20+
Enter the seed, enter q to quit, maximum 100 characters:
21+
import os
22+
import sys
23+
import subprocess
24+
import numpy as np
25+
q
26+
Enter number of characters you want to generate: 200
27+
Generating text: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 200/200 [00:44<00:00, 4.68it/s]
3028
Generated text:
31-
the duchess asked to the dormouse she wanted about for the world all her life i dont know what to think that it was so much sort of mine for the world a little like a stalking and was going to the mou
29+
import pandas as pd
30+
import os
31+
import numpy as np
32+
import pandas as pd
33+
import matplotlib.pyplot as plt
34+
import numpy as np
35+
import tensorflow as tf
36+
37+
config = tf.configproto(intra_op_parallelism_threads=n
38+
3239
```
Binary file not shown.
Binary file not shown.

machine-learning/nlp/text-generator/generate.py

+50-35
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@
55
from keras.layers import Dense, LSTM, Dropout, Activation
66
from keras.callbacks import ModelCheckpoint
77

8-
# seed = "do not try to"
98

10-
char2int = pickle.load(open("data/wonderland-char2int.pickle", "rb"))
11-
int2char = pickle.load(open("data/wonderland-int2char.pickle", "rb"))
9+
10+
message = """
11+
Please choose which model you want to generate text with:
12+
1 - Alice's wonderland
13+
2 - Python Code
14+
"""
15+
choice = int(input(message))
16+
assert choice == 1 or choice == 2
17+
18+
if choice == 1:
19+
char2int = pickle.load(open("data/wonderland-char2int.pickle", "rb"))
20+
int2char = pickle.load(open("data/wonderland-int2char.pickle", "rb"))
21+
elif choice == 2:
22+
char2int = pickle.load(open("data/python-char2int.pickle", "rb"))
23+
int2char = pickle.load(open("data/python-int2char.pickle", "rb"))
1224

1325
sequence_length = 100
1426
n_unique_chars = len(char2int)
@@ -21,35 +33,38 @@
2133
Dense(n_unique_chars, activation="softmax"),
2234
])
2335

24-
model.load_weights("results/wonderland-v2-0.75.h5")
25-
26-
if __name__ == "__main__":
27-
import argparse
28-
parser = argparse.ArgumentParser(description="Text generator that was trained on Alice's Adventures in the Wonderland book.")
29-
parser.add_argument("seed", help="Seed text to start with, can be any english text, but it's preferable you take from the book itself.")
30-
parser.add_argument("-n", "--n-chars", type=int, dest="n_chars", help="Number of characters to generate, default is 200.", default=200)
31-
args = parser.parse_args()
32-
33-
n_chars = args.n_chars
34-
seed = args.seed
35-
36-
# generate 400 characters
37-
generated = ""
38-
for i in tqdm.tqdm(range(n_chars), "Generating text"):
39-
# make the input sequence
40-
X = np.zeros((1, sequence_length, n_unique_chars))
41-
for t, char in enumerate(seed):
42-
X[0, (sequence_length - len(seed)) + t, char2int[char]] = 1
43-
# predict the next character
44-
predicted = model.predict(X, verbose=0)[0]
45-
# converting the vector to an integer
46-
next_index = np.argmax(predicted)
47-
# converting the integer to a character
48-
next_char = int2char[next_index]
49-
# add the character to results
50-
generated += next_char
51-
# shift seed and the predicted character
52-
seed = seed[1:] + next_char
53-
54-
print("Generated text:")
55-
print(generated)
36+
if choice == 1:
37+
model.load_weights("results/wonderland-v2-0.75.h5")
38+
elif choice == 2:
39+
model.load_weights("results/python-v2-0.30.h5")
40+
41+
seed = ""
42+
print("Enter the seed, enter q to quit, maximum 100 characters:")
43+
while True:
44+
result = input("")
45+
if result.lower() == "q":
46+
break
47+
seed += f"{result}\n"
48+
seed = seed.lower()
49+
n_chars = int(input("Enter number of characters you want to generate: "))
50+
51+
# generate 400 characters
52+
generated = ""
53+
for i in tqdm.tqdm(range(n_chars), "Generating text"):
54+
# make the input sequence
55+
X = np.zeros((1, sequence_length, n_unique_chars))
56+
for t, char in enumerate(seed):
57+
X[0, (sequence_length - len(seed)) + t, char2int[char]] = 1
58+
# predict the next character
59+
predicted = model.predict(X, verbose=0)[0]
60+
# converting the vector to an integer
61+
next_index = np.argmax(predicted)
62+
# converting the integer to a character
63+
next_char = int2char[next_index]
64+
# add the character to results
65+
generated += next_char
66+
# shift seed and the predicted character
67+
seed = seed[1:] + next_char
68+
69+
print("Generated text:")
70+
print(generated)
Binary file not shown.

0 commit comments

Comments
 (0)