File size: 5,479 Bytes
d54fa91
eb94e92
 
 
 
 
 
d54fa91
cbbf201
d54fa91
eb94e92
8a2c637
 
cbbf201
eb94e92
faeec87
cbbf201
8a2c637
cbbf201
 
eb94e92
faeec87
cbbf201
 
 
 
 
eb94e92
cbbf201
eb94e92
 
 
cbbf201
eb94e92
 
 
 
 
cbbf201
 
eb94e92
 
 
 
 
 
faeec87
eb94e92
 
 
cbbf201
eb94e92
 
 
 
 
 
faeec87
eb94e92
 
 
 
cbbf201
eb94e92
 
 
 
 
cbbf201
 
 
 
eb94e92
 
 
 
 
 
cbbf201
 
 
 
eb94e92
 
 
 
 
cbbf201
 
eb94e92
cbbf201
eb94e92
 
 
 
 
d54fa91
eb94e92
 
 
cbbf201
 
eb94e92
 
 
d54fa91
cbbf201
 
 
 
eb94e92
 
 
cbbf201
 
d54fa91
cbbf201
eb94e92
 
 
 
 
cbbf201
 
 
eb94e92
 
 
 
 
 
 
 
cbbf201
 
eb94e92
 
 
cbbf201
 
eb94e92
 
 
 
 
cbbf201
 
d54fa91
 
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
import gradio as gr
from src.core import (
    get_table_mapping,
    transform_source,
    process_csv_text,
    generate_mapping_code,
)

MAX_ROWS = 10


def generate_step_markdown(step_number: int, subtitle: str, description: str = None):
    return gr.Markdown(f"# Step {step_number}\n\n ### {subtitle}\n{description}")


# TODO: use tempfile
def export_csv(df, filename):
    df.to_csv(filename, index=False)
    return gr.File.update(value=filename, visible=True)


# TODO: use tempfile
def export_text(val, filename):
    with open(filename, "w") as f:
        f.write(val)
    return gr.File.update(value=filename, visible=True)


with gr.Blocks() as demo:
    gr.Markdown(
        "# LLM Data Mapper\n\nThis tool will help you map a source CSV to a template CSV, and then generate python code to transform the source CSV into the template CSV. You can edit all of the values, re-run the processes, and download files along the way."
    )
    # STEP 1
    generate_step_markdown(
        1,
        "Upload a Template CSV and a Source CSV.",
        "The schema will be extracted from the template file and the source file will be transformed to match the schema.",
    )
    with gr.Row():
        with gr.Column():
            upload_template_btn = gr.UploadButton(
                label="Upload Template File",
                file_types=[".csv"],
                live=True,
                file_count="single",
            )
            template_df = gr.Dataframe(max_rows=MAX_ROWS, interactive=False)
            upload_template_btn.upload(
                fn=process_csv_text, inputs=upload_template_btn, outputs=template_df
            )
        with gr.Column():
            upload_source_button = gr.UploadButton(
                label="Upload Source File",
                file_types=[".csv"],
                live=True,
                file_count="single",
            )
            source_df = gr.Dataframe(max_rows=MAX_ROWS, interactive=False)
            upload_source_button.upload(
                fn=process_csv_text, inputs=upload_source_button, outputs=source_df
            )

    # STEP 2
    generate_step_markdown(
        2,
        "Generate mapping from Source to Template.",
        "Once generated, you can edit the values directly in the table below and they will be incorporated into the mapping logic.",
    )
    with gr.Row():
        generate_mapping_btn = gr.Button(value="Generate Mapping", variant="primary")
    with gr.Row():
        table_mapping_df = gr.DataFrame(max_rows=MAX_ROWS, interactive=True)
        generate_mapping_btn.click(
            fn=get_table_mapping,
            inputs=[source_df, template_df],
            outputs=[table_mapping_df],
        )

    with gr.Row():
        save_mapping_btn = gr.Button(value="Save Mapping", variant="secondary")
    with gr.Row():
        csv = gr.File(interactive=False, visible=False)
        save_mapping_btn.click(
            lambda df: export_csv(df, "source_template_mapping.csv"),
            table_mapping_df,
            csv,
        )
        mapping_file = gr.File(label="Downloaded File", visible=False)
        mapping_file.change(lambda x: x, mapping_file, table_mapping_df)

    # STEP 3
    generate_step_markdown(
        3,
        "Generate python code to transform Source to Template, using the generated mapping.",
        "Once generated, you can edit the code directly in the code block below and it will be incorporated into the transformation logic. And this is re-runnable! Update the mapping logic above to try it out.",
    )
    with gr.Row():
        generate_code_btn = gr.Button(
            value="Generate Code from Mapping", variant="primary"
        )
    with gr.Row():
        code_block = gr.Code(language="python")
        generate_code_btn.click(
            fn=generate_mapping_code, inputs=[table_mapping_df], outputs=[code_block]
        )

    with gr.Row():
        save_code_btn = gr.Button(value="Save Code", variant="secondary")
    with gr.Row():
        text = gr.File(interactive=False, visible=False)
        save_code_btn.click(
            lambda txt: export_text(txt, "transformation_code.py"), code_block, text
        )
        code_file = gr.File(label="Downloaded File", visible=False)
        code_file.change(lambda x: x, code_file, code_block)

    # STEP 4
    generate_step_markdown(
        4,
        "Transform the Source CSV into the Template CSV using the generated code.",
        "And this is re-runnable! Update the logic above to try it out.",
    )
    with gr.Row():
        transform_btn = gr.Button(value="Transform Source", variant="primary")
    with gr.Row():
        source_df_transformed = gr.Dataframe(
            label="Source (transformed)", max_rows=MAX_ROWS
        )
        transform_btn.click(
            transform_source,
            inputs=[source_df, code_block],
            outputs=[source_df_transformed],
        )

    with gr.Row():
        save_transformed_source_btn = gr.Button(
            value="Save Transformed Source", variant="secondary"
        )
    with gr.Row():
        csv = gr.File(interactive=False, visible=False)
        save_transformed_source_btn.click(
            lambda df: export_csv(df, "transformed_source.csv"),
            source_df_transformed,
            csv,
        )
        transform_file = gr.File(label="Downloaded File", visible=False)
        transform_file.change(lambda x: x, transform_file, source_df_transformed)

demo.launch()