-
Notifications
You must be signed in to change notification settings - Fork 33
/
invoke-spandrel.py
52 lines (39 loc) · 1.35 KB
/
invoke-spandrel.py
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
import sys
import json
import importlib.util
import os
def is_installed(package: str) -> bool:
return importlib.util.find_spec(package) is not None
def return_success(data: object):
print("SUCCESS: " + json.dumps(data))
sys.exit(0)
def return_error(message: str):
print("ERROR: " + json.dumps({"message": message}))
sys.exit(0)
def print_metadata(file: str):
import spandrel
try:
loader = spandrel.ModelLoader()
model = loader.load_from_file(file)
return_success(
{
"architecture": model.architecture.name,
"tags": model.tags,
"scale": model.scale,
"inputChannels": model.input_channels,
"outputChannels": model.output_channels,
}
)
except spandrel.UnsupportedModelError:
return_error("Unsupported model architecture")
if __name__ == "__main__":
if not is_installed("spandrel"):
if is_installed("torch"):
print("Installing spandrel...")
current_python = sys.executable
os.system(current_python + " -m pip install spandrel")
else:
return_error(
"PyTorch is not installed. Install PyTorch on your system's Python installation to automatically detect model metadata."
)
print_metadata(sys.argv[1])