File size: 2,328 Bytes
4464055
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import gradio as gr

from project_settings import project_path


def get_fs_tab():
    with gr.TabItem("fs"):
        with gr.Row():
            with gr.Column(scale=3):
                fs_filename = gr.Textbox(label="filename", max_lines=10)
                fs_file = gr.File(label="file")
                # fs_file_dir = gr.Textbox(value="data", label="file_dir")
                fs_file_dir = gr.Dropdown(choices=["data/dataset", "data/eval_data"],
                                          value="data/dataset",
                                          label="file_dir")
                fs_query = gr.Button("query", variant="primary")
            with gr.Column(scale=7):
                fs_filelist_dataset_state = gr.State(value=[])
                fs_filelist_dataset = gr.Dataset(
                    components=[fs_filename, fs_file],
                    samples=fs_filelist_dataset_state.value,
                )

        def when_click_query_files(file_dir: str = "data"):
            file_dir = project_path / file_dir
            dataset_state = list()
            for filename in file_dir.glob("**/*.*"):
                if filename.is_dir():
                    continue
                if filename.stem.startswith("."):
                    continue
                if filename.name.endswith(".py"):
                    continue
                if filename.name.endswith(".raw"):
                    continue
                dataset_state.append((
                    filename.relative_to(file_dir).as_posix(),
                    filename.as_posix(),
                ))

            dataset = gr.Dataset(
                components=[fs_filename, fs_file],
                samples=dataset_state,
            )
            return dataset_state, dataset

        fs_filelist_dataset.click(
            fn=lambda x: (
                x[1], x[1]
            ),
            inputs=[fs_filelist_dataset],
            outputs=[fs_filename, fs_file]
        )
        fs_query.click(
            fn=when_click_query_files,
            inputs=[fs_file_dir],
            outputs=[fs_filelist_dataset_state, fs_filelist_dataset]
        )
    return locals()


if __name__ == "__main__":
    with gr.Blocks() as block:
        fs_components = get_fs_tab()
        block.launch()