From 3576dee75a1623aa2385b6afe8b922ad5affca26 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 6 Dec 2023 09:35:36 +0100 Subject: Add selenium to dockerfile Load model and provider list in gui Remove needs_auth in HuggingChat Add default model and login url in gui --- g4f/gui/client/js/chat.v1.js | 95 ++++++++++++++++---------------------------- g4f/gui/server/backend.py | 44 +++++++++++--------- g4f/gui/server/provider.py | 14 ------- 3 files changed, 60 insertions(+), 93 deletions(-) delete mode 100644 g4f/gui/server/provider.py (limited to 'g4f/gui') diff --git a/g4f/gui/client/js/chat.v1.js b/g4f/gui/client/js/chat.v1.js index 5b7a0bf0..2b1fdcb0 100644 --- a/g4f/gui/client/js/chat.v1.js +++ b/g4f/gui/client/js/chat.v1.js @@ -161,7 +161,7 @@ const ask_gpt = async (txtMsgs) => { text += chunk; - document.getElementById(`gpt_${window.token}`).innerHTML = markdown.render(text); + document.getElementById(`gpt_${window.token}`).innerHTML = markdown.render(text).replace(" { + response = await fetch('/backend-api/v2/models') + models = await response.json() + let select = document.getElementById('model'); + select.textContent = ''; + + let auto = document.createElement('option'); + auto.value = ''; + auto.text = 'Default Model'; + select.appendChild(auto); for (model of models) { - let model_info = document.createElement('option'); - model_info.value = model - model_info.text = model + let option = document.createElement('option'); + option.value = option.text = model; + select.appendChild(option); + } +})(); - MODELS_SELECT.appendChild(model_info); +(async () => { + response = await fetch('/backend-api/v2/providers') + providers = await response.json() + + let select = document.getElementById('provider'); + select.textContent = ''; + + let auto = document.createElement('option'); + auto.value = ''; + auto.text = 'Provider: Auto'; + select.appendChild(auto); + + for (provider of providers) { + let option = document.createElement('option'); + option.value = option.text = provider; + select.appendChild(option); } -} \ No newline at end of file +})(); \ No newline at end of file diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index 3d7bfedc..03363201 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -3,9 +3,8 @@ import g4f from flask import request from .internet import search from .config import special_instructions -from .provider import get_provider -g4f.logging = True +g4f.debug.logging = True class Backend_Api: def __init__(self, app) -> None: @@ -15,6 +14,10 @@ class Backend_Api: 'function': self.models, 'methods' : ['GET'] }, + '/backend-api/v2/providers': { + 'function': self.providers, + 'methods' : ['GET'] + }, '/backend-api/v2/conversation': { 'function': self._conversation, 'methods': ['POST'] @@ -37,6 +40,9 @@ class Backend_Api: def models(self): return g4f._all_models + def providers(self): + return [provider.__name__ for provider in g4f.Provider.__providers__ if provider.working] + def _gen_title(self): return { 'title': '' @@ -47,26 +53,26 @@ class Backend_Api: #jailbreak = request.json['jailbreak'] #internet_access = request.json['meta']['content']['internet_access'] #conversation = request.json['meta']['content']['conversation'] - prompt = request.json['meta']['content']['parts'] - model = request.json['model'] - provider = request.json.get('provider').split('g4f.Provider.')[1] - - messages = prompt - print(messages) + messages = request.json['meta']['content']['parts'] + model = request.json.get('model') + model = model if model else g4f.models.default + provider = request.json.get('provider', 'Auto').replace('g4f.Provider.', '') + provider = provider if provider != "Auto" else None + if provider != None: + provider = g4f.Provider.ProviderUtils.convert.get(provider) - def stream(): - yield from g4f.ChatCompletion.create( - model=model, - provider=get_provider(provider), - messages=messages, - stream=True, - ) if provider else g4f.ChatCompletion.create( - model=model, messages=messages, stream=True - ) + response = g4f.ChatCompletion.create( + model=model, + provider=provider, + messages=messages, + stream=True, + ignore_stream_and_auth=True + ) - return self.app.response_class(stream(), mimetype='text/event-stream') + return self.app.response_class(response, mimetype='text/event-stream') - except Exception as e: + except Exception as e: + print(e) return { 'code' : 'G4F_ERROR', '_action': '_ask', diff --git a/g4f/gui/server/provider.py b/g4f/gui/server/provider.py deleted file mode 100644 index 8c7ac755..00000000 --- a/g4f/gui/server/provider.py +++ /dev/null @@ -1,14 +0,0 @@ -from __future__ import annotations - -import g4f -from g4f import BaseProvider - - -def get_provider(provider: str) -> BaseProvider | None: - if not isinstance(provider, str): - return None - print(provider) - if provider == 'g4f.Provider.Auto': - return None - - return g4f.Provider.ProviderUtils.convert.get(provider) -- cgit v1.2.3 From c667f1cec521534781de51e316d5edeed89bdb89 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 6 Dec 2023 11:54:50 +0100 Subject: Improve docker image --- g4f/gui/client/js/chat.v1.js | 2 +- g4f/gui/server/backend.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/js/chat.v1.js b/g4f/gui/client/js/chat.v1.js index 2b1fdcb0..2844b73e 100644 --- a/g4f/gui/client/js/chat.v1.js +++ b/g4f/gui/client/js/chat.v1.js @@ -601,7 +601,7 @@ observer.observe(message_input, { attributes: true }); let auto = document.createElement('option'); auto.value = ''; - auto.text = 'Default Model'; + auto.text = 'Model: Default'; select.appendChild(auto); for (model of models) { diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index 03363201..e1abb764 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -41,7 +41,10 @@ class Backend_Api: return g4f._all_models def providers(self): - return [provider.__name__ for provider in g4f.Provider.__providers__ if provider.working] + return [ + provider.__name__ for provider in g4f.Provider.__providers__ + if provider.working and provider is not g4f.Provider.RetryProvider + ] def _gen_title(self): return { -- cgit v1.2.3