From b5b56f35caf584ec92e50998979aa8bc065f67d5 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sun, 17 Mar 2024 10:51:08 +0100 Subject: Fix abort in webview --- g4f/Provider/selenium/Bard.py | 2 +- g4f/Provider/selenium/Phind.py | 2 +- g4f/gui/__init__.py | 13 +++++++------ g4f/gui/client/static/js/chat.v1.js | 32 ++++++++++++++++++-------------- g4f/gui/server/api.py | 3 ++- g4f/gui/webview.py | 8 +++++++- 6 files changed, 36 insertions(+), 24 deletions(-) (limited to 'g4f') diff --git a/g4f/Provider/selenium/Bard.py b/g4f/Provider/selenium/Bard.py index 459f6f37..9c809128 100644 --- a/g4f/Provider/selenium/Bard.py +++ b/g4f/Provider/selenium/Bard.py @@ -18,7 +18,7 @@ from ...webdriver import WebDriver, WebDriverSession, element_send_text class Bard(AbstractProvider): url = "https://bard.google.com" - working = True + working = False needs_auth = True webdriver = True diff --git a/g4f/Provider/selenium/Phind.py b/g4f/Provider/selenium/Phind.py index b97d278f..b6f7cc07 100644 --- a/g4f/Provider/selenium/Phind.py +++ b/g4f/Provider/selenium/Phind.py @@ -10,7 +10,7 @@ from ...webdriver import WebDriver, WebDriverSession class Phind(AbstractProvider): url = "https://www.phind.com" - working = True + working = False supports_gpt_4 = True supports_stream = True diff --git a/g4f/gui/__init__.py b/g4f/gui/__init__.py index dff720ac..3f695200 100644 --- a/g4f/gui/__init__.py +++ b/g4f/gui/__init__.py @@ -1,12 +1,13 @@ from ..errors import MissingRequirementsError -try: - from .server.app import app - from .server.website import Website - from .server.backend import Backend_Api -except ImportError: - raise MissingRequirementsError('Install "flask" package for the gui') def run_gui(host: str = '0.0.0.0', port: int = 8080, debug: bool = False) -> None: + try: + from .server.app import app + from .server.website import Website + from .server.backend import Backend_Api + except ImportError: + raise MissingRequirementsError('Install "flask" package for the gui') + if debug: import g4f g4f.debug.logging = True diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index c2bb77a9..0da72988 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -19,6 +19,8 @@ const jailbreak = document.getElementById("jailbreak"); let prompt_lock = false; +let content, content_inner, content_count = null; + const options = ["switch", "model", "model2", "jailbreak", "patch", "provider", "history"]; messageInput.addEventListener("blur", () => { @@ -202,7 +204,7 @@ async function add_message_chunk(message) { console.info("Conversation used:", message.conversation) } else if (message.type == "provider") { window.provider_result = message.provider; - window.content.querySelector('.provider').innerHTML = ` + content.querySelector('.provider').innerHTML = ` ${message.provider.name} @@ -210,10 +212,10 @@ async function add_message_chunk(message) { ` } else if (message.type == "message") { console.error(messag.message) - return; } else if (message.type == "error") { + window.error = message.error console.error(message.error); - window.content_inner.innerHTML += `

An error occured: ${message.error}

`; + content_inner.innerHTML += `

An error occured: ${message.error}

`; } else if (message.type == "content") { window.text += message.content; html = markdown_render(window.text); @@ -228,11 +230,10 @@ async function add_message_chunk(message) { if (lastIndex) { html = html.substring(0, lastIndex) + '' + lastElement; } - window.content_inner.innerHTML = html; - window.content_count.innerText = count_words_and_tokens(text, window.provider_result?.model); - highlight(window.content_inner); + content_inner.innerHTML = html; + content_count.innerText = count_words_and_tokens(text, window.provider_result?.model); + highlight(content_inner); } - window.scrollTo(0, 0); if (message_box.scrollTop >= message_box.scrollHeight - message_box.clientHeight - 100) { message_box.scrollTo({ top: message_box.scrollHeight, behavior: "auto" }); @@ -271,11 +272,12 @@ const ask_gpt = async () => { window.controller = new AbortController(); window.text = ""; window.error = null; + window.abort = false; window.provider_result = null; - window.content = document.getElementById(`gpt_${window.token}`); - window.content_inner = content.querySelector('.content_inner'); - window.content_count = content.querySelector('.count'); + content = document.getElementById(`gpt_${window.token}`); + content_inner = content.querySelector('.content_inner'); + content_count = content.querySelector('.count'); message_box.scrollTop = message_box.scrollHeight; window.scrollTo(0, 0); @@ -307,9 +309,6 @@ const ask_gpt = async () => { error = true; text = "oops ! something went wrong, please try again / reload. [stacktrace in console]"; content_inner.innerHTML = text; - } else { - content_inner.innerHTML += " [aborted]"; - if (text) text += " [aborted]"; } } if (!error && text) { @@ -583,8 +582,13 @@ const load_conversations = async () => { } }; -document.getElementById(`cancelButton`).addEventListener(`click`, async () => { +document.getElementById("cancelButton").addEventListener("click", async () => { window.controller.abort(); + if (!window.abort) { + window.abort = true; + content_inner.innerHTML += " [aborted]"; + if (window.text) window.text += " [aborted]"; + } console.log(`aborted ${window.conversation_id}`); }); diff --git a/g4f/gui/server/api.py b/g4f/gui/server/api.py index 966319e4..4dfc43d4 100644 --- a/g4f/gui/server/api.py +++ b/g4f/gui/server/api.py @@ -80,7 +80,8 @@ class Api(): self._prepare_conversation_kwargs(options, kwargs), options.get("conversation_id") ): - window.evaluate_js(f"this.add_message_chunk({json.dumps(message)})") + if not window.evaluate_js(f"if (!this.abort) this.add_message_chunk({json.dumps(message)}); !this.abort && !this.error;"): + break def _prepare_conversation_kwargs(self, json_data: dict, kwargs: dict): """ diff --git a/g4f/gui/webview.py b/g4f/gui/webview.py index a8e745f3..ba764947 100644 --- a/g4f/gui/webview.py +++ b/g4f/gui/webview.py @@ -1,3 +1,5 @@ +import sys +import os.path import webview try: from platformdirs import user_config_dir @@ -14,9 +16,13 @@ def run_webview( debug: bool = False, storage_path: str = None ): + if getattr(sys, 'frozen', False): + dirname = sys._MEIPASS + else: + dirname = os.path.dirname(__file__) webview.create_window( f"g4f - {g4f.version.utils.current_version}", - "client/index.html", + os.path.join(dirname, "client/index.html"), text_select=True, js_api=Api(), ) -- cgit v1.2.3