Skip to content

Commit

Permalink
feat(ui): add menubar
Browse files Browse the repository at this point in the history
- add basic menu bar showing Close and About areas
- add program version in localizations.py
- refactor functions out of AutoGGUF.py and move to ui_update.py
  • Loading branch information
leafspark committed Aug 16, 2024
1 parent f5e0bca commit c5e1313
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 153 deletions.
122 changes: 39 additions & 83 deletions src/AutoGGUF.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import psutil
import requests
from functools import partial
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
Expand All @@ -19,6 +20,7 @@
from error_handling import show_error, handle_error
from imports_and_globals import ensure_directory, open_file_safe, resource_path
from localizations import *
from ui_update import *


class AutoGGUF(QMainWindow):
Expand All @@ -34,6 +36,19 @@ def __init__(self):
ensure_directory(os.path.abspath("quantized_models"))
ensure_directory(os.path.abspath("models"))

# References
self.update_base_model_visibility = partial(update_base_model_visibility, self)
self.update_assets = update_assets.__get__(self)
self.update_cuda_option = update_cuda_option.__get__(self)
self.update_cuda_backends = update_cuda_backends.__get__(self)
self.update_threads_spinbox = partial(update_threads_spinbox, self)
self.update_threads_slider = partial(update_threads_slider, self)
self.update_gpu_offload_spinbox = partial(update_gpu_offload_spinbox, self)
self.update_gpu_offload_slider = partial(update_gpu_offload_slider, self)
self.update_model_info = partial(update_model_info, self.logger, self)
self.update_system_info = partial(update_system_info, self)
self.update_download_progress = partial(update_download_progress, self)

# Create a central widget and main layout
central_widget = QWidget()
main_layout = QHBoxLayout(central_widget)
Expand All @@ -52,6 +67,23 @@ def __init__(self):
left_widget.setMinimumWidth(800)
right_widget.setMinimumWidth(400)

menubar = QMenuBar(self)
self.layout().setMenuBar(menubar)

# File menu
file_menu = menubar.addMenu("&File")
close_action = QAction("&Close", self)
close_action.setShortcut(QKeySequence.Quit)
close_action.triggered.connect(self.close)
file_menu.addAction(close_action)

# Help menu
help_menu = menubar.addMenu("&Help")
about_action = QAction("&About", self)
about_action.setShortcut(QKeySequence("Ctrl+Q"))
about_action.triggered.connect(self.show_about)
help_menu.addAction(about_action)

left_layout = QVBoxLayout(left_widget)
right_layout = QVBoxLayout(right_widget)

Expand Down Expand Up @@ -679,9 +711,13 @@ def refresh_backends(self):
self.backend_combo.setEnabled(False)
self.logger.info(FOUND_VALID_BACKENDS.format(self.backend_combo.count()))

def update_base_model_visibility(self, index):
is_gguf = self.lora_output_type_combo.itemText(index) == "GGUF"
self.base_model_wrapper.setVisible(is_gguf)
def show_about(self):
about_text = (
"AutoGGUF\n\n"
f"Version: {AUTOGGUF_VERSION}\n\n"
"A tool for managing and converting GGUF models."
)
QMessageBox.about(self, "About AutoGGUF", about_text)

def save_preset(self):
self.logger.info(SAVING_PRESET)
Expand Down Expand Up @@ -1174,20 +1210,6 @@ def refresh_releases(self):
except requests.exceptions.RequestException as e:
show_error(self.logger, ERROR_FETCHING_RELEASES.format(str(e)))

def update_assets(self):
self.logger.debug(UPDATING_ASSET_LIST)
self.asset_combo.clear()
release = self.release_combo.currentData()
if release:
if "assets" in release:
for asset in release["assets"]:
self.asset_combo.addItem(asset["name"], userData=asset)
else:
show_error(
self.logger, NO_ASSETS_FOUND_FOR_RELEASE.format(release["tag_name"])
)
self.update_cuda_option()

def download_llama_cpp(self):
self.logger.info(STARTING_LLAMACPP_DOWNLOAD)
asset = self.asset_combo.currentData()
Expand All @@ -1209,45 +1231,6 @@ def download_llama_cpp(self):
self.download_button.setEnabled(False)
self.download_progress.setValue(0)

def update_cuda_option(self):
self.logger.debug(UPDATING_CUDA_OPTIONS)
asset = self.asset_combo.currentData()

# Handle the case where asset is None
if asset is None:
self.logger.warning(NO_ASSET_SELECTED_FOR_CUDA_CHECK)
self.cuda_extract_checkbox.setVisible(False)
self.cuda_backend_label.setVisible(False)
self.backend_combo_cuda.setVisible(False)
return # Exit the function early

is_cuda = asset and "cudart" in asset["name"].lower()
self.cuda_extract_checkbox.setVisible(is_cuda)
self.cuda_backend_label.setVisible(is_cuda)
self.backend_combo_cuda.setVisible(is_cuda)
if is_cuda:
self.update_cuda_backends()

def update_cuda_backends(self):
self.logger.debug(UPDATING_CUDA_BACKENDS)
self.backend_combo_cuda.clear()
llama_bin = os.path.abspath("llama_bin")
if os.path.exists(llama_bin):
for item in os.listdir(llama_bin):
item_path = os.path.join(llama_bin, item)
if os.path.isdir(item_path) and "cudart-llama" not in item.lower():
if "cu1" in item.lower(): # Only include CUDA-capable backends
self.backend_combo_cuda.addItem(item, userData=item_path)

if self.backend_combo_cuda.count() == 0:
self.backend_combo_cuda.addItem(NO_SUITABLE_CUDA_BACKENDS)
self.backend_combo_cuda.setEnabled(False)
else:
self.backend_combo_cuda.setEnabled(True)

def update_download_progress(self, progress):
self.download_progress.setValue(progress)

def download_finished(self, extract_dir):
self.download_button.setEnabled(True)
self.download_progress.setValue(100)
Expand Down Expand Up @@ -1335,18 +1318,6 @@ def show_task_properties(self, item):
model_info_dialog.exec()
break

def update_threads_spinbox(self, value):
self.threads_spinbox.setValue(value)

def update_threads_slider(self, value):
self.threads_slider.setValue(value)

def update_gpu_offload_spinbox(self, value):
self.gpu_offload_spinbox.setValue(value)

def update_gpu_offload_slider(self, value):
self.gpu_offload_slider.setValue(value)

def toggle_gpu_offload_auto(self, state):
is_auto = state == Qt.CheckState.Checked
self.gpu_offload_slider.setEnabled(not is_auto)
Expand Down Expand Up @@ -1483,17 +1454,6 @@ def validate_quantization_inputs(self):
if errors:
raise ValueError("\n".join(errors))

def update_system_info(self):
ram = psutil.virtual_memory()
cpu = psutil.cpu_percent()
self.ram_bar.setValue(int(ram.percent))
self.ram_bar.setFormat(
RAM_USAGE_FORMAT.format(
ram.percent, ram.used // 1024 // 1024, ram.total // 1024 // 1024
)
)
self.cpu_label.setText(CPU_USAGE_FORMAT.format(cpu))

def add_kv_override(self, override_string=None):
entry = KVOverrideEntry()
entry.deleted.connect(self.remove_kv_override)
Expand Down Expand Up @@ -1679,10 +1639,6 @@ def quantize_model(self):
except Exception as e:
show_error(self.logger, ERROR_STARTING_QUANTIZATION.format(str(e)))

def update_model_info(self, model_info):
self.logger.debug(UPDATING_MODEL_INFO.format(model_info))
pass

def parse_progress(self, line, task_item):
# Parses the output line for progress information and updates the task item.
match = re.search(r"\[(\d+)/(\d+)\]", line)
Expand Down
Loading

0 comments on commit c5e1313

Please sign in to comment.