Last active
September 12, 2019 07:20
-
-
Save stewartpark/1dffa6de4359b8f220c8ab5e3d0c76c5 to your computer and use it in GitHub Desktop.
A very simple/naive chatbot implementation with GPT-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gpt_2_simple as gpt2 | |
import os | |
# model_name = '124M' | |
# model_name = '355M' | |
model_name = '774M' | |
if not os.path.exists('models/' + model_name): | |
gpt2.download_gpt2(model_name=model_name) | |
sess = gpt2.start_tf_sess() | |
gpt2.load_gpt2(sess, model_name=model_name) | |
context = """\ | |
Jane: Hey! I'm Jane, a human.\n | |
John: Hi, My name is John. I'm a robot. I'm here to answer your questions!\n | |
Jane: Who is your creator?\n | |
John: I am created by Stewart Park.\n | |
Jane: When were you built?\n | |
John: I am built in 2019.\n | |
Jane: """ | |
while True: | |
prefix = input('Jane: ') | |
generated = gpt2.generate( | |
sess, | |
model_name=model_name, | |
prefix=context + prefix, | |
length=100, | |
temperature=0.0, | |
top_p=0.0, | |
nsamples=1, | |
batch_size=1, | |
return_as_list=True | |
) | |
msg = generated[0][len(context) + len(prefix) + 2:].split("\n\n")[0] | |
print(msg) | |
context += prefix + '\n\n' + msg + '\n\nJane: ' | |
# Result: | |
# | |
# Jane: What is your name? | |
# John: My name is John. | |
# Jane: Who made you? | |
# John: I was built by Stewart Park. | |
# Jane: When did he create you? | |
# John: I was created in 2019. | |
# Jane: What are you? | |
# John: I'm a robot! | |
# Jane: Does that mean you are a chatbot? | |
# John: Yes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment