Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
try:
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
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)}"
|
29 |
-
|
30 |
-
# Return dataframes as HTML tables
|
31 |
-
return original_df.to_html(index=False), manipulated_df.to_html(index=False)
|
32 |
-
|
33 |
|
34 |
# Gradio Interface
|
35 |
with gr.Blocks() as demo:
|
36 |
-
gr.Markdown("# CSV Manipulation Tool")
|
37 |
-
file_input = gr.File(label="Upload CSV File", type="
|
38 |
-
instruction_input = gr.Textbox(label="Instruction (e.g., '
|
39 |
-
|
40 |
-
manipulated_output = gr.HTML(label="Manipulated CSV Data")
|
41 |
run_button = gr.Button("Apply Instruction")
|
42 |
|
43 |
# Link inputs and outputs
|
44 |
-
run_button.click(
|
45 |
|
46 |
# Launch Gradio Interface
|
47 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
from transformers import pipeline
|
4 |
+
import io
|
5 |
+
|
6 |
+
# Load the Hugging Face GPT-Neo model
|
7 |
+
generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B')
|
8 |
+
|
9 |
+
# Function to process CSV file and instruction using Hugging Face GPT-Neo
|
10 |
+
def process_csv_with_huggingface(file, instruction):
|
11 |
+
# Read CSV from file
|
12 |
+
df = pd.read_csv(file.name)
|
13 |
+
|
14 |
+
# Prepare the prompt for GPT-Neo to generate Python code
|
15 |
+
prompt = f"""
|
16 |
+
You are an expert in Python and data manipulation using Pandas. You will be given a DataFrame and an instruction.
|
17 |
+
Interpret the instruction and generate Python code to manipulate the DataFrame accordingly.
|
18 |
+
Only return the Python code.
|
19 |
+
|
20 |
+
Instruction: {instruction}
|
21 |
+
|
22 |
+
DataFrame:
|
23 |
+
{df.head().to_dict()}
|
24 |
+
"""
|
25 |
+
|
26 |
+
# Request code generation from GPT-Neo
|
27 |
+
response = generator(prompt, max_length=200, num_return_sequences=1)
|
28 |
+
|
29 |
+
# Extract the generated Python code
|
30 |
+
generated_code = response[0]['generated_text'].strip()
|
31 |
+
|
32 |
+
# Execute the generated code
|
33 |
try:
|
34 |
+
exec(generated_code, globals(), locals())
|
35 |
+
# After execution, the manipulated DataFrame is stored in 'df'
|
36 |
+
# Convert the DataFrame to CSV for download
|
37 |
+
output = io.StringIO()
|
38 |
+
df.to_csv(output, index=False)
|
39 |
+
output.seek(0)
|
40 |
+
return output.getvalue()
|
|
|
|
|
|
|
|
|
41 |
except Exception as e:
|
42 |
+
return f"Error while processing: {str(e)}"
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Gradio Interface
|
45 |
with gr.Blocks() as demo:
|
46 |
+
gr.Markdown("# LLM-Powered CSV Manipulation Tool")
|
47 |
+
file_input = gr.File(label="Upload CSV File", type="file")
|
48 |
+
instruction_input = gr.Textbox(label="Instruction (e.g., 'Add a new column Age with value 30')")
|
49 |
+
output_file = gr.File(label="Download Manipulated CSV")
|
|
|
50 |
run_button = gr.Button("Apply Instruction")
|
51 |
|
52 |
# Link inputs and outputs
|
53 |
+
run_button.click(process_csv_with_huggingface, inputs=[file_input, instruction_input], outputs=[output_file])
|
54 |
|
55 |
# Launch Gradio Interface
|
56 |
demo.launch()
|