From 2a4efc3d880601a7c607f0a7792fc92a6c514f5f Mon Sep 17 00:00:00 2001 From: cloveranon Date: Mon, 23 Dec 2019 03:21:26 -0500 Subject: [PATCH] Finally merged changes to a working state, move configparser and logging code to new file, and added config option to switch between cpu --- config.ini | 12 ++++++++++- getconfig.py | 24 ++++++++++++++++++++++ gpt2generator.py | 9 ++++---- new-requirements => new-requirements.txt | 0 play.py | 26 ++++-------------------- story/utils.py | 10 ++++----- 6 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 getconfig.py rename new-requirements => new-requirements.txt (100%) diff --git a/config.ini b/config.ini index 68f90dd..ba7f671 100644 --- a/config.ini +++ b/config.ini @@ -35,9 +35,19 @@ console-bell = on # Not sure of a good default but 80 was considered an ideal standard number of columns in old PCs. text-wrap-width = 80 +cpu = off + #Tensorflow log level # seems to give informative messages about what options your CPU and tensorflow install supports. Not sure what a good default should be. -log-level = 3 +log-level = 10 + +action-alternatives = 5 + +action-generate-num = 20 + +action-temp = 1 + +action-min-length = 1 #ECMA-48 set graphics codes #Check out "man console_codes" diff --git a/getconfig.py b/getconfig.py new file mode 100644 index 0000000..39675d0 --- /dev/null +++ b/getconfig.py @@ -0,0 +1,24 @@ +import configparser +config = configparser.ConfigParser() +config.read('config.ini') +settings=config['Settings'] +colors=config['Colors'] + +import logging + +logger = logging.getLogger(__name__) +logLevel = settings.getint('log-level') +oneLevelUp = 20 + +#I don't know if this will work before loading the transformers module? +#silence transformers outputs when loading model +logging.getLogger("transformers.tokenization_utils").setLevel(logLevel+oneLevelUp) +logging.getLogger("transformers.modeling_utils").setLevel(logLevel+oneLevelUp) +logging.getLogger("transformers.configuration_utils").setLevel(logLevel+oneLevelUp) + +logging.basicConfig( + format='%(asctime)s - %(levelname)s - %(message)s', + datefmt='%m/%d/%Y %H:%M%S', + level=logLevel+oneLevelUp +) +logger.setLevel(logLevel) diff --git a/gpt2generator.py b/gpt2generator.py index 8c21c01..94c8ff0 100644 --- a/gpt2generator.py +++ b/gpt2generator.py @@ -4,7 +4,8 @@ import torch.nn.functional as F from transformers import GPT2LMHeadModel, GPT2Tokenizer -from story.utils import cut_trailing_sentence, logger +from getconfig import settings, logger +from story.utils import cut_trailing_sentence # warnings.filterwarnings("ignore") MODEL_CLASSES = { @@ -103,17 +104,15 @@ class GPT2Generator: self.top_p = top_p self.censor = censor self.samples = 1 - self.dtype = torch.half + self.dtype = torch.float32 if settings.getboolean('cpu') else torch.float16 self.repetition_penalty = repetition_penalty self.batch_size = 1 self.stop_token = None self.model_name = "pytorch-gpt2-xl-aid2-v5" - # self.model_name = "model_v5_pytorch_half" self.model_dir = "models" self.checkpoint_path = os.path.join(self.model_dir, self.model_name) - # self.checkpoint_path = 'gpt2' # DEBUG quick test of a smaller untrained model - self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") + self.device = torch.device("cuda" if torch.cuda.is_available() and not settings.getboolean('cpu') else "cpu") logger.info("Using device={}, checkpoint={}".format(self.device, self.checkpoint_path)) # Load tokenizer and model diff --git a/new-requirements b/new-requirements.txt similarity index 100% rename from new-requirements rename to new-requirements.txt diff --git a/play.py b/play.py index 3c77bf3..fff9cbb 100644 --- a/play.py +++ b/play.py @@ -1,31 +1,19 @@ -import configparser import gc import textwrap -import logging from pathlib import Path from random import shuffle from shutil import get_terminal_size +from getconfig import settings, colors, logger from story.story_manager import * from story.utils import * from gpt2generator import GPT2Generator -#silence transformers outputs when loading model -logging.getLogger("transformers.tokenization_utils").setLevel(logging.WARN) -logging.getLogger("transformers.modeling_utils").setLevel(logging.WARN) -logging.getLogger("transformers.configuration_utils").setLevel(logging.WARN) -logging.basicConfig( - format='%(asctime)s - %(levelnames)s - %(messages)s', - datefmt='%m/%d/%Y %H:%M%S', - level=logging.INFO -) -logger.setLevel(10)#settings['log-level']) - -#TODO: Move all these utilty functions to seperate utily and config file +#TODO: Move all these utilty functions to seperate utily file #add color for windows users that install colorama -# It is not necessary to install colorama on most systems, but it does come with pip +# It is not necessary to install colorama on most systems try: import colorama colorama.init() @@ -35,12 +23,6 @@ except ModuleNotFoundError: with open(Path('interface', 'clover'), 'r', encoding='utf-8') as file: print(file.read()) -config = configparser.ConfigParser() -config.read('config.ini') -settings=config["Settings"] -colors=config["Colors"] - - #ECMA-48 set graphics codes for the curious. Check out "man console_codes" def colPrint(str, col='0', wrap=True): @@ -108,7 +90,7 @@ class AIPlayer: def get_action(self, prompt): result_raw = self.generator.generate_raw( - prompt, generate_num=settings.getint('action-generate-num'), temperature=settings.getint('temp')) + prompt, generate_num=settings.getint('action-generate-num'), temperature=settings.getint('action-temp')) return clean_suggested_action(result_raw, min_length=settings.getint('action-min-length')) def get_actions(self, prompt): diff --git a/story/utils.py b/story/utils.py index 6946702..5ae29b6 100644 --- a/story/utils.py +++ b/story/utils.py @@ -1,12 +1,10 @@ # coding: utf-8 import re -import logging +#TODO: try to get rid of this from pyjarowinkler import distance -logger = logging.getLogger(__name__) - - +from getconfig import logger @@ -82,7 +80,7 @@ def remove_profanity(text): def cut_trailing_quotes(text): num_quotes = text.count('"') - if num_quotes % 2 is 0: + if num_quotes % 2 == 0: return text else: final_ind = text.rfind('"') @@ -222,7 +220,7 @@ def mapping_variation_pairs(mapping): ) # Change you it's before a punctuation - if mapping[0] is "you": + if mapping[0] == "you": mapping = ("you", "me") mapping_list.append((" " + mapping[0] + ",", " " + mapping[1] + ",")) mapping_list.append((" " + mapping[0] + "\?", " " + mapping[1] + "\?"))