|
20 | 20 | from modules.generation_parameters_copypaste import paste_fields, registered_param_bindings, parse_generation_parameters |
21 | 21 | from modules.sd_models import checkpoints_list |
22 | 22 | from modules import ui_components |
| 23 | +from modules.paths_internal import data_path |
23 | 24 | import launch |
24 | 25 |
|
25 | 26 | from scripts import lightdiffusionflow_version, lightdiffusionflow_config |
|
28 | 29 |
|
29 | 30 | # current_path = os.path.abspath(os.path.dirname(__file__)) |
30 | 31 | # sys.path.append(os.path.join(current_path,"lib")) |
31 | | - |
| 32 | +api = None |
32 | 33 | workflow_json = {} |
33 | 34 | State_Comps = {} # 当前页面的按钮组件 |
34 | 35 | invisible_buttons = {} |
|
44 | 45 | txt2img_script_container = None |
45 | 46 | img2img_script_container = None |
46 | 47 |
|
47 | | -local_flows_path = "lightdiffusionflow" |
| 48 | +local_flows_path = "models/LightDiffusionFlow" |
| 49 | +flows_path = os.path.join(data_path, local_flows_path) |
| 50 | +flows_path = flows_path.replace("/","\\") |
| 51 | +if(not os.path.isdir(flows_path)): |
| 52 | + os.mkdir(flows_path) |
| 53 | + |
| 54 | +local_flow_list = [] |
48 | 55 | Need_Preload = False |
49 | 56 | Preload_File = r"" |
50 | 57 | File_extension = ".flow" |
|
54 | 61 | save_style_symbol = '\U0001f4be' # 💾 |
55 | 62 | apply_style_symbol = '\U0001f4cb' # 📋 |
56 | 63 |
|
| 64 | + |
57 | 65 | def test_func(): |
58 | 66 | gr.Warning("hello") |
59 | 67 | # global extensions_conponents, extensions_id_conponents |
@@ -97,16 +105,20 @@ def test_func(): |
97 | 105 | def custom_msg_box(): |
98 | 106 | global g_msg_info |
99 | 107 | if(g_msg_info != ""): |
| 108 | + print(f"gr.Info({g_msg_info})") |
100 | 109 | gr.Info(g_msg_info) |
101 | | - g_msg_info = "" |
| 110 | + |
| 111 | +def clear_markup(html_str): |
| 112 | + clearly_str = html_str |
| 113 | + results = re.findall("(<.+?>)",clearly_str) |
| 114 | + for res in results: |
| 115 | + clearly_str = clearly_str.replace(res,"") |
| 116 | + return clearly_str |
102 | 117 |
|
103 | 118 | def add_output_log(msg:str="", style:str=""): |
104 | 119 | global Output_Log |
105 | 120 | if(msg != ""): |
106 | | - clear_msg = msg |
107 | | - results = re.findall("(<.+?>)",msg) |
108 | | - for res in results: |
109 | | - clear_msg = clear_msg.replace(res,"") |
| 121 | + clear_msg = clear_markup(msg) |
110 | 122 | print(clear_msg) |
111 | 123 | Output_Log += f"<p style='color:rgb(192,192,192);{style}'>{msg}</p>" |
112 | 124 |
|
@@ -202,6 +214,37 @@ def SearchingCheckPointByHashFromCivitai(hash:str): |
202 | 214 | pass |
203 | 215 | return {} |
204 | 216 |
|
| 217 | +def refresh_local_flows(*inputs): |
| 218 | + print("refresh_local_flows") |
| 219 | + global local_flow_list,local_flows_path |
| 220 | + flows_path = os.path.join(data_path, local_flows_path) |
| 221 | + local_flow_list = [f for f in os.listdir(flows_path) if os.path.isfile( |
| 222 | + os.path.join(flows_path, f)) and os.path.splitext(f)[-1] == '.flow'] |
| 223 | + print(inputs) |
| 224 | + print(local_flow_list) |
| 225 | + ret = [] |
| 226 | + for dd in inputs: |
| 227 | + if dd in local_flow_list: |
| 228 | + selected = dd |
| 229 | + else: |
| 230 | + selected = None |
| 231 | + ret.append(gr.Dropdown.update(choices=local_flow_list, value=selected)) |
| 232 | + #ret = [gr.Dropdown.update(choices=local_flow_list, value=selected) for i in inputs] |
| 233 | + return ret |
| 234 | + |
| 235 | +def apply_local_flow(selected): |
| 236 | + global local_flow_list,local_flows_path |
| 237 | + global Need_Preload,Preload_File |
| 238 | + |
| 239 | + if(selected != "" and selected != None): |
| 240 | + flow_path = os.path.join(data_path, local_flows_path, selected) |
| 241 | + if(os.path.exists(flow_path)): |
| 242 | + print("OK,Local File!") |
| 243 | + print(flow_path) |
| 244 | + Preload_File = flow_path |
| 245 | + Need_Preload = True |
| 246 | + gr.Info(clear_markup(OutputPrompt.startimport())) |
| 247 | + |
205 | 248 | def set_lightdiffusionflow_file(): |
206 | 249 | global Preload_File |
207 | 250 | return Preload_File |
@@ -784,6 +827,9 @@ class png_info_params(BaseModel): |
784 | 827 | class file_params(BaseModel): |
785 | 828 | file_path:str |
786 | 829 |
|
| 830 | +class savefile_params(BaseModel): |
| 831 | + file_name:str |
| 832 | + file_data:dict |
787 | 833 |
|
788 | 834 | class StateApi(): |
789 | 835 |
|
@@ -819,7 +865,9 @@ def start(self, _: gr.Blocks, app: FastAPI): |
819 | 865 | # 传入一个文件路径,返回文件内容 |
820 | 866 | self.add_api_route('/local/read_file', self.read_file, methods=['POST']) |
821 | 867 | self.add_api_route('/local/need_preload', self.need_preload, methods=['GET']) |
822 | | - |
| 868 | + # 保存当前配置到本地文件夹 |
| 869 | + self.add_api_route('/local/save_flow_to_local', self.saveFlowToLocal, methods=['POST']) |
| 870 | + |
823 | 871 | self.add_api_route('/set_preload', self.set_preload, methods=['POST']) |
824 | 872 |
|
825 | 873 | def get_config(self): |
@@ -1011,6 +1059,41 @@ def need_preload(self): |
1011 | 1059 | return Preload_File |
1012 | 1060 | return "" |
1013 | 1061 |
|
| 1062 | + def saveFlowToLocal(self, data_to_save:savefile_params): |
| 1063 | + global local_flows_path |
| 1064 | + global add_output_log |
| 1065 | + |
| 1066 | + overall_data = {} |
| 1067 | + print(data_to_save.file_name) |
| 1068 | + print(data_to_save.file_data) |
| 1069 | + filedata = data_to_save.file_data |
| 1070 | + |
| 1071 | + # python处理的图片和dropdown信息 |
| 1072 | + overall_data = json.loads(self.get_lightdiffusionflow_config(True)) |
| 1073 | + for key in filedata.keys(): |
| 1074 | + if(filedata[key] != ""): |
| 1075 | + overall_data[key] = filedata[key] |
| 1076 | + |
| 1077 | + # Lora信息 |
| 1078 | + params = config_params(config_data=overall_data) |
| 1079 | + lora_info = self.parse_lora_info(params) |
| 1080 | + for key in lora_info.keys(): |
| 1081 | + overall_data[key] = lora_info[key] |
| 1082 | + |
| 1083 | + # 过滤掉一些没用的默认值的信息 |
| 1084 | + overall_data = config_filter(overall_data) |
| 1085 | + |
| 1086 | + flow_path = os.path.join(data_path, local_flows_path, data_to_save.file_name) |
| 1087 | + print(flow_path) |
| 1088 | + if(not os.path.exists(flow_path)): |
| 1089 | + with open(flow_path,"w") as f: |
| 1090 | + #json.dump(overall_data,f) |
| 1091 | + f.write(json.dumps(overall_data, ensure_ascii=False, indent=4)) |
| 1092 | + add_output_log(OutputPrompt.save_completed()) |
| 1093 | + else: |
| 1094 | + add_output_log(OutputPrompt.save_failed()) |
| 1095 | + |
| 1096 | + return flow_path |
1014 | 1097 |
|
1015 | 1098 | class Script(scripts.Script): |
1016 | 1099 |
|
@@ -1147,6 +1230,13 @@ def after_component(self, component, **kwargs): |
1147 | 1230 | target_comps.append(State_Comps["json2js"]) # 触发事件传递json给js |
1148 | 1231 | #target_comps.append(State_Comps["outlog"][0]) |
1149 | 1232 | #target_comps.append(State_Comps["outlog"][1]) # 因为显示日志的窗口分txt2img和img2img两个位置 所以两个位置同步导出 |
| 1233 | + |
| 1234 | + for i in range(len(State_Comps["local_flows"])): |
| 1235 | + #State_Comps["local_flows"] |
| 1236 | + State_Comps["refresh"][i].click(refresh_local_flows, inputs=State_Comps["local_flows"],outputs=State_Comps["local_flows"]) |
| 1237 | + State_Comps["apply"][i].click(apply_local_flow, inputs=[State_Comps["local_flows"][i]],outputs=[]) |
| 1238 | + State_Comps["save"][i].click(fn=None,_js="state.core.actions.saveFlowToLocal", inputs=[],outputs=[]) |
| 1239 | + |
1150 | 1240 |
|
1151 | 1241 | for btn in State_Comps["export"]: |
1152 | 1242 | btn.click(None,_js="state.core.actions.exportState") #, inputs=[],outputs=[] |
@@ -1236,10 +1326,15 @@ def custom_ui(self): |
1236 | 1326 | if(Flow_Save_mode == "Core"): |
1237 | 1327 | save_mode = " (only ControlNet)" |
1238 | 1328 |
|
| 1329 | + global local_flow_list,local_flows_path |
| 1330 | + flows_path = os.path.join(data_path, local_flows_path) |
| 1331 | + local_flow_list = [f for f in os.listdir(flows_path) if os.path.isfile( |
| 1332 | + os.path.join(flows_path, f)) and os.path.splitext(f)[-1] == '.flow'] |
| 1333 | + |
1239 | 1334 | with gr.Accordion('LightDiffusionFlow '+lightdiffusionflow_version.lightdiffusionflow_version + save_mode, open=True, visible=True, elem_id=cur_mode+'_lightdiffusionflow'): |
1240 | 1335 |
|
1241 | 1336 | with gr.Row(): |
1242 | | - State_Comps["local_flows"].append(gr.Dropdown(label="", show_label=False ,value='',elem_id=cur_mode+'_ldf_local_flows')) |
| 1337 | + State_Comps["local_flows"].append(gr.Dropdown(label="", show_label=False ,choices=local_flow_list,value='',elem_id=cur_mode+'_ldf_local_flows')) |
1243 | 1338 | State_Comps["apply"].append(ui_components.ToolButton(value=paste_symbol,elem_id=cur_mode+'_ldf_apply')) |
1244 | 1339 | State_Comps["save"].append(ui_components.ToolButton(value=save_style_symbol,elem_id=cur_mode+'_ldf_save')) |
1245 | 1340 | State_Comps["refresh"].append(ui_components.ToolButton(value=refresh_symbol,elem_id=cur_mode+'_ldf_refresh')) |
|
0 commit comments