From 9791e49b0e0e94dce07a3a173767f7486c2d1cbd Mon Sep 17 00:00:00 2001 From: Eric Curtin Date: Wed, 31 Jul 2024 12:02:20 +0100 Subject: [PATCH] Refactoring, huggingface Trying to keep functions no larger than a 80*43 terminal. Alphabetize usage help. Signed-off-by: Eric Curtin --- ramalama | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/ramalama b/ramalama index 6d76fe8d..92315811 100755 --- a/ramalama +++ b/ramalama @@ -137,7 +137,7 @@ def huggingface_download(ramalama_store, model, directory, filename): return run_cmd(["huggingface-cli", "download", directory, filename, "--cache-dir", ramalama_store + "/repos/huggingface/.cache", "--local-dir", ramalama_store + "/repos/huggingface"]) -def pull_huggingface(ramalama_store, model, directory, filename): +def try_huggingface_download(ramalama_store, model, directory, filename): huggingface_download(ramalama_store, model, directory, filename) proc = huggingface_download(ramalama_store, model, directory, filename) return proc.stdout.decode('utf-8') @@ -211,6 +211,23 @@ def list_cli(ramalama_store): os.chdir(mycwd) +def pull_huggingface(model, ramalama_store): + model = re.sub(r'^huggingface://', '', model) + directory, filename = model.rsplit('/', 1) + gguf_path = try_huggingface_download( + ramalama_store, model, directory, filename) + symlink_path = f"{ramalama_store}/models/huggingface/{filename}" + relative_target_path = os.path.relpath( + gguf_path.rstrip(), start=os.path.dirname(symlink_path)) + try: + run_cmd(["ln", "-sf", relative_target_path, symlink_path]) + except subprocess.CalledProcessError as e: + print_error(e) + sys.exit(e.returncode) + + return symlink_path + + def pull_cli(ramalama_store, args): if len(args) < 1: usage() @@ -224,20 +241,7 @@ def pull_cli(ramalama_store, args): return matching_files[0] if model.startswith("huggingface://"): - model = re.sub(r'^huggingface://', '', model) - directory, filename = model.rsplit('/', 1) - gguf_path = pull_huggingface( - ramalama_store, model, directory, filename) - symlink_path = f"{ramalama_store}/models/huggingface/{filename}" - relative_target_path = os.path.relpath( - gguf_path.rstrip(), start=os.path.dirname(symlink_path)) - try: - run_cmd(["ln", "-sf", relative_target_path, symlink_path]) - except subprocess.CalledProcessError as e: - print_error(e) - sys.exit(e.returncode) - - return symlink_path + return pull_huggingface(model, ramalama_store) repos_ollama = ramalama_store + "/repos/ollama" ramalama_models = ramalama_store + "/models/ollama" @@ -284,10 +288,10 @@ def usage(): print(f" {os.path.basename(__file__)} COMMAND") print() print("Commands:") - print(" run MODEL Run a model") + print(" list List models") print(" pull MODEL Pull a model") + print(" run MODEL Run a model") print(" serve MODEL Serve a model") - print(" list List models") sys.exit(1)