Skip to content

Commit

Permalink
Refactoring, huggingface
Browse files Browse the repository at this point in the history
Trying to keep functions no larger than a 80*43 terminal. Alphabetize
usage help.

Signed-off-by: Eric Curtin <ecurtin@redhat.com>
  • Loading branch information
ericcurtin committed Jul 31, 2024
1 parent 8877f80 commit 9791e49
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions ramalama
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand All @@ -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"
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 9791e49

Please sign in to comment.