Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -15,50 +15,17 @@ logger = logging.getLogger("waifuc_gui")
|
|
15 |
log_stream = []
|
16 |
|
17 |
class LogHandler(logging.Handler):
|
|
|
|
|
|
|
|
|
18 |
def emit(self, record):
|
19 |
-
log_stream.append(self.format(record))
|
20 |
|
21 |
log_handler = LogHandler()
|
22 |
log_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
|
23 |
logger.addHandler(log_handler)
|
24 |
|
25 |
-
def start_collection(
|
26 |
-
selected_source, params, selected_actions, action_params, dataset_name, selected_exporter,
|
27 |
-
source_manager, action_manager, exporter_manager, file_handler
|
28 |
-
):
|
29 |
-
output_dir = f"/tmp/user_{source_manager.config_manager.session_id}/{dataset_name}"
|
30 |
-
try:
|
31 |
-
logger.info(f"Starting collection for {selected_source} with dataset {dataset_name}")
|
32 |
-
os.makedirs(output_dir, exist_ok=True)
|
33 |
-
source = source_manager.instantiate_source(selected_source, params, file_handler)
|
34 |
-
actions = action_manager.instantiate_actions(selected_actions, action_params)
|
35 |
-
exporter = exporter_manager.instantiate_exporter(selected_exporter, dataset_name)
|
36 |
-
|
37 |
-
logger.info(f"Attaching actions: {selected_actions}")
|
38 |
-
os.chdir(f"/tmp/user_{source_manager.config_manager.session_id}")
|
39 |
-
source.attach(*actions).export(exporter)
|
40 |
-
logger.info(f"Export completed, creating ZIP")
|
41 |
-
zip_path = file_handler.create_zip(dataset_name)
|
42 |
-
logger.info(f"Collection completed: {zip_path}")
|
43 |
-
return (
|
44 |
-
f"Data collection completed, output file: {dataset_name}.zip" if source_manager.config_manager.get_config("language") == "en" else
|
45 |
-
f"数据收集完成,输出文件:{dataset_name}.zip"
|
46 |
-
), zip_path, "\n".join(log_stream)
|
47 |
-
except Exception as e:
|
48 |
-
logger.error(f"Collection failed: {str(e)}")
|
49 |
-
return (
|
50 |
-
f"Data collection failed: {str(e)}" if source_manager.config_manager.get_config("language") == "en" else
|
51 |
-
f"数据收集失败:{str(e)}"
|
52 |
-
), None, "\n".join(log_stream)
|
53 |
-
|
54 |
-
def download_log(file_handler):
|
55 |
-
log_content = "\n".join(log_stream)
|
56 |
-
return file_handler.save_log(log_content)
|
57 |
-
|
58 |
-
def cleanup_session(config_manager):
|
59 |
-
config_manager.cleanup()
|
60 |
-
return "Session cleaned" if config_manager.get_config("language") == "en" else "会话已清理"
|
61 |
-
|
62 |
def main():
|
63 |
session_id = str(uuid.uuid4())
|
64 |
config_manager = ConfigManager(session_id)
|
@@ -69,37 +36,6 @@ def main():
|
|
69 |
interface = Interface(source_manager, action_manager, exporter_manager, config_manager)
|
70 |
|
71 |
ui = interface.build()
|
72 |
-
ui["start_btn"].click(
|
73 |
-
fn=start_collection,
|
74 |
-
inputs=[
|
75 |
-
ui["source_dropdown"],
|
76 |
-
gr.State(lambda: interface.collect_params(
|
77 |
-
ui["source_dropdown"].value,
|
78 |
-
*[p.value for p in ui["param_components"]]
|
79 |
-
)),
|
80 |
-
ui["action_checkboxes"],
|
81 |
-
ui["action_param_components"],
|
82 |
-
ui["dataset_name_input"],
|
83 |
-
ui["exporter_dropdown"]
|
84 |
-
],
|
85 |
-
outputs=[ui["status"], ui["output_file"], ui["log_output"]],
|
86 |
-
api_name="start_collection"
|
87 |
-
)
|
88 |
-
|
89 |
-
ui["log_download_btn"].click(
|
90 |
-
fn=download_log,
|
91 |
-
inputs=[gr.State(value=file_handler)],
|
92 |
-
outputs=ui["log_download_file"]
|
93 |
-
)
|
94 |
-
|
95 |
-
cleanup_btn = gr.Button("Cleanup Session" if config_manager.get_config("language") == "en" else "清理会话")
|
96 |
-
cleanup_status = gr.Textbox(label="Cleanup Status" if config_manager.get_config("language") == "en" else "清理状态")
|
97 |
-
cleanup_btn.click(
|
98 |
-
fn=cleanup_session,
|
99 |
-
inputs=[gr.State(value=config_manager)],
|
100 |
-
outputs=cleanup_status
|
101 |
-
)
|
102 |
-
|
103 |
ui["demo"].launch()
|
104 |
|
105 |
if __name__ == "__main__":
|
|
|
15 |
log_stream = []
|
16 |
|
17 |
class LogHandler(logging.Handler):
|
18 |
+
def __init__(self):
|
19 |
+
super().__init__()
|
20 |
+
self.log_stream = log_stream
|
21 |
+
|
22 |
def emit(self, record):
|
23 |
+
self.log_stream.append(self.format(record))
|
24 |
|
25 |
log_handler = LogHandler()
|
26 |
log_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
|
27 |
logger.addHandler(log_handler)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
def main():
|
30 |
session_id = str(uuid.uuid4())
|
31 |
config_manager = ConfigManager(session_id)
|
|
|
36 |
interface = Interface(source_manager, action_manager, exporter_manager, config_manager)
|
37 |
|
38 |
ui = interface.build()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
ui["demo"].launch()
|
40 |
|
41 |
if __name__ == "__main__":
|