Spaces:
Runtime error
Runtime error
Update waifuc_gui/interface.py
Browse files- waifuc_gui/interface.py +251 -244
waifuc_gui/interface.py
CHANGED
@@ -1,244 +1,251 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from .source_manager import SourceManager
|
3 |
-
from .action_manager import ActionManager
|
4 |
-
from .exporter_manager import ExporterManager
|
5 |
-
from .config_manager import ConfigManager
|
6 |
-
|
7 |
-
class Interface:
|
8 |
-
def __init__(self, source_manager: SourceManager, action_manager: ActionManager, exporter_manager: ExporterManager, config_manager: ConfigManager):
|
9 |
-
self.source_manager = source_manager
|
10 |
-
self.action_manager = action_manager
|
11 |
-
self.exporter_manager = exporter_manager
|
12 |
-
self.config_manager = config_manager
|
13 |
-
self.params = source_manager.create_param_inputs()
|
14 |
-
self.param_components = [param_input for source_params in self.params.values() for param_input in source_params.values()]
|
15 |
-
self.language_dict = {
|
16 |
-
"zh": {
|
17 |
-
"title": "Waifuc 数据收集工具",
|
18 |
-
"data_collection": "数据收集",
|
19 |
-
"config": "配置",
|
20 |
-
"logs": "日志",
|
21 |
-
"select_source": "选择数据源",
|
22 |
-
"select_actions": "选择动作",
|
23 |
-
"dataset_name": "数据集名称",
|
24 |
-
"select_exporter": "选择导出器",
|
25 |
-
"start_collection": "开始收集",
|
26 |
-
"status": "状态",
|
27 |
-
"download_data": "下载收集的数据",
|
28 |
-
"config_management": "配置管理",
|
29 |
-
"pixiv_token": "Pixiv刷新令牌",
|
30 |
-
"output_dir": "输出目录",
|
31 |
-
"num_items": "默认图片数量",
|
32 |
-
"resize_size": "默认调整大小",
|
33 |
-
"language": "语言",
|
34 |
-
"save_config": "保存配置",
|
35 |
-
"export_config": "导出配置",
|
36 |
-
"import_config": "导入配置",
|
37 |
-
"config_status": "配置状态",
|
38 |
-
"view_logs": "查看日志",
|
39 |
-
"download_log": "下载日志"
|
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 |
-
label=self.get_text("
|
119 |
-
value=
|
120 |
-
info="
|
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 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
self.
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
"
|
174 |
-
"
|
175 |
-
"
|
176 |
-
"
|
177 |
-
"
|
178 |
-
"
|
179 |
-
"
|
180 |
-
"
|
181 |
-
"
|
182 |
-
"
|
183 |
-
"
|
184 |
-
"
|
185 |
-
"
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
self.
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
"
|
239 |
-
"
|
240 |
-
"
|
241 |
-
"
|
242 |
-
"
|
243 |
-
"
|
244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from .source_manager import SourceManager
|
3 |
+
from .action_manager import ActionManager
|
4 |
+
from .exporter_manager import ExporterManager
|
5 |
+
from .config_manager import ConfigManager
|
6 |
+
|
7 |
+
class Interface:
|
8 |
+
def __init__(self, source_manager: SourceManager, action_manager: ActionManager, exporter_manager: ExporterManager, config_manager: ConfigManager):
|
9 |
+
self.source_manager = source_manager
|
10 |
+
self.action_manager = action_manager
|
11 |
+
self.exporter_manager = exporter_manager
|
12 |
+
self.config_manager = config_manager
|
13 |
+
self.params = source_manager.create_param_inputs()
|
14 |
+
self.param_components = [param_input for source_params in self.params.values() for param_input in source_params.values()]
|
15 |
+
self.language_dict = {
|
16 |
+
"zh": {
|
17 |
+
"title": "Waifuc 数据收集工具",
|
18 |
+
"data_collection": "数据收集",
|
19 |
+
"config": "配置",
|
20 |
+
"logs": "日志",
|
21 |
+
"select_source": "选择数据源",
|
22 |
+
"select_actions": "选择动作",
|
23 |
+
"dataset_name": "数据集名称",
|
24 |
+
"select_exporter": "选择导出器",
|
25 |
+
"start_collection": "开始收集",
|
26 |
+
"status": "状态",
|
27 |
+
"download_data": "下载收集的数据",
|
28 |
+
"config_management": "配置管理",
|
29 |
+
"pixiv_token": "Pixiv刷新令牌",
|
30 |
+
"output_dir": "输出目录",
|
31 |
+
"num_items": "默认图片数量",
|
32 |
+
"resize_size": "默认调整大小",
|
33 |
+
"language": "语言",
|
34 |
+
"save_config": "保存配置",
|
35 |
+
"export_config": "导出配置",
|
36 |
+
"import_config": "导入配置",
|
37 |
+
"config_status": "配置状态",
|
38 |
+
"view_logs": "查看日志",
|
39 |
+
"download_log": "下载日志",
|
40 |
+
"local_source_info": "上传包含图片的zip文件用于LocalSource"
|
41 |
+
},
|
42 |
+
"en": {
|
43 |
+
"title": "Waifuc Data Collection Tool",
|
44 |
+
"data_collection": "Data Collection",
|
45 |
+
"config": "Configuration",
|
46 |
+
"logs": "Logs",
|
47 |
+
"select_source": "Select Data Source",
|
48 |
+
"select_actions": "Select Actions",
|
49 |
+
"dataset_name": "Dataset Name",
|
50 |
+
"select_exporter": "Select Exporter",
|
51 |
+
"start_collection": "Start Collection",
|
52 |
+
"status": "Status",
|
53 |
+
"download_data": "Download Collected Data",
|
54 |
+
"config_management": "Configuration Management",
|
55 |
+
"pixiv_token": "Pixiv Refresh Token",
|
56 |
+
"output_dir": "Output Directory",
|
57 |
+
"num_items": "Default Number of Items",
|
58 |
+
"resize_size": "Default Resize Size",
|
59 |
+
"language": "Language",
|
60 |
+
"save_config": "Save Configuration",
|
61 |
+
"export_config": "Export Configuration",
|
62 |
+
"import_config": "Import Configuration",
|
63 |
+
"config_status": "Configuration Status",
|
64 |
+
"view_logs": "View Logs",
|
65 |
+
"download_log": "Download Log",
|
66 |
+
"local_source_info": "Upload a ZIP file containing images for LocalSource"
|
67 |
+
}
|
68 |
+
}
|
69 |
+
|
70 |
+
def get_text(self, key):
|
71 |
+
return self.language_dict[self.config_manager.get_config("language")][key]
|
72 |
+
|
73 |
+
def update_params_visibility(self, selected_source):
|
74 |
+
updates = []
|
75 |
+
for source, source_params in self.params.items():
|
76 |
+
for param_name, param_input in source_params.items():
|
77 |
+
updates.append(param_input.update(visible=(source == selected_source)))
|
78 |
+
local_source_info_visible = selected_source == "LocalSource"
|
79 |
+
return updates + [gr.Markdown.update(visible=local_source_info_visible)]
|
80 |
+
|
81 |
+
def collect_params(self, selected_source, *param_values):
|
82 |
+
collected = {}
|
83 |
+
source_params = self.params.get(selected_source, {})
|
84 |
+
for param_name, param_value in zip(source_params.keys(), param_values):
|
85 |
+
collected[param_name] = param_value
|
86 |
+
return collected
|
87 |
+
|
88 |
+
def update_action_params(self, selected_actions):
|
89 |
+
action_params = self.action_manager.create_action_param_inputs(selected_actions)
|
90 |
+
components = []
|
91 |
+
for action, params in action_params.items():
|
92 |
+
for param_name, param_input in params.items():
|
93 |
+
components.append(param_input)
|
94 |
+
return components, action_params
|
95 |
+
|
96 |
+
def build(self):
|
97 |
+
with gr.Blocks(title=self.get_text("title")) as demo:
|
98 |
+
language_dropdown = gr.Dropdown(
|
99 |
+
choices=["zh", "en"],
|
100 |
+
label=self.get_text("language"),
|
101 |
+
value=self.config_manager.get_config("language")
|
102 |
+
)
|
103 |
+
with gr.Tab(self.get_text("data_collection")):
|
104 |
+
gr.Markdown("### " + self.get_text("data_collection"))
|
105 |
+
source_dropdown = gr.Dropdown(
|
106 |
+
choices=self.source_manager.source_names,
|
107 |
+
label=self.get_text("select_source"),
|
108 |
+
value=self.source_manager.source_names[0] if self.source_manager.source_names else None,
|
109 |
+
info="Choose a data source like DanbooruSource or LocalSource" if self.config_manager.get_config("language") == "en" else "选择数据源,如DanbooruSource或LocalSource"
|
110 |
+
)
|
111 |
+
local_source_info = gr.Markdown(
|
112 |
+
self.get_text("local_source_info"),
|
113 |
+
visible=False
|
114 |
+
)
|
115 |
+
param_components = self.param_components
|
116 |
+
action_checkboxes = gr.CheckboxGroup(
|
117 |
+
choices=self.action_manager.action_names,
|
118 |
+
label=self.get_text("select_actions"),
|
119 |
+
value=[],
|
120 |
+
info="Select actions to process images, e.g., NoMonochromeAction" if self.config_manager.get_config("language") == "en" else "选择处理图片的动作,例如NoMonochromeAction"
|
121 |
+
)
|
122 |
+
action_param_components = gr.State(value={})
|
123 |
+
action_param_inputs = []
|
124 |
+
dataset_name_input = gr.Textbox(
|
125 |
+
label=self.get_text("dataset_name"),
|
126 |
+
value="waifuc_dataset",
|
127 |
+
info="Name for the output dataset" if self.config_manager.get_config("language") == "en" else "输出数据集的名称"
|
128 |
+
)
|
129 |
+
exporter_dropdown = gr.Dropdown(
|
130 |
+
choices=self.exporter_manager.exporter_names,
|
131 |
+
label=self.get_text("select_exporter"),
|
132 |
+
value=self.exporter_manager.exporter_names[0] if self.exporter_manager.exporter_names else None,
|
133 |
+
info="Choose an exporter like TextualInversionExporter" if self.config_manager.get_config("language") == "en" else "选择导出器,如TextualInversionExporter"
|
134 |
+
)
|
135 |
+
start_btn = gr.Button(self.get_text("start_collection"))
|
136 |
+
status = gr.Textbox(label=self.get_text("status"), interactive=False)
|
137 |
+
output_file = gr.File(label=self.get_text("download_data"))
|
138 |
+
with gr.Tab(self.get_text("config")):
|
139 |
+
gr.Markdown("### " + self.get_text("config_management"))
|
140 |
+
pixiv_token_input = gr.Textbox(
|
141 |
+
label=self.get_text("pixiv_token"),
|
142 |
+
value=self.config_manager.get_config("pixiv_refresh_token"),
|
143 |
+
type="password",
|
144 |
+
info="Required for PixivSearchSource, stored in session only" if self.config_manager.get_config("language") == "en" else "PixivSearchSource所需,仅存储在会话中"
|
145 |
+
)
|
146 |
+
output_dir_input = gr.Textbox(
|
147 |
+
label=self.get_text("output_dir"),
|
148 |
+
value=self.config_manager.get_config("output_dir"),
|
149 |
+
info="Directory for output data, session-specific" if self.config_manager.get_config("language") == "en" else "输出数据的目录,会话专用"
|
150 |
+
)
|
151 |
+
num_items_input = gr.Number(
|
152 |
+
label=self.get_text("num_items"),
|
153 |
+
value=self.config_manager.get_config("default_num_items"),
|
154 |
+
info="Default number of images to collect" if self.config_manager.get_config("language") == "en" else "默认收集的图片数量"
|
155 |
+
)
|
156 |
+
resize_size_input = gr.Number(
|
157 |
+
label=self.get_text("resize_size"),
|
158 |
+
value=self.config_manager.get_config("default_resize_size"),
|
159 |
+
info="Default size for ResizeAction" if self.config_manager.get_config("language") == "en" else "ResizeAction的默认尺寸"
|
160 |
+
)
|
161 |
+
config_export_btn = gr.Button(self.get_text("export_config"))
|
162 |
+
config_import_file = gr.File(label=self.get_text("import_config"))
|
163 |
+
save_config_btn = gr.Button(self.get_text("save_config"))
|
164 |
+
config_status = gr.Textbox(label=self.get_text("config_status"), interactive=False)
|
165 |
+
with gr.Tab(self.get_text("logs")):
|
166 |
+
gr.Markdown("### " + self.get_text("view_logs"))
|
167 |
+
log_output = gr.Textbox(label=self.get_text("view_logs"), interactive=False, lines=10)
|
168 |
+
log_download_btn = gr.Button(self.get_text("download_log"))
|
169 |
+
log_download_file = gr.File(label=self.get_text("download_log"))
|
170 |
+
def update_language(language):
|
171 |
+
self.config_manager.set_config("language", language)
|
172 |
+
return {
|
173 |
+
"demo": demo.update(title=self.get_text("title")),
|
174 |
+
"language_dropdown": language_dropdown.update(label=self.get_text("language")),
|
175 |
+
"source_dropdown": source_dropdown.update(label=self.get_text("select_source")),
|
176 |
+
"action_checkboxes": action_checkboxes.update(label=self.get_text("select_actions")),
|
177 |
+
"dataset_name_input": dataset_name_input.update(label=self.get_text("dataset_name")),
|
178 |
+
"exporter_dropdown": exporter_dropdown.update(label=self.get_text("select_exporter")),
|
179 |
+
"start_btn": start_btn.update(value=self.get_text("start_collection")),
|
180 |
+
"status": status.update(label=self.get_text("status")),
|
181 |
+
"output_file": output_file.update(label=self.get_text("download_data")),
|
182 |
+
"pixiv_token_input": pixiv_token_input.update(label=self.get_text("pixiv_token")),
|
183 |
+
"output_dir_input": output_dir_input.update(label=self.get_text("output_dir")),
|
184 |
+
"num_items_input": num_items_input.update(label=self.get_text("num_items")),
|
185 |
+
"resize_size_input": resize_size_input.update(label=self.get_text("resize_size")),
|
186 |
+
"config_export_btn": config_export_btn.update(value=self.get_text("export_config")),
|
187 |
+
"config_import_file": config_import_file.update(label=self.get_text("import_config")),
|
188 |
+
"save_config_btn": save_config_btn.update(value=self.get_text("save_config")),
|
189 |
+
"config_status": config_status.update(label=self.get_text("config_status")),
|
190 |
+
"log_output": log_output.update(label=self.get_text("view_logs")),
|
191 |
+
"log_download_btn": log_download_btn.update(value=self.get_text("download_log")),
|
192 |
+
"log_download_file": log_download_file.update(label=self.get_text("download_log"))
|
193 |
+
}
|
194 |
+
language_dropdown.change(
|
195 |
+
fn=update_language,
|
196 |
+
inputs=language_dropdown,
|
197 |
+
outputs=[
|
198 |
+
demo, language_dropdown, source_dropdown, action_checkboxes,
|
199 |
+
dataset_name_input, exporter_dropdown, start_btn, status,
|
200 |
+
output_file, pixiv_token_input, output_dir_input, num_items_input,
|
201 |
+
resize_size_input, config_export_btn, config_import_file,
|
202 |
+
save_config_btn, config_status, log_output, log_download_btn,
|
203 |
+
log_download_file
|
204 |
+
]
|
205 |
+
)
|
206 |
+
source_dropdown.change(
|
207 |
+
fn=self.update_params_visibility,
|
208 |
+
inputs=source_dropdown,
|
209 |
+
outputs=param_components + [local_source_info]
|
210 |
+
)
|
211 |
+
action_checkboxes.change(
|
212 |
+
fn=self.update_action_params,
|
213 |
+
inputs=action_checkboxes,
|
214 |
+
outputs=[gr.State(), gr.State()]
|
215 |
+
)
|
216 |
+
def save_configs(pixiv_token, output_dir, num_items, resize_size):
|
217 |
+
self.config_manager.set_config("pixiv_refresh_token", pixiv_token)
|
218 |
+
self.config_manager.set_config("output_dir", output_dir)
|
219 |
+
self.config_manager.set_config("default_num_items", int(num_items))
|
220 |
+
self.config_manager.set_config("default_resize_size", int(resize_size))
|
221 |
+
return self.get_text("config_status") + ": " + ("Configuration saved" if self.config_manager.get_config("language") == "en" else "配置已保存")
|
222 |
+
save_config_btn.click(
|
223 |
+
fn=save_configs,
|
224 |
+
inputs=[pixiv_token_input, output_dir_input, num_items_input, resize_size_input],
|
225 |
+
outputs=config_status
|
226 |
+
)
|
227 |
+
config_export_btn.click(
|
228 |
+
fn=self.config_manager.export_config,
|
229 |
+
inputs=[],
|
230 |
+
outputs=gr.File(label=self.get_text("export_config"))
|
231 |
+
)
|
232 |
+
config_import_file.upload(
|
233 |
+
fn=self.config_manager.import_config,
|
234 |
+
inputs=config_import_file,
|
235 |
+
outputs=config_status
|
236 |
+
)
|
237 |
+
return {
|
238 |
+
"demo": demo,
|
239 |
+
"source_dropdown": source_dropdown,
|
240 |
+
"param_components": param_components,
|
241 |
+
"action_checkboxes": action_checkboxes,
|
242 |
+
"action_param_components": action_param_components,
|
243 |
+
"dataset_name_input": dataset_name_input,
|
244 |
+
"exporter_dropdown": exporter_dropdown,
|
245 |
+
"start_btn": start_btn,
|
246 |
+
"status": status,
|
247 |
+
"output_file": output_file,
|
248 |
+
"log_output": log_output,
|
249 |
+
"log_download_btn": log_download_btn,
|
250 |
+
"log_download_file": log_download_file
|
251 |
+
}
|