Catch import errors

This commit is contained in:
hlohaus 2025-03-29 20:47:10 +01:00
parent 494746302a
commit 5bf80ec280
3 changed files with 81 additions and 57 deletions

View file

@ -4,48 +4,71 @@ from ..providers.types import BaseProvider, ProviderType
from ..providers.retry_provider import RetryProvider, IterListProvider from ..providers.retry_provider import RetryProvider, IterListProvider
from ..providers.base_provider import AsyncProvider, AsyncGeneratorProvider from ..providers.base_provider import AsyncProvider, AsyncGeneratorProvider
from ..providers.create_images import CreateImagesProvider from ..providers.create_images import CreateImagesProvider
from .. import debug
from .deprecated import * try:
from .deprecated import *
except ImportError as e:
debug.error("Deprecated providers not loaded:", e)
from .needs_auth import * from .needs_auth import *
from .not_working import *
from .local import *
from .hf import HuggingFace, HuggingChat, HuggingFaceAPI, HuggingFaceInference, HuggingFaceMedia
from .hf_space import *
from .mini_max import HailuoAI, MiniMax
from .template import OpenaiTemplate, BackendApi from .template import OpenaiTemplate, BackendApi
from .hf import HuggingFace, HuggingChat, HuggingFaceAPI, HuggingFaceInference, HuggingFaceMedia
try:
from .not_working import *
except ImportError as e:
debug.error("Not working providers not loaded:", e)
try:
from .local import *
except ImportError as e:
debug.error("Local providers not loaded:", e)
try:
from .hf_space import *
except ImportError as e:
debug.error("HuggingFace Space providers not loaded:", e)
try:
from .mini_max import HailuoAI, MiniMax
except ImportError as e:
debug.error("MiniMax providers not loaded:", e)
from .AllenAI import AllenAI try:
from .ARTA import ARTA from .AllenAI import AllenAI
from .Blackbox import Blackbox from .ARTA import ARTA
from .ChatGLM import ChatGLM from .Blackbox import Blackbox
from .ChatGpt import ChatGpt from .ChatGLM import ChatGLM
from .ChatGptEs import ChatGptEs from .ChatGpt import ChatGpt
from .Cloudflare import Cloudflare from .ChatGptEs import ChatGptEs
from .Copilot import Copilot from .Cloudflare import Cloudflare
from .DDG import DDG from .Copilot import Copilot
from .DeepInfraChat import DeepInfraChat from .DDG import DDG
from .Dynaspark import Dynaspark from .DeepInfraChat import DeepInfraChat
from .Free2GPT import Free2GPT from .Dynaspark import Dynaspark
from .FreeGpt import FreeGpt except ImportError as e:
from .GizAI import GizAI debug.error("Providers not loaded (A-D):", e)
from .Glider import Glider try:
from .Goabror import Goabror from .Free2GPT import Free2GPT
from .ImageLabs import ImageLabs from .FreeGpt import FreeGpt
from .Jmuz import Jmuz from .GizAI import GizAI
from .LambdaChat import LambdaChat from .Glider import Glider
from .Liaobots import Liaobots from .Goabror import Goabror
from .OIVSCode import OIVSCode from .ImageLabs import ImageLabs
from .PerplexityLabs import PerplexityLabs from .Jmuz import Jmuz
from .Pi import Pi from .LambdaChat import LambdaChat
from .Pizzagpt import Pizzagpt from .Liaobots import Liaobots
from .PollinationsAI import PollinationsAI from .OIVSCode import OIVSCode
from .PollinationsImage import PollinationsImage except ImportError as e:
from .TeachAnything import TeachAnything debug.error("Providers not loaded (F-L):", e)
from .TypeGPT import TypeGPT try:
from .You import You from .PerplexityLabs import PerplexityLabs
from .Websim import Websim from .Pi import Pi
from .Yqcloud import Yqcloud from .Pizzagpt import Pizzagpt
from .PollinationsAI import PollinationsAI
from .PollinationsImage import PollinationsImage
from .TeachAnything import TeachAnything
from .TypeGPT import TypeGPT
from .You import You
from .Websim import Websim
from .Yqcloud import Yqcloud
except ImportError as e:
debug.error("Providers not loaded (M-Z):", e)
import sys import sys
@ -61,9 +84,9 @@ __providers__: list[ProviderType] = [
__all__: list[str] = [ __all__: list[str] = [
provider.__name__ for provider in __providers__ provider.__name__ for provider in __providers__
] ]
__map__: dict[str, ProviderType] = dict([ __map__: dict[str, ProviderType] = {
(provider.__name__, provider) for provider in __providers__ provider.__name__: provider for provider in __providers__
]) }
class ProviderUtils: class ProviderUtils:
convert: dict[str, ProviderType] = __map__ convert: dict[str, ProviderType] = __map__

View file

@ -1,7 +1,11 @@
from __future__ import annotations from __future__ import annotations
import os import os
import ssl try:
import ssl
has_ssl = True
except ImportError:
has_ssl = False
import time import time
import uuid import uuid
from pathlib import Path from pathlib import Path
@ -80,19 +84,19 @@ class GigaChat(AsyncGeneratorProvider, ProviderModelMixin):
model = cls.get_model(model) model = cls.get_model(model)
if not api_key: if not api_key:
raise MissingAuthError('Missing "api_key"') raise MissingAuthError('Missing "api_key"')
# Create certificate file in cookies directory # Create certificate file in cookies directory
cookies_dir = Path(get_cookies_dir()) cookies_dir = Path(get_cookies_dir())
cert_file = cookies_dir / 'russian_trusted_root_ca.crt' cert_file = cookies_dir / 'russian_trusted_root_ca.crt'
# Write certificate if it doesn't exist # Write certificate if it doesn't exist
if not cert_file.exists(): if not cert_file.exists():
cert_file.write_text(RUSSIAN_CA_CERT) cert_file.write_text(RUSSIAN_CA_CERT)
ssl_context = ssl.create_default_context(cafile=str(cert_file)) if has_ssl and connector is None:
if connector is None: ssl_context = ssl.create_default_context(cafile=str(cert_file))
connector = TCPConnector(ssl_context=ssl_context) connector = TCPConnector(ssl_context=ssl_context)
async with ClientSession(connector=get_connector(connector, proxy)) as session: async with ClientSession(connector=get_connector(connector, proxy)) as session:
if token_expires_at - int(time.time() * 1000) < 60000: if token_expires_at - int(time.time() * 1000) < 60000:
async with session.post(url="https://ngw.devices.sberbank.ru:9443/api/v2/oauth", async with session.post(url="https://ngw.devices.sberbank.ru:9443/api/v2/oauth",

View file

@ -1,21 +1,18 @@
import sys import sys
from typing import Callable, List, Optional, Any from typing import Callable, List, Optional, Any
# Warning: name could conflict with Python's built-in logging module
logging: bool = False logging: bool = False
version_check: bool = True version_check: bool = True
version: Optional[str] = None version: Optional[str] = None
log_handler: Callable = print # More specifically: Callable[[Any, Optional[Any]], None] log_handler: Callable = print # More specifically: Callable[[Any, Optional[Any]], None]
logs: List[str] = [] logs: List[str] = []
def log(text: Any, file: Optional[Any] = None) -> None: def log(*text: Any, file: Optional[Any] = None) -> None:
"""Log a message if logging is enabled.""" """Log a message if logging is enabled."""
if logging: if logging:
log_handler(text, file=file) log_handler(*text, file=file)
def error(error: Any, name: Optional[str] = None) -> None: def error(*error: Any, name: Optional[str] = None) -> None:
"""Log an error message to stderr.""" """Log an error message to stderr."""
log( error = [e if isinstance(e, str) else f"{type(e).__name__ if name is None else name}: {e}" for e in error]
error if isinstance(error, str) else f"{type(error).__name__ if name is None else name}: {error}", log(*error, file=sys.stderr)
file=sys.stderr
)