mirror of
https://github.com/oobabooga/text-generation-webui.git
synced 2024-11-26 17:50:22 +01:00
Simplify some chat functions
This commit is contained in:
parent
04b98a8485
commit
435f8cc0e7
@ -10,7 +10,6 @@ from pathlib import Path
|
|||||||
import yaml
|
import yaml
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
import modules.extensions as extensions_module
|
|
||||||
import modules.shared as shared
|
import modules.shared as shared
|
||||||
from modules.extensions import apply_extensions
|
from modules.extensions import apply_extensions
|
||||||
from modules.html_generator import chat_html_wrapper, make_thumbnail
|
from modules.html_generator import chat_html_wrapper, make_thumbnail
|
||||||
@ -30,8 +29,8 @@ def generate_chat_prompt(user_input, state, **kwargs):
|
|||||||
chat_prompt_size = state['chat_prompt_size']
|
chat_prompt_size = state['chat_prompt_size']
|
||||||
if shared.soft_prompt:
|
if shared.soft_prompt:
|
||||||
chat_prompt_size -= shared.soft_prompt_tensor.shape[1]
|
chat_prompt_size -= shared.soft_prompt_tensor.shape[1]
|
||||||
max_length = min(get_max_prompt_length(state), chat_prompt_size)
|
|
||||||
|
|
||||||
|
max_length = min(get_max_prompt_length(state), chat_prompt_size)
|
||||||
if is_instruct:
|
if is_instruct:
|
||||||
prefix1 = f"{state['name1']}\n"
|
prefix1 = f"{state['name1']}\n"
|
||||||
prefix2 = f"{state['name2']}\n"
|
prefix2 = f"{state['name2']}\n"
|
||||||
@ -57,7 +56,6 @@ def generate_chat_prompt(user_input, state, **kwargs):
|
|||||||
min_rows = 2
|
min_rows = 2
|
||||||
rows.append(f"{prefix1.strip() if not is_instruct else prefix1}")
|
rows.append(f"{prefix1.strip() if not is_instruct else prefix1}")
|
||||||
elif not _continue:
|
elif not _continue:
|
||||||
|
|
||||||
# Adding the user message
|
# Adding the user message
|
||||||
if len(user_input) > 0:
|
if len(user_input) > 0:
|
||||||
this_prefix1 = prefix1.replace('<|round|>', f'{len(shared.history["internal"])}') # for ChatGLM
|
this_prefix1 = prefix1.replace('<|round|>', f'{len(shared.history["internal"])}') # for ChatGLM
|
||||||
@ -68,8 +66,8 @@ def generate_chat_prompt(user_input, state, **kwargs):
|
|||||||
|
|
||||||
while len(rows) > min_rows and len(encode(''.join(rows))[0]) >= max_length:
|
while len(rows) > min_rows and len(encode(''.join(rows))[0]) >= max_length:
|
||||||
rows.pop(1)
|
rows.pop(1)
|
||||||
prompt = ''.join(rows)
|
|
||||||
|
|
||||||
|
prompt = ''.join(rows)
|
||||||
if also_return_rows:
|
if also_return_rows:
|
||||||
return prompt, rows
|
return prompt, rows
|
||||||
else:
|
else:
|
||||||
@ -81,6 +79,7 @@ def get_stopping_strings(state):
|
|||||||
stopping_strings = [f"\n{state['name1']}", f"\n{state['name2']}"]
|
stopping_strings = [f"\n{state['name1']}", f"\n{state['name2']}"]
|
||||||
else:
|
else:
|
||||||
stopping_strings = [f"\n{state['name1']}:", f"\n{state['name2']}:"]
|
stopping_strings = [f"\n{state['name1']}:", f"\n{state['name2']}:"]
|
||||||
|
|
||||||
stopping_strings += ast.literal_eval(f"[{state['custom_stopping_strings']}]")
|
stopping_strings += ast.literal_eval(f"[{state['custom_stopping_strings']}]")
|
||||||
return stopping_strings
|
return stopping_strings
|
||||||
|
|
||||||
@ -111,13 +110,13 @@ def extract_message_from_reply(reply, state):
|
|||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
return reply, next_character_found
|
return reply, next_character_found
|
||||||
|
|
||||||
|
|
||||||
def chatbot_wrapper(text, state, regenerate=False, _continue=False):
|
def chatbot_wrapper(text, state, regenerate=False, _continue=False):
|
||||||
|
|
||||||
if shared.model_name == 'None' or shared.model is None:
|
if shared.model_name == 'None' or shared.model is None:
|
||||||
print("No model is loaded! Select one in the Model tab.")
|
print("No model is loaded! Select one in the Model tab.")
|
||||||
yield shared.history['visible']
|
yield shared.history['visible']
|
||||||
@ -125,18 +124,30 @@ def chatbot_wrapper(text, state, regenerate=False, _continue=False):
|
|||||||
|
|
||||||
# Defining some variables
|
# Defining some variables
|
||||||
cumulative_reply = ''
|
cumulative_reply = ''
|
||||||
last_reply = [shared.history['internal'][-1][1], shared.history['visible'][-1][1]] if _continue else None
|
|
||||||
just_started = True
|
just_started = True
|
||||||
visible_text = None
|
visible_text = None
|
||||||
eos_token = '\n' if state['stop_at_newline'] else None
|
eos_token = '\n' if state['stop_at_newline'] else None
|
||||||
stopping_strings = get_stopping_strings(state)
|
stopping_strings = get_stopping_strings(state)
|
||||||
|
|
||||||
text, visible_text = apply_extensions('input_hijack', text, visible_text)
|
# Preparing the input
|
||||||
|
if not any((regenerate, _continue)):
|
||||||
|
text, visible_text = apply_extensions('input_hijack', text, visible_text)
|
||||||
|
if visible_text is None:
|
||||||
|
visible_text = text
|
||||||
|
|
||||||
if visible_text is None:
|
text = apply_extensions('input', text)
|
||||||
visible_text = text
|
# *Is typing...*
|
||||||
if not _continue:
|
yield shared.history['visible'] + [[visible_text, shared.processing_message]]
|
||||||
text = apply_extensions("input", text)
|
else:
|
||||||
|
text, visible_text = shared.history['internal'][-1][0], shared.history['visible'][-1][0]
|
||||||
|
if regenerate:
|
||||||
|
shared.history['visible'].pop()
|
||||||
|
shared.history['internal'].pop()
|
||||||
|
# *Is typing...*
|
||||||
|
yield shared.history['visible'] + [[visible_text, shared.processing_message]]
|
||||||
|
elif _continue:
|
||||||
|
last_reply = [shared.history['internal'][-1][1], shared.history['visible'][-1][1]]
|
||||||
|
yield shared.history['visible'][:-1] + [[visible_text, last_reply[1] + '...']]
|
||||||
|
|
||||||
# Generating the prompt
|
# Generating the prompt
|
||||||
kwargs = {'_continue': _continue}
|
kwargs = {'_continue': _continue}
|
||||||
@ -144,10 +155,6 @@ def chatbot_wrapper(text, state, regenerate=False, _continue=False):
|
|||||||
if prompt is None:
|
if prompt is None:
|
||||||
prompt = generate_chat_prompt(text, state, **kwargs)
|
prompt = generate_chat_prompt(text, state, **kwargs)
|
||||||
|
|
||||||
# Yield *Is typing...*
|
|
||||||
if not any((regenerate, _continue)):
|
|
||||||
yield shared.history['visible'] + [[visible_text, shared.processing_message]]
|
|
||||||
|
|
||||||
# Generate
|
# Generate
|
||||||
for i in range(state['chat_generation_attempts']):
|
for i in range(state['chat_generation_attempts']):
|
||||||
reply = None
|
reply = None
|
||||||
@ -158,26 +165,26 @@ def chatbot_wrapper(text, state, regenerate=False, _continue=False):
|
|||||||
reply, next_character_found = extract_message_from_reply(reply, state)
|
reply, next_character_found = extract_message_from_reply(reply, state)
|
||||||
visible_reply = re.sub("(<USER>|<user>|{{user}})", state['name1'], reply)
|
visible_reply = re.sub("(<USER>|<user>|{{user}})", state['name1'], reply)
|
||||||
visible_reply = apply_extensions("output", visible_reply)
|
visible_reply = apply_extensions("output", visible_reply)
|
||||||
|
if _continue:
|
||||||
|
sep = ' ' if last_reply[0][-1] not in [' ', '\n'] else ''
|
||||||
|
reply = last_reply[0] + sep + reply
|
||||||
|
sep = ' ' if last_reply[1][-1] not in [' ', '\n'] else ''
|
||||||
|
visible_reply = last_reply[1] + sep + visible_reply
|
||||||
|
|
||||||
# We need this global variable to handle the Stop event,
|
# We need this global variable to handle the Stop event,
|
||||||
# otherwise gradio gets confused
|
# otherwise gradio gets confused
|
||||||
if shared.stop_everything:
|
if shared.stop_everything:
|
||||||
return shared.history['visible']
|
return shared.history['visible']
|
||||||
|
|
||||||
if just_started:
|
if just_started:
|
||||||
just_started = False
|
just_started = False
|
||||||
if not _continue:
|
if not _continue:
|
||||||
shared.history['internal'].append(['', ''])
|
shared.history['internal'].append(['', ''])
|
||||||
shared.history['visible'].append(['', ''])
|
shared.history['visible'].append(['', ''])
|
||||||
|
|
||||||
if _continue:
|
shared.history['internal'][-1] = [text, reply]
|
||||||
sep = list(map(lambda x: ' ' if len(x) > 0 and x[-1] != ' ' else '', last_reply))
|
shared.history['visible'][-1] = [visible_text, visible_reply]
|
||||||
shared.history['internal'][-1] = [text, f'{last_reply[0]}{sep[0]}{reply}']
|
yield shared.history['visible']
|
||||||
shared.history['visible'][-1] = [visible_text, f'{last_reply[1]}{sep[1]}{visible_reply}']
|
|
||||||
else:
|
|
||||||
shared.history['internal'][-1] = [text, reply]
|
|
||||||
shared.history['visible'][-1] = [visible_text, visible_reply]
|
|
||||||
if not shared.args.no_stream:
|
|
||||||
yield shared.history['visible']
|
|
||||||
if next_character_found:
|
if next_character_found:
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -188,7 +195,6 @@ def chatbot_wrapper(text, state, regenerate=False, _continue=False):
|
|||||||
|
|
||||||
|
|
||||||
def impersonate_wrapper(text, state):
|
def impersonate_wrapper(text, state):
|
||||||
|
|
||||||
if shared.model_name == 'None' or shared.model is None:
|
if shared.model_name == 'None' or shared.model is None:
|
||||||
print("No model is loaded! Select one in the Model tab.")
|
print("No model is loaded! Select one in the Model tab.")
|
||||||
yield ''
|
yield ''
|
||||||
@ -202,7 +208,6 @@ def impersonate_wrapper(text, state):
|
|||||||
|
|
||||||
# Yield *Is typing...*
|
# Yield *Is typing...*
|
||||||
yield shared.processing_message
|
yield shared.processing_message
|
||||||
|
|
||||||
for i in range(state['chat_generation_attempts']):
|
for i in range(state['chat_generation_attempts']):
|
||||||
reply = None
|
reply = None
|
||||||
for reply in generate_reply(f"{prompt}{' ' if len(cumulative_reply) > 0 else ''}{cumulative_reply}", state, eos_token=eos_token, stopping_strings=stopping_strings):
|
for reply in generate_reply(f"{prompt}{' ' if len(cumulative_reply) > 0 else ''}{cumulative_reply}", state, eos_token=eos_token, stopping_strings=stopping_strings):
|
||||||
@ -227,23 +232,16 @@ def regenerate_wrapper(text, state):
|
|||||||
if (len(shared.history['visible']) == 1 and not shared.history['visible'][0][0]) or len(shared.history['internal']) == 0:
|
if (len(shared.history['visible']) == 1 and not shared.history['visible'][0][0]) or len(shared.history['internal']) == 0:
|
||||||
yield chat_html_wrapper(shared.history['visible'], state['name1'], state['name2'], state['mode'])
|
yield chat_html_wrapper(shared.history['visible'], state['name1'], state['name2'], state['mode'])
|
||||||
else:
|
else:
|
||||||
last_visible = shared.history['visible'].pop()
|
for history in chatbot_wrapper('', state, regenerate=True):
|
||||||
last_internal = shared.history['internal'].pop()
|
yield chat_html_wrapper(history, state['name1'], state['name2'], state['mode'])
|
||||||
# Yield '*Is typing...*'
|
|
||||||
yield chat_html_wrapper(shared.history['visible'] + [[last_visible[0], shared.processing_message]], state['name1'], state['name2'], state['mode'])
|
|
||||||
for history in chatbot_wrapper(last_internal[0], state, regenerate=True):
|
|
||||||
shared.history['visible'][-1] = [last_visible[0], history[-1][1]]
|
|
||||||
yield chat_html_wrapper(shared.history['visible'], state['name1'], state['name2'], state['mode'])
|
|
||||||
|
|
||||||
|
|
||||||
def continue_wrapper(text, state):
|
def continue_wrapper(text, state):
|
||||||
if (len(shared.history['visible']) == 1 and not shared.history['visible'][0][0]) or len(shared.history['internal']) == 0:
|
if (len(shared.history['visible']) == 1 and not shared.history['visible'][0][0]) or len(shared.history['internal']) == 0:
|
||||||
yield chat_html_wrapper(shared.history['visible'], state['name1'], state['name2'], state['mode'])
|
yield chat_html_wrapper(shared.history['visible'], state['name1'], state['name2'], state['mode'])
|
||||||
else:
|
else:
|
||||||
# Yield ' ...'
|
for history in chatbot_wrapper('', state, _continue=True):
|
||||||
yield chat_html_wrapper(shared.history['visible'][:-1] + [[shared.history['visible'][-1][0], shared.history['visible'][-1][1] + ' ...']], state['name1'], state['name2'], state['mode'])
|
yield chat_html_wrapper(history, state['name1'], state['name2'], state['mode'])
|
||||||
for history in chatbot_wrapper(shared.history['internal'][-1][0], state, _continue=True):
|
|
||||||
yield chat_html_wrapper(shared.history['visible'], state['name1'], state['name2'], state['mode'])
|
|
||||||
|
|
||||||
|
|
||||||
def remove_last_message(name1, name2, mode):
|
def remove_last_message(name1, name2, mode):
|
||||||
@ -281,6 +279,7 @@ def send_dummy_reply(text, name1, name2, mode):
|
|||||||
if len(shared.history['visible']) > 0 and not shared.history['visible'][-1][1] == '':
|
if len(shared.history['visible']) > 0 and not shared.history['visible'][-1][1] == '':
|
||||||
shared.history['visible'].append(['', ''])
|
shared.history['visible'].append(['', ''])
|
||||||
shared.history['internal'].append(['', ''])
|
shared.history['internal'].append(['', ''])
|
||||||
|
|
||||||
shared.history['visible'][-1][1] = text
|
shared.history['visible'][-1][1] = text
|
||||||
shared.history['internal'][-1][1] = apply_extensions("input", text)
|
shared.history['internal'][-1][1] = apply_extensions("input", text)
|
||||||
return chat_html_wrapper(shared.history['visible'], name1, name2, mode)
|
return chat_html_wrapper(shared.history['visible'], name1, name2, mode)
|
||||||
@ -300,7 +299,6 @@ def clear_chat_log(name1, name2, greeting, mode):
|
|||||||
|
|
||||||
# Save cleared logs
|
# Save cleared logs
|
||||||
save_history(mode)
|
save_history(mode)
|
||||||
|
|
||||||
return chat_html_wrapper(shared.history['visible'], name1, name2, mode)
|
return chat_html_wrapper(shared.history['visible'], name1, name2, mode)
|
||||||
|
|
||||||
|
|
||||||
@ -321,8 +319,8 @@ def tokenize_dialogue(dialogue, name1, name2, mode):
|
|||||||
|
|
||||||
for i in range(len(idx) - 1):
|
for i in range(len(idx) - 1):
|
||||||
messages.append(dialogue[idx[i]:idx[i + 1]].strip())
|
messages.append(dialogue[idx[i]:idx[i + 1]].strip())
|
||||||
messages.append(dialogue[idx[-1]:].strip())
|
|
||||||
|
|
||||||
|
messages.append(dialogue[idx[-1]:].strip())
|
||||||
entry = ['', '']
|
entry = ['', '']
|
||||||
for i in messages:
|
for i in messages:
|
||||||
if i.startswith(f'{name1}:'):
|
if i.startswith(f'{name1}:'):
|
||||||
@ -331,6 +329,7 @@ def tokenize_dialogue(dialogue, name1, name2, mode):
|
|||||||
entry[1] = i[len(f'{name2}:'):].strip()
|
entry[1] = i[len(f'{name2}:'):].strip()
|
||||||
if not (len(entry[0]) == 0 and len(entry[1]) == 0):
|
if not (len(entry[0]) == 0 and len(entry[1]) == 0):
|
||||||
history.append(entry)
|
history.append(entry)
|
||||||
|
|
||||||
entry = ['', '']
|
entry = ['', '']
|
||||||
|
|
||||||
print("\033[1;32;1m\nDialogue tokenized to:\033[0;37;0m\n", end='')
|
print("\033[1;32;1m\nDialogue tokenized to:\033[0;37;0m\n", end='')
|
||||||
@ -339,6 +338,7 @@ def tokenize_dialogue(dialogue, name1, name2, mode):
|
|||||||
print("\n")
|
print("\n")
|
||||||
for line in column.strip().split('\n'):
|
for line in column.strip().split('\n'):
|
||||||
print("| " + line + "\n")
|
print("| " + line + "\n")
|
||||||
|
|
||||||
print("|\n")
|
print("|\n")
|
||||||
print("------------------------------")
|
print("------------------------------")
|
||||||
|
|
||||||
@ -351,14 +351,17 @@ def save_history(mode, timestamp=False):
|
|||||||
if mode == 'instruct':
|
if mode == 'instruct':
|
||||||
if not timestamp:
|
if not timestamp:
|
||||||
return
|
return
|
||||||
|
|
||||||
fname = f"Instruct_{datetime.now().strftime('%Y%m%d-%H%M%S')}.json"
|
fname = f"Instruct_{datetime.now().strftime('%Y%m%d-%H%M%S')}.json"
|
||||||
else:
|
else:
|
||||||
if timestamp:
|
if timestamp:
|
||||||
fname = f"{shared.character}_{datetime.now().strftime('%Y%m%d-%H%M%S')}.json"
|
fname = f"{shared.character}_{datetime.now().strftime('%Y%m%d-%H%M%S')}.json"
|
||||||
else:
|
else:
|
||||||
fname = f"{shared.character}_persistent.json"
|
fname = f"{shared.character}_persistent.json"
|
||||||
|
|
||||||
if not Path('logs').exists():
|
if not Path('logs').exists():
|
||||||
Path('logs').mkdir()
|
Path('logs').mkdir()
|
||||||
|
|
||||||
with open(Path(f'logs/{fname}'), 'w', encoding='utf-8') as f:
|
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))
|
f.write(json.dumps({'data': shared.history['internal'], 'data_visible': shared.history['visible']}, indent=2))
|
||||||
|
|
||||||
@ -389,8 +392,10 @@ def build_pygmalion_style_context(data):
|
|||||||
context = ""
|
context = ""
|
||||||
if 'char_persona' in data and data['char_persona'] != '':
|
if 'char_persona' in data and data['char_persona'] != '':
|
||||||
context += f"{data['char_name']}'s Persona: {data['char_persona']}\n"
|
context += f"{data['char_name']}'s Persona: {data['char_persona']}\n"
|
||||||
|
|
||||||
if 'world_scenario' in data and data['world_scenario'] != '':
|
if 'world_scenario' in data and data['world_scenario'] != '':
|
||||||
context += f"Scenario: {data['world_scenario']}\n"
|
context += f"Scenario: {data['world_scenario']}\n"
|
||||||
|
|
||||||
context = f"{context.strip()}\n<START>\n"
|
context = f"{context.strip()}\n<START>\n"
|
||||||
return context
|
return context
|
||||||
|
|
||||||
@ -405,6 +410,7 @@ def generate_pfp_cache(character):
|
|||||||
img = make_thumbnail(Image.open(path))
|
img = make_thumbnail(Image.open(path))
|
||||||
img.save(Path('cache/pfp_character.png'), format='PNG')
|
img.save(Path('cache/pfp_character.png'), format='PNG')
|
||||||
return img
|
return img
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -488,13 +494,17 @@ def upload_character(json_file, img, tavern=False):
|
|||||||
while Path(f'characters/{outfile_name}.json').exists():
|
while Path(f'characters/{outfile_name}.json').exists():
|
||||||
outfile_name = f'{data["char_name"]}_{i:03d}'
|
outfile_name = f'{data["char_name"]}_{i:03d}'
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
if tavern:
|
if tavern:
|
||||||
outfile_name = f'TavernAI-{outfile_name}'
|
outfile_name = f'TavernAI-{outfile_name}'
|
||||||
|
|
||||||
with open(Path(f'characters/{outfile_name}.json'), 'w', encoding='utf-8') as f:
|
with open(Path(f'characters/{outfile_name}.json'), 'w', encoding='utf-8') as f:
|
||||||
f.write(json_file)
|
f.write(json_file)
|
||||||
|
|
||||||
if img is not None:
|
if img is not None:
|
||||||
img = Image.open(io.BytesIO(img))
|
img = Image.open(io.BytesIO(img))
|
||||||
img.save(Path(f'characters/{outfile_name}.png'))
|
img.save(Path(f'characters/{outfile_name}.png'))
|
||||||
|
|
||||||
print(f'New character saved to "characters/{outfile_name}.json".')
|
print(f'New character saved to "characters/{outfile_name}.json".')
|
||||||
return outfile_name
|
return outfile_name
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user