2023-04-05 13:05:50 -04:00
|
|
|
import os
|
2023-04-21 16:20:59 -04:00
|
|
|
import warnings
|
2023-05-21 21:42:34 -04:00
|
|
|
|
|
|
|
from modules.logging_colors import logger
|
2023-07-07 01:24:52 -04:00
|
|
|
from modules.block_requests import OpenMonkeyPatch, RequestBlocker
|
2023-04-21 16:18:34 -04:00
|
|
|
|
2023-04-05 13:05:50 -04:00
|
|
|
os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
|
2023-04-13 11:04:39 -04:00
|
|
|
os.environ['BITSANDBYTES_NOWELCOME'] = '1'
|
2023-04-13 11:09:42 -04:00
|
|
|
warnings.filterwarnings('ignore', category=UserWarning, message='TypedStorage is deprecated')
|
2023-05-21 21:42:34 -04:00
|
|
|
|
2023-06-18 15:31:14 -04:00
|
|
|
with RequestBlocker():
|
|
|
|
import gradio as gr
|
2023-04-05 13:05:50 -04:00
|
|
|
|
2023-04-19 00:43:10 -04:00
|
|
|
import matplotlib
|
2023-05-03 20:43:17 -04:00
|
|
|
matplotlib.use('Agg') # This fixes LaTeX rendering on some systems
|
2023-04-19 00:43:10 -04:00
|
|
|
|
2023-02-10 13:40:03 -05:00
|
|
|
import json
|
2023-04-10 10:36:39 -04:00
|
|
|
import os
|
2023-04-12 20:24:26 -04:00
|
|
|
import sys
|
2023-02-10 13:40:03 -05:00
|
|
|
import time
|
2023-04-24 02:05:47 -04:00
|
|
|
from functools import partial
|
2023-01-07 14:33:43 -05:00
|
|
|
from pathlib import Path
|
2023-05-24 08:38:20 -04:00
|
|
|
from threading import Lock
|
2023-04-10 10:36:39 -04:00
|
|
|
|
2023-04-15 11:54:02 -04:00
|
|
|
import yaml
|
2023-05-07 22:47:02 -04:00
|
|
|
|
2023-02-23 10:05:25 -05:00
|
|
|
import modules.extensions as extensions_module
|
2023-08-06 20:49:27 -04:00
|
|
|
from modules import (
|
|
|
|
chat,
|
|
|
|
shared,
|
|
|
|
training,
|
|
|
|
ui,
|
|
|
|
ui_chat,
|
|
|
|
ui_default,
|
|
|
|
ui_file_saving,
|
|
|
|
ui_model_menu,
|
|
|
|
ui_notebook,
|
|
|
|
ui_parameters,
|
|
|
|
ui_session,
|
|
|
|
utils,
|
|
|
|
)
|
2023-05-16 23:03:39 -04:00
|
|
|
from modules.extensions import apply_extensions
|
2023-03-17 10:42:25 -04:00
|
|
|
from modules.LoRA import add_lora_to_model
|
2023-08-06 20:49:27 -04:00
|
|
|
from modules.models import load_model
|
2023-06-25 00:44:36 -04:00
|
|
|
from modules.models_settings import (
|
|
|
|
get_model_settings_from_yamls,
|
|
|
|
update_model_parameters
|
|
|
|
)
|
2023-07-03 23:03:30 -04:00
|
|
|
from modules.utils import gradio
|
2023-03-16 20:31:39 -04:00
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-04-14 10:07:28 -04:00
|
|
|
def create_interface():
|
|
|
|
|
|
|
|
title = 'Text generation web UI'
|
2023-01-19 12:03:47 -05:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Password authentication
|
|
|
|
auth = []
|
2023-05-23 19:39:26 -04:00
|
|
|
if shared.args.gradio_auth:
|
2023-08-06 20:49:27 -04:00
|
|
|
auth.extend(x.strip() for x in shared.args.gradio_auth.strip('"').replace('\n', '').split(',') if x.strip())
|
|
|
|
if shared.args.gradio_auth_path:
|
2023-04-14 10:07:28 -04:00
|
|
|
with open(shared.args.gradio_auth_path, 'r', encoding="utf8") as file:
|
2023-08-06 20:49:27 -04:00
|
|
|
auth.extend(x.strip() for line in file for x in line.split(',') if x.strip())
|
|
|
|
auth = [tuple(cred.split(':')) for cred in auth]
|
2023-03-15 11:01:32 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Import the extensions and execute their setup() functions
|
2023-03-15 22:29:56 -04:00
|
|
|
if shared.args.extensions is not None and len(shared.args.extensions) > 0:
|
|
|
|
extensions_module.load_extensions()
|
2023-03-15 11:01:32 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Force some events to be triggered on page load
|
2023-07-25 23:37:01 -04:00
|
|
|
shared.persistent_interface_state.update({
|
|
|
|
'loader': shared.args.loader or 'Transformers',
|
2023-08-13 00:12:15 -04:00
|
|
|
'mode': shared.settings['mode'],
|
|
|
|
'character_menu': shared.args.character or shared.settings['character'],
|
2023-08-14 10:46:07 -04:00
|
|
|
'instruction_template': shared.settings['instruction_template'],
|
|
|
|
'prompt_menu-default': shared.settings['prompt-default'],
|
|
|
|
'prompt_menu-notebook': shared.settings['prompt-notebook'],
|
2023-07-25 23:37:01 -04:00
|
|
|
})
|
|
|
|
|
2023-08-13 00:12:15 -04:00
|
|
|
if Path("cache/pfp_character.png").exists():
|
|
|
|
Path("cache/pfp_character.png").unlink()
|
2023-07-25 23:37:01 -04:00
|
|
|
|
2023-05-16 23:03:39 -04:00
|
|
|
# css/js strings
|
2023-08-13 00:12:15 -04:00
|
|
|
css = ui.css
|
|
|
|
js = ui.js
|
2023-05-16 23:03:39 -04:00
|
|
|
css += apply_extensions('css')
|
|
|
|
js += apply_extensions('js')
|
|
|
|
|
2023-08-13 00:12:15 -04:00
|
|
|
# Interface state elements
|
2023-08-06 20:49:27 -04:00
|
|
|
shared.input_elements = ui.list_interface_input_elements()
|
|
|
|
|
2023-05-16 23:03:39 -04:00
|
|
|
with gr.Blocks(css=css, analytics_enabled=False, title=title, theme=ui.theme) as shared.gradio['interface']:
|
2023-08-06 20:49:27 -04:00
|
|
|
|
2023-08-13 00:12:15 -04:00
|
|
|
# Interface state
|
|
|
|
shared.gradio['interface_state'] = gr.State({k: None for k in shared.input_elements})
|
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Audio notification
|
2023-05-19 22:16:06 -04:00
|
|
|
if Path("notification.mp3").exists():
|
|
|
|
shared.gradio['audio_notification'] = gr.Audio(interactive=False, value="notification.mp3", elem_id="audio_notification", visible=False)
|
2023-04-11 10:46:30 -04:00
|
|
|
|
2023-06-11 20:20:51 -04:00
|
|
|
# Floating menus for saving/deleting files
|
2023-08-06 20:49:27 -04:00
|
|
|
ui_file_saving.create_ui()
|
2023-06-11 20:20:51 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Temporary clipboard for saving files
|
2023-08-02 21:50:13 -04:00
|
|
|
shared.gradio['temporary_text'] = gr.Textbox(visible=False)
|
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Text Generation tab
|
2023-08-13 00:12:15 -04:00
|
|
|
ui_chat.create_ui()
|
|
|
|
ui_default.create_ui()
|
|
|
|
ui_notebook.create_ui()
|
2023-05-08 11:35:03 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
ui_parameters.create_ui(shared.settings['preset']) # Parameters tab
|
|
|
|
ui_model_menu.create_ui() # Model tab
|
|
|
|
training.create_ui() # Training tab
|
|
|
|
ui_session.create_ui() # Session tab
|
2023-03-15 22:29:56 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Generation events
|
2023-08-13 00:12:15 -04:00
|
|
|
ui_chat.create_event_handlers()
|
|
|
|
ui_default.create_event_handlers()
|
|
|
|
ui_notebook.create_event_handlers()
|
2023-06-11 11:19:18 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Other events
|
|
|
|
ui_file_saving.create_event_handlers()
|
|
|
|
ui_parameters.create_event_handlers()
|
|
|
|
ui_model_menu.create_event_handlers()
|
2023-05-22 18:45:11 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Interface launch events
|
2023-08-07 22:03:09 -04:00
|
|
|
if shared.settings['dark_theme']:
|
|
|
|
shared.gradio['interface'].load(lambda: None, None, None, _js="() => document.getElementsByTagName('body')[0].classList.add('dark')")
|
2023-08-13 00:12:15 -04:00
|
|
|
|
2023-07-25 23:37:01 -04:00
|
|
|
shared.gradio['interface'].load(lambda: None, None, None, _js=f"() => {{{js}}}")
|
|
|
|
shared.gradio['interface'].load(partial(ui.apply_interface_values, {}, use_persistent=True), None, gradio(ui.list_interface_input_elements()), show_progress=False)
|
2023-08-13 00:12:15 -04:00
|
|
|
shared.gradio['interface'].load(chat.redraw_html, gradio(ui_chat.reload_arr), gradio('display'))
|
2023-05-22 18:45:11 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
extensions_module.create_extensions_tabs() # Extensions tabs
|
|
|
|
extensions_module.create_extensions_block() # Extensions block
|
2023-04-24 02:05:47 -04:00
|
|
|
|
2023-03-15 22:29:56 -04:00
|
|
|
# Launch the interface
|
2023-08-07 22:57:25 -04:00
|
|
|
shared.gradio['interface'].queue(concurrency_count=64)
|
2023-07-07 01:24:52 -04:00
|
|
|
with OpenMonkeyPatch():
|
2023-08-04 12:57:31 -04:00
|
|
|
shared.gradio['interface'].launch(
|
|
|
|
prevent_thread_lock=True,
|
|
|
|
share=shared.args.share,
|
2023-08-06 20:49:27 -04:00
|
|
|
server_name=None if not shared.args.listen else (shared.args.listen_host or '0.0.0.0'),
|
2023-08-04 12:57:31 -04:00
|
|
|
server_port=shared.args.listen_port,
|
|
|
|
inbrowser=shared.args.auto_launch,
|
2023-08-06 20:49:27 -04:00
|
|
|
auth=auth or None,
|
2023-08-04 12:57:31 -04:00
|
|
|
ssl_verify=False if (shared.args.ssl_keyfile or shared.args.ssl_certfile) else True,
|
|
|
|
ssl_keyfile=shared.args.ssl_keyfile,
|
|
|
|
ssl_certfile=shared.args.ssl_certfile
|
|
|
|
)
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
|
2023-04-14 10:07:28 -04:00
|
|
|
if __name__ == "__main__":
|
2023-08-06 20:49:27 -04:00
|
|
|
|
|
|
|
# Load custom settings
|
2023-04-14 10:07:28 -04:00
|
|
|
settings_file = None
|
|
|
|
if shared.args.settings is not None and Path(shared.args.settings).exists():
|
|
|
|
settings_file = Path(shared.args.settings)
|
2023-05-28 21:34:12 -04:00
|
|
|
elif Path('settings.yaml').exists():
|
|
|
|
settings_file = Path('settings.yaml')
|
2023-04-14 10:07:28 -04:00
|
|
|
elif Path('settings.json').exists():
|
|
|
|
settings_file = Path('settings.json')
|
2023-05-12 05:09:45 -04:00
|
|
|
|
2023-04-14 10:07:28 -04:00
|
|
|
if settings_file is not None:
|
2023-05-21 21:42:34 -04:00
|
|
|
logger.info(f"Loading settings from {settings_file}...")
|
2023-05-28 21:34:12 -04:00
|
|
|
file_contents = open(settings_file, 'r', encoding='utf-8').read()
|
|
|
|
new_settings = json.loads(file_contents) if settings_file.suffix == "json" else yaml.safe_load(file_contents)
|
2023-08-06 20:49:27 -04:00
|
|
|
shared.settings.update(new_settings)
|
2023-04-14 10:07:28 -04:00
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Fallback settings for models
|
2023-05-12 05:09:45 -04:00
|
|
|
shared.model_config['.*'] = {
|
|
|
|
'wbits': 'None',
|
|
|
|
'model_type': 'None',
|
|
|
|
'groupsize': 'None',
|
|
|
|
'pre_layer': 0,
|
|
|
|
'mode': shared.settings['mode'],
|
|
|
|
'skip_special_tokens': shared.settings['skip_special_tokens'],
|
|
|
|
'custom_stopping_strings': shared.settings['custom_stopping_strings'],
|
2023-06-05 11:07:52 -04:00
|
|
|
'truncation_length': shared.settings['truncation_length'],
|
2023-07-24 18:26:29 -04:00
|
|
|
'n_gqa': 0,
|
|
|
|
'rms_norm_eps': 0,
|
2023-05-12 05:09:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
shared.model_config.move_to_end('.*', last=False) # Move to the beginning
|
|
|
|
|
2023-08-06 20:49:27 -04:00
|
|
|
# Activate the extensions listed on settings.yaml
|
2023-05-05 22:14:56 -04:00
|
|
|
extensions_module.available_extensions = utils.get_available_extensions()
|
2023-08-13 00:12:15 -04:00
|
|
|
for extension in shared.settings['default_extensions']:
|
|
|
|
shared.args.extensions = shared.args.extensions or []
|
|
|
|
if extension not in shared.args.extensions:
|
|
|
|
shared.args.extensions.append(extension)
|
2023-04-14 10:07:28 -04:00
|
|
|
|
2023-05-05 22:14:56 -04:00
|
|
|
available_models = utils.get_available_models()
|
2023-04-14 10:07:28 -04:00
|
|
|
|
|
|
|
# Model defined through --model
|
|
|
|
if shared.args.model is not None:
|
|
|
|
shared.model_name = shared.args.model
|
|
|
|
|
|
|
|
# Select the model from a command-line menu
|
|
|
|
elif shared.args.model_menu:
|
|
|
|
if len(available_models) == 0:
|
2023-05-21 21:42:34 -04:00
|
|
|
logger.error('No models are available! Please download at least one.')
|
2023-04-14 10:07:28 -04:00
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
print('The following models are available:\n')
|
|
|
|
for i, model in enumerate(available_models):
|
|
|
|
print(f'{i+1}. {model}')
|
2023-04-24 02:05:47 -04:00
|
|
|
|
2023-04-14 10:07:28 -04:00
|
|
|
print(f'\nWhich one do you want to load? 1-{len(available_models)}\n')
|
|
|
|
i = int(input()) - 1
|
|
|
|
print()
|
2023-04-24 02:05:47 -04:00
|
|
|
|
2023-04-14 10:07:28 -04:00
|
|
|
shared.model_name = available_models[i]
|
|
|
|
|
|
|
|
# If any model has been selected, load it
|
|
|
|
if shared.model_name != 'None':
|
2023-06-16 18:00:37 -04:00
|
|
|
model_settings = get_model_settings_from_yamls(shared.model_name)
|
2023-04-14 10:07:28 -04:00
|
|
|
shared.settings.update(model_settings) # hijacking the interface defaults
|
2023-04-14 14:24:03 -04:00
|
|
|
update_model_parameters(model_settings, initial=True) # hijacking the command-line arguments
|
2023-04-14 10:07:28 -04:00
|
|
|
|
|
|
|
# Load the model
|
|
|
|
shared.model, shared.tokenizer = load_model(shared.model_name)
|
|
|
|
if shared.args.lora:
|
2023-04-25 21:58:48 -04:00
|
|
|
add_lora_to_model(shared.args.lora)
|
2023-04-14 10:07:28 -04:00
|
|
|
|
2023-05-24 08:38:20 -04:00
|
|
|
shared.generation_lock = Lock()
|
2023-07-03 23:03:30 -04:00
|
|
|
|
2023-04-14 10:07:28 -04:00
|
|
|
# Launch the web UI
|
|
|
|
create_interface()
|
|
|
|
while True:
|
|
|
|
time.sleep(0.5)
|
|
|
|
if shared.need_restart:
|
|
|
|
shared.need_restart = False
|
2023-05-21 23:26:07 -04:00
|
|
|
time.sleep(0.5)
|
2023-04-14 10:07:28 -04:00
|
|
|
shared.gradio['interface'].close()
|
2023-05-16 21:27:43 -04:00
|
|
|
time.sleep(0.5)
|
2023-04-14 10:07:28 -04:00
|
|
|
create_interface()
|