|
5 | 5 | import platform
|
6 | 6 | import re
|
7 | 7 | import subprocess
|
| 8 | +from pathlib import Path |
8 | 9 | import sys
|
9 | 10 | import threading
|
10 | 11 | from enum import Enum
|
|
54 | 55 |
|
55 | 56 |
|
56 | 57 | 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 |
57 | 74 | ext = {"Darwin": "dylib", "Linux": "so", "Windows": "dll"}[platform.system()]
|
58 | 75 |
|
| 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 | + |
59 | 89 | 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": |
64 | 94 | 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)) |
67 | 97 |
|
68 | 98 | return lib
|
69 | 99 |
|
|
0 commit comments