Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from io import StringIO
|
4 |
+
|
5 |
+
# Function to manipulate CSV based on instruction
|
6 |
+
def manipulate_csv(file, instruction):
|
7 |
+
if file is None:
|
8 |
+
return "Please upload a CSV file", None
|
9 |
+
|
10 |
+
# Read the CSV file
|
11 |
+
original_df = pd.read_csv(file)
|
12 |
+
manipulated_df = original_df.copy()
|
13 |
+
|
14 |
+
# Process instruction (basic examples)
|
15 |
+
try:
|
16 |
+
if "add column" in instruction.lower():
|
17 |
+
column_name = instruction.split("add column ")[1]
|
18 |
+
manipulated_df[column_name] = "New Data"
|
19 |
+
elif "filter rows" in instruction.lower():
|
20 |
+
column, value = instruction.split("filter rows ")[1].split("=")
|
21 |
+
manipulated_df = manipulated_df[manipulated_df[column.strip()] == value.strip()]
|
22 |
+
elif "drop column" in instruction.lower():
|
23 |
+
column_name = instruction.split("drop column ")[1]
|
24 |
+
manipulated_df = manipulated_df.drop(columns=[column_name.strip()])
|
25 |
+
else:
|
26 |
+
return "Instruction not recognized. Try: 'add column X', 'filter rows column=value', 'drop column X'", None
|
27 |
+
except Exception as e:
|
28 |
+
return f"Error: {str(e)}", None
|
29 |
+
|
30 |
+
# Return dataframes as HTML tables
|
31 |
+
return original_df.to_html(index=False), manipulated_df.to_html(index=False)
|
32 |
+
|
33 |
+
# Gradio Interface
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("# CSV Manipulation Tool")
|
36 |
+
file_input = gr.File(label="Upload CSV File", type="file")
|
37 |
+
instruction_input = gr.Textbox(label="Instruction (e.g., 'add column new_column')")
|
38 |
+
original_output = gr.HTML(label="Original CSV Data")
|
39 |
+
manipulated_output = gr.HTML(label="Manipulated CSV Data")
|
40 |
+
run_button = gr.Button("Apply Instruction")
|
41 |
+
|
42 |
+
# Link inputs and outputs
|
43 |
+
run_button.click(manipulate_csv, inputs=[file_input, instruction_input], outputs=[original_output, manipulated_output])
|
44 |
+
|
45 |
+
# Launch Gradio Interface
|
46 |
+
demo.launch()
|