From 700a12076969a1acd45e23a809dd1a7070ce1840 Mon Sep 17 00:00:00 2001 From: Nick Walton Date: Thu, 11 Apr 2019 20:06:40 -0600 Subject: [PATCH] auto ml working --- __pycache__/utils.cpython-37.pyc | Bin 0 -> 2068 bytes main.py | 66 +++++++++++++++++-------------- requirements.txt | 1 + static/script.js | 43 +++++++++----------- static/style.css | 2 +- utils.py | 2 +- 6 files changed, 59 insertions(+), 55 deletions(-) create mode 100644 __pycache__/utils.cpython-37.pyc diff --git a/__pycache__/utils.cpython-37.pyc b/__pycache__/utils.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a35f0fbee4223288d477fec24e1644a02d527162 GIT binary patch literal 2068 zcmb_cO>g5w7@o16#C1M4E$j!JOe;|Zw8;&m6+(QJ6(A8IdRPdoyspP>tT=WUPqs~z zoEH8Ed*sAVK>Q6~IrYRnC!TlQxM^w8N?^%v0%Tjg&}G$jV1f>-V?T9 zpu+ACtmA1g8d#6D&4!U0Skj+_gW8dwDPM{~P%A|$EtoD1ubR71QC>Wjku}YKPNu+uQUp+hw`#HER-I>Juqbl=?QgN<^&S}>s3*kX(36<@Fy zoc;qNPJeEE;m$3ReQutHFi&8t?`T|c$59f_t1i4UTk*Y|bqq;|(Z}m>Ng1oL7p8(? zUiahJyI}1;CbAX`5hm{%a z#JHqe_CG{u(lwc_1MF`Es9z6I3iK^J|3x7EuOsTEh>dk0lr&#mu(>_LMJDm)_HZf{ z^;Z0QNffX6Q*cUB-a>7Il6{T)U98I}N10K|sO6NTZAr6>I!Sl8D@C{FcW<-V79Tw?Z?dQC^;%}WJ z7&|98=dZ)y-de{|B08qd(7bUR$xL~ZP)4c9w~JAD7R3HoSE%bL+$vUND3@Upgzsa^ z0zU4hW3>4WxA~6IFeK&o75GGf&f{n3yc+xhytn~AhyX36hu~L=Q3+q(K^#fa$Zaw^ zWL|@TteS~}0eP3^?~x&aHg0Q^ZtKxhc^dKooHrwV0M8YqrO;o(b1PCycK;zTYP1&k z@pXZ9MXK~~!URPtazoNVJx0C0P;}7NgMSh9ZUBTz`cWJuC*E4uZwPHsok&+`3SCiq z-^6^svz6C8Po#n8>1wCzkS;5duD;HEt-mB{^gkQiYL<05v_BZ9Vj71ZHYpk@Wtn!( NZrB#yhTXJVe*=pVu6_Uj literal 0 HcmV?d00001 diff --git a/main.py b/main.py index 7c7ec58..5c0f991 100644 --- a/main.py +++ b/main.py @@ -16,69 +16,78 @@ import datetime from flask import g import os - -os.environ['GOOGLE_APPLICATION_CREDENTIALS']="./AI-Adventure-2bb65e3a4e2f.json" - +import googleapiclient.discovery +from utils import * from google.cloud import storage from google import cloud import json from flask import Flask, render_template, request, abort -storage_client = storage.Client() -bucket = storage_client.get_bucket("dungeon-cache") from flask import Response import requests -app = Flask(__name__) +import pdb import gpt2.src.encoder as encoder # App Info 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 = {} +app = Flask(__name__) +# Encoder Info encoder_path='gpt2/models/117M' - enc = encoder.get_encoder(encoder_path) +# Model/Cache Info project = "ai-adventure" model = "generator_v1" version = "version2" +os.environ['GOOGLE_APPLICATION_CREDENTIALS']="./AI-Adventure-2bb65e3a4e2f.json" +storage_client = storage.Client() +bucket = storage_client.get_bucket("dungeon-cache") + 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) + instance = context_tokens if version is not None: name += '/versions/{}'.format(version) response = service.projects(). predict( name=name, - body={'instances': [instance]} + body={'instances': [{'context': instance}]} ).execute() if 'error' in response: raise RuntimeError(response['error']) return response['predictions'] - - - + + def generate(prompt): - context_tokens = [enc.encode(prompt)] - - + context_tokens = enc.encode(prompt) pred = predict(context_tokens) - - - output = enc.decode(pred[0]) + pred = pred[0][len(context_tokens):] + output = enc.decode(pred) return output +def generate_story_block(prompt): + block = generate(prompt) + block = cut_trailing_sentence(block) + block = story_replace(block) + + return block + +def generate_action_result(prompt, phrase): + action = phrase + generate(prompt + phrase) + action_result = cut_trailing_sentence(action) + action_result = story_replace(action_result) + + action = first_sentence(action) + + return action, action_result @app.route('/') @@ -146,9 +155,8 @@ def story_request(): if action_results is not None: response = action_results else: - response = requests.post(gen_ip + "/generate", - data={"actions":"true","seed":seed, "prompt_num":prompt_num, "prompt": prompt, "choices": json.dumps(choices)}) - response = response.text + action_results = [generate_action_result(prompt, phrase) for phrase in phrases] + response = json.dumps(action_results) cache_file(seed, prompt_num, choices, response, "choices") else: @@ -158,8 +166,7 @@ def story_request(): if result is not None: response = result else: - response = requests.post(gen_ip + "/generate", data={"actions":"false","seed":seed, "prompt_num":prompt_num}) - response=response.text + response = generate_story_block(prompts[prompt_num]) cache_file(seed, prompt_num, [], response, "story") print("\nGenerated response is: \n", response) @@ -168,7 +175,8 @@ def story_request(): return response if __name__ == '__main__': - app.run(host='0.0.0.0', port=8080) + + app.run(host='0.0.0.0', port=8080) diff --git a/requirements.txt b/requirements.txt index c2e4d02..18c6d2c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ google-cloud-storage numpy flask gunicorn +google-api-python-client diff --git a/static/script.js b/static/script.js index cc76314..897f689 100644 --- a/static/script.js +++ b/static/script.js @@ -15,25 +15,21 @@ var action_list = ["You attack", "You tell", "You use", "You go"] var prompt_num = 0 var seed_max = 1000 var seed_min = 0 -var seed = Math.floor(Math.random() * (+seed_max - +seed_min)) + +seed_min; -//var seed = 108 +//var seed = Math.floor(Math.random() * (+seed_max - +seed_min)) + +seed_min; +var seed = 999 console.log("Seed is ", seed) 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'; + +function buttonCheck(){ + if(typing == true){ + setTimeout(buttonCheck, 500); } else{ - setTimeout(checkButtonDisplay, 1000); - console.log("Not mobile device"); - document.getElementById('buttons').style.visibility='hidden'; - + document.getElementById('buttons').style.visibility='visible'; } } @@ -62,29 +58,29 @@ var StoryTracker = { StoryTracker.makeActionRequests(StoryTracker.firstStory + StoryTracker.lastStory) Typer.appendToText(story) - Typer.appendToText("\n\nOptions:") + action_waiting = true + setTimeout(StoryTracker.actionWait, 10000); }, actionWait:function(){ if(action_waiting == true){ if(typing == true || acceptInput == true){ - setTimeout(StoryTracker.actionWait, 5000); + setTimeout(StoryTracker.actionWait, 10000); } else{ - Typer.appendToText(" Generating...") + Typer.appendToText("\n\n Generating options... (~20s)") } } }, - addNextAction:function(action_result){ action_waiting = false var action_results = JSON.parse(action_result) - + Typer.appendToText("\n\nOptions:") for (i = 0; i < 4; i++){ action_result = action_results[i] @@ -102,10 +98,9 @@ var StoryTracker = { Typer.appendToText("\nWhich action do you choose? ") StoryTracker.action_int = 0 acceptInput = true - - if(isMobileDevice(){ - setTimeout(checkButtonDisplay, 1000); - + + if(isMobileDevice()){ + setTimeout(buttonCheck, 500); } } } @@ -133,7 +128,6 @@ var StoryTracker = { processInput:function(){ var choice_int = parseInt(inputStr, 10) - if(choice_int >= 0 && choice_int <= 3){ console.log("choice_int is %d", choice_int) @@ -142,10 +136,9 @@ var StoryTracker = { StoryTracker.lastStory = StoryTracker.results[choice_int] StoryTracker.makeActionRequests(StoryTracker.firstStory + StoryTracker.lastStory) action_waiting = true - setTimeout(StoryTracker.actionWait, 4000); + setTimeout(StoryTracker.actionWait, 10000); Typer.appendToText("\n") Typer.appendToText(StoryTracker.lastStory) - Typer.appendToText("\n\nOptions:") } else{ @@ -200,7 +193,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,"
")) } else{ @@ -293,6 +286,8 @@ function start(){ startTyping() Typer.startBlinker() + console.log("Not mobile device"); + document.getElementById('buttons').style.visibility='hidden'; } diff --git a/static/style.css b/static/style.css index 4009191..5a96607 100644 --- a/static/style.css +++ b/static/style.css @@ -118,7 +118,7 @@ button:focus{ text-align: center; padding: 10px 5px; text-decoration: none; - font-size:calc(12px + 1.0vh ); + font-size:calc(14px + 0.5vh ); min-font-size: } diff --git a/utils.py b/utils.py index bb07a51..0da1f05 100644 --- a/utils.py +++ b/utils.py @@ -14,7 +14,7 @@ replacements: def remove_profanity(text): - remove_words = ["fuck", "Fuck"] + remove_words = ["fuck", "Fuck", "shit", "rape", "bastard", "bitch"] for word in remove_words: text = text.replace(word, "****")