Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ericcurtin committed Jan 30, 2025
1 parent 59d2fcd commit e15ffdf
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
12 changes: 6 additions & 6 deletions ramalama/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,14 +857,14 @@ def rm_cli(args):


def New(model, args):
if model.startswith("huggingface://") or model.startswith("hf://") or model.startswith("hf.co/"):
if model.startswith("hf://") or model.startswith("huggingface://") or model.startswith("hf.co/"):
return Huggingface(model)
if model.startswith("ollama"):
return Ollama(model)
if model.startswith("oci://") or model.startswith("docker://"):
return OCI(model, args.engine)
if model.startswith("http://") or model.startswith("https://") or model.startswith("file://"):
elif (
model.startswith("https://") or model.startswith("http://") or model.startswith("file://")
) and not model.startswith("https://ollama.com/library/"):
return URL(model)
elif model.startswith("oci://") or model.startswith("docker://"):
return OCI(model, args.engine)

transport = config.get("transport", "ollama")
if transport == "huggingface":
Expand Down
7 changes: 3 additions & 4 deletions ramalama/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pathlib
import urllib.request
from ramalama.common import available, run_cmd, exec_cmd, download_file, verify_checksum, perror
from ramalama.model import Model
from ramalama.model import Model, rm_until_substring

missing_huggingface = """
Optional: Huggingface models require the huggingface-cli module.
Expand Down Expand Up @@ -33,9 +33,8 @@ def fetch_checksum_from_api(url):

class Huggingface(Model):
def __init__(self, model):
model = model.removeprefix("huggingface://")
model = model.removeprefix("hf://")
model = model.removeprefix("hf.co/")
model = rm_until_substring(model, "hf.co/")
model = rm_until_substring(model, "://")
super().__init__(model)
self.type = "huggingface"
split = self.model.rsplit("/", 1)
Expand Down
8 changes: 8 additions & 0 deletions ramalama/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,11 @@ def distinfo_volume():
return ""

return f"-v{path}:/usr/share/ramalama/{dist_info}:ro"

def rm_until_substring(model, substring):
pos = model.find(substring)
if pos == -1:
return model

# Create a new string starting after the found substring
return ''.join(model[i] for i in range(pos + len(substring), len(model)))
16 changes: 4 additions & 12 deletions ramalama/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
import tempfile

import ramalama.annotations as annotations
from ramalama.model import Model, MODEL_TYPES
from ramalama.common import (
engine_version,
exec_cmd,
MNT_FILE,
perror,
run_cmd,
)
from ramalama.model import Model, rm_until_substring
from ramalama.common import engine_version, exec_cmd, MNT_FILE, perror, run_cmd

prefix = "oci://"

Expand Down Expand Up @@ -103,10 +97,8 @@ def list_models(args):

class OCI(Model):
def __init__(self, model, conman):
super().__init__(model.removeprefix(prefix).removeprefix("docker://"))
for t in MODEL_TYPES:
if self.model.startswith(t + "://"):
raise ValueError(f"{model} invalid: Only OCI Model types supported")
model = rm_until_substring(model, "://")
super().__init__(model)
self.type = "OCI"
self.conman = conman

Expand Down
6 changes: 4 additions & 2 deletions ramalama/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import urllib.request
import json
from ramalama.common import run_cmd, verify_checksum, download_file
from ramalama.model import Model
from ramalama.model import Model, rm_until_substring


def fetch_manifest_data(registry_head, model_tag, accept):
Expand Down Expand Up @@ -60,7 +60,9 @@ def init_pull(repos, accept, registry_head, model_name, model_tag, models, model

class Ollama(Model):
def __init__(self, model):
super().__init__(model.removeprefix("ollama://"))
model = rm_until_substring(model, "ollama.com/library/")
model = rm_until_substring(model, "://")
super().__init__(model)
self.type = "Ollama"

def _local(self, args):
Expand Down
12 changes: 4 additions & 8 deletions ramalama/url.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import os
from ramalama.common import download_file
from ramalama.model import Model
from ramalama.model import Model, rm_until_substring
from urllib.parse import urlparse


class URL(Model):
def __init__(self, model):
self.type = ""
for prefix in ["file", "http", "https"]:
if model.startswith(f"{prefix}://"):
self.type = prefix
model = model.removeprefix(f"{prefix}://")
break

self.type = urlparse(model).scheme
model = rm_until_substring(model, "://")
super().__init__(model)
split = self.model.rsplit("/", 1)
self.directory = split[0].removeprefix("/") if len(split) > 1 else ""
Expand Down

0 comments on commit e15ffdf

Please sign in to comment.