mirror of
https://github.com/LostRuins/koboldcpp.git
synced 2025-09-10 17:14:36 +00:00
added handling for remembering file paths, added gui option to disable zenity in GUI
This commit is contained in:
parent
f6b7fea979
commit
a56cc72bd0
1 changed files with 47 additions and 46 deletions
93
koboldcpp.py
93
koboldcpp.py
|
@ -110,7 +110,8 @@ start_time = time.time()
|
|||
last_req_time = time.time()
|
||||
last_non_horde_req_time = time.time()
|
||||
currfinishreason = None
|
||||
|
||||
zenity_recent_dir = os.getcwd()
|
||||
zenity_permitted = True
|
||||
|
||||
saved_stdout = None
|
||||
saved_stderr = None
|
||||
|
@ -3456,37 +3457,28 @@ def RunServerMultiThreaded(addr, port, server_handler):
|
|||
sys.exit(0)
|
||||
|
||||
# Based on https://github.com/mathgeniuszach/xdialog/blob/main/xdialog/zenity_dialogs.py - MIT license | - Expanded version by Henk717
|
||||
def zenity(filetypes=None, initialdir="", initialfile="", **kwargs) -> Tuple[int, str]:
|
||||
import shutil
|
||||
import subprocess
|
||||
global zenity_recent_dir, zenity_permitted
|
||||
|
||||
def zenity_clean(txt: str):
|
||||
return txt\
|
||||
.replace("\\", "\\\\")\
|
||||
.replace("$", "\\$")\
|
||||
.replace("!", "\\!")\
|
||||
.replace("*", "\\*")\
|
||||
.replace("?", "\\?")\
|
||||
.replace("&", "&")\
|
||||
.replace("|", "|")\
|
||||
.replace("<", "<")\
|
||||
.replace(">", ">")\
|
||||
.replace("(", "\\(")\
|
||||
.replace(")", "\\)")\
|
||||
.replace("[", "\\[")\
|
||||
.replace("]", "\\]")\
|
||||
.replace("{", "\\{")\
|
||||
.replace("}", "\\}")\
|
||||
|
||||
def zenity(typ, filetypes=None, initialdir="", initialfile="", **kwargs) -> Tuple[int, str]:
|
||||
import shutil, subprocess, os, platform
|
||||
if not platform.system() == "Linux":
|
||||
raise Exception("This feature should only be used on Linux, if you see this error there is no TK fallback implemented in the code.")
|
||||
if not zenity_permitted:
|
||||
raise Exception("Zenity disabled, attempting to use TK GUI.")
|
||||
if sys.platform != "linux":
|
||||
raise Exception("Zenity GUI is only usable on Linux, attempting to use TK GUI.")
|
||||
zenity_bin = shutil.which("zenity")
|
||||
if not zenity_bin:
|
||||
zenity_bin = shutil.which("yad")
|
||||
zenity_bin = shutil.which("yad")
|
||||
if not zenity_bin:
|
||||
raise Exception("Zenity not present")
|
||||
raise Exception("Zenity not present, falling back to TK GUI.")
|
||||
|
||||
def zenity_clean(txt: str):
|
||||
return txt.replace("\\", "\\\\").replace("$", "\\$").replace("!", "\\!").replace("*", "\\*")\
|
||||
.replace("?", "\\?").replace("&", "&").replace("|", "|").replace("<", "<").replace(">", ">")\
|
||||
.replace("(", "\\(").replace(")", "\\)").replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("}", "\\}")
|
||||
|
||||
# Build args based on keywords
|
||||
args = ['/usr/bin/env', zenity_bin, '--'+typ]
|
||||
args = ['/usr/bin/env', zenity_bin, '--file-selection']
|
||||
for k, v in kwargs.items():
|
||||
if v is True:
|
||||
args.append(f'--{k.replace("_", "-").strip("-")}')
|
||||
|
@ -3503,7 +3495,7 @@ def zenity(typ, filetypes=None, initialdir="", initialfile="", **kwargs) -> Tupl
|
|||
|
||||
# Default filename and folder
|
||||
if initialdir is None:
|
||||
initialdir=os.getcwd()
|
||||
initialdir=zenity_recent_dir
|
||||
if initialfile is None:
|
||||
initialfile=""
|
||||
initialpath = os.path.join(initialdir, initialfile)
|
||||
|
@ -3513,53 +3505,54 @@ def zenity(typ, filetypes=None, initialdir="", initialfile="", **kwargs) -> Tupl
|
|||
clean_env.pop("LD_LIBRARY_PATH", None)
|
||||
clean_env["PATH"] = "/usr/bin:/bin"
|
||||
|
||||
proc = subprocess.Popen(
|
||||
procres = subprocess.run(
|
||||
args,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
shell=False,
|
||||
env=clean_env
|
||||
env=clean_env,
|
||||
check=False
|
||||
)
|
||||
stdout, _ = proc.communicate()
|
||||
|
||||
return (proc.returncode, stdout.decode('utf-8').strip())
|
||||
result = procres.stdout.decode('utf-8').strip()
|
||||
if procres.returncode==0 and result:
|
||||
directory = result
|
||||
if not os.path.isdir(result):
|
||||
directory = os.path.dirname(result)
|
||||
zenity_recent_dir = directory
|
||||
return (procres.returncode, result)
|
||||
|
||||
# note: In this section we wrap around file dialogues to allow for zenity
|
||||
|
||||
def zentk_askopenfilename(**options):
|
||||
try:
|
||||
from os.path import isfile
|
||||
result = zenity('file-selection', filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), title=options.get("title"))[1]
|
||||
if result and not isfile(result):
|
||||
result = zenity(filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), title=options.get("title"))[1]
|
||||
if result and not os.path.isfile(result):
|
||||
print("A folder was selected while we need a file, ignoring selection.")
|
||||
return ''
|
||||
except:
|
||||
except Exception:
|
||||
from tkinter.filedialog import askopenfilename
|
||||
result = askopenfilename(**options)
|
||||
return result
|
||||
|
||||
def zentk_askopenmultiplefilenames(**options):
|
||||
try:
|
||||
from os.path import isfile
|
||||
files = zenity('file-selection', filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), title=options.get("title"), multiple=True, separator="\n")[1].splitlines()
|
||||
result = tuple(filter(isfile, files))
|
||||
except:
|
||||
files = zenity(filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), title=options.get("title"), multiple=True, separator="\n")[1].splitlines()
|
||||
result = tuple(filter(os.path.isfile, files))
|
||||
except Exception:
|
||||
from tkinter.filedialog import askopenfilenames
|
||||
result = askopenfilenames(**options)
|
||||
return result
|
||||
|
||||
def zentk_askdirectory(**options):
|
||||
try:
|
||||
result = zenity('file-selection', initialdir=options.get("initialdir"), title=options.get("title"), directory=True)[1]
|
||||
except:
|
||||
result = zenity(initialdir=options.get("initialdir"), title=options.get("title"), directory=True)[1]
|
||||
except Exception:
|
||||
from tkinter.filedialog import askdirectory
|
||||
result = askdirectory(**options)
|
||||
return result
|
||||
|
||||
def zentk_asksaveasfilename(**options):
|
||||
try:
|
||||
result = zenity('file-selection', filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), initialfile=options.get("initialfile"), title=options.get("title"), save=True)[1]
|
||||
except:
|
||||
result = zenity(filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), initialfile=options.get("initialfile"), title=options.get("title"), save=True)[1]
|
||||
except Exception:
|
||||
from tkinter.filedialog import asksaveasfilename
|
||||
result = asksaveasfilename(**options)
|
||||
return result
|
||||
|
@ -3802,6 +3795,8 @@ def show_gui():
|
|||
admin_dir_var = ctk.StringVar()
|
||||
admin_password_var = ctk.StringVar()
|
||||
|
||||
nozenity_var = ctk.IntVar(value=0)
|
||||
|
||||
curr_tab_idx = 0
|
||||
|
||||
def tabbuttonaction(name):
|
||||
|
@ -4439,6 +4434,12 @@ def show_gui():
|
|||
ctk.CTkButton(extra_tab , text = "Generate LaunchTemplate", command = kcpp_export_template ).grid(row=5,column=0, stick="w", padx= 8, pady=2)
|
||||
makelabel(extra_tab, "Analyze GGUF Metadata", 6, 0,tooltiptxt="Reads the metadata, weight types and tensor names in any GGUF file.")
|
||||
ctk.CTkButton(extra_tab , text = "Analyze GGUF", command = analyze_gguf_model_wrapper ).grid(row=7,column=0, stick="w", padx= 8, pady=2)
|
||||
if sys.platform == "linux":
|
||||
def togglezenity(a,b,c):
|
||||
global zenity_permitted
|
||||
zenity_permitted = (nozenity_var.get()==0)
|
||||
makecheckbox(extra_tab, "Use Classic FilePicker", nozenity_var, 20, tooltiptxt="Use the classic TKinter file picker instead.")
|
||||
nozenity_var.trace("w", togglezenity)
|
||||
|
||||
# launch
|
||||
def guilaunch():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue