File size: 4,956 Bytes
e368cec
 
 
 
944dd2b
e368cec
 
 
 
91d0d23
e368cec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b740cd
e368cec
 
 
 
 
 
 
 
 
 
 
 
 
7b740cd
e368cec
 
 
 
 
944dd2b
 
 
 
 
 
 
 
7b740cd
944dd2b
 
 
 
3efdac8
 
944dd2b
e368cec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
944dd2b
 
e368cec
 
 
 
 
 
 
944dd2b
 
e368cec
 
 
 
 
 
1b1beb7
e368cec
 
c45a043
 
e368cec
 
 
a4a785c
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
99
100
101
102
103
104
105
import gradio as gr
import os
from serve.gradio_web import *
from serve.gradio_web_image_editing import *
from serve.gradio_web_video_generation import *
from serve.leaderboard import build_leaderboard_tab
from model.model_manager import ModelManager
from pathlib import Path
from serve.constants import SERVER_PORT, ROOT_PATH, ELO_RESULTS_DIR

def build_combine_demo(models, elo_results_file, leaderboard_table_file):

    with gr.Blocks(
        title="Play with Open Vision Models",
        theme=gr.themes.Default(),
        css=block_css,
    ) as demo:
        with gr.Tabs() as tabs_combine:
            with gr.Tab("Image Generation", id=0):
                with gr.Tabs() as tabs_ig:
                    with gr.Tab("Generation Arena (battle)", id=0):
                        build_side_by_side_ui_anony(models)

                    with gr.Tab("Generation Arena (side-by-side)", id=1):
                        build_side_by_side_ui_named(models)

                    with gr.Tab("Generation Playground", id=2): #Direct Chat
                        build_single_model_ui(models, add_promotion_links=True)
                    if elo_results_file:
                        with gr.Tab("Generation Leaderboard", id=3):
                            build_leaderboard_tab(elo_results_file['t2i_generation'], leaderboard_table_file['t2i_generation'])
            
            with gr.Tab("Image Edition", id=5):
                with gr.Tabs() as tabs_ie:
                    with gr.Tab("Edition Arena (battle)", id=5):
                        build_side_by_side_ui_anony_ie(models)

                    with gr.Tab("Edition Arena (side-by-side)", id=6):
                        build_side_by_side_ui_named_ie(models)

                    with gr.Tab("Edition Playground", id=7): #Direct Chat
                        build_single_model_ui_ie(models, add_promotion_links=True)
                    if elo_results_file:
                        with gr.Tab("Edition Leaderboard", id=8):
                            build_leaderboard_tab(elo_results_file['image_editing'], leaderboard_table_file['image_editing'])

            with gr.Tab("Video Generation", id=10):
                with gr.Tabs() as tabs_vg:
                    with gr.Tab("Video Generation Arena (battle)", id=10):
                        build_side_by_side_ui_anony_vg(models)

                    with gr.Tab("Video Generation Arena (side-by-side)", id=11):
                        build_side_by_side_ui_named_vg(models)

                    with gr.Tab("Video Generation Playground", id=12): #Direct Chat
                        build_single_model_ui_vg(models, add_promotion_links=True)
                    if elo_results_file and 'video_generation' in elo_results_file:
                        with gr.Tab("Video Generation Leaderboard", id=13):
                            build_leaderboard_tab(elo_results_file['video_generation'], leaderboard_table_file['video_generation'])
            with gr.Tab("About Us", id=4): 
                build_about()

    return demo


def load_elo_results(elo_results_dir):
    from collections import defaultdict
    elo_results_file = defaultdict(lambda: None)
    leaderboard_table_file = defaultdict(lambda: None)
    if elo_results_dir is not None:
        elo_results_dir = Path(elo_results_dir)
        elo_results_file = {}
        leaderboard_table_file = {}
        for file in elo_results_dir.glob('elo_results_*.pkl'):
            if 't2i_generation' in file.name:
                elo_results_file['t2i_generation'] = file
            elif 'image_editing' in file.name:
                elo_results_file['image_editing'] = file
            elif 'video_generation' in file.name:
                elo_results_file['video_generation'] = file
            else:
                raise ValueError(f"Unknown file name: {file.name}")
        for file in elo_results_dir.glob('*_leaderboard.csv'):
            if 't2i_generation' in file.name:
                leaderboard_table_file['t2i_generation'] = file
            elif 'image_editing' in file.name:
                leaderboard_table_file['image_editing'] = file
            elif 'video_generation' in file.name:
                leaderboard_table_file['video_generation'] = file
            else:
                raise ValueError(f"Unknown file name: {file.name}")
            
    return elo_results_file, leaderboard_table_file
    
if __name__ == "__main__":
    server_port = int(SERVER_PORT)
    root_path = ROOT_PATH
    elo_results_dir = ELO_RESULTS_DIR
    models = ModelManager(enable_nsfw=False, do_pre_download=True, do_debug_packages=True)
    # models = ModelManager(enable_nsfw=False, do_pre_download=False, do_debug_packages=False)
    
    elo_results_file, leaderboard_table_file = load_elo_results(elo_results_dir)
    demo = build_combine_demo(models, elo_results_file, leaderboard_table_file)
    demo.queue(max_size=20).launch(server_port=server_port, root_path=ROOT_PATH)