From 4d4275d1b8d5470171451c73d04fd37326d8c549 Mon Sep 17 00:00:00 2001 From: cebtenzzre Date: Thu, 12 Oct 2023 13:35:27 -0400 Subject: [PATCH] python: replace deprecated pkg_resources with importlib (#1505) --- gpt4all-bindings/cli/app.py | 8 +++---- gpt4all-bindings/python/gpt4all/pyllmodel.py | 22 +++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/gpt4all-bindings/cli/app.py b/gpt4all-bindings/cli/app.py index 51324d6d..c89f2461 100644 --- a/gpt4all-bindings/cli/app.py +++ b/gpt4all-bindings/cli/app.py @@ -4,13 +4,13 @@ The GPT4All CLI is a self-contained script based on the `gpt4all` and `typer` pa REPL to communicate with a language model similar to the chat GUI application, but more basic. """ +import importlib.metadata import io -import pkg_resources # should be present as a dependency of gpt4all import sys -import typer - from collections import namedtuple from typing_extensions import Annotated + +import typer from gpt4all import GPT4All @@ -79,7 +79,7 @@ def repl( use_new_loop = False try: - version = pkg_resources.Environment()['gpt4all'][0].version + version = importlib.metadata.version('gpt4all') version_major = int(version.split('.')[0]) if version_major >= 1: use_new_loop = True diff --git a/gpt4all-bindings/python/gpt4all/pyllmodel.py b/gpt4all-bindings/python/gpt4all/pyllmodel.py index 648e5dd6..1018f65d 100644 --- a/gpt4all-bindings/python/gpt4all/pyllmodel.py +++ b/gpt4all-bindings/python/gpt4all/pyllmodel.py @@ -1,23 +1,27 @@ +import atexit import ctypes +import importlib.resources import logging import os import platform -from queue import Queue import re import subprocess import sys import threading +from contextlib import ExitStack +from queue import Queue from typing import Callable, Iterable, List -import pkg_resources - logger: logging.Logger = logging.getLogger(__name__) -# TODO: provide a config file to make this more robust -LLMODEL_PATH = os.path.join("llmodel_DO_NOT_MODIFY", "build").replace("\\", "\\\\") -MODEL_LIB_PATH = str(pkg_resources.resource_filename("gpt4all", LLMODEL_PATH)).replace("\\", "\\\\") +file_manager = ExitStack() +atexit.register(file_manager.close) # clean up files on exit +# TODO: provide a config file to make this more robust +MODEL_LIB_PATH = file_manager.enter_context(importlib.resources.as_file( + importlib.resources.files("gpt4all") / "llmodel_DO_NOT_MODIFY" / "build", +)) def load_llmodel_library(): system = platform.system() @@ -36,9 +40,7 @@ def load_llmodel_library(): llmodel_file = "libllmodel" + "." + c_lib_ext - llmodel_dir = str(pkg_resources.resource_filename("gpt4all", os.path.join(LLMODEL_PATH, llmodel_file))).replace( - "\\", "\\\\" - ) + llmodel_dir = str(MODEL_LIB_PATH / llmodel_file).replace("\\", r"\\") llmodel_lib = ctypes.CDLL(llmodel_dir) @@ -131,7 +133,7 @@ llmodel.llmodel_set_implementation_search_path.restype = None llmodel.llmodel_threadCount.argtypes = [ctypes.c_void_p] llmodel.llmodel_threadCount.restype = ctypes.c_int32 -llmodel.llmodel_set_implementation_search_path(MODEL_LIB_PATH.encode("utf-8")) +llmodel.llmodel_set_implementation_search_path(str(MODEL_LIB_PATH).replace("\\", r"\\").encode("utf-8")) llmodel.llmodel_available_gpu_devices.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.POINTER(ctypes.c_int32)] llmodel.llmodel_available_gpu_devices.restype = ctypes.POINTER(LLModelGPUDevice)