This commit is contained in:
Nick Walton
2019-04-11 19:05:55 -06:00
parent 16c44830cb
commit b641b13739
6 changed files with 182 additions and 42 deletions
Binary file not shown.
+117
View File
@@ -0,0 +1,117 @@
"""Byte pair encoding utilities"""
import os
import json
import regex as re
from functools import lru_cache
@lru_cache()
def bytes_to_unicode():
"""
Returns list of utf-8 byte and a corresponding list of unicode strings.
The reversible bpe codes work on unicode strings.
This means you need a large # of unicode characters in your vocab if you want to avoid UNKs.
When you're at something like a 10B token dataset you end up needing around 5K for decent coverage.
This is a signficant percentage of your normal, say, 32K bpe vocab.
To avoid that, we want lookup tables between utf-8 bytes and unicode strings.
And avoids mapping to whitespace/control characters the bpe code barfs on.
"""
bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1))
cs = bs[:]
n = 0
for b in range(2**8):
if b not in bs:
bs.append(b)
cs.append(2**8+n)
n += 1
cs = [chr(n) for n in cs]
return dict(zip(bs, cs))
def get_pairs(word):
"""Return set of symbol pairs in a word.
Word is represented as tuple of symbols (symbols being variable-length strings).
"""
pairs = set()
prev_char = word[0]
for char in word[1:]:
pairs.add((prev_char, char))
prev_char = char
return pairs
class Encoder:
def __init__(self, encoder, bpe_merges, errors='replace'):
self.encoder = encoder
self.decoder = {v:k for k,v in self.encoder.items()}
self.errors = errors # how to handle errors in decoding
self.byte_encoder = bytes_to_unicode()
self.byte_decoder = {v:k for k, v in self.byte_encoder.items()}
self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges))))
self.cache = {}
# Should haved added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
self.pat = re.compile(r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+""")
def bpe(self, token):
if token in self.cache:
return self.cache[token]
word = tuple(token)
pairs = get_pairs(word)
if not pairs:
return token
while True:
bigram = min(pairs, key = lambda pair: self.bpe_ranks.get(pair, float('inf')))
if bigram not in self.bpe_ranks:
break
first, second = bigram
new_word = []
i = 0
while i < len(word):
try:
j = word.index(first, i)
new_word.extend(word[i:j])
i = j
except:
new_word.extend(word[i:])
break
if word[i] == first and i < len(word)-1 and word[i+1] == second:
new_word.append(first+second)
i += 2
else:
new_word.append(word[i])
i += 1
new_word = tuple(new_word)
word = new_word
if len(word) == 1:
break
else:
pairs = get_pairs(word)
word = ' '.join(word)
self.cache[token] = word
return word
def encode(self, text):
bpe_tokens = []
for token in re.findall(self.pat, text):
token = ''.join(self.byte_encoder[b] for b in token.encode('utf-8'))
bpe_tokens.extend(self.encoder[bpe_token] for bpe_token in self.bpe(token).split(' '))
return bpe_tokens
def decode(self, tokens):
text = ''.join([self.decoder[token] for token in tokens])
text = bytearray([self.byte_decoder[c] for c in text]).decode('utf-8', errors=self.errors)
return text
def get_encoder(model_path):
with open(os.path.join(model_path, 'encoder.json'), 'r') as f:
encoder = json.load(f)
with open(os.path.join(model_path, 'vocab.bpe'), 'r', encoding="utf-8") as f:
bpe_data = f.read()
bpe_merges = [tuple(merge_str.split()) for merge_str in bpe_data.split('\n')[1:-1]]
return Encoder(
encoder=encoder,
bpe_merges=bpe_merges,
)
+46 -9
View File
@@ -28,15 +28,59 @@ bucket = storage_client.get_bucket("dungeon-cache")
from flask import Response
import requests
app = Flask(__name__)
import gpt2.src.encoder as encoder
# App Info
#gen_ip = "http://35.192.97.36:8010/"
gen_ip = "http://0.0.0.0:8090"
phrases = [" You attack", " You use", " You tell", " You go"]
prompts = ["You enter a dungeon with your trusty sword and shield. You are searching for the evil necromancer who killed your family. You've heard that he resides at the bottom of the dungeon, guarded by legions of the undead. You enter the first door and see"]
requested_map = {}
encoder_path='gpt2/models/117M'
enc = encoder.get_encoder(encoder_path)
project = "ai-adventure"
model = "generator_v1"
version = "version2"
def predict(context_tokens):
# Create the ML Engine service object.
# To authenticate set the environment variable
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
service = googleapiclient.discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model)
instance = json.loads(context_tokens)
if version is not None:
name += '/versions/{}'.format(version)
response = service.projects(). predict(
name=name,
body={'instances': [instance]}
).execute()
if 'error' in response:
raise RuntimeError(response['error'])
return response['predictions']
def generate(prompt):
context_tokens = [enc.encode(prompt)]
pred = predict(context_tokens)
output = enc.decode(pred[0])
return output
@app.route('/')
def root():
return render_template('index.html')
@@ -119,13 +163,6 @@ def story_request():
return response
@app.teardown_appcontext
def teardown_sess(_):
sess = g.pop("sess",None)
if sess is not None:
sess.close()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
-23
View File
@@ -1,23 +0,0 @@
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import main
def test_index():
main.app.testing = True
client = main.app.test_client()
r = client.get('/')
assert r.status_code == 200
+18 -9
View File
@@ -23,6 +23,19 @@ function isMobileDevice() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
};
function checkButtonDisplay()
if(typing==false){
console.log("Mobile device");
document.getElementById('buttons').style.visibility='visible';
}
else{
setTimeout(checkButtonDisplay, 1000);
console.log("Not mobile device");
document.getElementById('buttons').style.visibility='hidden';
}
}
var StoryTracker = {
firstStory: null,
lastStory: null,
@@ -60,6 +73,7 @@ var StoryTracker = {
}
},
addNextAction:function(action_result){
@@ -84,6 +98,9 @@ var StoryTracker = {
Typer.appendToText("\nWhich action do you choose? ")
StoryTracker.action_int = 0
acceptInput = true
if(isMobileDevice(){
setTimeout(checkButtonDisplay, 1000);
}
}
}
@@ -172,7 +189,7 @@ var Typer={
}
var text=Typer.text.substring(0,Typer.index)
var rtn= new RegExp("\n", "g")
A solution would be to add position: relative for the button. This will move it above the label.
$("#console").html(text.replace(rtn,"<br/>"))
window.scrollBy(0,50)
}
@@ -264,14 +281,6 @@ function start(){
startTyping()
Typer.startBlinker()
if(isMobileDevice()){
console.log("Mobile device");
}
else{
console.log("Not mobile device");
document.getElementById('buttons').style.visibility='hidden';
}
}
+1 -1
View File
@@ -67,7 +67,7 @@ a {
}
#buttons {
position: absolute;
position: relative;
bottom: 80px;
height: 60px;
width: 100%;