File size: 1,900 Bytes
fee7fa9
 
 
 
 
3b0c7e4
 
fee7fa9
 
 
3b0c7e4
fee7fa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b0c7e4
fee7fa9
 
 
3b0c7e4
fee7fa9
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from io import StringIO

# Function to manipulate CSV based on instruction
def manipulate_csv(file_path, instruction):
    if file_path is None:
        return "Please upload a CSV file", None

    # Read the CSV file
    original_df = pd.read_csv(file_path)
    manipulated_df = original_df.copy()

    # Process instruction (basic examples)
    try:
        if "add column" in instruction.lower():
            column_name = instruction.split("add column ")[1]
            manipulated_df[column_name] = "New Data"
        elif "filter rows" in instruction.lower():
            column, value = instruction.split("filter rows ")[1].split("=")
            manipulated_df = manipulated_df[manipulated_df[column.strip()] == value.strip()]
        elif "drop column" in instruction.lower():
            column_name = instruction.split("drop column ")[1]
            manipulated_df = manipulated_df.drop(columns=[column_name.strip()])
        else:
            return "Instruction not recognized. Try: 'add column X', 'filter rows column=value', 'drop column X'", None
    except Exception as e:
        return f"Error: {str(e)}", None

    # Return dataframes as HTML tables
    return original_df.to_html(index=False), manipulated_df.to_html(index=False)


# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("# CSV Manipulation Tool")
    file_input = gr.File(label="Upload CSV File", type="filepath")
    instruction_input = gr.Textbox(label="Instruction (e.g., 'add column new_column')")
    original_output = gr.HTML(label="Original CSV Data")
    manipulated_output = gr.HTML(label="Manipulated CSV Data")
    run_button = gr.Button("Apply Instruction")

    # Link inputs and outputs
    run_button.click(manipulate_csv, inputs=[file_input, instruction_input], outputs=[original_output, manipulated_output])

# Launch Gradio Interface
demo.launch()