Spaces:
Runtime error
Runtime error
Update waifuc_gui/interface.py
Browse files- waifuc_gui/interface.py +92 -15
waifuc_gui/interface.py
CHANGED
@@ -93,6 +93,46 @@ class Interface:
|
|
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(
|
@@ -167,6 +207,8 @@ class Interface:
|
|
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 {
|
@@ -234,18 +276,53 @@ class Interface:
|
|
234 |
inputs=config_import_file,
|
235 |
outputs=config_status
|
236 |
)
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
components.append(param_input)
|
94 |
return components, action_params
|
95 |
|
96 |
+
def start_collection(
|
97 |
+
self, selected_source, params, selected_actions, action_params, dataset_name, selected_exporter,
|
98 |
+
source_manager, action_manager, exporter_manager, file_handler
|
99 |
+
):
|
100 |
+
output_dir = f"/tmp/user_{source_manager.config_manager.session_id}/{dataset_name}"
|
101 |
+
try:
|
102 |
+
output_dir = f"/tmp/user_{source_manager.config_manager.session_id}/{dataset_name}"
|
103 |
+
logger = logging.getLogger("waifuc_gui")
|
104 |
+
logger.info(f"Starting collection for {selected_source} with dataset {dataset_name}")
|
105 |
+
os.makedirs(output_dir, exist_ok=True)
|
106 |
+
source = source_manager.instantiate_source(selected_source, params, file_handler)
|
107 |
+
actions = action_manager.instantiate_actions(selected_actions, action_params)
|
108 |
+
exporter = exporter_manager.instantiate_exporter(selected_exporter, dataset_name)
|
109 |
+
|
110 |
+
logger.info(f"Attaching actions: {selected_actions}")
|
111 |
+
os.chdir(f"/tmp/user_{source_manager.config_manager.session_id}")
|
112 |
+
source.attach(*actions).export(exporter)
|
113 |
+
logger.info(f"Export completed, creating ZIP")
|
114 |
+
zip_path = file_handler.create_zip(dataset_name)
|
115 |
+
logger.info(f"Collection completed: {zip_path}")
|
116 |
+
return (
|
117 |
+
f"Data collection completed, output file: {dataset_name}.zip" if source_manager.config_manager.get_config("language") == "en" else
|
118 |
+
f"数据收集完成,输出文件:{dataset_name}.zip"
|
119 |
+
), zip_path, "\n".join(logger.handlers[0].log_stream)
|
120 |
+
except Exception as e:
|
121 |
+
logger.error(f"Collection failed: {str(e)}")
|
122 |
+
return (
|
123 |
+
f"Data collection failed: {str(e)}" if source_manager.config_manager.get_config("language") == "en" else
|
124 |
+
f"数据收集失败:{str(e)}"
|
125 |
+
), None, "\n".join(logger.handlers[0].log_stream)
|
126 |
+
|
127 |
+
def download_log(self, file_handler):
|
128 |
+
logger = logging.getLogger("waifuc_gui")
|
129 |
+
log_content = "\n".join(logger.handlers[0].log_stream)
|
130 |
+
return file_handler.save_log(log_content)
|
131 |
+
|
132 |
+
def cleanup_session(self, config_manager):
|
133 |
+
config_manager.cleanup()
|
134 |
+
return "Session cleaned" if config_manager.get_config("language") == "en" else "会话已清理"
|
135 |
+
|
136 |
def build(self):
|
137 |
with gr.Blocks(title=self.get_text("title")) as demo:
|
138 |
language_dropdown = gr.Dropdown(
|
|
|
207 |
log_output = gr.Textbox(label=self.get_text("view_logs"), interactive=False, lines=10)
|
208 |
log_download_btn = gr.Button(self.get_text("download_log"))
|
209 |
log_download_file = gr.File(label=self.get_text("download_log"))
|
210 |
+
|
211 |
+
# 事件绑定
|
212 |
def update_language(language):
|
213 |
self.config_manager.set_config("language", language)
|
214 |
return {
|
|
|
276 |
inputs=config_import_file,
|
277 |
outputs=config_status
|
278 |
)
|
279 |
+
start_btn.click(
|
280 |
+
fn=self.start_collection,
|
281 |
+
inputs=[
|
282 |
+
source_dropdown,
|
283 |
+
gr.State(lambda: self.collect_params(
|
284 |
+
source_dropdown.value,
|
285 |
+
*[p.value for p in param_components]
|
286 |
+
)),
|
287 |
+
action_checkboxes,
|
288 |
+
action_param_components,
|
289 |
+
dataset_name_input,
|
290 |
+
exporter_dropdown,
|
291 |
+
gr.State(value=self.source_manager),
|
292 |
+
gr.State(value=self.action_manager),
|
293 |
+
gr.State(value=self.exporter_manager),
|
294 |
+
gr.State(value=FileHandler())
|
295 |
+
],
|
296 |
+
outputs=[status, output_file, log_output],
|
297 |
+
api_name="start_collection"
|
298 |
+
)
|
299 |
+
log_download_btn.click(
|
300 |
+
fn=self.download_log,
|
301 |
+
inputs=[gr.State(value=FileHandler())],
|
302 |
+
outputs=log_download_file
|
303 |
+
)
|
304 |
+
cleanup_btn = gr.Button("Cleanup Session" if self.config_manager.get_config("language") == "en" else "清理会话")
|
305 |
+
cleanup_status = gr.Textbox(label="Cleanup Status" if self.config_manager.get_config("language") == "en" else "清理状态")
|
306 |
+
cleanup_btn.click(
|
307 |
+
fn=self.cleanup_session,
|
308 |
+
inputs=[gr.State(value=self.config_manager)],
|
309 |
+
outputs=cleanup_status
|
310 |
+
)
|
311 |
+
|
312 |
+
return {
|
313 |
+
"demo": demo,
|
314 |
+
"source_dropdown": source_dropdown,
|
315 |
+
"param_components": param_components,
|
316 |
+
"action_checkboxes": action_checkboxes,
|
317 |
+
"action_param_components": action_param_components,
|
318 |
+
"dataset_name_input": dataset_name_input,
|
319 |
+
"exporter_dropdown": exporter_dropdown,
|
320 |
+
"start_btn": start_btn,
|
321 |
+
"status": status,
|
322 |
+
"output_file": output_file,
|
323 |
+
"log_output": log_output,
|
324 |
+
"log_download_btn": log_download_btn,
|
325 |
+
"log_download_file": log_download_file,
|
326 |
+
"cleanup_btn": cleanup_btn,
|
327 |
+
"cleanup_status": cleanup_status
|
328 |
+
}
|