Skip to content

Commit

Permalink
refactor: use localizations for menubar and structure optimization
Browse files Browse the repository at this point in the history
- use localizations for menubar
- bump AutoGGUF version to v2.0.0
- rename imports_and_globals.py to globals.py
- reformat code
- use file select for Merge/Split GGUF functions
- move general functions verify_gguf and process_args to globals.py
- create Plugins class for extensibility
  • Loading branch information
leafspark committed Oct 6, 2024
1 parent b1b3a35 commit 35839ee
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 151 deletions.
195 changes: 49 additions & 146 deletions src/AutoGGUF.py

Large diffs are not rendered by default.

27 changes: 26 additions & 1 deletion src/Localizations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import re

AUTOGGUF_VERSION = "v1.9.0"
AUTOGGUF_VERSION = "v2.0.0"


class _Localization:
Expand Down Expand Up @@ -429,6 +429,31 @@ def __init__(self):
self.SELECT_FOLDER = "Select Folder"
self.SELECT_FILE = "Select File"

# Menubar
self.CLOSE = "Close"
self.FILE = "File"
self.FOLDER = "Folder"
self.HELP = "Help"
self.ABOUT = "About"

self.AUTOFP8 = "AutoFP8"
self.TOOLS = "Tools"
self.HF_TRANSFER = "HF Transfer"
self.MERGE_GGUF = "Merge GGUF"

self.HF_UPLOAD = "HF Upload"
self.HF_REPOSITORY = "Repository:"
self.HF_REMOTE_PATH = "Remote Path:"
self.HF_LOCAL_PATH = "Local Path:"
self.MODEL = "Model"
self.DATASET = "Dataset"
self.SPACE = "Space"
self.HF_REPOSITORY_TYPE = "Repository Type"
self.UPLOAD_TYPE = "Upload Type"
self.UPLOAD = "Upload"

self.EXTRA_COMMAND_ARGUMENTS = "Additional command-line arguments"


class _French(_Localization):
def __init__(self):
Expand Down
81 changes: 81 additions & 0 deletions src/Plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import importlib
import os
from typing import Any, Dict
from Localizations import *


class Plugins:

def load_plugins(self) -> Dict[str, Dict[str, Any]]:
plugins = {}
plugin_dir = "plugins"

if not os.path.exists(plugin_dir):
self.logger.info(PLUGINS_DIR_NOT_EXIST.format(plugin_dir))
return plugins

if not os.path.isdir(plugin_dir):
self.logger.warning(PLUGINS_DIR_NOT_DIRECTORY.format(plugin_dir))
return plugins

for file in os.listdir(plugin_dir):
if file.endswith(".py") and not file.endswith(".disabled.py"):
name = file[:-3]
path = os.path.join(plugin_dir, file)

try:
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

for item_name in dir(module):
item = getattr(module, item_name)
if isinstance(item, type) and hasattr(item, "__data__"):
plugin_instance = item()
plugin_data = plugin_instance.__data__()

compatible_versions = plugin_data.get(
"compatible_versions", []
)
if (
"*" in compatible_versions
or AUTOGGUF_VERSION in compatible_versions
):
plugins[name] = {
"instance": plugin_instance,
"data": plugin_data,
}
self.logger.info(
PLUGIN_LOADED.format(
plugin_data["name"], plugin_data["version"]
)
)
else:
self.logger.warning(
PLUGIN_INCOMPATIBLE.format(
plugin_data["name"],
plugin_data["version"],
AUTOGGUF_VERSION,
", ".join(compatible_versions),
)
)
break
except Exception as e:
self.logger.error(PLUGIN_LOAD_FAILED.format(name, str(e)))

return plugins

def apply_plugins(self) -> None:
if not self.plugins:
self.logger.info(NO_PLUGINS_LOADED)
return

for plugin_name, plugin_info in self.plugins.items():
plugin_instance = plugin_info["instance"]
for attr_name in dir(plugin_instance):
if not attr_name.startswith("__") and attr_name != "init":
attr_value = getattr(plugin_instance, attr_name)
setattr(self, attr_name, attr_value)

if hasattr(plugin_instance, "init") and callable(plugin_instance.init):
plugin_instance.init(self)
2 changes: 1 addition & 1 deletion src/QuantizationThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from PySide6.QtCore import Signal, QThread

from imports_and_globals import open_file_safe
from globals import open_file_safe
from Localizations import IN_PROGRESS, COMPLETED


Expand Down
30 changes: 29 additions & 1 deletion src/imports_and_globals.py → src/globals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import re
import sys
from typing import Any, TextIO, Union
from typing import Any, List, TextIO, Union

from PySide6.QtWidgets import (
QMessageBox,
Expand All @@ -15,6 +15,34 @@
)


def verify_gguf(file_path) -> bool:
try:
with open(file_path, "rb") as f:
magic = f.read(4)
return magic == b"GGUF"
except (FileNotFoundError, IOError, OSError):
return False


def process_args(args: List[str]) -> bool:
try:
i = 1
while i < len(args):
key = (
args[i][2:].replace("-", "_").upper()
) # Strip the first two '--' and replace '-' with '_'
if i + 1 < len(args) and not args[i + 1].startswith("--"):
value = args[i + 1]
i += 2
else:
value = "enabled"
i += 1
os.environ[key] = value
return True
except Exception:
return False


def load_dotenv(self=Any) -> None:
if not os.path.isfile(".env"):
self.logger.warning(DOTENV_FILE_NOT_FOUND)
Expand Down
2 changes: 1 addition & 1 deletion src/lora_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from QuantizationThread import QuantizationThread
from TaskListItem import TaskListItem
from error_handling import handle_error, show_error
from imports_and_globals import ensure_directory
from globals import ensure_directory
from Localizations import *


Expand Down
2 changes: 1 addition & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from DownloadThread import DownloadThread
from Localizations import *
from error_handling import show_error
from imports_and_globals import ensure_directory
from globals import ensure_directory
from KVOverrideEntry import KVOverrideEntry


Expand Down

0 comments on commit 35839ee

Please sign in to comment.