Skip to content

Commit e0e5b74

Browse files
committed
load model library when using pyinstaller to build executable file in windows
Signed-off-by: NathanFu <nathan@appflowy.io>
1 parent 1ce65e8 commit e0e5b74

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

gpt4all-bindings/python/gpt4all/_pyllmodel.py

+36-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import platform
66
import re
77
import subprocess
8+
from pathlib import Path
89
import sys
910
import threading
1011
from enum import Enum
@@ -54,16 +55,45 @@
5455

5556

5657
def load_llmodel_library():
58+
"""
59+
Loads the llmodel shared library based on the current operating system.
60+
61+
This function attempts to load the shared library using the appropriate file
62+
extension for the operating system. It first tries to load the library with the
63+
'lib' prefix (common for macOS, Linux, and MinGW on Windows). If the file is not
64+
found and the operating system is Windows, it attempts to load the library without
65+
the 'lib' prefix (common for MSVC on Windows).
66+
67+
Returns:
68+
ctypes.CDLL: The loaded shared library.
69+
70+
Raises:
71+
OSError: If the shared library cannot be found.
72+
"""
73+
# Determine the appropriate file extension for the shared library based on the platform
5774
ext = {"Darwin": "dylib", "Linux": "so", "Windows": "dll"}[platform.system()]
5875

76+
# Define library names with and without the 'lib' prefix
77+
library_name_with_lib_prefix = f"libllmodel.{ext}"
78+
library_name_without_lib_prefix = "llmodel.dll"
79+
80+
# Handle PyInstaller frozen path
81+
if getattr(sys, 'frozen', False):
82+
# sys._MEIPASS is an attribute used by PyInstaller to refer to the
83+
# directory where it extracts the bundled application and its
84+
# dependencies when running a frozen (packaged) executable.
85+
base_path = Path(sys._MEIPASS) / 'gpt4all' / 'llmodel_DO_NOT_MODIFY' / 'build'
86+
else:
87+
base_path = MODEL_LIB_PATH
88+
5989
try:
60-
# macOS, Linux, MinGW
61-
lib = ctypes.CDLL(str(MODEL_LIB_PATH / f"libllmodel.{ext}"))
62-
except FileNotFoundError:
63-
if ext != 'dll':
90+
# Attempt to load the shared library with the 'lib' prefix (common for macOS, Linux, and MinGW)
91+
lib = ctypes.CDLL(str(base_path / library_name_with_lib_prefix))
92+
except OSError: # OSError is more general and includes FileNotFoundError
93+
if ext != "dll":
6494
raise
65-
# MSVC
66-
lib = ctypes.CDLL(str(MODEL_LIB_PATH / "llmodel.dll"))
95+
# For Windows (ext == 'dll'), attempt to load the shared library without the 'lib' prefix (common for MSVC)
96+
lib = ctypes.CDLL(str(base_path / library_name_without_lib_prefix))
6797

6898
return lib
6999

0 commit comments

Comments
 (0)