From 8fa72525e0a99ee2fc371de96e46ba71a12a285c Mon Sep 17 00:00:00 2001 From: cloveranon Date: Wed, 18 Dec 2019 02:17:14 -0500 Subject: [PATCH] custom prompts are back --- .gitignore | 2 +- config.ini | 14 ++++---- interface/instructions.txt | 4 +-- interface/prompt-instructions.txt | 3 ++ play.py | 55 ++++++++++++++++++++----------- story/story_manager.py | 1 + story/utils.py | 15 --------- 7 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 interface/prompt-instructions.txt diff --git a/.gitignore b/.gitignore index 51da1f2..eca65bc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ venv/ saves/* story*.json !*.gitkeep -prompts/* +/prompts/* !/prompts/cloveranon-prompts/* diff --git a/config.ini b/config.ini index 23f9140..4cd0faf 100644 --- a/config.ini +++ b/config.ini @@ -67,14 +67,16 @@ loglevel = 3 [Colors] default = 0 error = 7 -loadingMessage = 7;34 +loading-message = 7;34 +message=7;35 title = 31 subtitle = 36 instructions = 33 -selectionPrompt = 7;32 -selectionValue = 35 +selection-prompt = 7;32 +selection-value = 35 menu = 36 -AIText = 37 -mainPrompt = 34 -userText = 36 +query = 7;42 +ai-text = 37 +main-prompt = 34 +user-text = 36 printStory = 37 diff --git a/interface/instructions.txt b/interface/instructions.txt index 4245120..ce1198b 100644 --- a/interface/instructions.txt +++ b/interface/instructions.txt @@ -1,11 +1,9 @@ -AI Dungeon 2 Instructions: +AID2: Clover Edition Instructions: Enter actions starting with a verb ex. "go to the tavern" or "attack the orc." To speak enter 'say "(thing you want to say)"' or just "(thing you want to say)" The following commands can be entered for any action: "revert" Reverts the last action allowing you to pick a different action. "quit" Quits the game and saves "restart" Starts a new game and saves your current one - "save" Makes a new save of your game and gives you the save ID - "load" Asks for a save ID and loads the game if the ID is valid "print" Prints a transcript of your adventure (without extra newline formatting) "help" Prints these instructions again diff --git a/interface/prompt-instructions.txt b/interface/prompt-instructions.txt new file mode 100644 index 0000000..3b3d635 --- /dev/null +++ b/interface/prompt-instructions.txt @@ -0,0 +1,3 @@ +Write a prompt that describes your character and starting situation in as little space as possible. +The first line will be permanently added to the AI's memory for this session. +The second line will eventually be forgotten and so can be longer and include temporary information. diff --git a/play.py b/play.py index a8e259f..959a325 100644 --- a/play.py +++ b/play.py @@ -32,10 +32,10 @@ def colInput(str, col1=colors["default"], col2=colors["default"]): return val def getNumberInput(n): - val=colInput("Enter a number from above:", colors["selectionPrompt"], colors["selectionValue"]) + val=colInput("Enter a number from above (default 0):", colors["selection-prompt"], colors["selection-value"]) if val=='': return 0 - elif 0>int(val) or int(val)>=n: + elif 0>int(val) or int(val)>n: colPrint("Invalid choice.", colors["error"]) return getNumberInput(n) else: @@ -47,7 +47,7 @@ def selectFile(p=Path('prompts')): shuffle(files) for n in range(len(files)): colPrint('{}: {}'.format(n, re.sub(r'\.txt$', '', files[n].name)), colors["menu"]) - return selectFile(files[getNumberInput(len(files))]) + return selectFile(files[getNumberInput(len(files)-1)]) else: with p.open() as file: line1=file.readline() @@ -66,7 +66,7 @@ def instructions(): def play(): - colPrint("\nInitializing AI Dungeon! (This might take a few minutes)\n", colors["loadingMessage"]) + colPrint("\nInitializing AI Dungeon! (This might take a few minutes)\n", colors["loading-message"]) generator = GPT2Generator( generate_num=settings.getint('generatenum'), temperature=settings.getfloat("temp"), @@ -91,21 +91,36 @@ def play(): del story_manager.story print("\n\n") - - context, prompt = selectFile() + + colPrint("0: Pick Prompt From File (Default if you type nothing)\n1: Write Custom Prompt", colors["menu"]) + + if getNumberInput(1) == 1: + with open(Path('interface', 'prompt-instructions.txt'), 'r') as file: + colPrint(file.read(), colors['instructions']) + context=colInput('Context>', colors['main-prompt'], colors['user-text']) + prompt=colInput('Prompt>', colors['main-prompt'], colors['user-text']) + filename=colInput('Name to save prompt as? (Leave blank for no save): ', colors['query'], colors['user-text']) + filename=re.sub('-$','',re.sub('^-', '', re.sub('[^a-zA-Z0-9_-]+', '-', filename)))+'.txt' + if filename != '': + with open(Path('prompts', filename), 'w') as f: + #this saves unix style line endings which might be an issue + #don't know how to do this properly + f.write(context+'\n'+prompt+'\n') + else: + context, prompt = selectFile() instructions() - colPrint("\nGenerating story...", colors["loadingMessage"]) + colPrint("\nGenerating story...", colors["loading-message"]) story_manager.start_new_story( prompt, context=context ) print("\n") - colPrint(str(story_manager.story), colors["AIText"]) + colPrint(str(story_manager.story), colors["ai-text"]) while True: - action = colInput("> ", colors["mainPrompt"], colors["userText"]) + action = colInput("> ", colors["main-prompt"], colors["user-text"]) if action == "restart": #rating = input("Please rate the story quality from 1-10: ") #rating_float = float(rating) @@ -160,11 +175,11 @@ def play(): story_manager.story.actions = story_manager.story.actions[:-1] story_manager.story.results = story_manager.story.results[:-1] - colPrint("Last action reverted. ", colors["loadingMessage"]) + colPrint("Last action reverted. ", colors["message"]) if len(story_manager.story.results) > 0: - colPrint(story_manager.story.results[-1], colors["AIText"]) + colPrint(story_manager.story.results[-1], colors["ai-text"]) else: - console_print(story_manager.story.story_start, colors["AIText"]) + colPrint(story_manager.story.story_start, colors["ai-text"]) continue else: @@ -172,7 +187,7 @@ def play(): action = "" result = story_manager.act(action) print('\x07', end='') - colPrint(result, colors["AIText"]) + colPrint(result, colors["ai-text"]) elif action[0] == '"': action = "You say " + action @@ -204,21 +219,21 @@ def play(): continue if player_won(result): - colPrint(result + "\n CONGRATS YOU WIN", colors["loadingMessage"]) + colPrint(result + "\n CONGRATS YOU WIN", colors["message"]) break elif player_died(result): - colPrint(result, colors["AIText"]) + colPrint(result, colors["ai-text"]) colPrint("YOU DIED. GAME OVER", colors["error"]) - console_print("\nOptions:\n0)Start a new game\n1)\"I'm not dead yet!\" (If you didn't actually die)", colors["menu"]) - choice = getNumberInput(2) + colPrint("\nOptions:\n0)Start a new game\n1)\"I'm not dead yet!\" (If you didn't actually die)", colors["menu"]) + choice = getNumberInput(1) if choice == 0: break else: - colPrint("Sorry about that...where were we?", colors["menu"]) - console_print(result, colors["AIText"]) + colPrint("Sorry about that...where were we?", colors["query"]) + colPrint(result, colors["ai-text"]) else: - colPrint(result, colors["AIText"]) + colPrint(result, colors["ai-text"]) play() diff --git a/story/story_manager.py b/story/story_manager.py index c1f17f6..6962c8a 100644 --- a/story/story_manager.py +++ b/story/story_manager.py @@ -89,6 +89,7 @@ class Story: return "".join(story_list) + #delet this def to_json(self): story_dict = {} story_dict["story_start"] = self.story_start diff --git a/story/utils.py b/story/utils.py index 7d21eaa..d160b71 100644 --- a/story/utils.py +++ b/story/utils.py @@ -21,21 +21,6 @@ def console_print(text, width=75): def get_similarity(a, b): return SequenceMatcher(None, a, b).ratio() -#delet this -def get_num_options(num): - - while True: - choice = input("Enter the number of your choice: ") - try: - result = int(choice) - if result >= 0 and result < num: - return result - else: - print("Error invalid choice. ") - except ValueError: - print("Error invalid choice. ") - - def player_died(text): """ TODO: Add in more sophisticated NLP, maybe a custom classifier