Skip to content

Commit 7f48ed3

Browse files
committed
allow unpacking in CLI
1 parent 8e5fd6f commit 7f48ed3

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

koboldcpp.py

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,47 @@ def tryparseint(value):
391391
except ValueError:
392392
return value
393393

394+
def unpack_to_dir(destpath = ""):
395+
import shutil
396+
srcpath = os.path.abspath(os.path.dirname(__file__))
397+
cliunpack = False if destpath == "" else True
398+
print("Attempt to unpack KoboldCpp into directory...")
399+
400+
if not cliunpack:
401+
from tkinter.filedialog import askdirectory
402+
from tkinter import messagebox
403+
destpath = askdirectory(title='Select an empty folder to unpack KoboldCpp')
404+
if not destpath:
405+
return
406+
407+
if os.path.isdir(srcpath) and os.path.isdir(destpath) and not os.listdir(destpath):
408+
try:
409+
if cliunpack:
410+
print(f"KoboldCpp will be extracted to {destpath}\nThis process may take several seconds to complete.")
411+
else:
412+
messagebox.showinfo("Unpack Starting", f"KoboldCpp will be extracted to {destpath}\nThis process may take several seconds to complete.")
413+
for item in os.listdir(srcpath):
414+
s = os.path.join(srcpath, item)
415+
d = os.path.join(destpath, item)
416+
if os.path.isdir(s):
417+
shutil.copytree(s, d, False, None)
418+
else:
419+
shutil.copy2(s, d)
420+
if cliunpack:
421+
print(f"KoboldCpp successfully extracted to {destpath}")
422+
else:
423+
messagebox.showinfo("KoboldCpp Unpack Success", f"KoboldCpp successfully extracted to {destpath}")
424+
except Exception as e:
425+
if cliunpack:
426+
print(f"An error occurred while unpacking: {e}")
427+
else:
428+
messagebox.showerror("Error", f"An error occurred while unpacking: {e}")
429+
else:
430+
if cliunpack:
431+
print(f"The target folder is not empty or invalid. Please select an empty folder.")
432+
else:
433+
messagebox.showwarning("Invalid Selection", "The target folder is not empty or invalid. Please select an empty folder.")
434+
394435
def load_model(model_filename):
395436
global args
396437
inputs = load_model_inputs()
@@ -2466,30 +2507,6 @@ def toggletaesd(a,b,c):
24662507
audio_tab = tabcontent["Audio"]
24672508
makefileentry(audio_tab, "Whisper Model (Speech-To-Text):", "Select Whisper .bin Model File", whisper_model_var, 1, width=280, filetypes=[("*.bin","*.bin")], tooltiptxt="Select a Whisper .bin model file on disk to be loaded.")
24682509

2469-
def unpack_to_dir():
2470-
from tkinter.filedialog import askdirectory
2471-
from tkinter import messagebox
2472-
import shutil
2473-
destpath = askdirectory(title='Select an empty folder to unpack KoboldCpp')
2474-
if not destpath:
2475-
return
2476-
srcpath = os.path.abspath(os.path.dirname(__file__))
2477-
if os.path.isdir(srcpath) and os.path.isdir(destpath) and not os.listdir(destpath):
2478-
try:
2479-
messagebox.showinfo("Unpack Starting", f"KoboldCpp will be extracted to {destpath}\nThis process may take several seconds to complete.")
2480-
for item in os.listdir(srcpath):
2481-
s = os.path.join(srcpath, item)
2482-
d = os.path.join(destpath, item)
2483-
if os.path.isdir(s):
2484-
shutil.copytree(s, d, False, None)
2485-
else:
2486-
shutil.copy2(s, d)
2487-
messagebox.showinfo("KoboldCpp Unpack Success", f"KoboldCpp extracted to {destpath}")
2488-
except Exception as e:
2489-
messagebox.showerror("Error", f"An error occurred while unpacking: {e}")
2490-
else:
2491-
messagebox.showwarning("Invalid Selection", "The folder is not empty or invalid. Please select an empty folder.")
2492-
24932510
# extra tab
24942511
extra_tab = tabcontent["Extra"]
24952512
makelabel(extra_tab, "Unpack KoboldCpp to a local directory to modify its files.", 1, 0)
@@ -3279,6 +3296,10 @@ def main(launch_args,start_server=True):
32793296
print(f"Error cleaning up orphaned pyinstaller dirs: {e}")
32803297

32813298
args = launch_args
3299+
if args.unpack:
3300+
unpack_to_dir(args.unpack)
3301+
return
3302+
32823303
if args.config and len(args.config)==1:
32833304
if isinstance(args.config[0], str) and os.path.exists(args.config[0]):
32843305
loadconfigfile(args.config[0])
@@ -3756,7 +3777,7 @@ def range_checker(arg: str):
37563777
# print("Python version: " + sys.version)
37573778
parser = argparse.ArgumentParser(description='KoboldCpp Server')
37583779
modelgroup = parser.add_mutually_exclusive_group() #we want to be backwards compatible with the unnamed positional args
3759-
modelgroup.add_argument("--model", metavar=('filename'), help="Model file to load", nargs="?")
3780+
modelgroup.add_argument("--model", metavar=('[filename]'), help="Model file to load", type=str, default="")
37603781
modelgroup.add_argument("model_param", help="Model file to load (positional)", nargs="?")
37613782
portgroup = parser.add_mutually_exclusive_group() #we want to be backwards compatible with the unnamed positional args
37623783
portgroup.add_argument("--port", metavar=('[portnumber]'), help="Port to listen on", default=defaultport, type=int, action='store')
@@ -3811,6 +3832,8 @@ def range_checker(arg: str):
38113832
advparser.add_argument("--quantkv", help="Sets the KV cache data type quantization, 0=f16, 1=q8, 2=q4. Requires Flash Attention, and disables context shifting.",metavar=('[quantization level 0/1/2]'), type=int, choices=[0,1,2], default=0)
38123833
advparser.add_argument("--forceversion", help="If the model file format detection fails (e.g. rogue modified model) you can set this to override the detected format (enter desired version, e.g. 401 for GPTNeoX-Type2).",metavar=('[version]'), type=int, default=0)
38133834
advparser.add_argument("--smartcontext", help="Reserving a portion of context to try processing less frequently. Outdated. Not recommended.", action='store_true')
3835+
advparser.add_argument("--unpack", help="Extracts the file contents of the KoboldCpp binary into a target directory.", metavar=('destination'), type=str, default="")
3836+
38143837

38153838
hordeparsergroup = parser.add_argument_group('Horde Worker Commands')
38163839
hordeparsergroup.add_argument("--hordemodelname", metavar=('[name]'), help="Sets your AI Horde display model name.", default="")

0 commit comments

Comments
 (0)