From 42709f55b27f52048939907b34f42cfdc3537ec0 Mon Sep 17 00:00:00 2001 From: H Lohaus Date: Sun, 14 Jan 2024 16:37:21 +0100 Subject: Fix unittests (#1472) * Fix unittests * Fix missing distutils, install setuptools * Fix version not found * Remove deprecation in get_event_loop --- g4f/Provider/helper.py | 14 ++++---------- g4f/gui/server/backend.py | 7 ++++++- 2 files changed, 10 insertions(+), 11 deletions(-) (limited to 'g4f') diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index fce1ee6f..160074f7 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -5,7 +5,7 @@ import os import random import secrets import string -from asyncio import AbstractEventLoop, BaseEventLoop +from asyncio import AbstractEventLoop from platformdirs import user_config_dir from browser_cookie3 import ( chrome, chromium, opera, opera_gx, @@ -27,19 +27,13 @@ def get_event_loop() -> AbstractEventLoop: AbstractEventLoop: The current or new event loop. """ try: - loop = asyncio.get_event_loop() - if isinstance(loop, BaseEventLoop): - loop._check_closed() - except RuntimeError: - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - try: - asyncio.get_running_loop() + loop = asyncio.get_running_loop() if not hasattr(loop.__class__, "_nest_patched"): import nest_asyncio nest_asyncio.apply(loop) except RuntimeError: - pass + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) except ImportError: raise RuntimeError( 'Use "create_async" instead of "create" function in a running event loop. Or install "nest_asyncio" package.' diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index 4a5cafa8..b4c8f56c 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -5,6 +5,7 @@ from typing import Generator from g4f import debug, version, models from g4f import _all_models, get_last_provider, ChatCompletion from g4f.image import is_allowed_extension, to_image +from g4f.errors import VersionNotFoundError from g4f.Provider import __providers__ from g4f.Provider.bing.create_images import patch_provider from .internet import get_search_message @@ -91,8 +92,12 @@ class Backend_Api: Returns: dict: A dictionary containing the current and latest version. """ + try: + current_version = version.utils.current_version + except VersionNotFoundError: + current_version = None return { - "version": version.utils.current_version, + "version": current_version, "latest_version": version.get_latest_version(), } -- cgit v1.2.3