dwolfe66/text-generation-webui-space
1
1import extensions2import modules.shared as shared3 4state = {}5available_extensions = []6 7def load_extensions():8 global state9 for i, name in enumerate(shared.args.extensions):10 if name in available_extensions:11 print(f'Loading the extension "{name}"... ', end='')12 exec(f"import extensions.{name}.script")13 state[name] = [True, i]14 print('Ok.')15 16# This iterator returns the extensions in the order specified in the command-line17def iterator():18 for name in sorted(state, key=lambda x : state[x][1]):19 if state[name][0] == True:20 yield eval(f"extensions.{name}.script"), name21 22# Extension functions that map string -> string23def apply_extensions(text, typ):24 for extension, _ in iterator():25 if typ == "input" and hasattr(extension, "input_modifier"):26 text = extension.input_modifier(text)27 elif typ == "output" and hasattr(extension, "output_modifier"):28 text = extension.output_modifier(text)29 elif typ == "bot_prefix" and hasattr(extension, "bot_prefix_modifier"):30 text = extension.bot_prefix_modifier(text)31 return text32 33def create_extensions_block():34 # Updating the default values35 for extension, name in iterator():36 if hasattr(extension, 'params'):37 for param in extension.params:38 _id = f"{name}-{param}"39 if _id in shared.settings:40 extension.params[param] = shared.settings[_id]41 42 # Creating the extension ui elements43 for extension, name in iterator():44 if hasattr(extension, "ui"):45 extension.ui()46 