diff --git a/__pycache__/generator.cpython-36.pyc b/__pycache__/generator.cpython-36.pyc deleted file mode 100644 index a0f7f3b..0000000 Binary files a/__pycache__/generator.cpython-36.pyc and /dev/null differ diff --git a/__pycache__/utils.cpython-36.pyc b/__pycache__/utils.cpython-36.pyc deleted file mode 100644 index a0806d4..0000000 Binary files a/__pycache__/utils.cpython-36.pyc and /dev/null differ diff --git a/__pycache__/utils.cpython-37.pyc b/__pycache__/utils.cpython-37.pyc deleted file mode 100644 index 252863a..0000000 Binary files a/__pycache__/utils.cpython-37.pyc and /dev/null differ diff --git a/gpt2/__init__.py b/generator/__init__.py similarity index 100% rename from gpt2/__init__.py rename to generator/__init__.py diff --git a/gpt2/pytorch_generator.py b/generator/pytorch/pytorch_generator.py similarity index 100% rename from gpt2/pytorch_generator.py rename to generator/pytorch/pytorch_generator.py diff --git a/gpt2/CONTRIBUTORS.md b/generator/tf_local/CONTRIBUTORS.md similarity index 100% rename from gpt2/CONTRIBUTORS.md rename to generator/tf_local/CONTRIBUTORS.md diff --git a/gpt2/DEVELOPERS.md b/generator/tf_local/DEVELOPERS.md similarity index 100% rename from gpt2/DEVELOPERS.md rename to generator/tf_local/DEVELOPERS.md diff --git a/gpt2/LICENSE b/generator/tf_local/LICENSE similarity index 100% rename from gpt2/LICENSE rename to generator/tf_local/LICENSE diff --git a/gpt2/README.md b/generator/tf_local/README.md similarity index 100% rename from gpt2/README.md rename to generator/tf_local/README.md diff --git a/generator/tf_local/__init__.py b/generator/tf_local/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gpt2/download_model.py b/generator/tf_local/download_model.py similarity index 100% rename from gpt2/download_model.py rename to generator/tf_local/download_model.py diff --git a/generator.py b/generator/tf_local/generator_local.py similarity index 98% rename from generator.py rename to generator/tf_local/generator_local.py index 4e2576a..f7408e3 100644 --- a/generator.py +++ b/generator/tf_local/generator_local.py @@ -7,7 +7,7 @@ import gpt2.src.model as model from tensorflow.contrib import predictor import gpt2.src.sample as sample import gpt2.src.encoder as encoder -from utils import * +from story.utils import * import pdb pos_action_starts = ["You attack", "You tell", "You use", "You go"] @@ -50,29 +50,29 @@ class StoryGenerator(): text = self.enc.decode(out[0]) return text - + def generate_story_block(self, prompt): block = self.generate(prompt) block = cut_trailing_sentence(block) block = story_replace(block) - + return block - + def generate_action_options(self, prompt, action_starts=pos_action_starts): - + possible_actions = [] for phrase in action_starts: action = phrase + self.generate(prompt + phrase) action = first_sentence(action) possible_actions.append(action) - + return possible_actions - + def generate_action_result(self, prompt, phrase): action = phrase + self.generate(prompt + phrase) action_result = cut_trailing_sentence(action) action_result = story_replace(action_result) - + action = first_sentence(action) return action, action_result diff --git a/gpt2/models/117M/.gitignore b/generator/tf_local/models/117M/.gitignore similarity index 100% rename from gpt2/models/117M/.gitignore rename to generator/tf_local/models/117M/.gitignore diff --git a/gpt2/models/117M/checkpoint b/generator/tf_local/models/117M/checkpoint similarity index 100% rename from gpt2/models/117M/checkpoint rename to generator/tf_local/models/117M/checkpoint diff --git a/gpt2/models/117M/encoder.json b/generator/tf_local/models/117M/encoder.json similarity index 100% rename from gpt2/models/117M/encoder.json rename to generator/tf_local/models/117M/encoder.json diff --git a/gpt2/models/117M/hparams.json b/generator/tf_local/models/117M/hparams.json similarity index 100% rename from gpt2/models/117M/hparams.json rename to generator/tf_local/models/117M/hparams.json diff --git a/gpt2/models/117M/vocab.bpe b/generator/tf_local/models/117M/vocab.bpe similarity index 100% rename from gpt2/models/117M/vocab.bpe rename to generator/tf_local/models/117M/vocab.bpe diff --git a/gpt2/src/encoder.py b/generator/tf_local/src/encoder.py similarity index 100% rename from gpt2/src/encoder.py rename to generator/tf_local/src/encoder.py diff --git a/gpt2/src/model.py b/generator/tf_local/src/model.py similarity index 100% rename from gpt2/src/model.py rename to generator/tf_local/src/model.py diff --git a/gpt2/src/sample.py b/generator/tf_local/src/sample.py similarity index 100% rename from gpt2/src/sample.py rename to generator/tf_local/src/sample.py diff --git a/gpt2/__pycache__/__init__.cpython-36.pyc b/gpt2/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index 4421449..0000000 Binary files a/gpt2/__pycache__/__init__.cpython-36.pyc and /dev/null differ diff --git a/gpt2/models/117M/model.ckpt.index b/gpt2/models/117M/model.ckpt.index deleted file mode 100644 index 24729aa..0000000 Binary files a/gpt2/models/117M/model.ckpt.index and /dev/null differ diff --git a/gpt2/models/117M/model.ckpt.meta b/gpt2/models/117M/model.ckpt.meta deleted file mode 100644 index 2541738..0000000 Binary files a/gpt2/models/117M/model.ckpt.meta and /dev/null differ diff --git a/gpt2/src/__pycache__/encoder.cpython-36.pyc b/gpt2/src/__pycache__/encoder.cpython-36.pyc deleted file mode 100644 index cf5999a..0000000 Binary files a/gpt2/src/__pycache__/encoder.cpython-36.pyc and /dev/null differ diff --git a/gpt2/src/__pycache__/encoder.cpython-37.pyc b/gpt2/src/__pycache__/encoder.cpython-37.pyc deleted file mode 100644 index 4a29047..0000000 Binary files a/gpt2/src/__pycache__/encoder.cpython-37.pyc and /dev/null differ diff --git a/gpt2/src/__pycache__/model.cpython-36.pyc b/gpt2/src/__pycache__/model.cpython-36.pyc deleted file mode 100644 index 8ef3bed..0000000 Binary files a/gpt2/src/__pycache__/model.cpython-36.pyc and /dev/null differ diff --git a/gpt2/src/__pycache__/sample.cpython-36.pyc b/gpt2/src/__pycache__/sample.cpython-36.pyc deleted file mode 100644 index 7c9ca06..0000000 Binary files a/gpt2/src/__pycache__/sample.cpython-36.pyc and /dev/null differ diff --git a/main.py b/main.py index f1dd7a5..b44a96a 100644 --- a/main.py +++ b/main.py @@ -1,17 +1,11 @@ -import datetime from flask import g from flask import session import os import googleapiclient.discovery -from utils import * +from story.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 from generator import StoryGenerator import gpt2.src.encoder as encoder diff --git a/story/story_manager.py b/story/story_manager.py new file mode 100644 index 0000000..d32974c --- /dev/null +++ b/story/story_manager.py @@ -0,0 +1,85 @@ +from story.utils import * + +class Story(): + + def __init__(self, story_start): + + self.story_start = story_start + + # list of actions. First action is the prompt length should always equal that of story blocks + self.actions = [] + + # list of story blocks first story block follows prompt and is intro story + self.results = [] + + def add_to_story(self, action, story_block): + self.actions.append(action) + self.results.append(story_block) + + def latest_result(self): + if len(self.results) > 0: + return self.results[-1] + else: + return "" + + def __str__(self): + story_list = [self.story_start] + for i in range(len(self.results)): + story_list.append(self.actions[i]) + story_list.append(self.results[i]) + + return sum(story_list) + + +class UnconstrainedStoryGenerator(): + + def __init__(self, generator, story_start): + self.story = Story(story_start) + self.generator = generator + + def act(self, action_choice): + + result = self.generate_result(action_choice) + self.story.add_to_story(action_choice, result) + return result + + def story_context(self): + return self.story.latest_result() + + def generate_result(self, action): + block = self.generator.generate(self.story_context() + action) + block = cut_trailing_sentence(block) + block = story_replace(block) + return block + + +class ConstrainedStoryGenerator(): + + def __init__(self, generator, story_start): + self.story = Story(story_start) + self.generator = generator + self.possible_action_results = self.get_action_results() + self.action_phrases = ["You attack", "You tell", "You use", "You go"] + + def act(self, action_choice): + + action, result = self.possible_action_results[action_choice] + self.story.add_to_story(action, result) + self.possible_action_results = self.get_action_results() + return result, self.possible_action_results + + def story_context(self): + return self.story.latest_result() + + def get_action_results(self): + return [self.generate_action_result(self.story_context(), phrase) for phrase in self.action_phrases] + + def generate_action_result(self, prompt, phrase): + action = phrase + self.generator.generate(prompt + phrase) + action_result = cut_trailing_sentence(action) + + action, result = split_first_sentence(action_result) + result = story_replace(action_result) + action = action_replace(action) + + return action, result diff --git a/utils.py b/story/utils.py similarity index 90% rename from utils.py rename to story/utils.py index 0da1f05..2af9820 100644 --- a/utils.py +++ b/story/utils.py @@ -52,18 +52,19 @@ def text_replace(text): return text -def first_sentence(text): +def split_first_sentence(text): first_period = text.find('.') first_exclamation = text.find('!') if first_exclamation < first_period and first_exclamation > 0: - text = text[0:first_exclamation+1] + split_point = first_exclamation+1 elif first_period > 0: - text = text[0:first_period+1] + split_point = first_period+1 else: - return text[0:20] + split_point = text[0:20] - return text + return text[0:split_point], text[split_point:] + def all_but_first(text): first_period = text.find('.')