From 76c368340384df9dccbeda76f8e31774c5626337 Mon Sep 17 00:00:00 2001 From: H Lohaus Date: Sun, 8 Dec 2024 20:39:40 +0100 Subject: [PATCH] =?UTF-8?q?Remove=20webview=20js=20api,=20Add=20unittest?= =?UTF-8?q?=20for=20provider=20has=20model,=20Use=20cooki=E2=80=A6=20(#247?= =?UTF-8?q?0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove webview js api, Add unittest for provider has model, Use cookies dir for cache --- etc/tool/create_provider.py | 2 +- etc/unittest/__main__.py | 1 + etc/unittest/model.py | 8 +- etc/unittest/models.py | 23 +++++ g4f/Provider/Airforce.py | 2 +- g4f/Provider/Blackbox.py | 21 ++--- g4f/Provider/Flux.py | 2 +- g4f/Provider/RobocodersAPI.py | 3 +- g4f/Provider/needs_auth/CopilotAccount.py | 5 +- g4f/Provider/needs_auth/MetaAI.py | 2 +- g4f/gui/client/index.html | 26 ++--- g4f/gui/client/static/js/chat.v1.js | 49 +--------- .../client/static/js/highlightjs-copy.min.js | 4 - g4f/gui/server/android_gallery.py | 67 ------------- g4f/gui/server/js_api.py | 94 ------------------- g4f/gui/webview.py | 2 - g4f/image.py | 3 +- g4f/models.py | 42 +-------- 18 files changed, 63 insertions(+), 293 deletions(-) create mode 100644 etc/unittest/models.py delete mode 100644 g4f/gui/server/android_gallery.py delete mode 100644 g4f/gui/server/js_api.py diff --git a/etc/tool/create_provider.py b/etc/tool/create_provider.py index 7a9827a8acc..319676b6f18 100644 --- a/etc/tool/create_provider.py +++ b/etc/tool/create_provider.py @@ -113,7 +113,7 @@ async def create_async_generator( print("Create code...") response = [] for chunk in g4f.ChatCompletion.create( - model=g4f.models.default, + model=g4f.models.gpt_4o, messages=[{"role": "user", "content": prompt}], timeout=300, stream=True, diff --git a/etc/unittest/__main__.py b/etc/unittest/__main__.py index 3719c374dbb..6594e6a2677 100644 --- a/etc/unittest/__main__.py +++ b/etc/unittest/__main__.py @@ -8,5 +8,6 @@ from .image_client import * from .include import * from .retry_provider import * +from .models import * unittest.main() \ No newline at end of file diff --git a/etc/unittest/model.py b/etc/unittest/model.py index 0d318e7a5f9..2a3db0f4b85 100644 --- a/etc/unittest/model.py +++ b/etc/unittest/model.py @@ -4,24 +4,24 @@ from .mocks import ModelProviderMock DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}] - + test_model = g4f.models.Model( name = "test/test_model", base_provider = "", best_provider = ModelProviderMock ) g4f.models.ModelUtils.convert["test_model"] = test_model - + class TestPassModel(unittest.TestCase): def test_model_instance(self): response = ChatCompletion.create(test_model, DEFAULT_MESSAGES) self.assertEqual(test_model.name, response) - + def test_model_name(self): response = ChatCompletion.create("test_model", DEFAULT_MESSAGES) self.assertEqual(test_model.name, response) - + def test_model_pass(self): response = ChatCompletion.create("test/test_model", DEFAULT_MESSAGES, ModelProviderMock) self.assertEqual(test_model.name, response) \ No newline at end of file diff --git a/etc/unittest/models.py b/etc/unittest/models.py new file mode 100644 index 00000000000..b9b452d785e --- /dev/null +++ b/etc/unittest/models.py @@ -0,0 +1,23 @@ +import unittest +from typing import Type +import asyncio + +from g4f.models import __models__ +from g4f.providers.base_provider import BaseProvider, ProviderModelMixin +from g4f.models import Model + +class TestProviderHasModel(unittest.IsolatedAsyncioTestCase): + cache: dict = {} + + async def test_provider_has_model(self): + for model, providers in __models__.values(): + for provider in providers: + if issubclass(provider, ProviderModelMixin): + if model.name not in provider.model_aliases: + await asyncio.wait_for(self.provider_has_model(provider, model), 10) + + async def provider_has_model(self, provider: Type[BaseProvider], model: Model): + if provider.__name__ not in self.cache: + self.cache[provider.__name__] = provider.get_models() + if self.cache[provider.__name__]: + self.assertIn(model.name, self.cache[provider.__name__], provider.__name__) \ No newline at end of file diff --git a/g4f/Provider/Airforce.py b/g4f/Provider/Airforce.py index 5e5d5194a84..474c9f88892 100644 --- a/g4f/Provider/Airforce.py +++ b/g4f/Provider/Airforce.py @@ -60,6 +60,7 @@ class Airforce(AsyncGeneratorProvider, ProviderModelMixin): "evil": "any-uncensored", "sdxl": "stable-diffusion-xl-base", "flux-pro": "flux-1.1-pro", + "llama-3.1-8b": "llama-3.1-8b-chat" } @classmethod @@ -85,7 +86,6 @@ def get_models(cls): cls.models = [model for model in cls.models if model not in cls.hidden_models] except Exception as e: debug.log(f"Error fetching text models: {e}") - cls.models = [cls.default_model] return cls.models diff --git a/g4f/Provider/Blackbox.py b/g4f/Provider/Blackbox.py index 9c02f3d7569..537a3ea8441 100644 --- a/g4f/Provider/Blackbox.py +++ b/g4f/Provider/Blackbox.py @@ -7,21 +7,20 @@ import re import aiohttp -import os import json from pathlib import Path from ..typing import AsyncResult, Messages, ImageType from .base_provider import AsyncGeneratorProvider, ProviderModelMixin from ..image import ImageResponse, to_data_uri - +from ..cookies import get_cookies_dir from .helper import format_prompt class Blackbox(AsyncGeneratorProvider, ProviderModelMixin): label = "Blackbox AI" url = "https://www.blackbox.ai" api_endpoint = "https://www.blackbox.ai/api/chat" - + working = True supports_stream = True supports_system_message = True @@ -38,7 +37,7 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin): agentMode = { 'ImageGeneration': {'mode': True, 'id': "ImageGenerationLV45LJp", 'name': "Image Generation"} } - + trendingAgentMode = { "gemini-1.5-flash": {'mode': True, 'id': 'Gemini'}, "llama-3.1-8b": {'mode': True, 'id': "llama-3.1-8b"}, @@ -108,19 +107,11 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin): "flux": "ImageGeneration", } - @classmethod - def _get_cache_dir(cls) -> Path: - # Get the path to the current file - current_file = Path(__file__) - # Create the path to the .cache directory - cache_dir = current_file.parent / '.cache' - # Create a directory if it does not exist - cache_dir.mkdir(exist_ok=True) - return cache_dir - @classmethod def _get_cache_file(cls) -> Path: - return cls._get_cache_dir() / 'blackbox.json' + dir = Path(get_cookies_dir()) + dir.mkdir(exist_ok=True) + return dir / 'blackbox.json' @classmethod def _load_cached_value(cls) -> str | None: diff --git a/g4f/Provider/Flux.py b/g4f/Provider/Flux.py index 059836782e4..7a00e75ad6c 100644 --- a/g4f/Provider/Flux.py +++ b/g4f/Provider/Flux.py @@ -12,7 +12,7 @@ class Flux(AsyncGeneratorProvider, ProviderModelMixin): url = "https://black-forest-labs-flux-1-dev.hf.space" api_endpoint = "/gradio_api/call/infer" working = True - default_model = 'flux-1-dev' + default_model = 'flux-dev' models = [default_model] image_models = [default_model] diff --git a/g4f/Provider/RobocodersAPI.py b/g4f/Provider/RobocodersAPI.py index d8330024cd1..3a94e2719ec 100755 --- a/g4f/Provider/RobocodersAPI.py +++ b/g4f/Provider/RobocodersAPI.py @@ -14,6 +14,7 @@ from aiohttp import ClientTimeout from ..errors import MissingRequirementsError from ..typing import AsyncResult, Messages +from ..cookies import get_cookies_dir from .base_provider import AsyncGeneratorProvider, ProviderModelMixin from .helper import format_prompt @@ -30,7 +31,7 @@ class RobocodersAPI(AsyncGeneratorProvider, ProviderModelMixin): agent = [default_model, "RepoAgent", "FrontEndAgent"] models = [*agent] - CACHE_DIR = Path(__file__).parent / ".cache" + CACHE_DIR = Path(get_cookies_dir()) CACHE_FILE = CACHE_DIR / "robocoders.json" @classmethod diff --git a/g4f/Provider/needs_auth/CopilotAccount.py b/g4f/Provider/needs_auth/CopilotAccount.py index 497aab989f0..0dfb9bd4f57 100644 --- a/g4f/Provider/needs_auth/CopilotAccount.py +++ b/g4f/Provider/needs_auth/CopilotAccount.py @@ -9,4 +9,7 @@ class CopilotAccount(Copilot, ProviderModelMixin): default_model = "Copilot" default_vision_model = default_model models = [default_model] - image_models = models \ No newline at end of file + image_models = models + model_aliases = { + "dall-e-3": default_model + } \ No newline at end of file diff --git a/g4f/Provider/needs_auth/MetaAI.py b/g4f/Provider/needs_auth/MetaAI.py index 568de701f7e..a8b6708ecd2 100644 --- a/g4f/Provider/needs_auth/MetaAI.py +++ b/g4f/Provider/needs_auth/MetaAI.py @@ -29,7 +29,7 @@ class MetaAI(AsyncGeneratorProvider, ProviderModelMixin): label = "Meta AI" url = "https://www.meta.ai" working = True - default_model = '' + default_model = 'meta-ai' def __init__(self, proxy: str = None, connector: BaseConnector = None): self.session = ClientSession(connector=get_connector(connector, proxy), headers=DEFAULT_HEADERS) diff --git a/g4f/gui/client/index.html b/g4f/gui/client/index.html index cd1f72a645e..5410b46e7dd 100644 --- a/g4f/gui/client/index.html +++ b/g4f/gui/client/index.html @@ -1,6 +1,5 @@ - @@ -62,9 +61,8 @@ onInit: (el, pswp) => { lightbox.pswp.on('change', () => { const currSlideElement = lightbox.pswp.currSlide.data.element; - let captionHTML = ''; if (currSlideElement) { - el.innerHTML = currSlideElement.querySelector('img').getAttribute('alt'); + el.innerText = currSlideElement.querySelector('img').getAttribute('alt'); } }); } @@ -80,7 +78,6 @@ g4f - gui -
@@ -92,12 +89,6 @@
-
-