From bd27353a08df9896ef6b560cde7e3dcb33b7d67d Mon Sep 17 00:00:00 2001 From: Maya <48323879+mayaeary@users.noreply.github.com> Date: Sun, 19 Mar 2023 12:51:27 +0000 Subject: [PATCH 1/6] Fix duplicating server on ui reload --- extensions/api/script.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/extensions/api/script.py b/extensions/api/script.py index 53e47f3f..1c57c72a 100644 --- a/extensions/api/script.py +++ b/extensions/api/script.py @@ -8,6 +8,8 @@ params = { 'port': 5000, } +server = None + class Handler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/api/v1/model': @@ -73,6 +75,7 @@ class Handler(BaseHTTPRequestHandler): def run_server(): + global server server_addr = ('0.0.0.0' if shared.args.listen else '127.0.0.1', params['port']) server = ThreadingHTTPServer(server_addr, Handler) if shared.args.share: @@ -87,4 +90,5 @@ def run_server(): server.serve_forever() def ui(): - Thread(target=run_server, daemon=True).start() \ No newline at end of file + if server is None: + Thread(target=run_server, daemon=True).start() \ No newline at end of file From 099d7a844b67caa4d33ad272a03d2ab9e18c2a0b Mon Sep 17 00:00:00 2001 From: Maya <48323879+mayaeary@users.noreply.github.com> Date: Sun, 19 Mar 2023 13:22:24 +0000 Subject: [PATCH 2/6] Add setup method to extensions --- extensions/api/script.py | 8 ++------ modules/extensions.py | 8 ++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/extensions/api/script.py b/extensions/api/script.py index 1c57c72a..bbd8551a 100644 --- a/extensions/api/script.py +++ b/extensions/api/script.py @@ -8,8 +8,6 @@ params = { 'port': 5000, } -server = None - class Handler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/api/v1/model': @@ -75,7 +73,6 @@ class Handler(BaseHTTPRequestHandler): def run_server(): - global server server_addr = ('0.0.0.0' if shared.args.listen else '127.0.0.1', params['port']) server = ThreadingHTTPServer(server_addr, Handler) if shared.args.share: @@ -89,6 +86,5 @@ def run_server(): print(f'Starting KoboldAI compatible api at http://{server_addr[0]}:{server_addr[1]}/api') server.serve_forever() -def ui(): - if server is None: - Thread(target=run_server, daemon=True).start() \ No newline at end of file +def setup(): + Thread(target=run_server, daemon=True).start() \ No newline at end of file diff --git a/modules/extensions.py b/modules/extensions.py index 836fbc60..9f11c882 100644 --- a/modules/extensions.py +++ b/modules/extensions.py @@ -5,6 +5,7 @@ import modules.shared as shared state = {} available_extensions = [] +setup_called = False def load_extensions(): global state @@ -44,6 +45,13 @@ def create_extensions_block(): if _id in shared.settings: extension.params[param] = shared.settings[_id] + # Running setup function + if not setup_called: + for extension, name in iterator(): + if hasattr(extension, "setup"): + extension.setup() + setup_called = True + # Creating the extension ui elements if len(state) > 0: with gr.Box(elem_id="extensions"): From 81c9d130f2a02410ce6da17806564677c09ab844 Mon Sep 17 00:00:00 2001 From: Maya <48323879+mayaeary@users.noreply.github.com> Date: Sun, 19 Mar 2023 13:25:49 +0000 Subject: [PATCH 3/6] Fix global --- modules/extensions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/extensions.py b/modules/extensions.py index 9f11c882..defc0d66 100644 --- a/modules/extensions.py +++ b/modules/extensions.py @@ -37,6 +37,7 @@ def apply_extensions(text, typ): return text def create_extensions_block(): + global setup_called # Updating the default values for extension, name in iterator(): if hasattr(extension, 'params'): From acdbd6b708b6c6e91b601a28cdfdb8d86a3cc395 Mon Sep 17 00:00:00 2001 From: Maya <48323879+mayaeary@users.noreply.github.com> Date: Sun, 19 Mar 2023 13:31:21 +0000 Subject: [PATCH 4/6] Check if app should display extensions ui --- modules/extensions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/extensions.py b/modules/extensions.py index defc0d66..b363bc39 100644 --- a/modules/extensions.py +++ b/modules/extensions.py @@ -46,15 +46,18 @@ def create_extensions_block(): if _id in shared.settings: extension.params[param] = shared.settings[_id] + should_display_ui = False # Running setup function if not setup_called: for extension, name in iterator(): if hasattr(extension, "setup"): extension.setup() + if hasattr(extension, "ui"): + should_display_ui = True setup_called = True # Creating the extension ui elements - if len(state) > 0: + if should_display_ui: with gr.Box(elem_id="extensions"): gr.Markdown("Extensions") for extension, name in iterator(): From 8fad84abc2c8eb90718ef7d7084a22c740e20d9b Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Fri, 24 Mar 2023 16:51:27 -0300 Subject: [PATCH 5/6] Update extensions.py --- modules/extensions.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/extensions.py b/modules/extensions.py index b363bc39..c55dc978 100644 --- a/modules/extensions.py +++ b/modules/extensions.py @@ -1,3 +1,5 @@ +import traceback + import gradio as gr import extensions @@ -18,6 +20,7 @@ def load_extensions(): print('Ok.') except: print('Fail.') + traceback.print_exc() # This iterator returns the extensions in the order specified in the command-line def iterator(): @@ -38,6 +41,7 @@ def apply_extensions(text, typ): def create_extensions_block(): global setup_called + # Updating the default values for extension, name in iterator(): if hasattr(extension, 'params'): @@ -47,6 +51,7 @@ def create_extensions_block(): extension.params[param] = shared.settings[_id] should_display_ui = False + # Running setup function if not setup_called: for extension, name in iterator(): From 4a724ed22fc8942677f44df39674b571450ea51c Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Fri, 24 Mar 2023 16:53:56 -0300 Subject: [PATCH 6/6] Reorder imports --- extensions/api/script.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/extensions/api/script.py b/extensions/api/script.py index bbd8551a..7783594c 100644 --- a/extensions/api/script.py +++ b/extensions/api/script.py @@ -1,8 +1,9 @@ +import json from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from threading import Thread + from modules import shared -from modules.text_generation import generate_reply, encode -import json +from modules.text_generation import encode, generate_reply params = { 'port': 5000, @@ -87,4 +88,4 @@ def run_server(): server.serve_forever() def setup(): - Thread(target=run_server, daemon=True).start() \ No newline at end of file + Thread(target=run_server, daemon=True).start()