2023-03-24 15:51:27 -04:00
|
|
|
import traceback
|
|
|
|
|
2023-03-15 14:11:16 -04:00
|
|
|
import gradio as gr
|
|
|
|
|
2023-02-23 10:05:25 -05:00
|
|
|
import extensions
|
2023-02-23 12:41:42 -05:00
|
|
|
import modules.shared as shared
|
2023-02-23 10:05:25 -05:00
|
|
|
|
2023-02-24 08:01:21 -05:00
|
|
|
state = {}
|
2023-02-23 10:05:25 -05:00
|
|
|
available_extensions = []
|
2023-03-19 09:22:24 -04:00
|
|
|
setup_called = False
|
2023-02-23 10:05:25 -05:00
|
|
|
|
2023-02-23 12:49:02 -05:00
|
|
|
def load_extensions():
|
2023-02-24 08:01:21 -05:00
|
|
|
global state
|
|
|
|
for i, name in enumerate(shared.args.extensions):
|
|
|
|
if name in available_extensions:
|
|
|
|
print(f'Loading the extension "{name}"... ', end='')
|
2023-03-15 22:29:56 -04:00
|
|
|
try:
|
|
|
|
exec(f"import extensions.{name}.script")
|
|
|
|
state[name] = [True, i]
|
|
|
|
print('Ok.')
|
|
|
|
except:
|
|
|
|
print('Fail.')
|
2023-03-24 15:51:27 -04:00
|
|
|
traceback.print_exc()
|
2023-02-23 12:49:02 -05:00
|
|
|
|
2023-02-24 10:41:27 -05:00
|
|
|
# This iterator returns the extensions in the order specified in the command-line
|
2023-02-24 08:01:21 -05:00
|
|
|
def iterator():
|
|
|
|
for name in sorted(state, key=lambda x : state[x][1]):
|
|
|
|
if state[name][0] == True:
|
|
|
|
yield eval(f"extensions.{name}.script"), name
|
|
|
|
|
2023-02-24 10:41:27 -05:00
|
|
|
# Extension functions that map string -> string
|
2023-02-23 10:05:25 -05:00
|
|
|
def apply_extensions(text, typ):
|
2023-02-24 08:01:21 -05:00
|
|
|
for extension, _ in iterator():
|
|
|
|
if typ == "input" and hasattr(extension, "input_modifier"):
|
|
|
|
text = extension.input_modifier(text)
|
|
|
|
elif typ == "output" and hasattr(extension, "output_modifier"):
|
|
|
|
text = extension.output_modifier(text)
|
|
|
|
elif typ == "bot_prefix" and hasattr(extension, "bot_prefix_modifier"):
|
|
|
|
text = extension.bot_prefix_modifier(text)
|
2023-02-23 10:05:25 -05:00
|
|
|
return text
|
|
|
|
|
2023-02-23 12:55:21 -05:00
|
|
|
def create_extensions_block():
|
2023-03-19 09:25:49 -04:00
|
|
|
global setup_called
|
2023-03-24 15:51:27 -04:00
|
|
|
|
2023-02-24 17:00:11 -05:00
|
|
|
# Updating the default values
|
2023-02-24 08:01:21 -05:00
|
|
|
for extension, name in iterator():
|
2023-02-25 01:00:19 -05:00
|
|
|
if hasattr(extension, 'params'):
|
|
|
|
for param in extension.params:
|
|
|
|
_id = f"{name}-{param}"
|
|
|
|
if _id in shared.settings:
|
|
|
|
extension.params[param] = shared.settings[_id]
|
2023-02-23 12:55:21 -05:00
|
|
|
|
2023-03-19 09:31:21 -04:00
|
|
|
should_display_ui = False
|
2023-03-24 15:51:27 -04:00
|
|
|
|
2023-03-19 09:22:24 -04:00
|
|
|
# Running setup function
|
|
|
|
if not setup_called:
|
|
|
|
for extension, name in iterator():
|
|
|
|
if hasattr(extension, "setup"):
|
|
|
|
extension.setup()
|
2023-03-19 09:31:21 -04:00
|
|
|
if hasattr(extension, "ui"):
|
|
|
|
should_display_ui = True
|
2023-03-19 09:22:24 -04:00
|
|
|
setup_called = True
|
|
|
|
|
2023-02-24 17:00:11 -05:00
|
|
|
# Creating the extension ui elements
|
2023-03-19 09:31:21 -04:00
|
|
|
if should_display_ui:
|
2023-03-26 21:20:30 -04:00
|
|
|
with gr.Column(elem_id="extensions"):
|
2023-03-15 22:29:56 -04:00
|
|
|
for extension, name in iterator():
|
2023-03-26 21:20:30 -04:00
|
|
|
gr.Markdown(f"\n### {name}")
|
2023-03-15 22:29:56 -04:00
|
|
|
if hasattr(extension, "ui"):
|
|
|
|
extension.ui()
|