Spaces:
Running
Running
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() | |