From b641b137398c552145819b0e706904eedd64febb Mon Sep 17 00:00:00 2001 From: Nick Walton Date: Thu, 11 Apr 2019 19:05:55 -0600 Subject: [PATCH] update --- gpt2/src/__pycache__/encoder.cpython-37.pyc | Bin 0 -> 4961 bytes gpt2/src/encoder.py | 117 ++++++++++++++++++++ main.py | 55 +++++++-- main_test.py | 23 ---- static/script.js | 27 +++-- static/style.css | 2 +- 6 files changed, 182 insertions(+), 42 deletions(-) create mode 100644 gpt2/src/__pycache__/encoder.cpython-37.pyc create mode 100644 gpt2/src/encoder.py delete mode 100644 main_test.py diff --git a/gpt2/src/__pycache__/encoder.cpython-37.pyc b/gpt2/src/__pycache__/encoder.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c5e7be4e29abb385e1695ce6081e2f143914f74e GIT binary patch literal 4961 zcmbtY&vP6{74Dv2JG)vdsvztnBw?I{*o$S!aY7|f!bxmH04FLF0XB=wX0+Yf9cy-G z)zkXPY>Fz0N-n8D@efExswlYRz>O;SE0`-M{{dWFRKfRpc2;ZS3$S+g%=EnOe*NC} zzV}Ag&zxy6{QmT}KdvQnjQyP+s-KOUchIuC=mcvs!G#s`HaAzRZJDdxc5t<0ci^=> z&ZKw9gd^M|cF5Z`;fdN2Yy0mp;fwka6ZJ!@T^9{8gVBbtR#~fg@ci|CErVegsX!*( zR76QH*w#@T=}2Y|us2(2@uD89?M^rB_GOob=;xq$XgAQZFVJbGxrSPY)LVy8>l{18 zbv*8varbomTs6ZpZ)($TMl2_#{a;znvJn^ck+t`h=Av=PZ?by}BRjWqwpmmCoE_u- zgmaejV_P(G>l`yrmayMsBWtUFhlv@{Ji_~%Fv^H)N4q(mNHZAVO!!TUu||O3hf;5= zBn#pw(?PlcD{NeTD_AGn?afc+fSYqWx+s(P+~vb4iYJ8+c;D`8N6&%8qezXLlt&4mJgzY zWL2<}cEj}`+AuSB!$b!<4Z@u?62XsGZjIHt)0am4g2I_}kfj5u`{eOB+Qdlk+Lh~= zwh18->M)bqtR`{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100644 index 0000000..e9fed43 --- /dev/null +++ b/gpt2/src/encoder.py @@ -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, + ) diff --git a/main.py b/main.py index 2646317..61a0a9c 100644 --- a/main.py +++ b/main.py @@ -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= + 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) diff --git a/main_test.py b/main_test.py deleted file mode 100644 index 380ee88..0000000 --- a/main_test.py +++ /dev/null @@ -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 diff --git a/static/script.js b/static/script.js index 5152471..58f2eae 100644 --- a/static/script.js +++ b/static/script.js @@ -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,"
")) 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'; - } - } diff --git a/static/style.css b/static/style.css index bf00844..33b212b 100644 --- a/static/style.css +++ b/static/style.css @@ -67,7 +67,7 @@ a { } #buttons { - position: absolute; + position: relative; bottom: 80px; height: 60px; width: 100%;