From c923e9fe46be437fc07560a774733a6f935f3bde Mon Sep 17 00:00:00 2001 From: Concedo <39025047+LostRuins@users.noreply.github.com> Date: Sat, 31 May 2025 11:51:09 +0800 Subject: [PATCH] added option to unload model from admin control --- koboldcpp.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/koboldcpp.py b/koboldcpp.py index 0f821ed0a..7fffd4b51 100644 --- a/koboldcpp.py +++ b/koboldcpp.py @@ -2946,6 +2946,7 @@ Change Mode
if args.admin and args.admindir and os.path.exists(args.admindir) and self.check_header_password(args.adminpassword): dirpath = os.path.abspath(args.admindir) opts = [f for f in sorted(os.listdir(dirpath)) if (f.endswith(".kcpps") or f.endswith(".kcppt") or f.endswith(".gguf")) and os.path.isfile(os.path.join(dirpath, f))] + opts.append("unload_model") response_body = (json.dumps(opts).encode()) elif self.path.endswith(('/api/extra/perf')): @@ -3437,13 +3438,18 @@ Change Mode
except Exception: targetfile = "" if targetfile and targetfile!="": - dirpath = os.path.abspath(args.admindir) - targetfilepath = os.path.join(dirpath, targetfile) - opts = [f for f in os.listdir(dirpath) if (f.lower().endswith(".kcpps") or f.lower().endswith(".kcppt") or f.lower().endswith(".gguf")) and os.path.isfile(os.path.join(dirpath, f))] - if targetfile in opts and os.path.exists(targetfilepath): - print(f"Admin: Received request to reload config to {targetfile}") - global_memory["restart_target"] = targetfile + if targetfile=="unload_model": #special request to simply unload model + print("Admin: Received request to unload model") + global_memory["restart_target"] = "unload_model" resp = {"success": True} + else: + dirpath = os.path.abspath(args.admindir) + targetfilepath = os.path.join(dirpath, targetfile) + opts = [f for f in os.listdir(dirpath) if (f.lower().endswith(".kcpps") or f.lower().endswith(".kcppt") or f.lower().endswith(".gguf")) and os.path.isfile(os.path.join(dirpath, f))] + if targetfile in opts and os.path.exists(targetfilepath): + print(f"Admin: Received request to reload config to {targetfile}") + global_memory["restart_target"] = targetfile + resp = {"success": True} response_body = (json.dumps(resp).encode()) elif self.path.endswith('/set_tts_settings'): #return dummy response @@ -6139,7 +6145,7 @@ def main(launch_args, default_args): if args.admin and args.admindir: dirpath = os.path.abspath(args.admindir) targetfilepath = os.path.join(dirpath, restart_target) - if os.path.exists(targetfilepath): + if os.path.exists(targetfilepath) or restart_target=="unload_model": print("Terminating old process...") global_memory["load_complete"] = False kcpp_instance.terminate() @@ -6147,7 +6153,12 @@ def main(launch_args, default_args): kcpp_instance = None print("Restarting KoboldCpp...") fault_recovery_mode = True - if targetfilepath.endswith(".gguf"): + if restart_target=="unload_model": + reload_from_new_args(vars(default_args)) + args.model_param = None + args.model = None + args.nomodel = True + elif targetfilepath.endswith(".gguf"): reload_from_new_args(vars(default_args)) args.model_param = targetfilepath else: