Finally merged changes to a working state, move configparser and logging code to new file, and added config option to switch between cpu

This commit is contained in:
cloveranon
2019-12-23 03:21:26 -05:00
parent 646b589bee
commit 2a4efc3d88
6 changed files with 47 additions and 34 deletions
+11 -1
View File
@@ -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"
+24
View File
@@ -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)
+4 -5
View File
@@ -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
+4 -22
View File
@@ -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):
+4 -6
View File
@@ -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] + "\?"))