Skip to content

Commit 186e85c

Browse files
author
legionfu
committed
纠正旧版序号问题
1 parent ae8f81a commit 186e85c

File tree

3 files changed

+97
-55
lines changed

3 files changed

+97
-55
lines changed

ChangeLog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 2.1.2
1+
## 2.1.3
22

33
### Features:
44
* 缺失LoRA模型提示。

javascript/state.utils.js

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,7 @@ state.utils = {
77
testFunction: function testFunction() {
88
//console.log(state.extensions)
99
// const button = gradioApp().getElementById("lightdiffusionflow_set_elements");
10-
// button.click();
11-
json_obj = {
12-
"state-ext-control-net-txt2img_0-enable": "true",
13-
"state-ext-control-net-txt2img_0-canny-high-threshold": "255",
14-
"state-ext-control-net-txt2img_3-enable": "true",
15-
"state-ext-control-net-txt2img_0-starting-control-step": "1",
16-
"state-ext-control-net-txt2img_0-canny-low-threshold": "255",
17-
"state-ext-control-net-txt2img_0-control-weight": "2",
18-
"state-ext-control-net-txt2img_3-control-type": "OpenPose",
19-
"state-ext-control-net-txt2img_0-control-mode": "ControlNet is more important",
20-
"state-ext-control-net-txt2img_0-control-type": "Canny",
21-
"state-setting_sd_model_checkpoint": "majicmixRealistic_betterV2V25.safetensors [d7e2ac2f4a]",
22-
"state-ext-control-net-txt2img_0-preprocessor-resolution": "2048",
23-
"state-ext-control-net-txt2img-tab": "ControlNet Unit 3 ",
24-
"state-ext-control-net-txt2img_1-enable": "false",
25-
"state-tab": "txt2img ",
26-
"state-ext-control-net-txt2img-toggled": "true",
27-
"txt2img_controlnet_ControlNet-1_input_image": "data:image",
28-
"state-ext-control-net-txt2img_0-preprocessor": "canny",
29-
"state-ext-control-net-txt2img_0-model": "control_v11p_sd15_canny [d14c016b]",
30-
"state-ext-control-net-txt2img_3-preprocessor": "openpose_full",
31-
"state-ext-control-net-txt2img_3-model": "control_v11p_sd15_openpose [cab727d4]"
32-
}
33-
34-
console.log(json_obj["state-ext-control-net-txt2img_0-enable"])
35-
console.log(json_obj["state-ext-control-net-txt2img_0-enabl1e"])
36-
37-
},
38-
39-
fix_old_json_error: function fix_old_json_error(json_obj){
40-
41-
cn_index = 1
42-
for (let cn_index=1; cn_index < 10; cn_index++){
43-
44-
}
45-
46-
return json_obj
10+
// button.click();
4711
},
4812

4913
target_is_newer_version: function(cur_version, target_version){

scripts/state_api.py

Lines changed: 95 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ def set_dropdowns():
188188
if(option == new_value):
189189
matching_successed = True
190190
break
191+
192+
# 去掉模型的多余路径?
193+
# if(os.path.split(option)[-1] == os.path.split(new_value)[-1]):
194+
# matching_successed = True
195+
# break
191196

192197
# 没有完全匹配
193198
if(not matching_successed):
@@ -358,7 +363,7 @@ def get_lora_from_prompt(prompt):
358363
#print(available_loras)
359364

360365
missing_loras = []
361-
re_lora_prompt = re.compile("<lora:([\w_\s.]+):([\d.]+)>", re.IGNORECASE)
366+
re_lora_prompt = re.compile("<lora:([\w_\s.]+):([ \d.]+)>", re.IGNORECASE)
362367
results = re.findall(re_lora_prompt, prompt)
363368
# print("使用到的lora:")
364369
# print(results)
@@ -494,6 +499,72 @@ def func_for_invisiblebutton():
494499
return Webui_Comps_Cur_Val[temp_index], next_index, Output_Log, Output_Log
495500

496501

502+
def config_filter(config):
503+
global extensions_id_conponents
504+
new_config = config
505+
for comp_type in extensions_id_conponents.keys():
506+
for comp_id in extensions_id_conponents[comp_type].keys():
507+
try:
508+
# 筛掉python相关组件的默认值选项
509+
default_val = extensions_id_conponents[comp_type][comp_id].get_config()["value"]
510+
if(default_val == new_config[comp_id]):
511+
del new_config[comp_id]
512+
except KeyError as e:
513+
pass
514+
515+
# 处理旧版插件保存的错误参数问题
516+
#print("-------处理错误-------")
517+
#print(new_config)
518+
print("-------处理错误-------")
519+
found_tabs = []
520+
fixed_config = {}
521+
for param in new_config.keys():
522+
res = re.match("state-ext-control-net-txt2img_([0-9]+)-(.+)",param)
523+
if(res != None):
524+
if(res.group(2) not in ["presets","preprocessor","model"]):
525+
try:
526+
found_tabs.index(res.group(1))
527+
except ValueError:
528+
found_tabs.append(res.group(1))
529+
530+
for param in new_config.keys():
531+
532+
# 缝缝补补。。。 js保存的参数因为是汉语反推回英文,所以就会有 model->模型->models 的情况。
533+
res = re.search("[-_](model|models|checkpoint|checkpoints)$",param)
534+
if(res != None):
535+
for key in ["model","models","checkpoint","checkpoints"]:
536+
target_word = str(res.group()).replace(res.group(1),key)
537+
new_param = re.sub("[-_](model|models|checkpoint|checkpoints)($|[_-])", target_word, param)
538+
fixed_config[new_param] = new_config[param]
539+
540+
# 纠正编号
541+
res = re.match("state-ext-control-net-txt2img_([0-9]+)-(.+)",param)
542+
if(res != None):
543+
if(res.group(1) != "0" and res.group(2) not in ["presets","preprocessor","model"]):
544+
tab_num = int(res.group(1))
545+
if(tab_num%3 == 0):
546+
try:
547+
found_tabs.index(str(tab_num/3))
548+
# 如果是9 如果发现3的位置有参数,就还需要检查一下1
549+
if(tab_num == 9):
550+
found_tabs.index("1")
551+
except ValueError:
552+
new_key = f"state-ext-control-net-txt2img_{int(tab_num/3)}-{res.group(2)}"
553+
fixed_config[new_key] = new_config[param]
554+
print(f" {param} 改为 {new_key}")
555+
continue
556+
557+
# 其余参数照搬
558+
fixed_config[param] = new_config[param]
559+
560+
#print("-------处理错误-------")
561+
#print(fixed_config)
562+
print("-------处理错误-------")
563+
564+
new_config = fixed_config
565+
return new_config
566+
567+
497568
def fn_import_workflow(workflow_file):
498569
global workflow_json, Output_Log
499570
global extensions_id_conponents, Webui_Comps_Cur_Val, temp_index, next_index
@@ -516,6 +587,8 @@ def fn_import_workflow(workflow_file):
516587
else:
517588
print("invalid file!")
518589

590+
workflow_json = config_filter(workflow_json)
591+
519592
Webui_Comps_Cur_Val = []
520593
for key in extensions_id_conponents["image"].keys():
521594
#for key in lf_config.Image_Components_Key:
@@ -561,8 +634,9 @@ def fn_import_workflow(workflow_file):
561634
# for short_hash in loras_info.keys():
562635
# if(loras_info[short_hash].name == lora_name or loras_info[short_hash].alias == lora_name):
563636
# pass
564-
except KeyError:
637+
except KeyError as e:
565638
pass
639+
#print(f"except missing loras error: {e}")
566640

567641
#print(Webui_Comps_Cur_Val)
568642
#set_elements()
@@ -632,18 +706,7 @@ def get_ext_list(self):
632706
return ext_str
633707

634708
def useless_config_filter(self, config:config_params):
635-
global extensions_id_conponents
636-
new_config = config.config_data
637-
for comp_type in extensions_id_conponents.keys():
638-
for comp_id in extensions_id_conponents[comp_type].keys():
639-
try:
640-
# 筛掉python相关组件的默认值选项
641-
default_val = extensions_id_conponents[comp_type][comp_id].get_config()["value"]
642-
if(default_val == new_config[comp_id]):
643-
del new_config[comp_id]
644-
except KeyError as e:
645-
pass
646-
return new_config
709+
return config_filter(config.config_data)
647710

648711
def get_lightdiffusionflow_config(self, onlyimg:bool = False):
649712
global workflow_json, extensions_id_conponents, extensions_id_conponents_value
@@ -884,7 +947,7 @@ def after_component(self, component, **kwargs):
884947
get_script_container(component)
885948

886949
if (isinstance(component, gr.Button) and kwargs["elem_id"] == "img2img_generation_info_button"): # 加载到最后一个组件了。 兼容旧版,暂时不使用“img2img_preview_filename”
887-
950+
888951
searching_extensions_title()
889952
#print(extensions_conponents)
890953

@@ -929,7 +992,7 @@ def after_component(self, component, **kwargs):
929992
except KeyError as e:
930993
pass
931994

932-
#print(extensions_conponents) # 整理好的第三方插件用到的组件
995+
# print(extensions_conponents) # 整理好的第三方插件用到的组件
933996
# --------------------------------------组件分类--------------------------------------------------
934997

935998
if(self.is_img2img):
@@ -1065,11 +1128,26 @@ def custom_ui(self):
10651128
invisible_buttons[elem_id] = gr.Button(value=elem_id, elem_id=elem_id, visible=False)
10661129

10671130

1131+
1132+
def on_after_component(component, **kwargs):
1133+
global Webui_Comps
1134+
1135+
# img2img和txt2img面板以外的组件信息只能在这里获取
1136+
# 如果希望python里控制这些组件,还需要把部分代码移到这里实现
1137+
try:
1138+
if(Webui_Comps.get(kwargs["elem_id"], None) == None):
1139+
Webui_Comps[kwargs["elem_id"]] = component
1140+
#if( kwargs["elem_id"] == "setting_sd_model_checkpoint"):
1141+
# print("--------------setting_sd_model_checkpoint---------------")
1142+
except KeyError:
1143+
pass
1144+
1145+
10681146
def on_before_reload():
10691147
lightdiffusionflow_config.init()
10701148

10711149
# add callbacks
10721150
api = StateApi()
10731151
script_callbacks.on_app_started(api.start)
1074-
#script_callbacks.on_after_component(on_after_component)
1152+
script_callbacks.on_after_component(on_after_component)
10751153
script_callbacks.on_before_reload(on_before_reload)

0 commit comments

Comments
 (0)