File size: 3,167 Bytes
8918ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
import json
import time
import gradio as gr
from web.utils.monitor import TrainingMonitor
from web.train_tab import create_train_tab
from web.eval_tab import create_eval_tab
from web.download_tab import create_download_tab
from web.predict_tab import create_predict_tab
from web.manual_tab import create_manual_tab

def load_constant():
    """Load constant values from config files"""
    try:
        return json.load(open("src/constant.json"))
    except Exception as e:
        return {"error": f"Failed to load constant.json: {str(e)}"}

def create_ui():
    monitor = TrainingMonitor()
    constant = load_constant()
    
    def update_output():
        try:
            if monitor.is_training:
                messages = monitor.get_messages()
                loss_plot = monitor.get_loss_plot()
                metrics_plot = monitor.get_metrics_plot()
                return messages, loss_plot, metrics_plot
            else:
                if monitor.error_message:
                    return f"Training stopped with error:\n{monitor.error_message}", None, None
                return "Click Start to begin training!", None, None
        except Exception as e:
            return f"Error in UI update: {str(e)}", None, None
    
    with gr.Blocks() as demo:
        gr.Markdown("# VenusFactory")
        
        # Create tabs
        with gr.Tabs():
            try:
                train_components = {"output_text": None, "loss_plot": None, "metrics_plot": None}
                train_tab = create_train_tab(constant)
                if train_components["output_text"] is not None and train_components["loss_plot"] is not None and train_components["metrics_plot"] is not None:
                    train_components["output_text"] = train_tab["output_text"]
                    train_components["loss_plot"] = train_tab["loss_plot"]
                    train_components["metrics_plot"] = train_tab["metrics_plot"]
                eval_components = create_eval_tab(constant)
                predict_components = create_predict_tab(constant)
                download_components = create_download_tab(constant)
                manual_components = create_manual_tab(constant)
            except Exception as e:
                gr.Markdown(f"Error creating UI components: {str(e)}")
                train_components = {"output_text": None, "loss_plot": None, "metrics_plot": None}
        
        if train_components["output_text"] is not None and train_components["loss_plot"] is not None and train_components["metrics_plot"] is not None:
            demo.load(
                fn=update_output,
                inputs=None,
                outputs=[
                    train_components["output_text"], 
                    train_components["loss_plot"],
                    train_components["metrics_plot"]
                ]
            )
        
    return demo

if __name__ == "__main__":
    try:
        demo = create_ui()
        demo.launch(server_name="0.0.0.0", share=True, allowed_paths=["img"])
    except Exception as e:
        print(f"Failed to launch UI: {str(e)}")