File size: 6,618 Bytes
b7c7aa0
9176c50
9c55a60
790c71e
 
 
0970521
 
 
 
158d544
 
11ebb9a
 
 
 
 
790c71e
 
 
 
 
 
 
 
 
0970521
790c71e
158d544
caba228
790c71e
 
 
 
2be6031
 
 
d5fd67e
 
 
b479e99
 
 
0ae46c9
d5fd67e
 
2be6031
d5fd67e
2be6031
 
d5fd67e
2be6031
 
7033a80
 
 
790c71e
 
 
8697a9a
 
5df185e
790c71e
 
 
 
0507cca
 
790c71e
 
5df185e
 
 
 
790c71e
 
 
 
 
 
 
 
 
caba228
158d544
 
caba228
 
790c71e
 
 
 
 
b7c7aa0
 
790c71e
 
 
 
 
 
 
 
 
 
 
0970521
 
790c71e
 
 
0507cca
b7c7aa0
790c71e
 
 
50e7d4a
790c71e
 
 
b7c7aa0
666edb3
2be6031
 
 
 
 
 
fab0fb7
790c71e
 
 
 
 
 
caba228
 
50e7d4a
790c71e
 
 
2be6031
 
790c71e
 
 
 
caba228
790c71e
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import gradio as gr
from huggingface.user import HFUser, GR_CONF
import os

Theme = gr.Theme.load(GR_CONF["theme"])
GR_CONF["theme"] = Theme
dataset_options = [
    "OpenVideo/pexels-raw",
    "OpenVideo/Sample-2k",
]
fetch_files = []
fetch_files_select_idx = 0
custom_css = """
    .class-for-small-font label {
        font-size: 12px; /* Adjust the font size as needed */
    }
"""

def login(token):
    u = HFUser.from_token(token)
    return u, u.name, gr.Column(visible=False)

def show_time(u, name):
    return u.ping(name), gr.Column(visible=True)

def list_dataset(u, repo):
    print(f"repo is: {repo}")
    files = u.list_dataset(repo)
    global fetch_files
    fetch_files = files
    return gr.Dropdown(value=files[0], choices=files), gr.Column(visible=True), gr.Column(visible=False)

def fetch_parquet(u, fname):
    _cache = u.fetch_file(fname)
    auto_tag_content_1 = ""
    auto_tag_content_2 = ""
    auto_tag_content_3 = ""
    if fname.endswith(".mp4"):
        directory_name = os.path.dirname(fname)
        file_name = os.path.basename(fname)
        file_name_without_extension = os.path.splitext(file_name)[0]
        gpt_4o_0621_tag = os.path.join(directory_name, "gpt-4o", "dev-20240621", file_name_without_extension+".txt")
        gpt_4o_0626_tag = os.path.join(directory_name, "gpt-4o", "dev-20240626", file_name_without_extension + ".txt")
        gemini_0626_tag = os.path.join(directory_name, "gemini-1.5-pro-latest", "dev-20240626", file_name_without_extension + ".txt")
        gpt_4o_0621_tag_cache = u.fetch_file(gpt_4o_0621_tag)
        with open(gpt_4o_0621_tag_cache, 'r') as file:
            auto_tag_content_1 = auto_tag_content_1 + file.read()
        gpt_4o_0626_tag_cache = u.fetch_file(gpt_4o_0626_tag)
        with open(gpt_4o_0626_tag_cache, 'r') as file:
            auto_tag_content_2 = auto_tag_content_2 + file.read()
        gemini_0626_tag_cache = u.fetch_file(gemini_0626_tag)
        with open(gemini_0626_tag_cache, 'r') as file:
            auto_tag_content_3 = auto_tag_content_3 + file.read()
    return _cache, gr.Label(value=auto_tag_content_1, label="gpt_4o_0621"), \
           gr.Label(value=auto_tag_content_2, label="gpt_4o_0626"), \
           gr.Label(value=auto_tag_content_3, label="gemini_0626")

def split_parquet(u, file, batch_size):
    batch_size = int(batch_size)
    if file.lower().endswith(".mp4"):
        print(f"file is: {file}")
        return file, [[file]], gr.Slider(value=0, maximum=batch_size-1), gr.Column(visible=True)
    file_slice = u.split_parquet(file, batch_size)
    return file_slice[0][0], file_slice, gr.Slider(value=0, maximum=batch_size-1), gr.Column(visible=True)

def select_video(chunks, epoch_idx , batch_idx):
    print(f"chunks: {chunks}, epoch_idx: {epoch_idx}, batch_idx: {batch_idx}")
    chunks_size = len(chunks)
    epoch_idx = int(epoch_idx)
    batch_idx = int(batch_idx)
    if epoch_idx >= chunks_size:
        epoch_idx = chunks_size-1
    if batch_idx >= len(chunks[epoch_idx]):
        batch_idx = len(chunks[epoch_idx])-1
    return chunks[epoch_idx][batch_idx]

def show_lables():
    return gr.Column(visible=True)

def next_chunks(video_chunks, epoch_idx):
    length = len(video_chunks)
    return (epoch_idx+1)%length, gr.Slider(value=0)

def next_mp4_chunks(video_chunks, epoch_idx):
    global fetch_files_select_idx
    fetch_files_select_idx = fetch_files_select_idx + 1
    return gr.Dropdown(value=fetch_files[fetch_files_select_idx + 1], choices=fetch_files)

with gr.Blocks(**GR_CONF) as Core:
    user = gr.State()
    epoch_idx = gr.State(0)
    video_chunks = gr.State()

    with gr.Row(variant="panel"):
        with gr.Column(scale=6):
            _video = gr.Video(height=720)
        with gr.Column(scale=2):
            with gr.Column() as Auth:
                _token = gr.Textbox(label="Huggingface Token")
                _auth = gr.Button("Auth", variant="primary", size="lg")

            with gr.Row() as UUID:
                name= gr.Textbox(label="Name", interactive=False, scale=1)
                time= gr.Textbox(label="Time", interactive=False, scale=1)

            with gr.Column(visible=False) as Repo:
                # raw_dataset= gr.Textbox("OpenVideo/pexels-raw", label="Raw Dataset")
                raw_dataset = gr.Dropdown(choices=dataset_options, label="Raw Dataset")
                _list = gr.Button("List", variant='secondary', size='sm')

            with gr.Column(visible=False) as Batch:
                file = gr.Dropdown(label="Parquet/MP4")
                with gr.Row():
                    _cache= gr.Textbox("Downloading", label="Cache")
                    batch_size= gr.Textbox("8", label="Batch")
                _fetch = gr.Button("Fetch", variant='primary', size='sm')

            with gr.Column(visible=False) as Pick:
                _pick = gr.Slider(0, 7, value=0, step=1, label="Batch", info="Choose between 1 and $BATCH")
                gr.Label()

    with gr.Row(variant="panel") as Auto_Tag:
        with gr.Column():
            auto_tag_1 = gr.Label()
        with gr.Column():
            auto_tag_2 = gr.Label()
        with gr.Column():
            auto_tag_3 = gr.Label()

    with gr.Row(variant="panel", visible=False) as Tag:
        _human_tag = gr.Textbox(label="Tag", scale=2)
        with gr.Column():
            submit = gr.Button("Submit", variant="primary", size="sm", scale=1)
            with gr.Row():
                rst = gr.Button("Reset", variant="stop", size="sm", scale=1)
                nxt = gr.Button("Next Batch", variant="secondary", size="sm", scale=1)
                nxt_mp4 = gr.Button("Next MP4", variant="secondary", size="sm", scale=1)

    _auth.click(fn=login, inputs=_token, outputs=[user, name,  Auth])
    name.change(fn=show_time, inputs=[user, name], outputs=[time, Repo])
    _list.click(fn=list_dataset, inputs=[user, raw_dataset], outputs=[file, Batch,  Repo])
    _fetch.click(fn=fetch_parquet, inputs=[user, file], outputs=[_cache, auto_tag_1, auto_tag_2, auto_tag_3] )
    file.change(fn=fetch_parquet, inputs=[user, file], outputs=[_cache, auto_tag_1, auto_tag_2, auto_tag_3] )
    _cache.change(fn=split_parquet, inputs=[user, _cache, batch_size], outputs=[_video, video_chunks, _pick, Pick])
    _pick.change(fn=select_video, inputs=[video_chunks, epoch_idx, _pick], outputs=_video)
    _video.change(fn=show_lables, outputs=Tag)
    nxt.click(fn=next_chunks, inputs=[video_chunks, epoch_idx], outputs=[epoch_idx, _pick])
    nxt_mp4.click(fn=next_mp4_chunks, inputs=[video_chunks, epoch_idx], outputs=[file])

if __name__ == "__main__":
    Core.launch()