csv / app.py
Imsachinsingh00's picture
Create app.py
fee7fa9 verified
raw
history blame
1.88 kB
import gradio as gr
import pandas as pd
from io import StringIO
# Function to manipulate CSV based on instruction
def manipulate_csv(file, instruction):
if file is None:
return "Please upload a CSV file", None
# Read the CSV file
original_df = pd.read_csv(file)
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="file")
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()