File size: 2,449 Bytes
ed3849d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from utils import *


def infer(input_file: str):
    clean_cache()
    try:
        data_list = eval(f'encoder_{MODE["from"]}')(input_file)
        output_file = eval(f'decoder_{MODE["to"]}')(data_list)
        return output_file, pd.DataFrame(data_list)

    except Exception as e:
        return None, pd.DataFrame([{"请上传标准的数据文件": f"{e}"}])


if __name__ == "__main__":
    with gr.Blocks() as demo:
        for item in TAB_CONFIG:
            types = item.split(" ⇆ ")
            with gr.Tab(item) as tab:
                with gr.Row():
                    with gr.Column():
                        option = gr.Dropdown(
                            choices=[
                                f"{types[0]}{types[1]}",
                                f"{types[0]}{types[1]}",
                            ],
                            label="模式 Mode",
                            value=f"{types[0]}{types[1]}",
                        )
                        input_file = gr.components.File(
                            type="filepath",
                            label="上传原数据 Upload input file",
                            file_types=[f".{types[0]}", f".{types[1]}"],
                        )
                        convert_btn = gr.Button("转换 Convert")

                    with gr.Column():
                        output_file = gr.components.File(
                            type="filepath", label="下载转换数据 Download output file"
                        )
                        data_viewer = gr.Dataframe(label="数据预览 Data viewer")

            option.change(change_mode, inputs=option)
            tab.select(change_mode, inputs=option)
            convert_btn.click(
                infer, inputs=input_file, outputs=[output_file, data_viewer]
            )

        gr.Markdown(
            """
## 支持的 JSON 格式 (Supported JSON format)
```
[
    {
        "key1": "val11",
        "key2": "val12",
        ...
    },
    {
        "key1": "val21",
        "key2": "val22",
        ...
    },
    ...
]
```
## 支持的 JSON Lines 格式 (Supported jsonl format)
```
{"key1": "val11", "key2": "val12", ...}
{"key1": "val21", "key2": "val22", ...}
...
```             
## 支持的 CSV 格式 (Supported CSV format)
```
key1, key2, ...
val11, val12, ...
val21, val22, ...
...
```
"""
        )

    demo.launch()