mirror of
https://github.com/wassname/Clover-Edition.git
synced 2026-09-09 11:13:26 +08:00
refactored
This commit is contained in:
+8
-6
@@ -4,14 +4,14 @@ from google.cloud import storage
|
||||
import json
|
||||
from story.story_manager import *
|
||||
from generator.web.web_generator import *
|
||||
from generator.ctrl.ctrl_generator import *
|
||||
#from generator.ctrl.ctrl_generator import *
|
||||
import tensorflow as tf
|
||||
import textwrap
|
||||
|
||||
CRED_FILE = "./AI-Adventure-2bb65e3a4e2f.json"
|
||||
|
||||
# Set the key
|
||||
def console_print(str, pycharm=False):
|
||||
def console_print(str, pycharm=True):
|
||||
if pycharm:
|
||||
LINE_WIDTH=80
|
||||
|
||||
@@ -41,8 +41,8 @@ def play_unconstrained():
|
||||
|
||||
def play_constrained():
|
||||
print("\n")
|
||||
#generator = WebGenerator(CRED_FILE)
|
||||
generator = CTRLGenerator()
|
||||
generator = WebGenerator(CRED_FILE)
|
||||
#generator = CTRLGenerator()
|
||||
story_start = "haunted"
|
||||
prompt = get_story_start(story_start)
|
||||
story_manager = CTRLStoryManager(generator)
|
||||
@@ -70,7 +70,9 @@ def play_constrained():
|
||||
|
||||
def play_cached():
|
||||
generator = WebGenerator(CRED_FILE)
|
||||
story_manager = CachedStoryManager(generator, CRED_FILE)
|
||||
story_manager = ConstrainedStoryManager(generator)
|
||||
story_manager.enable_caching(CRED_FILE)
|
||||
|
||||
story_manager.start_new_story(get_story_start("classic"), 0)
|
||||
|
||||
console_print(str(story_manager.story))
|
||||
@@ -90,7 +92,7 @@ def play_cached():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
play_constrained()
|
||||
play_cached()
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -33,8 +33,6 @@ class WebGenerator():
|
||||
|
||||
def generate(self, prompt, options={}):
|
||||
|
||||
print("Prompt to generate from is ", prompt)
|
||||
|
||||
while (True):
|
||||
context_tokens = self.enc.encode(prompt)
|
||||
try:
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ from google.cloud import storage
|
||||
import os
|
||||
|
||||
|
||||
class cacher():
|
||||
class Cacher():
|
||||
|
||||
def __init__(self, credentials_file, bucket_name="dungeon-cache"):
|
||||
# Model/Cache Info
|
||||
|
||||
@@ -8,3 +8,10 @@ action_verbs:
|
||||
|
||||
anything: ["You", "You"]
|
||||
|
||||
ctrl_verbs:
|
||||
non_movement: ["take", "put", "give", "set", "keep", "help", "show", "pay", "read", "start", "stay", "call",
|
||||
"change", "ask", "open", "look", "make", "say", "tell", "attack", "use", "fight", "scream", "yell"]
|
||||
movement: ["walk", "go", "run", "move"]
|
||||
|
||||
rooms:
|
||||
haunted_hospital: ["lobby", "hallway", "parking", "roof", "pharmacy"]
|
||||
+43
-63
@@ -117,26 +117,34 @@ class ConstrainedStoryManager(StoryManager):
|
||||
|
||||
def enable_caching(self, credentials_file=None, seed=0, bucket_name="dungeon-cache"):
|
||||
self.cache = True
|
||||
self.cacher = cacher(credentials_file, bucket_name)
|
||||
self.cacher = Cacher(credentials_file, bucket_name)
|
||||
self.seed = seed
|
||||
|
||||
def start_new_story(self, story_prompt, game_state=None):
|
||||
|
||||
if self.cache:
|
||||
result = self.cacher.retrieve_from_cache(self.seed, [], "story")
|
||||
if result is not None:
|
||||
story_start = story_prompt + result
|
||||
self.story = Story(story_start, seed=self.seed)
|
||||
else:
|
||||
story_start = super().start_new_story(story_prompt)
|
||||
self.story.seed = self.seed
|
||||
self.cacher.cache_file(self.seed, [], story_start, "story")
|
||||
return self.start_new_story_cache(story_prompt, game_state=game_state)
|
||||
else:
|
||||
super().start_new_story(story_prompt, game_state=game_state)
|
||||
return self.start_new_story_cache(story_prompt, game_state=game_state)
|
||||
|
||||
def start_new_story_generate(self, story_prompt, game_state=None):
|
||||
super().start_new_story(story_prompt, game_state=game_state)
|
||||
self.story.possible_action_results = self.get_action_results()
|
||||
return self.story.story_start
|
||||
|
||||
def start_new_story_cache(self, story_prompt, game_state=None):
|
||||
|
||||
response = self.cacher.retrieve_from_cache(self.seed, [], "story")
|
||||
if response is not None:
|
||||
story_start = story_prompt + response
|
||||
self.story = Story(story_start, seed=self.seed)
|
||||
self.story.possible_action_results = self.get_action_results()
|
||||
else:
|
||||
story_start = self.start_new_story_generate(story_prompt, game_state=game_state)
|
||||
self.story.seed = self.seed
|
||||
self.cacher.cache_file(self.seed, [], story_start, "story")
|
||||
|
||||
return story_start
|
||||
|
||||
def load_story(self, story, from_json=False):
|
||||
story_string = super().load_story(story, from_json=from_json)
|
||||
return story_string
|
||||
@@ -166,30 +174,35 @@ class ConstrainedStoryManager(StoryManager):
|
||||
return result, self.get_possible_actions()
|
||||
|
||||
def get_action_results(self):
|
||||
return [self.generate_action_result(self.story_context(), phrase) for phrase in self.action_phrases]
|
||||
if self.cache:
|
||||
return self.get_action_results_cache()
|
||||
else:
|
||||
return self.get_action_results_generate()
|
||||
|
||||
def get_action_results_generate(self):
|
||||
action_results = [self.generate_action_result(self.story_context(), phrase) for phrase in self.action_phrases]
|
||||
return action_results
|
||||
|
||||
def get_action_results_cache(self):
|
||||
response = self.cacher.retrieve_from_cache(self.story.seed, self.story.choices, "choices")
|
||||
|
||||
if response is not None:
|
||||
print("Retrieved from cache")
|
||||
return json.loads(response)
|
||||
else:
|
||||
print("Didn't receive from cache")
|
||||
action_results = self.get_action_results_generate()
|
||||
response = json.dumps(action_results)
|
||||
self.cacher.cache_file(self.story.seed, self.story.choices, response, "choices")
|
||||
return action_results
|
||||
|
||||
def generate_action_result(self, prompt, phrase, options=None):
|
||||
if self.cache:
|
||||
response = self.cacher.retrieve_from_cache(self.story.seed, self.story.choices, "choices")
|
||||
|
||||
if response is not None:
|
||||
action_results = json.loads(response)
|
||||
else:
|
||||
print("Not found in cache. Generating...")
|
||||
action_results = self.get_action_results()
|
||||
response = json.dumps(action_results)
|
||||
self.cacher.cache_file(self.story.seed, self.story.choices, response, "choices")
|
||||
|
||||
else:
|
||||
|
||||
|
||||
action = phrase + " " + self.generator.generate(prompt + " " + phrase, options)
|
||||
action_result = cut_trailing_sentence(action)
|
||||
action, result = split_first_sentence(action_result)
|
||||
|
||||
return action, result
|
||||
|
||||
possible_rooms = ["lobby", "hallway", "parking", "roof", "pharmacy"]
|
||||
|
||||
class CTRLStoryManager(ConstrainedStoryManager):
|
||||
def __init__(self, generator, action_verbs_key="anything"):
|
||||
@@ -204,18 +217,17 @@ class CTRLStoryManager(ConstrainedStoryManager):
|
||||
def get_constrained_movement_options(self):
|
||||
options = {}
|
||||
options["word_whitelist"] = dict()
|
||||
options["word_whitelist"][0] = get_possible_verbs(type="movement")
|
||||
options["word_whitelist"][0] = get_ctrl_verbs("movement")
|
||||
options["word_whitelist"][1] = ["to"]
|
||||
options["word_whitelist"][2] = ["the"]
|
||||
options["word_whitelist"][3] = \
|
||||
[room for room in possible_rooms if room is not self.story.game_state["current_room"]]
|
||||
[room for room in get_rooms("haunted_hospital") if room is not self.story.game_state["current_room"]]
|
||||
options["word_whitelist"][4] = ["and"]
|
||||
options["word_whitelist"][5] = ["see"]
|
||||
|
||||
return options, 3
|
||||
|
||||
|
||||
def get_action_results(self):
|
||||
def get_action_results_generate(self):
|
||||
results = []
|
||||
options, location_pos = self.get_constrained_movement_options()
|
||||
for phrase in self.action_phrases:
|
||||
@@ -225,35 +237,3 @@ class CTRLStoryManager(ConstrainedStoryManager):
|
||||
|
||||
results.append(result)
|
||||
return results
|
||||
|
||||
|
||||
class CachedStoryManager(ConstrainedStoryManager):
|
||||
|
||||
def start_new_story(self, prompt, seed=0):
|
||||
|
||||
result = self.cacher.retrieve_from_cache(seed, [], "story")
|
||||
if result is not None:
|
||||
story_start = prompt + result
|
||||
self.story = Story(story_start, seed=seed)
|
||||
else:
|
||||
story_start = super().start_new_story(prompt)
|
||||
self.story.seed = seed
|
||||
self.cacher.cache_file(seed, [], story_start, "story")
|
||||
|
||||
self.story.possible_action_results = None
|
||||
|
||||
return story_start
|
||||
|
||||
def get_action_results(self):
|
||||
|
||||
response = self.cacher.retrieve_from_cache(self.story.seed, self.story.choices, "choices")
|
||||
|
||||
if response is not None:
|
||||
action_results = json.loads(response)
|
||||
else:
|
||||
print("Not found in cache. Generating...")
|
||||
action_results = super().get_action_results()
|
||||
response = json.dumps(action_results)
|
||||
self.cacher.cache_file(self.story.seed, self.story.choices, response, "choices")
|
||||
|
||||
return action_results
|
||||
|
||||
+14
-11
@@ -20,6 +20,20 @@ def get_action_verbs(key):
|
||||
return data_loaded["action_verbs"][key]
|
||||
|
||||
|
||||
def get_ctrl_verbs(key):
|
||||
with open(YAML_FILE, 'r') as stream:
|
||||
data_loaded = yaml.safe_load(stream)
|
||||
|
||||
return data_loaded["ctrl_verbs"][key]
|
||||
|
||||
|
||||
def get_rooms(key):
|
||||
with open(YAML_FILE, 'r') as stream:
|
||||
data_loaded = yaml.safe_load(stream)
|
||||
|
||||
return data_loaded["rooms"][key]
|
||||
|
||||
|
||||
def remove_profanity(text):
|
||||
return pf.censor(text)
|
||||
|
||||
@@ -132,14 +146,3 @@ def second_to_first_person(text):
|
||||
|
||||
return capitalize_first_letters(text)
|
||||
|
||||
|
||||
possible_verbs = ["take", "put", "give", "set", "keep", "help", "show", "pay", "read", "start", "stay", "call",
|
||||
"change", "ask", "open", "look", "make", "say", "tell", "attack", "use", "fight", "scream", "yell"]
|
||||
|
||||
movement_verbs = ["walk", "go", "run", "move"]
|
||||
|
||||
def get_possible_verbs(type=""):
|
||||
if type is "movement":
|
||||
return movement_verbs
|
||||
else:
|
||||
return possible_verbs
|
||||
Reference in New Issue
Block a user