File size: 6,586 Bytes
bad52c6
6ed06d5
bad52c6
 
 
 
 
 
 
 
6ed06d5
 
bad52c6
 
 
 
 
 
6ed06d5
bad52c6
 
 
6ed06d5
bad52c6
 
 
 
 
 
6ed06d5
bad52c6
 
 
 
 
 
 
 
 
 
 
 
6ed06d5
bad52c6
 
 
 
 
 
6ed06d5
bad52c6
 
 
 
 
6ed06d5
bad52c6
 
 
6ed06d5
bad52c6
 
 
 
 
 
6ed06d5
bad52c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed06d5
bad52c6
 
 
 
 
6ed06d5
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
"""
ai_csv_editor_hf.py ── AI-powered CSV editor using a Hugging Face model on CPU.

Features:
- Upload one or more CSV files (main + optional lookup tables)
- Type spreadsheet-style commands: CONCAT, VLOOKUP, XLOOKUP, SUMIF
- LLM (google/flan-t5-base) converts commands β†’ JSON β€œedit plan”
- pandas applies each action in sequence
- Preview first 20 rows & download modified CSV
"""

import json
import io
import tempfile
import textwrap
import pathlib
from typing import List, Dict, Any

import pandas as pd
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM

# ──────────────────────────────────────────────────────────
# 1.  LOAD A SMALL INSTRUCTION-FOLLOWING MODEL (CPU only)
# ──────────────────────────────────────────────────────────
MODEL_NAME  = "google/flan-t5-base"
MAX_NEW_TOK  = 256
TEMPERATURE  = 0.0

tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model     = AutoModelForSeq2SeqLM.from_pretrained(
                MODEL_NAME,
                device_map="cpu",      # force CPU
                torch_dtype="auto"
            )
generator = pipeline(
    "text2text-generation",
    model=model,
    tokenizer=tokenizer,
    device=-1,                  # -1 = CPU
)

# ──────────────────────────────────────────────────────────
# 2.  PROMPT β†’ JSON β€œEDIT PLAN”
# ──────────────────────────────────────────────────────────
SYSTEM_PROMPT = textwrap.dedent("""\
You are an assistant that converts natural-language spreadsheet commands
into JSON edit plans. Respond with ONLY valid JSON matching this schema:

{
  "actions": [
    {
      "operation": "concat | vlookup | xlookup | sumif",
      "target": "string",

      # For CONCAT:
      "columns": ["colA","colB"],
      "separator": " ",

      # For VLOOKUP / XLOOKUP:
      "lookup_value": "KeyInMain",
      "lookup_file": "other.csv",
      "lookup_column": "KeyInOther",
      "return_column": "Value",
      "exact": true,

      # For SUMIF:
      "criteria_column": "Category",
      "criteria": "Foo",
      "sum_column": "Amount"
    }
  ]
}
""")

def plan_from_command(cmd: str) -> Dict[str, Any]:
    prompt = f"{SYSTEM_PROMPT}\n\nUser: {cmd}\nJSON:"
    output = generator(
        prompt,
        max_new_tokens=MAX_NEW_TOK,
        temperature=TEMPERATURE,
        do_sample=False,
    )[0]["generated_text"]
    try:
        return json.loads(output)
    except json.JSONDecodeError as e:
        raise ValueError(f"Model returned invalid JSON:\n{output}") from e

# ──────────────────────────────────────────────────────────
# 3.  DATA OPERATIONS
# ──────────────────────────────────────────────────────────
def apply_action(df: pd.DataFrame,
                 uploads: Dict[str, pd.DataFrame],
                 act: Dict[str, Any]) -> pd.DataFrame:
    op = act["operation"]

    if op == "concat":
        sep = act.get("separator", "")
        df[act["target"]] = (
            df[act["columns"]]
            .astype(str)
            .agg(sep.join, axis=1)
        )

    elif op in {"vlookup", "xlookup"}:
        lookup_df = uploads[act["lookup_file"]]
        # select only the two relevant columns and rename for merging
        right = lookup_df[[act["lookup_column"], act["return_column"]]] \
            .rename(columns={
                act["lookup_column"]: act["lookup_value"],
                act["return_column"]: act["target"]
            })
        df = df.merge(right, on=act["lookup_value"], how="left")

    elif op == "sumif":
        mask = df[act["criteria_column"]] == act["criteria"]
        total = df.loc[mask, act["sum_column"]].sum()
        df[act["target"]] = total

    else:
        raise ValueError(f"Unsupported operation: {op}")

    return df

# ──────────────────────────────────────────────────────────
# 4.  GRADIO UI
# ──────────────────────────────────────────────────────────
def run_editor(files: List[gr.File], command: str):
    if not files:
        return None, "⚠️ Please upload at least one CSV file.", None

    # Load uploaded CSVs into a dictionary
    uploads = {
        pathlib.Path(f.name).name: pd.read_csv(f.name)
        for f in files
    }
    # Treat the first file as the main dataset
    main_name = list(uploads.keys())[0]
    df = uploads[main_name]

    # Generate plan
    try:
        plan = plan_from_command(command)
    except Exception as e:
        return None, f"❌ LLM error: {e}", None

    # Apply actions
    try:
        for act in plan["actions"]:
            df = apply_action(df, uploads, act)
    except Exception as e:
        return None, f"❌ Execution error: {e}", None

    # Write modified CSV to a temp file and return
    tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
    df.to_csv(tmp.name, index=False)
    return df.head(20), "βœ… Success! Download below.", tmp.name

with gr.Blocks(title="AI CSV Editor (HF, CPU)") as demo:
    gr.Markdown("## AI-powered CSV Editor  \n"
                "1. Upload one main CSV (first) plus any lookup tables  \n"
                "2. Type a spreadsheet-style instruction  \n"
                "3. Download the modified CSV")
    csv_files = gr.Files(file_types=[".csv"], label="Upload CSV file(s)")
    cmd_box   = gr.Textbox(lines=2, placeholder="e.g. concat First Last β†’ FullName")
    run_btn   = gr.Button("Apply")
    preview   = gr.Dataframe(label="Preview (first 20 rows)")
    status    = gr.Markdown()
    download  = gr.File(label="Download Result")

    run_btn.click(
        fn=run_editor,
        inputs=[csv_files, cmd_box],
        outputs=[preview, status, download]
    )

if __name__ == "__main__":
    demo.launch()