<jupyter_start><jupyter_text>GPT Adventure*By <NAME>, based on a tutorial by [<NAME>](http://minimaxir.com)*This is the game! Because it takes a fairly large amount of computing power and data storage, it runs on Google's servers through their Colab service. All you need to do to play it is run the startup code and then the "Play the game" code. Setup stepsBefore playing this must be run to put the needed files on Google's servers. Click the arrow on the top left of the box below to run setup.<jupyter_code>#Click the number or arrow to the left to run

!pip install -q gpt-2-simple
import gpt_2_simple as gpt2
from datetime import datetime
from google.colab import files
!wget -O checkpoint_run1.tar https://northwestern.box.com/shared/static/8k34b5sfq1ib5e4kmwjehxtpjotfdszl.tar
!tar -xvf checkpoint_run1.tar
sess = gpt2.start_tf_sess()
gpt2.load_gpt2(sess)
print("Setup complete!")<jupyter_output>[?25l
[K     |▌                               | 10kB 17.2MB/s eta 0:00:01
[K     |█                               | 20kB 21.0MB/s eta 0:00:01
[K     |█▌                              | 30kB 26.5MB/s eta 0:00:01
[K     |██                              | 40kB 9.4MB/s eta 0:00:01
[K     |██▌                             | 51kB 11.0MB/s eta 0:00:01
[K     |███                             | 61kB 12.6MB/s eta 0:00:01
[K     |███▌                            | 71kB 14.1MB/s eta 0:00:01
[K     |████                            | 81kB 15.6MB/s eta 0:00:01
[K     |████▌                           | 92kB 17.1MB/s eta 0:00:01
[K     |█████                           | 102kB 18.4MB/s eta 0:00:01
[K     |█████▌                          | 112kB 18.4MB/s eta 0:00:01
[K     |██████                          | 122kB 18.4MB/s eta 0:00:01
[K     |██████▌                         | 133kB 18.4MB/s eta 0:00:01
[K     |███████                         | 143kB 18.4MB/s eta 0:00:01
[K     |███████▌      [...]<jupyter_text>Play the gameOnce you see "Setup complete" in the box above, click the arrow in the box below to start running the game. It will take a couple of seconds for the game to generate the first prompt.<jupyter_code>#Click the number or arrow to the left to run



def isContext(text): #is a iece of textthis a context change? (i.e. moving to a new room)
  if "you are" in text or "we are" in text or "this is" in text or '''you're''' in text: #context change
    return True
  else:
    return False



#This text defines where your character starts! You can make it anything!
history="you float in space, " 

STRICT_MODE=True #Change to True to make the game a bit more coherent, but possibly a bit less interesting

locContext=""
alreadyDone=""
prompt=""
while True:
  newprompt="no response"
  gpo=gpt2.generate(sess, temperature=0.1,prefix=history,run_name='run1',length=100,return_as_list=True,nsamples=3,batch_size=3,top_p=0.99)
  for candidate in gpo:
    goodCandidate=False
    result=candidate
    splitup=result.split("\n")
    newprompt=""
    hasContext=False
    for item in splitup:
      
      if item not in alreadyDone and item not in newprompt:
        if ("." in item or "?" in item or "!" in item) and (hasContext==False or  isContext(item)==False): #If it's a next player action, then stop, otherwise keep going
          newprompt=newprompt+item
          goodCandidate=True
          if isContext(item):
            hasContext=True
        else:
          if goodCandidate and STRICT_MODE: #This prevents GPT from taking actions on our behalf. If strict mode is on actions are never taken, if it is off they are taklen but not shown to us. Strict mode on can make the game more playable at the expense of less interesting descriptions 
            break
    if goodCandidate:
      break
  if len(newprompt) > 3: #this will be blank if GPT couldn't come up with anything
    if isContext(newprompt): #This is updating the location context
      locContext=newprompt
    print(newprompt.replace(".","\n").upper())
    prompt=newprompt
    action=input()
    history=locContext+"\n"+prompt+"\n"+action
    alreadyDone=alreadyDone+prompt+"\n"+action
  else:
    print("I don't know how to do that".upper())
    newaction=input()
    history=locContext+"\n"+prompt+"\n"+newaction
    alreadyDone=alreadyDone+"\n"+newaction<jupyter_output>W0905 17:58:27.372755 140436562417536 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/gpt_2_simple/src/sample.py:32: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
{'fraction_non_alphanumeric': 0.10828300258843832, 'fraction_numerical': 0.04637618636755824, 'mean_word_length': 2.6001552795031055, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '136322', 'license': 'Unlicense', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1470, 'n_words': 502, 'path': 'GPT2_Adventure.ipynb', 'repo_name': 'nathanww/gptadventure', 'size': '4648'}