From 7e55a8ad2172e19e93892e2d27d4d00d987f539c Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 23 Oct 2019 22:36:44 -0600 Subject: [PATCH] addedstorywriter --- main.py | 3 +- old_main.py | 264 ------------------------------------------------ story_writer.py | 53 ++++++++++ 3 files changed, 55 insertions(+), 265 deletions(-) delete mode 100644 old_main.py create mode 100644 story_writer.py diff --git a/main.py b/main.py index 2d569d3..b3ac428 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,7 @@ app = Flask(__name__) app.secret_key = '#d\xe0\xd1\xfb\xee\xa4\xbb\xd0\xf0/e)\xb5g\xdd<`\xc7\xa5\xb0-\xb8d0S' CRED_FILE = "./AI-Adventure-2bb65e3a4e2f.json" generator = WebGenerator(CRED_FILE) -story_manager = CachedStoryManager(generator, CRED_FILE) +story_manager = ConstrainedStoryManager(generator) def get_response_string(story_text, possible_actions): string_list = ["\n\n", story_text, "\n\nOptions:" + "\n"] @@ -38,6 +38,7 @@ def generate(): if "story" not in session or session["story"] is None: print("Starting new story") seed = np.random.randint(100) + story_manager.enable_caching(credentials_file=CRED_FILE, seed=seed, bucket_name="dungeon-cache") prompt = get_story_start("classic") story_manager.start_new_story(prompt, seed) possible_actions = story_manager.get_possible_actions() diff --git a/old_main.py b/old_main.py deleted file mode 100644 index 4ced5f2..0000000 --- a/old_main.py +++ /dev/null @@ -1,264 +0,0 @@ -# Copyright 2018 Google LLC -# -# 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. - -# [START gae_python37_render_template] -import datetime -from flask import g -import os -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 -from flask import Response -import requests -import pdb -import sys -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"] -continuing_prompts = [ - "You are in a dungeon with your sword and shield. You are on a quest to defeat the necromancer. This dungeon is full of zombie and skeletons."] -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): - service = googleapiclient.discovery.build('ml', 'v1') - name = 'projects/{}/models/{}'.format(project, model) - instance = context_tokens - - if version is not None: - name += '/versions/{}'.format(version) - - response = service.projects().predict( - name=name, - body={'instances': [{'context': instance}]} - ).execute() - - if 'error' in response: - raise RuntimeError(response['error']) - - return response['predictions'] - - -def generate(prompt): - while (True): - context_tokens = enc.encode(prompt) - try: - pred = predict(context_tokens) - pred = pred[0]["output"][len(context_tokens):] - output = enc.decode(pred) - return output - except: - print("generate request failed, trying again") - continue - - -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('/') -def root(): - seed = -1 - data = {'seed': seed} - return render_template('index.html', data=data) - - -@app.route('/') -def rootseed(seed): - if seed == "": - seed = -1 - else: - seed = int(seed) - data = {'seed': seed} - return render_template('index.html', data=data) - - -@app.route('/index.html') -def index(): - data = {'seed': -1} - return render_template('index.html', data=data) - - -@app.route('/about.html') -def about(): - return render_template('about.html') - - -def cache_file(seed, prompt_num, choices, response, tag): - blob_file_name = "prompt" + str(prompt_num) + "/seed" + str(seed) + "/" + tag - for action in choices: - blob_file_name = blob_file_name + str(action) - blob = bucket.blob(blob_file_name) - - blob.upload_from_string(response) - - print("File ", blob_file_name, " cached") - - -def retrieve_from_cache(seed, prompt_num, choices, tag): - blob_file_name = "prompt" + str(prompt_num) + "/seed" + str(seed) + "/" + tag - - for action in choices: - blob_file_name = blob_file_name + str(action) - - blob = bucket.blob(blob_file_name) - - if blob.exists(storage_client): - result = blob.download_as_string().decode("utf-8") - print(blob_file_name, " found in cache") - else: - result = None - print(blob_file_name, " not found in cache") - - return result - - -@app.route('/generate', methods=['POST']) -def story_request(): - print("****Generating Story****") - seed = request.form["seed"] - prompt_num = int(request.form["prompt_num"]) - gen_actions = request.form["actions"] - - if int(seed) < 0 or int(seed) > 100: - print("Invalid seed: " + seed) - abort(404) - - if gen_actions == "true": - - # prompt = request.form["prompt"] - choices = json.loads(request.form["choices"]) - print("Getting response for seed ", seed, " prompt_num ", prompt_num, " and choices ", choices) - - action_results = retrieve_from_cache(seed, prompt_num, choices, "choices") - - if action_results is not None: - response = action_results - else: - last_action_result = request.form["last_action_result"] - prompt = continuing_prompts[prompt_num] + last_action_result - print("\n\nAction prompt is \n ", prompt) - 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: - - print("Getting response for seed ", seed, " prompt_num ", prompt_num) - result = retrieve_from_cache(seed, prompt_num, [], "story") - - if result is not None: - response = result - else: - prompt = prompts[prompt_num] - response = generate_story_block(prompt) - cache_file(seed, prompt_num, [], response, "story") - - print("\nGenerated response is: \n", response) - print("") - - return response - - -def generate_cache(): - start_seed = int(sys.argv[1]) - end_seed = int(sys.argv[2]) - - # Generate story sections - prompt_num = 0 - action_queue = [] - prompt = prompts[prompt_num] - for seed in range(start_seed, end_seed): - result = retrieve_from_cache(seed, prompt_num, [], "story") - if result is not None: - response = result - else: - prompt = prompts[prompt_num] - # print("\n Story prompt is ", prompt) - response = generate_story_block(prompt) - # print("\n Story response is ", response) - cache_file(seed, prompt_num, [], response, "story") - - action_queue.append([seed, 0, [], response]) - - while (True): - - next_gen = action_queue.pop(0) - seed = next_gen[0] - prompt_num = next_gen[1] - choices = next_gen[2] - last_action_result = next_gen[3] - - action_results = retrieve_from_cache(seed, prompt_num, choices, "choices") - - if action_results is not None: - response = action_results - - else: - if len(choices) is 0: - prompt = prompts[prompt_num] + last_action_result - else: - prompt = continuing_prompts[prompt_num] + last_action_result - # print("\n\n Action prompt is \n ", prompt) - action_results = [generate_action_result(prompt, phrase) for phrase in phrases] - response = json.dumps(action_results) - - # print("\n\n Action - cache_file(seed, prompt_num, choices, response, "choices") - - un_jsoned = json.loads(response) - for j in range(4): - new_choices = choices[:] - new_choices.append(j) - action_queue.append([seed, 0, new_choices, un_jsoned[j][1]]) - - -if __name__ == '__main__': - if (len(sys.argv) > 1): - generate_cache() - else: - app.run(host='0.0.0.0', port=8080) - -# [START gae_python37_render_template] \ No newline at end of file diff --git a/story_writer.py b/story_writer.py new file mode 100644 index 0000000..73fd774 --- /dev/null +++ b/story_writer.py @@ -0,0 +1,53 @@ +from flask import g +from flask import session +import os +from story.utils import * +import json +from flask import Flask, render_template, request, abort +from story.story_manager import * +from generator.web.web_generator import * +from other.cacher import * +import numpy as np + +app = Flask(__name__) +app.secret_key = '#d\xe0\xd1\xfb\xee\xa4\xbb\xd0\xf0/e)\xb5g\xdd<`\xc7\xa5\xb0-\xb8d0S' +CRED_FILE = "./AI-Adventure-2bb65e3a4e2f.json" +story = Story("") + +# Bread and butter of app, updates story and returns based on choice +@app.route('/generate', methods=['POST']) +def generate(): + action = request.form["action"] + + # If there is no story in session, make a new one + if "prompt" not in session or session["prompt"] is None: + session["prompt"] = get_story_start("classic") + response = "Continue the initial story block:\n\n" + session["prompt"] + + # If there is a story in session continue from it. + elif "story" not in session or session["story"] is None: + story_start = session["prompt"] + action + story.story_start = story_start + + session["story"] = story.to_json() + response = "Enter the action then two newlines then the result:\n\n> " + + else: + story = story.initialize_from_json(session["story"]) + action, result = action.split("\n") + story.add_to_story(action, result) + session["story"] = story.to_json() + + response = "Enter the action then two newlines then the result:\n\n> " + + print("Returning response") + return response + +# Routes to index +@app.route('/') +def root(): + session["story"] = None + return render_template('index.html') + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=8080) \ No newline at end of file