Skip to content

Commit e0c3f04

Browse files
committed
People search for ollama models using the web ui, this change allows one to copy the url from the browser and for it to be compatible with ramalama run. Signed-off-by: Eric Curtin <[email protected]>
1 parent 59d2fcd commit e0c3f04

File tree

6 files changed

+30
-32
lines changed

6 files changed

+30
-32
lines changed

ramalama/cli.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -857,14 +857,14 @@ def rm_cli(args):
857857

858858

859859
def New(model, args):
860-
if model.startswith("huggingface://") or model.startswith("hf://") or model.startswith("hf.co/"):
860+
if model.startswith("hf://") or model.startswith("huggingface://") or model.startswith("hf.co/"):
861861
return Huggingface(model)
862-
if model.startswith("ollama"):
863-
return Ollama(model)
864-
if model.startswith("oci://") or model.startswith("docker://"):
865-
return OCI(model, args.engine)
866-
if model.startswith("http://") or model.startswith("https://") or model.startswith("file://"):
862+
elif (
863+
model.startswith("https://") or model.startswith("http://") or model.startswith("file://")
864+
) and not model.startswith("https://ollama.com/library/"):
867865
return URL(model)
866+
elif model.startswith("oci://") or model.startswith("docker://"):
867+
return OCI(model, args.engine)
868868

869869
transport = config.get("transport", "ollama")
870870
if transport == "huggingface":

ramalama/common.py

+9
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,12 @@ def get_env_vars():
243243
# env_vars[gpu_type] = str(gpu_num)
244244

245245
return env_vars
246+
247+
248+
def rm_until_substring(model, substring):
249+
pos = model.find(substring)
250+
if pos == -1:
251+
return model
252+
253+
# Create a new string starting after the found substring
254+
return ''.join(model[i] for i in range(pos + len(substring), len(model)))

ramalama/huggingface.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import pathlib
33
import urllib.request
4-
from ramalama.common import available, run_cmd, exec_cmd, download_file, verify_checksum, perror
4+
from ramalama.common import available, run_cmd, exec_cmd, download_file, verify_checksum, perror, rm_until_substring
55
from ramalama.model import Model
66

77
missing_huggingface = """
@@ -33,9 +33,8 @@ def fetch_checksum_from_api(url):
3333

3434
class Huggingface(Model):
3535
def __init__(self, model):
36-
model = model.removeprefix("huggingface://")
37-
model = model.removeprefix("hf://")
38-
model = model.removeprefix("hf.co/")
36+
model = rm_until_substring(model, "hf.co/")
37+
model = rm_until_substring(model, "://")
3938
super().__init__(model)
4039
self.type = "huggingface"
4140
split = self.model.rsplit("/", 1)

ramalama/oci.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@
44
import tempfile
55

66
import ramalama.annotations as annotations
7-
from ramalama.model import Model, MODEL_TYPES
8-
from ramalama.common import (
9-
engine_version,
10-
exec_cmd,
11-
MNT_FILE,
12-
perror,
13-
run_cmd,
14-
)
7+
from ramalama.model import Model
8+
from ramalama.common import engine_version, exec_cmd, MNT_FILE, perror, run_cmd, rm_until_substring
159

1610
prefix = "oci://"
1711

@@ -103,10 +97,8 @@ def list_models(args):
10397

10498
class OCI(Model):
10599
def __init__(self, model, conman):
106-
super().__init__(model.removeprefix(prefix).removeprefix("docker://"))
107-
for t in MODEL_TYPES:
108-
if self.model.startswith(t + "://"):
109-
raise ValueError(f"{model} invalid: Only OCI Model types supported")
100+
model = rm_until_substring(model, "://")
101+
super().__init__(model)
110102
self.type = "OCI"
111103
self.conman = conman
112104

ramalama/ollama.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import urllib.request
33
import json
4-
from ramalama.common import run_cmd, verify_checksum, download_file
4+
from ramalama.common import run_cmd, verify_checksum, download_file, rm_until_substring
55
from ramalama.model import Model
66

77

@@ -60,7 +60,9 @@ def init_pull(repos, accept, registry_head, model_name, model_tag, models, model
6060

6161
class Ollama(Model):
6262
def __init__(self, model):
63-
super().__init__(model.removeprefix("ollama://"))
63+
model = rm_until_substring(model, "ollama.com/library/")
64+
model = rm_until_substring(model, "://")
65+
super().__init__(model)
6466
self.type = "Ollama"
6567

6668
def _local(self, args):

ramalama/url.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import os
2-
from ramalama.common import download_file
2+
from ramalama.common import download_file, rm_until_substring
33
from ramalama.model import Model
4+
from urllib.parse import urlparse
45

56

67
class URL(Model):
78
def __init__(self, model):
8-
self.type = ""
9-
for prefix in ["file", "http", "https"]:
10-
if model.startswith(f"{prefix}://"):
11-
self.type = prefix
12-
model = model.removeprefix(f"{prefix}://")
13-
break
14-
9+
self.type = urlparse(model).scheme
10+
model = rm_until_substring(model, "://")
1511
super().__init__(model)
1612
split = self.model.rsplit("/", 1)
1713
self.directory = split[0].removeprefix("/") if len(split) > 1 else ""

0 commit comments

Comments
 (0)