Imsachinsingh00 commited on
Commit
5bab888
·
verified ·
1 Parent(s): 3b0c7e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -34
app.py CHANGED
@@ -1,47 +1,56 @@
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_path, instruction):
7
- if file_path is None:
8
- return "Please upload a CSV file", None
9
-
10
- # Read the CSV file
11
- original_df = pd.read_csv(file_path)
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
 
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="filepath")
38
- instruction_input = gr.Textbox(label="Instruction (e.g., 'add column new_column')")
39
- original_output = gr.HTML(label="Original CSV Data")
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(manipulate_csv, inputs=[file_input, instruction_input], outputs=[original_output, manipulated_output])
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()