Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,107 +1,103 @@
|
|
1 |
-
import os
|
2 |
-
import logging
|
3 |
-
import uuid
|
4 |
-
import shutil
|
5 |
-
from waifuc_gui.source_manager import SourceManager
|
6 |
-
from waifuc_gui.action_manager import ActionManager
|
7 |
-
from waifuc_gui.exporter_manager import ExporterManager
|
8 |
-
from waifuc_gui.file_handler import FileHandler
|
9 |
-
from waifuc_gui.interface import Interface
|
10 |
-
from waifuc_gui.config_manager import ConfigManager
|
11 |
-
import gradio as gr
|
12 |
-
|
13 |
-
logging.basicConfig(level=logging.INFO)
|
14 |
-
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)
|
65 |
-
source_manager = SourceManager(config_manager)
|
66 |
-
action_manager = ActionManager(config_manager)
|
67 |
-
exporter_manager = ExporterManager(config_manager)
|
68 |
-
file_handler = FileHandler()
|
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 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
ui["demo"].launch()
|
105 |
-
|
106 |
-
if __name__ == "__main__":
|
107 |
-
main()
|
|
|
1 |
+
import os
|
2 |
+
import logging
|
3 |
+
import uuid
|
4 |
+
import shutil
|
5 |
+
from waifuc_gui.source_manager import SourceManager
|
6 |
+
from waifuc_gui.action_manager import ActionManager
|
7 |
+
from waifuc_gui.exporter_manager import ExporterManager
|
8 |
+
from waifuc_gui.file_handler import FileHandler
|
9 |
+
from waifuc_gui.interface import Interface
|
10 |
+
from waifuc_gui.config_manager import ConfigManager
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
logging.basicConfig(level=logging.INFO)
|
14 |
+
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)
|
65 |
+
source_manager = SourceManager(config_manager)
|
66 |
+
action_manager = ActionManager(config_manager)
|
67 |
+
exporter_manager = ExporterManager(config_manager)
|
68 |
+
file_handler = FileHandler()
|
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()
|
|
|
|
|
|
|
|