-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
145 lines (128 loc) · 5.97 KB
/
__init__.py
File metadata and controls
145 lines (128 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# # # <!-- // /* SPDX-License-Identifier: blessing) */ -->
# # # <!-- // /* d a r k s h a p e s */ -->
"""Register model types"""
# pylint: disable=line-too-long, import-outside-toplevel
from typing import Dict, List, Tuple
from pydantic import BaseModel, computed_field
from nnll_01 import debug_message as dbug, debug_monitor # , info_message as nfo
from nnll_15.constants import VALID_CONVERSIONS, VALID_TASKS, LibType
# import open_webui
# from package import response_panel
class RegistryEntry(BaseModel):
"""Validate Hub / Ollama / LMStudio model input"""
model: str
size: int
tags: list[str]
library: LibType
timestamp: int
# tokenizer: None
@computed_field
@property
def available_tasks(self) -> List[Tuple]:
"""Filter tag tasks into edge coordinates for graphing"""
import re
default_task = None
library_tasks = {}
processed_tasks = []
library_tasks = VALID_TASKS[self.library]
if self.library == LibType.OLLAMA or self.library == LibType.CORTEX:
default_task = ("text", "text")
elif self.library == LibType.HUB:
pattern = re.compile(r"(\w+)-to-(\w+)")
for tag in self.tags:
match = pattern.search(tag)
if match and all(group in VALID_CONVERSIONS for group in match.groups()):
processed_tasks.append((match.group(1), match.group(2)))
# placeholder for VLLM/LMSTUDIO Libraries
for tag in self.tags:
for (graph_src, graph_dest), tags in library_tasks.items():
if tag in tags and (graph_src, graph_dest) not in processed_tasks:
processed_tasks.append((graph_src, graph_dest))
if default_task and default_task not in processed_tasks:
processed_tasks.append(default_task)
return processed_tasks
@classmethod
@debug_monitor
def from_model_data(cls) -> list[tuple[str]]: # lib_type: LibType) model_data: tuple[frozenset[str]]
"""# todo - split into dependency-specific implementations
Create RegistryEntry instances based on source\n
Extract common model information and stack by newest model first for each conversion type.\n
:param lib_type: Origin of this data (eg: HuggingFace, Ollama, CivitAI, ModelScope)
:return: A list of RegistryEntry objects containing model metadata relevant to execution\n
"""
entries = []
if LibType.OLLAMA:
try:
from ollama import ListResponse, list as ollama_list
model_data: ListResponse = ollama_list() # type: ignore
except (ConnectionError, ModuleNotFoundError, ImportError) as error_log:
dbug(error_log)
else:
for model in model_data.models: # pylint:disable=no-member
entry = cls(
model=f"ollama_chat/{model.model}",
size=model.size.real,
tags=[model.details.family],
library=LibType.OLLAMA,
timestamp=int(model.modified_at.timestamp()),
)
entries.append(entry)
if LibType.HUB:
try:
from huggingface_hub import scan_cache_dir, repocard # type: ignore
except (ModuleNotFoundError, ImportError) as error_log:
dbug(error_log)
else:
model_data = scan_cache_dir()
for repo in model_data.repos:
try:
meta = repocard.RepoCard.load(repo.repo_id).data
except ValueError as error_log:
dbug(error_log)
continue
tags = []
if hasattr(meta, "tags"):
tags.extend(meta.tags)
if hasattr(meta, "pipeline_tag"):
tags.append(meta.pipeline_tag)
if not tags:
tags = ["unknown"]
entry = cls(model=repo.repo_id, size=repo.size_on_disk, tags=tags, library=LibType.HUB, timestamp=int(repo.last_modified))
entries.append(entry)
if LibType.CORTEX:
import requests
from datetime import datetime
response = requests.get("http://127.0.0.1:39281/v1/models", timeout=(3, 3))
model = response.json()
for model_data in model["data"]:
entry = cls(
model=f"openai/{model_data.get('model')}",
size=model_data.get("size", 0),
tags=[model_data.get("modalities", ["text", "text"])],
library=LibType.CORTEX,
timestamp=datetime.timestamp(datetime.now()), # no api for this data in cortex
)
entries.append(entry)
if LibType.LM_STUDIO: # doesn't populate RegitryEntry yet
try:
from lmstudio import get_default_client, list_downloaded_models # type: ignore
except (ModuleNotFoundError, ImportError) as error_log:
dbug(error_log)
if LibType.VLLM: # placeholder
try:
import vllm # type: ignore # noqa: F401 #pylint:disable=unused-import
except (ModuleNotFoundError, ImportError) as error_log:
dbug(error_log)
# else:
# nfo("Unsupported source")
# raise ValueError("Unsupported source")
return sorted(entries, key=lambda x: x.timestamp, reverse=True)
@debug_monitor
def from_cache() -> Dict[str, RegistryEntry]:
"""
Retrieve models from ollama server, local huggingface hub cache, !!! Incomplete! local lmstudio cache & vllm.
我們不應該繼續為LMStudio編碼。 歡迎貢獻者來改進它。 LMStudio is not OSS, but contributions are welcome.
"""
models = None
models = RegistryEntry.from_model_data()
return models