File size: 9,800 Bytes
3c16f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43600c2
3c16f2f
 
 
 
 
43600c2
 
 
 
 
 
 
 
 
 
3c16f2f
 
 
43600c2
3c16f2f
 
 
43600c2
3c16f2f
43600c2
 
 
 
 
 
3c16f2f
76ed0bb
3c16f2f
43600c2
 
 
3c16f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76ed0bb
 
 
 
3c16f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import os
import io
import textwrap
import tempfile

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import gradio as gr

from openai import OpenAI

# --------- OpenAI client helper ---------
def get_client(api_key: str = None):
    key = api_key or os.getenv("OPENAI_API_KEY")
    if not key:
        raise ValueError("OpenAI API key not provided. "
                         "Either set OPENAI_API_KEY env var or pass it in the UI.")
    return OpenAI(api_key=key)


# --------- Data summarisation helpers ---------
def summarize_dataframe(df: pd.DataFrame, max_cols=15, max_rows=5) -> str:
    buf = []

    # Basic info
    buf.append("### 1. Basic Structure")
    buf.append(f"- Number of rows: {df.shape[0]}")
    buf.append(f"- Number of columns: {df.shape[1]}")
    buf.append("")

    # Dtypes
    buf.append("### 2. Column Types")
    dtypes_summary = df.dtypes.astype(str).value_counts()
    for t, c in dtypes_summary.items():
        buf.append(f"- {t}: {c} columns")
    buf.append("")

    # Per-column summary
    buf.append("### 3. Column-wise Summary")
    cols_to_show = df.columns[:max_cols]
    for col in cols_to_show:
        series = df[col]
        col_info = [f"**Column:** {col}"]
        col_info.append(f"- dtype: {series.dtype}")
        col_info.append(f"- Missing values: {series.isna().sum()} "
                        f"({series.isna().mean():.2%} of rows)")

        if pd.api.types.is_numeric_dtype(series):
            desc = series.describe()
            col_info.append(
                "- Stats: "
                f"min={desc['min']:.4g}, "
                f"25%={desc['25%']:.4g}, "
                f"mean={desc['mean']:.4g}, "
                f"50%={desc['50%']:.4g}, "
                f"75%={desc['75%']:.4g}, "
                f"max={desc['max']:.4g}"
            )
        else:
            # Categorical/text summary
            nunique = series.nunique(dropna=True)
            top_vals = series.value_counts(dropna=True).head(5)
            col_info.append(f"- Unique values (non-null): {nunique}")
            tv_str = ", ".join([f"{idx} ({val})" for idx, val in top_vals.items()])
            col_info.append(f"- Top values: {tv_str}")

        buf.append("\n".join(col_info))
        buf.append("")

    if df.shape[1] > max_cols:
        buf.append(f"... ({df.shape[1] - max_cols} more columns not listed here)")
        buf.append("")

    # Correlation summary for numeric columns
    num_cols = df.select_dtypes(include=[np.number]).columns
    if len(num_cols) >= 2:
        buf.append("### 4. Numeric Correlations (Top pairs)")
        corr = df[num_cols].corr().abs()
        # Get upper triangle pairs
        pairs = []
        for i in range(len(num_cols)):
            for j in range(i + 1, len(num_cols)):
                pairs.append((num_cols[i], num_cols[j], corr.iloc[i, j]))
        pairs.sort(key=lambda x: x[2], reverse=True)
        top_pairs = pairs[:10]
        for a, b, v in top_pairs:
            buf.append(f"- {a} vs {b}: correlation={v:.3f}")
        buf.append("")

    # Small sample of rows
    buf.append("### 5. Sample Rows")
    sample = df.head(max_rows)
    buf.append(sample.to_markdown(index=False))

    return "\n".join(buf)


# --------- Plotting helpers ---------
def make_distribution_plots(df: pd.DataFrame, max_numeric=4, max_categorical=4):
    plots = []

    # Numeric distributions
    num_cols = df.select_dtypes(include=[np.number]).columns[:max_numeric]
    for col in num_cols:
        fig, ax = plt.subplots()
        sns.histplot(df[col].dropna(), kde=True, ax=ax)
        ax.set_title(f"Distribution of {col}")
        ax.set_xlabel(col)
        ax.set_ylabel("Count")
        plt.tight_layout()
        plots.append(fig)

    # Categorical distributions
    cat_cols = df.select_dtypes(exclude=[np.number]).columns[:max_categorical]
    for col in cat_cols:
        fig, ax = plt.subplots()
        value_counts = df[col].value_counts().head(15)
        sns.barplot(x=value_counts.values, y=value_counts.index, ax=ax)
        ax.set_title(f"Top categories in {col}")
        ax.set_xlabel("Count")
        ax.set_ylabel(col)
        plt.tight_layout()
        plots.append(fig)

    # Correlation heatmap
    if len(df.select_dtypes(include=[np.number]).columns) >= 2:
        fig, ax = plt.subplots(figsize=(6, 5))
        corr = df.select_dtypes(include=[np.number]).corr()
        sns.heatmap(corr, annot=False, cmap="coolwarm", ax=ax)
        ax.set_title("Correlation Heatmap (Numeric Features)")
        plt.tight_layout()
        plots.append(fig)

    return plots


# --------- OpenAI analysis ---------
def generate_ai_report(df_summary: str, api_key: str = None, model: str = "gpt-4o-mini") -> str:
    client = get_client(api_key)

    system_msg = (
        "You are a senior data analyst. You receive a structured summary of a dataset. "
        "Your job is to produce a VERY detailed, structured analysis report.\n\n"
        "Your report MUST include at least these sections:\n"
        "1. Dataset Overview\n"
        "2. Data Quality & Missing Values\n"
        "3. Univariate Analysis\n"
        "4. Bivariate & Correlation Insights\n"
        "5. Target Variables & Use Cases\n"
        "6. Feature Engineering Ideas\n"
        "7. Recommended Visualizations\n"
        "8. Risks, Biases & Limitations\n"
        "9. Next Steps for Modelling\n"
    )

    user_msg = (
        "Here is a detailed summary of the dataset. Use ONLY this information while reasoning:\n\n"
        f"{df_summary}"
    )

    response = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": system_msg},
            {"role": "user", "content": user_msg},
        ],  
        max_tokens=2000,
        temperature=0.7
    )
    

    return response.choices[0].message.content


    # Extract text from the first output
    chunks = []
    for item in response.output[0].content:
        if item.type == "output_text":
            chunks.append(item.text)

    return "\n".join(chunks).strip()


# --------- Main Gradio function ---------
def analyze_dataset(file, api_key, model_name, sample_rows, max_cols_summary):
    if file is None:
        return "Please upload a CSV file.", None

    try:
        # Read CSV
        df = pd.read_csv(file.name)

        # Optional sampling for very large datasets
        if sample_rows and df.shape[0] > sample_rows:
            df = df.sample(sample_rows, random_state=42)

        # Build summary for the LLM
        df_summary = summarize_dataframe(df, max_cols=max_cols_summary)
        ai_report = generate_ai_report(df_summary, api_key=api_key, model=model_name)

        # Generate plots
        figs = make_distribution_plots(df)

        return ai_report, figs

    except Exception as e:
        return f"❌ Error while processing file: {e}", None


# --------- Build Gradio UI ---------
def build_interface():
    with gr.Blocks(title="AI Data Analyst", theme=gr.themes.Soft()) as demo:
        gr.Markdown(
            """
            # πŸ“Š AI Data Analyst – Dataset Explorer

            Upload a CSV dataset and let an OpenAI model act as your **senior data analyst**.

            - βœ… Automatic structural summary (rows, columns, types, missingness)
            - βœ… AI-generated **very detailed** analysis report
            - βœ… Auto-generated plots (distributions & correlation heatmap)

            **Note:** For security, prefer setting your `OPENAI_API_KEY` as an environment variable
            instead of typing it in the UI.
            """
        )

        with gr.Row():
            with gr.Column(scale=1):
                file_input = gr.File(label="Upload CSV file", file_types=[".csv"])

                api_key_input = gr.Textbox(
                    label="OpenAI API Key (optional, leave blank to use environment variable)",
                    type="password",
                    placeholder="sk-...",
                )

                model_dropdown = gr.Dropdown(
                                label="OpenAI Model",
                                choices=["gpt-4o-mini", "gpt-4o", "gpt-4.1-mini", "gpt-4.1"],
                                value="gpt-4o-mini",
                            )

                sample_rows = gr.Slider(
                    minimum=0,
                    maximum=5000,
                    value=2000,
                    step=100,
                    label="Max rows to sample for analysis (0 = use all rows)",
                )

                max_cols_summary = gr.Slider(
                    minimum=5,
                    maximum=40,
                    value=15,
                    step=1,
                    label="Max columns to include in text summary",
                )

                analyze_button = gr.Button("πŸ” Analyze Dataset", variant="primary")

            with gr.Column(scale=2):
                report_output = gr.Markdown(label="AI Analysis Report")
                plots_output = gr.Gallery(
                    label="Auto-generated Plots",
                    columns=2,
                    height="auto",
                    preview=True,
                )

        def _wrapped_analyze(file, api_key, model_name, sample_rows_val, max_cols_val):
            sr = int(sample_rows_val) if sample_rows_val and sample_rows_val > 0 else None
            return analyze_dataset(file, api_key, model_name, sr, int(max_cols_val))

        analyze_button.click(
            _wrapped_analyze,
            inputs=[file_input, api_key_input, model_dropdown, sample_rows, max_cols_summary],
            outputs=[report_output, plots_output],
        )

    return demo


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