File size: 3,226 Bytes
bf7dd8f
 
 
 
a3a6aee
 
 
bf7dd8f
 
 
55c80a3
 
bf7dd8f
 
a3a6aee
bf7dd8f
a3a6aee
bf7dd8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
import datetime
import os

print(f"pip3 uninstall gradio")
results = os.popen(f"pip3 uninstall gradio -y")
print(results.readlines())
print(f"pip3 uninstall scepter")
results = os.popen(f"pip3 uninstall scepter -y")
print(results.readlines())
results = os.popen(f"pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple")
print(results.readlines())
results = os.popen(f"pip3 install scepter -i https://pypi.tuna.tsinghua.edu.cn/simple")
print(results.readlines())
print("finish install")

import random
import gradio as gr

import scepter
from scepter.modules.utils.config import Config
from scepter.modules.utils.file_system import FS
from scepter.modules.utils.logger import get_logger, init_logger


def prepare(config):
    if 'FILE_SYSTEM' in config:
        for fs_info in config['FILE_SYSTEM']:
            FS.init_fs_client(fs_info)

    if 'LOG_FILE' in config:
        logger = get_logger()
        tid = '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now()) + ''.join(
            [str(random.randint(1, 10)) for i in range(3)])
        init_logger(logger, log_file=config['LOG_FILE'].format(tid))

class TabManager():
    def __init__(self):
        pass

config_file = os.path.join(os.path.dirname(scepter.dirname), "scepter/methods/studio/scepter_ui.yaml")
config = Config(load=True, cfg_file=config_file)

for v in config.FILE_SYSTEM:
    v.TEMP_DIR = os.path.join("/home", v.TEMP_DIR)
    os.makedirs(v.TEMP_DIR, exist_ok=True)

prepare(config)

language = "en"
debug = False


tab_manager = TabManager()
interfaces = []
for info in config['INTERFACE']:
    name = info.get('NAME_EN',
                    '') if language == 'en' else info['NAME']
    ifid = info['IFID']
    if not FS.exists(info['CONFIG']):
        info['CONFIG'] = os.path.join(os.path.dirname(scepter.dirname), info['CONFIG'])
    if not FS.exists(info['CONFIG']):
        raise f"{info['CONFIG']} doesn't exist."
    interface = None
    if ifid == 'home':
        from scepter.studio.home.home import HomeUI
        interface = HomeUI(info['CONFIG'],
            is_debug=debug,
            language=language,
            root_work_dir=config.WORK_DIR) 
    if ifid == 'inference':
        from scepter.studio.inference.inference import InferenceUI
        interface = InferenceUI(
            info['CONFIG'],
            is_debug=debug,
            language=language,
            root_work_dir=config.WORK_DIR)
    if ifid == '':
        pass  # TODO: Add New Features
    if interface:
        interfaces.append((interface, name, ifid))
        setattr(tab_manager, ifid, interface)

with gr.Blocks() as demo:
    if 'BANNER' in config:
        gr.HTML(config.BANNER)
    else:
        gr.Markdown(
            f"<h2><center>{config.get('TITLE', 'SCEPTER Studio')}</center></h2>"
        )
    with gr.Tabs(elem_id='tabs') as tabs:
        for interface, label, ifid in interfaces:
            with gr.TabItem(label, id=ifid, elem_id=f'tab_{ifid}'):
                interface.create_ui()
        for interface, label, ifid in interfaces:
            interface.set_callbacks(tab_manager)

demo.queue(status_update_rate=1, api_open=False).launch(share=False, show_error=True, enable_queue=True)