forked from Uminosachi/sd-webui-inpaint-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ia_config.py
150 lines (112 loc) · 4.72 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import configparser
import json
import os
from types import SimpleNamespace
from modules import shared
from ia_ui_items import get_inp_model_ids, get_inp_webui_model_ids, get_sam_model_ids
class IAConfig:
SECTIONS = SimpleNamespace(
DEFAULT=configparser.DEFAULTSECT,
USER="USER",
)
KEYS = SimpleNamespace(
SAM_MODEL_ID="sam_model_id",
INP_MODEL_ID="inp_model_id",
INP_WEBUI_MODEL_ID="inp_webui_model_id",
)
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 __init__(self):
self.ids_dict = {}
self.ids_dict[IAConfig.KEYS.SAM_MODEL_ID] = {
"list": get_sam_model_ids(),
"index": 1,
}
self.ids_dict[IAConfig.KEYS.INP_MODEL_ID] = {
"list": get_inp_model_ids(),
"index": 0,
}
self.ids_dict[IAConfig.KEYS.INP_WEBUI_MODEL_ID] = {
"list": get_inp_webui_model_ids(),
"index": 0,
}
self.webui_keys = {}
self.webui_keys[IAConfig.KEYS.SAM_MODEL_ID] = "inpaint_anything/Segment Anything Model ID/value"
self.webui_keys[IAConfig.KEYS.INP_MODEL_ID] = "inpaint_anything/Inpainting Model ID/value"
self.webui_keys[IAConfig.KEYS.INP_WEBUI_MODEL_ID] = "inpaint_anything/Inpainting Model ID webui/value"
ia_config = IAConfig()
def setup_ia_config_ini():
ia_config_ini = configparser.ConfigParser(defaults={})
if os.path.isfile(IAConfig.PATHS.INI):
ia_config_ini.read(IAConfig.PATHS.INI, encoding="utf-8")
changed = False
for key, ids_info in ia_config.ids_dict.items():
if not ia_config_ini.has_option(IAConfig.SECTIONS.DEFAULT, key):
if len(ids_info["list"]) > ids_info["index"]:
ia_config_ini[IAConfig.SECTIONS.DEFAULT][key] = ids_info["list"][ids_info["index"]]
changed = True
else:
if len(ids_info["list"]) > ids_info["index"] and ia_config_ini[IAConfig.SECTIONS.DEFAULT][key] != ids_info["list"][ids_info["index"]]:
ia_config_ini[IAConfig.SECTIONS.DEFAULT][key] = ids_info["list"][ids_info["index"]]
changed = True
if changed:
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(defaults={})
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)
ids_dict = ia_config.ids_dict
if value is None:
if key in ids_dict.keys():
ids_info = ids_dict[key]
return ids_info["index"]
else:
return 0
else:
if key in ids_dict.keys():
ids_info = ids_dict[key]
return ids_info["list"].index(value) if value in ids_info["list"] else ids_info["index"]
else:
return 0
def set_ia_config(key, value, section=IAConfig.SECTIONS.DEFAULT):
setup_ia_config_ini()
ia_config_ini = configparser.ConfigParser(defaults={})
ia_config_ini.read(IAConfig.PATHS.INI, encoding="utf-8")
if ia_config_ini.has_option(section, key) and ia_config_ini[section][key] == value:
return
if section != IAConfig.SECTIONS.DEFAULT and not ia_config_ini.has_section(section):
ia_config_ini[section] = {}
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):
try:
with open(IAConfig.PATHS.WEBUI_CONFIG, "r", encoding="utf-8") as f:
webui_config = json.load(f)
webui_keys = ia_config.webui_keys
if key in webui_keys.keys() and webui_keys[key] in webui_config.keys():
webui_config[webui_keys[key]] = value
with open(IAConfig.PATHS.WEBUI_CONFIG, "w", encoding="utf-8") as f:
json.dump(webui_config, f, indent=4)
except Exception:
pass
def get_webui_setting(key, default):
value = shared.opts.data.get(key, default)
if not isinstance(value, type(default)):
value = default
return value