Skip to content

Instantly share code, notes, and snippets.

@stewartpark stewartpark/chatbot.py
Last active Sep 12, 2019

Embed
What would you like to do?
A very simple/naive chatbot implementation with GPT-2
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
You can’t perform that action at this time.