Implement sessions + add basic multi-user support (#2991)
This commit is contained in:
parent
1f8cae14f9
commit
4b1804a438
17 changed files with 595 additions and 414 deletions
260
modules/chat.py
260
modules/chat.py
|
@ -3,7 +3,6 @@ import copy
|
|||
import functools
|
||||
import json
|
||||
import re
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
import gradio as gr
|
||||
|
@ -11,7 +10,6 @@ import yaml
|
|||
from PIL import Image
|
||||
|
||||
import modules.shared as shared
|
||||
from modules import utils
|
||||
from modules.extensions import apply_extensions
|
||||
from modules.html_generator import chat_html_wrapper, make_thumbnail
|
||||
from modules.logging_colors import logger
|
||||
|
@ -20,7 +18,12 @@ from modules.text_generation import (
|
|||
get_encoded_length,
|
||||
get_max_prompt_length
|
||||
)
|
||||
from modules.utils import delete_file, replace_all, save_file
|
||||
from modules.utils import (
|
||||
delete_file,
|
||||
get_available_characters,
|
||||
replace_all,
|
||||
save_file
|
||||
)
|
||||
|
||||
|
||||
def get_turn_substrings(state, instruct=False):
|
||||
|
@ -54,7 +57,7 @@ def generate_chat_prompt(user_input, state, **kwargs):
|
|||
impersonate = kwargs.get('impersonate', False)
|
||||
_continue = kwargs.get('_continue', False)
|
||||
also_return_rows = kwargs.get('also_return_rows', False)
|
||||
history = kwargs.get('history', shared.history)['internal']
|
||||
history = kwargs.get('history', state['history'])['internal']
|
||||
is_instruct = state['mode'] == 'instruct'
|
||||
|
||||
# Find the maximum prompt size
|
||||
|
@ -76,10 +79,10 @@ def generate_chat_prompt(user_input, state, **kwargs):
|
|||
if impersonate:
|
||||
wrapper += substrings['user_turn_stripped'].rstrip(' ')
|
||||
elif _continue:
|
||||
wrapper += apply_extensions("bot_prefix", substrings['bot_turn_stripped'])
|
||||
wrapper += apply_extensions('bot_prefix', substrings['bot_turn_stripped'], state)
|
||||
wrapper += history[-1][1]
|
||||
else:
|
||||
wrapper += apply_extensions("bot_prefix", substrings['bot_turn_stripped'].rstrip(' '))
|
||||
wrapper += apply_extensions('bot_prefix', substrings['bot_turn_stripped'].rstrip(' '), state)
|
||||
else:
|
||||
wrapper = '<|prompt|>'
|
||||
|
||||
|
@ -113,7 +116,7 @@ def generate_chat_prompt(user_input, state, **kwargs):
|
|||
|
||||
# Add the character prefix
|
||||
if state['mode'] != 'chat-instruct':
|
||||
rows.append(apply_extensions("bot_prefix", substrings['bot_turn_stripped'].rstrip(' ')))
|
||||
rows.append(apply_extensions('bot_prefix', substrings['bot_turn_stripped'].rstrip(' '), state))
|
||||
|
||||
while len(rows) > min_rows and get_encoded_length(wrapper.replace('<|prompt|>', ''.join(rows))) >= max_length:
|
||||
rows.pop(1)
|
||||
|
@ -153,7 +156,8 @@ def get_stopping_strings(state):
|
|||
return stopping_strings
|
||||
|
||||
|
||||
def chatbot_wrapper(text, history, state, regenerate=False, _continue=False, loading_message=True):
|
||||
def chatbot_wrapper(text, state, regenerate=False, _continue=False, loading_message=True):
|
||||
history = state['history']
|
||||
output = copy.deepcopy(history)
|
||||
output = apply_extensions('history', output)
|
||||
state = apply_extensions('state', state)
|
||||
|
@ -174,11 +178,11 @@ def chatbot_wrapper(text, history, state, regenerate=False, _continue=False, loa
|
|||
if visible_text is None:
|
||||
visible_text = text
|
||||
|
||||
text = apply_extensions('input', text, state)
|
||||
|
||||
# *Is typing...*
|
||||
if loading_message:
|
||||
yield {'visible': output['visible'] + [[visible_text, shared.processing_message]], 'internal': output['internal']}
|
||||
|
||||
text = apply_extensions('input', text)
|
||||
else:
|
||||
text, visible_text = output['internal'][-1][0], output['visible'][-1][0]
|
||||
if regenerate:
|
||||
|
@ -215,7 +219,7 @@ def chatbot_wrapper(text, history, state, regenerate=False, _continue=False, loa
|
|||
# We need this global variable to handle the Stop event,
|
||||
# otherwise gradio gets confused
|
||||
if shared.stop_everything:
|
||||
output['visible'][-1][1] = apply_extensions("output", output['visible'][-1][1])
|
||||
output['visible'][-1][1] = apply_extensions('output', output['visible'][-1][1], state)
|
||||
yield output
|
||||
return
|
||||
|
||||
|
@ -241,7 +245,7 @@ def chatbot_wrapper(text, history, state, regenerate=False, _continue=False, loa
|
|||
else:
|
||||
cumulative_reply = reply
|
||||
|
||||
output['visible'][-1][1] = apply_extensions("output", output['visible'][-1][1])
|
||||
output['visible'][-1][1] = apply_extensions('output', output['visible'][-1][1], state)
|
||||
yield output
|
||||
|
||||
|
||||
|
@ -274,14 +278,15 @@ def impersonate_wrapper(text, start_with, state):
|
|||
yield cumulative_reply.lstrip(' ')
|
||||
|
||||
|
||||
def generate_chat_reply(text, history, state, regenerate=False, _continue=False, loading_message=True):
|
||||
def generate_chat_reply(text, state, regenerate=False, _continue=False, loading_message=True):
|
||||
history = state['history']
|
||||
if regenerate or _continue:
|
||||
text = ''
|
||||
if (len(history['visible']) == 1 and not history['visible'][0][0]) or len(history['internal']) == 0:
|
||||
yield history
|
||||
return
|
||||
|
||||
for history in chatbot_wrapper(text, history, state, regenerate=regenerate, _continue=_continue, loading_message=loading_message):
|
||||
for history in chatbot_wrapper(text, state, regenerate=regenerate, _continue=_continue, loading_message=loading_message):
|
||||
yield history
|
||||
|
||||
|
||||
|
@ -296,144 +301,116 @@ def generate_chat_reply_wrapper(text, start_with, state, regenerate=False, _cont
|
|||
send_dummy_message(text)
|
||||
send_dummy_reply(start_with)
|
||||
|
||||
for i, history in enumerate(generate_chat_reply(text, shared.history, state, regenerate, _continue, loading_message=True)):
|
||||
if i != 0:
|
||||
shared.history = copy.deepcopy(history)
|
||||
|
||||
yield chat_html_wrapper(history['visible'], state['name1'], state['name2'], state['mode'], state['chat_style'])
|
||||
for i, history in enumerate(generate_chat_reply(text, state, regenerate, _continue, loading_message=True)):
|
||||
yield chat_html_wrapper(history, state['name1'], state['name2'], state['mode'], state['chat_style']), history
|
||||
|
||||
|
||||
def remove_last_message():
|
||||
if len(shared.history['visible']) > 0 and shared.history['internal'][-1][0] != '<|BEGIN-VISIBLE-CHAT|>':
|
||||
last = shared.history['visible'].pop()
|
||||
shared.history['internal'].pop()
|
||||
def remove_last_message(history):
|
||||
if len(history['visible']) > 0 and history['internal'][-1][0] != '<|BEGIN-VISIBLE-CHAT|>':
|
||||
last = history['visible'].pop()
|
||||
history['internal'].pop()
|
||||
else:
|
||||
last = ['', '']
|
||||
|
||||
return last[0]
|
||||
return last[0], history
|
||||
|
||||
|
||||
def send_last_reply_to_input():
|
||||
if len(shared.history['internal']) > 0:
|
||||
return shared.history['internal'][-1][1]
|
||||
def send_last_reply_to_input(history):
|
||||
if len(history['internal']) > 0:
|
||||
return history['internal'][-1][1]
|
||||
else:
|
||||
return ''
|
||||
|
||||
|
||||
def replace_last_reply(text):
|
||||
if len(shared.history['visible']) > 0:
|
||||
shared.history['visible'][-1][1] = text
|
||||
shared.history['internal'][-1][1] = apply_extensions("input", text)
|
||||
|
||||
|
||||
def send_dummy_message(text):
|
||||
shared.history['visible'].append([text, ''])
|
||||
shared.history['internal'].append([apply_extensions("input", text), ''])
|
||||
|
||||
|
||||
def send_dummy_reply(text):
|
||||
if len(shared.history['visible']) > 0 and not shared.history['visible'][-1][1] == '':
|
||||
shared.history['visible'].append(['', ''])
|
||||
shared.history['internal'].append(['', ''])
|
||||
|
||||
shared.history['visible'][-1][1] = text
|
||||
shared.history['internal'][-1][1] = apply_extensions("input", text)
|
||||
|
||||
|
||||
def clear_chat_log(greeting, mode):
|
||||
shared.history['visible'] = []
|
||||
shared.history['internal'] = []
|
||||
|
||||
if mode != 'instruct':
|
||||
if greeting != '':
|
||||
shared.history['internal'] += [['<|BEGIN-VISIBLE-CHAT|>', greeting]]
|
||||
shared.history['visible'] += [['', apply_extensions("output", greeting)]]
|
||||
|
||||
save_history(mode)
|
||||
|
||||
|
||||
def redraw_html(name1, name2, mode, style, reset_cache=False):
|
||||
return chat_html_wrapper(shared.history['visible'], name1, name2, mode, style, reset_cache=reset_cache)
|
||||
|
||||
|
||||
def tokenize_dialogue(dialogue, name1, name2):
|
||||
history = []
|
||||
messages = []
|
||||
dialogue = re.sub('<START>', '', dialogue)
|
||||
dialogue = re.sub('<start>', '', dialogue)
|
||||
dialogue = re.sub('(\n|^)[Aa]non:', '\\1You:', dialogue)
|
||||
dialogue = re.sub('(\n|^)\[CHARACTER\]:', f'\\g<1>{name2}:', dialogue)
|
||||
idx = [m.start() for m in re.finditer(f"(^|\n)({re.escape(name1)}|{re.escape(name2)}):", dialogue)]
|
||||
if len(idx) == 0:
|
||||
return history
|
||||
|
||||
for i in range(len(idx) - 1):
|
||||
messages.append(dialogue[idx[i]:idx[i + 1]].strip())
|
||||
|
||||
messages.append(dialogue[idx[-1]:].strip())
|
||||
entry = ['', '']
|
||||
for i in messages:
|
||||
if i.startswith(f'{name1}:'):
|
||||
entry[0] = i[len(f'{name1}:'):].strip()
|
||||
elif i.startswith(f'{name2}:'):
|
||||
entry[1] = i[len(f'{name2}:'):].strip()
|
||||
if not (len(entry[0]) == 0 and len(entry[1]) == 0):
|
||||
history.append(entry)
|
||||
|
||||
entry = ['', '']
|
||||
|
||||
print("\033[1;32;1m\nDialogue tokenized to:\033[0;37;0m\n", end='')
|
||||
for row in history:
|
||||
for column in row:
|
||||
print("\n")
|
||||
for line in column.strip().split('\n'):
|
||||
print("| " + line + "\n")
|
||||
|
||||
print("|\n")
|
||||
print("------------------------------")
|
||||
def replace_last_reply(text, state):
|
||||
history = state['history']
|
||||
if len(history['visible']) > 0:
|
||||
history['visible'][-1][1] = text
|
||||
history['internal'][-1][1] = apply_extensions('input', text, state)
|
||||
|
||||
return history
|
||||
|
||||
|
||||
def save_history(mode, timestamp=False, user_request=False):
|
||||
# Instruct mode histories should not be saved as if
|
||||
# Alpaca or Vicuna were characters
|
||||
if mode == 'instruct':
|
||||
if not timestamp:
|
||||
return
|
||||
|
||||
fname = f"Instruct_{datetime.now().strftime('%Y%m%d-%H%M%S')}.json"
|
||||
else:
|
||||
if shared.character == 'None' and not user_request:
|
||||
return
|
||||
|
||||
if timestamp:
|
||||
fname = f"{shared.character}_{datetime.now().strftime('%Y%m%d-%H%M%S')}.json"
|
||||
else:
|
||||
fname = f"{shared.character}_persistent.json"
|
||||
|
||||
if not Path('logs').exists():
|
||||
Path('logs').mkdir()
|
||||
|
||||
with open(Path(f'logs/{fname}'), 'w', encoding='utf-8') as f:
|
||||
f.write(json.dumps({'data': shared.history['internal'], 'data_visible': shared.history['visible']}, indent=2))
|
||||
|
||||
return Path(f'logs/{fname}')
|
||||
def send_dummy_message(text, state):
|
||||
history = state['history']
|
||||
history['visible'].append([text, ''])
|
||||
history['internal'].append([apply_extensions('input', text, state), ''])
|
||||
return history
|
||||
|
||||
|
||||
def load_history(file, name1, name2):
|
||||
file = file.decode('utf-8')
|
||||
def send_dummy_reply(text, state):
|
||||
history = state['history']
|
||||
if len(history['visible']) > 0 and not history['visible'][-1][1] == '':
|
||||
history['visible'].append(['', ''])
|
||||
history['internal'].append(['', ''])
|
||||
|
||||
history['visible'][-1][1] = text
|
||||
history['internal'][-1][1] = apply_extensions('input', text, state)
|
||||
return history
|
||||
|
||||
|
||||
def clear_chat_log(state):
|
||||
greeting = state['greeting']
|
||||
mode = state['mode']
|
||||
history = state['history']
|
||||
|
||||
history['visible'] = []
|
||||
history['internal'] = []
|
||||
if mode != 'instruct':
|
||||
if greeting != '':
|
||||
history['internal'] += [['<|BEGIN-VISIBLE-CHAT|>', greeting]]
|
||||
history['visible'] += [['', apply_extensions('output', greeting, state)]]
|
||||
|
||||
return history
|
||||
|
||||
|
||||
def redraw_html(history, name1, name2, mode, style, reset_cache=False):
|
||||
return chat_html_wrapper(history, name1, name2, mode, style, reset_cache=reset_cache)
|
||||
|
||||
|
||||
def save_history(history, path=None):
|
||||
p = path or Path('logs/exported_history.json')
|
||||
with open(p, 'w', encoding='utf-8') as f:
|
||||
f.write(json.dumps(history, indent=4))
|
||||
|
||||
return p
|
||||
|
||||
|
||||
def load_history(file, history):
|
||||
try:
|
||||
file = file.decode('utf-8')
|
||||
j = json.loads(file)
|
||||
if 'data' in j:
|
||||
shared.history['internal'] = j['data']
|
||||
if 'data_visible' in j:
|
||||
shared.history['visible'] = j['data_visible']
|
||||
else:
|
||||
shared.history['visible'] = copy.deepcopy(shared.history['internal'])
|
||||
if 'internal' in j and 'visible' in j:
|
||||
return j
|
||||
else:
|
||||
return history
|
||||
except:
|
||||
shared.history['internal'] = tokenize_dialogue(file, name1, name2)
|
||||
shared.history['visible'] = copy.deepcopy(shared.history['internal'])
|
||||
return history
|
||||
|
||||
|
||||
def save_persistent_history(history, character, mode):
|
||||
if mode in ['chat', 'chat-instruct'] and character not in ['', 'None', None] and not shared.args.multi_user:
|
||||
save_history(history, path=Path(f'logs/{character}_persistent.json'))
|
||||
|
||||
|
||||
def load_persistent_history(state):
|
||||
if shared.args.multi_user or state['mode'] == 'instruct':
|
||||
return state['history']
|
||||
|
||||
character = state['character_menu']
|
||||
greeting = state['greeting']
|
||||
p = Path(f'logs/{character}_persistent.json')
|
||||
if character not in ['None', '', None] and p.exists():
|
||||
f = json.loads(open(p, 'rb').read())
|
||||
if 'internal' in f and 'visible' in f:
|
||||
history = f
|
||||
else:
|
||||
history = {'internal': [], 'visible': []}
|
||||
if greeting != "":
|
||||
history['internal'] += [['<|BEGIN-VISIBLE-CHAT|>', greeting]]
|
||||
history['visible'] += [['', apply_extensions('output', greeting, state)]]
|
||||
|
||||
return history
|
||||
|
||||
|
||||
def replace_character_names(text, name1, name2):
|
||||
|
@ -468,7 +445,6 @@ def generate_pfp_cache(character):
|
|||
|
||||
|
||||
def load_character(character, name1, name2, instruct=False):
|
||||
shared.character = character
|
||||
context = greeting = turn_template = ""
|
||||
greeting_field = 'greeting'
|
||||
picture = None
|
||||
|
@ -477,7 +453,7 @@ def load_character(character, name1, name2, instruct=False):
|
|||
if Path("cache/pfp_character.png").exists():
|
||||
Path("cache/pfp_character.png").unlink()
|
||||
|
||||
if character != 'None':
|
||||
if character not in ['None', '', None]:
|
||||
folder = 'characters' if not instruct else 'characters/instruction-following'
|
||||
picture = generate_pfp_cache(character)
|
||||
for extension in ["yml", "yaml", "json"]:
|
||||
|
@ -527,20 +503,6 @@ def load_character(character, name1, name2, instruct=False):
|
|||
greeting = shared.settings['greeting']
|
||||
turn_template = shared.settings['turn_template']
|
||||
|
||||
if not instruct:
|
||||
shared.history['internal'] = []
|
||||
shared.history['visible'] = []
|
||||
if shared.character != 'None' and Path(f'logs/{shared.character}_persistent.json').exists():
|
||||
load_history(open(Path(f'logs/{shared.character}_persistent.json'), 'rb').read(), name1, name2)
|
||||
else:
|
||||
# Insert greeting if it exists
|
||||
if greeting != "":
|
||||
shared.history['internal'] += [['<|BEGIN-VISIBLE-CHAT|>', greeting]]
|
||||
shared.history['visible'] += [['', apply_extensions("output", greeting)]]
|
||||
|
||||
# Create .json log files since they don't already exist
|
||||
save_history('instruct' if instruct else 'chat')
|
||||
|
||||
return name1, name2, picture, greeting, context, turn_template.replace("\n", r"\n")
|
||||
|
||||
|
||||
|
@ -568,7 +530,7 @@ def upload_character(json_file, img, tavern=False):
|
|||
img.save(Path(f'characters/{outfile_name}.png'))
|
||||
|
||||
logger.info(f'New character saved to "characters/{outfile_name}.json".')
|
||||
return gr.update(value=outfile_name, choices=utils.get_available_characters())
|
||||
return gr.update(value=outfile_name, choices=get_available_characters())
|
||||
|
||||
|
||||
def upload_tavern_character(img, _json):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue