forked from Uminosachi/sd-webui-inpaint-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ia_config.py
126 lines (91 loc) · 3.75 KB
/
ia_config.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
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
import configparser
import json
import os
from types import SimpleNamespace
from modules import shared
from ia_ui_items import get_inp_model_ids, get_sam_model_ids
class IAConfig:
SECTIONS = SimpleNamespace(
DEFAULT="DEFAULT",
USER="USER",
)
KEYS = SimpleNamespace(
SAM_MODEL_ID="sam_model_id",
INP_MODEL_ID="inp_model_id",
)
WEBUI_KEYS = SimpleNamespace(
SAM_MODEL_ID="inpaint_anything/Segment Anything Model ID/value",
INP_MODEL_ID="inpaint_anything/Inpainting Model ID/value",
)
PATHS = SimpleNamespace(
INI=os.path.join(os.path.dirname(os.path.realpath(__file__)), "ia_config.ini"),
WEBUI_CONFIG=os.path.join(shared.data_path, "ui-config.json"),
)
def setup_ia_config_ini():
if not os.path.isfile(IAConfig.PATHS.INI):
ia_config_ini = configparser.ConfigParser()
sam_model_ids = get_sam_model_ids()
sam_model_index = 1
inp_model_ids = get_inp_model_ids()
inp_model_index = 0
ia_config_ini[IAConfig.SECTIONS.DEFAULT] = {
IAConfig.KEYS.SAM_MODEL_ID: sam_model_ids[sam_model_index],
IAConfig.KEYS.INP_MODEL_ID: inp_model_ids[inp_model_index],
}
with open(IAConfig.PATHS.INI, "w", encoding="utf-8") as f:
ia_config_ini.write(f)
def get_ia_config(key, section=IAConfig.SECTIONS.DEFAULT):
setup_ia_config_ini()
ia_config_ini = configparser.ConfigParser()
ia_config_ini.read(IAConfig.PATHS.INI, encoding="utf-8")
if ia_config_ini.has_option(section, key):
return ia_config_ini[section][key]
section = IAConfig.SECTIONS.DEFAULT
if ia_config_ini.has_option(section, key):
return ia_config_ini[section][key]
return None
def get_ia_config_index(key, section=IAConfig.SECTIONS.DEFAULT):
value = get_ia_config(key, section)
if value is None:
return None
if key == IAConfig.KEYS.SAM_MODEL_ID:
sam_model_ids = get_sam_model_ids()
idx = sam_model_ids.index(value) if value in sam_model_ids else 1
elif key == IAConfig.KEYS.INP_MODEL_ID:
inp_model_ids = get_inp_model_ids()
idx = inp_model_ids.index(value) if value in inp_model_ids else 0
else:
idx = None
return idx
def set_ia_config(key, value, section=IAConfig.SECTIONS.DEFAULT):
setup_ia_config_ini()
ia_config_ini = configparser.ConfigParser()
ia_config_ini.read(IAConfig.PATHS.INI, encoding="utf-8")
if section != IAConfig.SECTIONS.DEFAULT and not ia_config_ini.has_section(section):
ia_config_ini[section] = {}
else:
if ia_config_ini.has_option(section, key) and ia_config_ini[section][key] == value:
return
try:
ia_config_ini[section][key] = value
except Exception:
ia_config_ini[section] = {}
ia_config_ini[section][key] = value
with open(IAConfig.PATHS.INI, "w", encoding="utf-8") as f:
ia_config_ini.write(f)
if os.path.isfile(IAConfig.PATHS.WEBUI_CONFIG):
with open(IAConfig.PATHS.WEBUI_CONFIG, "r", encoding="utf-8") as f:
webui_config = json.load(f)
if key == IAConfig.KEYS.SAM_MODEL_ID:
if IAConfig.WEBUI_KEYS.SAM_MODEL_ID in webui_config.keys():
webui_config[IAConfig.WEBUI_KEYS.SAM_MODEL_ID] = value
elif key == IAConfig.KEYS.INP_MODEL_ID:
if IAConfig.WEBUI_KEYS.INP_MODEL_ID in webui_config.keys():
webui_config[IAConfig.WEBUI_KEYS.INP_MODEL_ID] = value
with open(IAConfig.PATHS.WEBUI_CONFIG, "w", encoding="utf-8") as f:
json.dump(webui_config, f, indent=4)
def get_webui_setting(key, default):
value = shared.opts.data.get(key, default)
if not isinstance(value, type(default)):
value = default
return value