File size: 17,830 Bytes
c08faed
 
 
 
b5fce9d
 
 
 
 
486ca98
5bd4d74
486ca98
c08faed
51fb89c
b5fce9d
486ca98
 
 
 
51fb89c
 
486ca98
 
 
 
 
 
b5fce9d
486ca98
 
b5fce9d
486ca98
9940006
 
b5fce9d
9940006
 
 
 
 
486ca98
 
9940006
c08faed
486ca98
 
9940006
 
 
 
 
 
 
486ca98
 
9940006
 
 
486ca98
 
9940006
 
 
 
 
 
5bd4d74
486ca98
9940006
 
 
 
 
 
 
486ca98
 
 
 
9940006
 
486ca98
9940006
 
486ca98
9940006
 
 
 
486ca98
 
9940006
 
 
 
 
f7b84f1
9940006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1956035
9940006
 
 
 
 
 
5bd4d74
9940006
f72077b
9940006
 
 
b5fce9d
9940006
 
 
 
 
 
 
 
486ca98
9940006
b5fce9d
486ca98
 
 
1956035
486ca98
 
 
 
 
9940006
1956035
486ca98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9940006
272b87c
486ca98
 
 
 
 
 
 
 
b5fce9d
9940006
486ca98
9940006
486ca98
9940006
486ca98
 
 
 
 
 
 
 
9940006
486ca98
 
 
 
 
9940006
b5fce9d
486ca98
 
 
 
 
 
 
 
 
 
 
 
 
 
9940006
486ca98
9940006
1956035
486ca98
f72077b
9940006
486ca98
9940006
 
 
 
 
 
 
486ca98
 
 
 
 
9940006
 
486ca98
 
 
 
 
 
9940006
 
486ca98
9940006
486ca98
 
 
 
 
 
 
9940006
f7b84f1
c08faed
486ca98
 
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
import gradio as gr
import pandas as pd
import numpy as np
import plotly.express as px
import io
import json
import warnings
import google.generativeai as genai
import os
from typing import List, Dict, Any, Tuple, Optional

# --- Configuration & Constants ---
warnings.filterwarnings('ignore')

CSS = """
/* --- Phoenix UI Professional Dark CSS --- */
body { --body-background-fill: #111827; }
.stat-card { border-radius: 12px !important; padding: 20px !important; background: #1f2937 !important; border: 1px solid #374151 !important; text-align: center; transition: all 0.3s ease; }
.stat-card:hover { transform: translateY(-5px); box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05); }
.stat-card-title { font-size: 16px; font-weight: 500; color: #9ca3af !important; margin-bottom: 8px; }
.stat-card-value { font-size: 32px; font-weight: 700; color: #f9fafb !important; }
.sidebar { background-color: #111827 !important; padding: 15px; border-right: 1px solid #374151 !important; min-height: 100vh; }
.sidebar .gr-button { width: 100%; text-align: left !important; background: none !important; border: none !important; box-shadow: none !important; color: #d1d5db !important; font-size: 16px !important; padding: 12px 10px !important; margin-bottom: 8px !important; border-radius: 8px !important; transition: background-color 0.2s ease; }
.sidebar .gr-button:hover { background-color: #374151 !important; }
.sidebar .gr-button.selected { background-color: #4f46e5 !important; font-weight: 600 !important; color: white !important; }
.explanation-block { background-color: #1e3a8a !important; border-left: 4px solid #3b82f6 !important; padding: 12px; color: #e5e7eb !important; border-radius: 4px; }
"""

class DataExplorerApp:
    """A professional-grade, AI-powered data exploration application."""

    def __init__(self):
        """Initializes the application and builds the UI and event listeners."""
        self.demo = self._build_ui()

    def _build_ui(self) -> gr.Blocks:
        """
        Defines all UI components, arranges them in the layout,
        and registers all event handlers within the same Blocks context.
        """
        with gr.Blocks(theme=gr.themes.Glass(primary_hue="indigo", secondary_hue="blue"), css=CSS, title="Professional AI Data Explorer") as demo:
            # --- State Management ---
            state_var = gr.State({})

            # --- Component Definition ---
            # Sidebar
            cockpit_btn = gr.Button("πŸ“Š Data Cockpit", elem_classes="selected", elem_id="cockpit")
            deep_dive_btn = gr.Button("πŸ” Deep Dive Builder", elem_id="deep_dive")
            copilot_btn = gr.Button("πŸ€– Chief Data Scientist", elem_id="co-pilot")
            file_input = gr.File(label="πŸ“ Upload CSV File", file_types=[".csv"])
            status_output = gr.Markdown("Status: Awaiting data...")
            api_key_input = gr.Textbox(label="πŸ”‘ Gemini API Key", type="password", placeholder="Enter key to enable AI...")
            suggestion_btn = gr.Button("Get Smart Suggestions", variant="secondary", interactive=False)
            
            # Cockpit
            rows_stat, cols_stat = gr.Textbox("0", interactive=False, elem_classes="stat-card-value"), gr.Textbox("0", interactive=False, elem_classes="stat-card-value")
            quality_stat, time_cols_stat = gr.Textbox("0%", interactive=False, elem_classes="stat-card-value"), gr.Textbox("0", interactive=False, elem_classes="stat-card-value")
            suggestion_buttons = [gr.Button(visible=False) for _ in range(5)]
            
            # Deep Dive
            plot_type_dd = gr.Dropdown(['histogram', 'bar', 'scatter', 'box'], label="Plot Type", value='histogram')
            x_col_dd = gr.Dropdown([], label="X-Axis / Column", interactive=False)
            y_col_dd = gr.Dropdown([], label="Y-Axis (for Scatter/Box)", visible=False, interactive=False)
            add_plot_btn = gr.Button("Add to Dashboard", variant="primary", interactive=False)
            clear_plots_btn = gr.Button("Clear Dashboard")
            dashboard_gallery = gr.Gallery(label="πŸ“Š Your Custom Dashboard", height="auto", columns=2, preview=True)

            # Co-pilot
            chatbot = gr.Chatbot(height=500, label="Conversation", show_copy_button=True)
            copilot_explanation = gr.Markdown(visible=False, elem_classes="explanation-block")
            copilot_code = gr.Code(language="python", visible=False, label="Executed Code")
            copilot_plot = gr.Plot(visible=False, label="Generated Visualization")
            copilot_table = gr.Dataframe(visible=False, label="Generated Table", wrap=True)
            chat_input = gr.Textbox(label="Your Question", placeholder="e.g., 'What is the relationship between age and salary?'", scale=4)
            chat_submit_btn = gr.Button("Ask AI", variant="primary")
            
            # --- Layout Arrangement ---
            with gr.Row():
                with gr.Column(scale=1, elem_classes="sidebar"):
                    gr.Markdown("## πŸš€ AI Explorer Pro"); cockpit_btn; deep_dive_btn; copilot_btn; gr.Markdown("---")
                    file_input; status_output; gr.Markdown("---"); api_key_input; suggestion_btn
                with gr.Column(scale=4):
                    welcome_page = gr.Column(visible=True)
                    with welcome_page:
                        gr.Markdown("# Welcome to the AI Data Explorer Pro\n> Please **upload a CSV file** and **enter your Gemini API key** to begin your analysis.")
                        gr.Image("workflow.png", show_label=False, show_download_button=False, container=False)
                    
                    cockpit_page = gr.Column(visible=False)
                    with cockpit_page:
                        gr.Markdown("## πŸ“Š Data Cockpit: At-a-Glance Overview")
                        with gr.Row():
                            with gr.Column(elem_classes="stat-card"): gr.Markdown("<div class='stat-card-title'>Rows</div>"); rows_stat
                            with gr.Column(elem_classes="stat-card"): gr.Markdown("<div class='stat-card-title'>Columns</div>"); cols_stat
                            with gr.Column(elem_classes="stat-card"): gr.Markdown("<div class='stat-card-title'>Data Quality</div>"); quality_stat
                            with gr.Column(elem_classes="stat-card"): gr.Markdown("<div class='stat-card-title'>Date/Time Cols</div>"); time_cols_stat
                        with gr.Accordion(label="✨ AI Smart Suggestions", open=True): [btn for btn in suggestion_buttons]

                    deep_dive_page = gr.Column(visible=False)
                    with deep_dive_page:
                        gr.Markdown("## πŸ” Deep Dive: Manual Dashboard Builder"); gr.Markdown("Construct your own visualizations to investigate specific relationships.")
                        with gr.Row(): plot_type_dd; x_col_dd; y_col_dd
                        with gr.Row(): add_plot_btn; clear_plots_btn
                        dashboard_gallery
                    
                    copilot_page = gr.Column(visible=False)
                    with copilot_page:
                        gr.Markdown("## πŸ€– Chief Data Scientist: Your AI Partner"); chatbot
                        with gr.Accordion("AI's Detailed Response", open=True): copilot_explanation; copilot_code; copilot_plot; copilot_table
                        with gr.Row(): chat_input; chat_submit_btn
            
            # --- Event Handlers Registration (inside the 'with' block) ---
            pages = [cockpit_page, deep_dive_page, copilot_page]
            nav_buttons = [cockpit_btn, deep_dive_btn, copilot_btn]
            
            for i, btn in enumerate(nav_buttons):
                btn.click(
                    lambda id=btn.elem_id: self._switch_page(id), outputs=pages
                ).then(
                    lambda i=i: [gr.update(elem_classes="selected" if j==i else "") for j in range(len(nav_buttons))], outputs=nav_buttons
                )

            file_input.upload(self.load_and_process_file, inputs=[file_input], outputs=[
                state_var, status_output, welcome_page, cockpit_page,
                rows_stat, cols_stat, quality_stat, time_cols_stat,
                x_col_dd, y_col_dd, add_plot_btn
            ]).then(lambda: self._switch_page("cockpit"), outputs=pages) \
            .then(lambda: [gr.update(elem_classes="selected"), gr.update(elem_classes=""), gr.update(elem_classes="")], outputs=nav_buttons)

            api_key_input.change(lambda x: gr.update(interactive=bool(x)), inputs=[api_key_input], outputs=[suggestion_btn])

            plot_type_dd.change(self._update_plot_controls, inputs=[plot_type_dd], outputs=[y_col_dd])
            add_plot_btn.click(self.add_plot_to_dashboard, inputs=[state_var, x_col_dd, y_col_dd, plot_type_dd], outputs=[state_var, dashboard_gallery])
            clear_plots_btn.click(self.clear_dashboard, inputs=[state_var], outputs=[state_var, dashboard_gallery])

            suggestion_btn.click(self.get_ai_suggestions, inputs=[state_var, api_key_input], outputs=suggestion_buttons)
            for btn in suggestion_buttons:
                btn.click(self.handle_suggestion_click, inputs=[btn], outputs=[cockpit_page, deep_dive_page, copilot_page, chat_input]) \
                .then(lambda: self._switch_page("co-pilot"), outputs=pages) \
                .then(lambda: (gr.update(elem_classes=""), gr.update(elem_classes=""), gr.update(elem_classes="selected")), outputs=nav_buttons)
            
            chat_submit_btn.click(self.respond_to_chat, [state_var, api_key_input, chat_input, chatbot], [chatbot, copilot_explanation, copilot_code, copilot_plot, copilot_table]).then(lambda: "", outputs=[chat_input])
            chat_input.submit(self.respond_to_chat, [state_var, api_key_input, chat_input, chatbot], [chatbot, copilot_explanation, copilot_code, copilot_plot, copilot_table]).then(lambda: "", outputs=[chat_input])
        
        return demo

    def launch(self):
        """Launches the Gradio application."""
        self.demo.launch(debug=True)

    # --- Backend Logic Methods ---
    def _switch_page(self, page_id: str) -> Tuple[gr.update, ...]:
        return gr.update(visible=page_id=="cockpit"), gr.update(visible=page_id=="deep_dive"), gr.update(visible=page_id=="co-pilot")
    
    def _update_plot_controls(self, plot_type: str) -> gr.update:
        return gr.update(visible=plot_type in ['scatter', 'box'])

    def load_and_process_file(self, file_obj: Any) -> Tuple[Any, ...]:
        try:
            df = pd.read_csv(file_obj.name, low_memory=False)
            for col in df.select_dtypes(include=['object']).columns:
                try: df[col] = pd.to_datetime(df[col], errors='raise')
                except (ValueError, TypeError): continue
            
            metadata = self._extract_dataset_metadata(df)
            state = {'df': df, 'metadata': metadata, 'dashboard_plots': []}
            status_msg = f"βœ… **{os.path.basename(file_obj.name)}** loaded."
            rows, cols, quality = metadata['shape'][0], metadata['shape'][1], metadata['data_quality']
            
            return (state, status_msg, gr.update(visible=False), gr.update(visible=True),
                    f"{rows:,}", f"{cols}", f"{quality}%", f"{len(metadata['datetime_cols'])}",
                    gr.update(choices=metadata['columns'], interactive=True), gr.update(choices=metadata['columns'], interactive=True), gr.update(interactive=True))
        except Exception as e:
            gr.Error(f"File Load Error: {e}"); return {}, f"❌ Error: {e}", gr.update(visible=True), gr.update(visible=False), "0", "0", "0%", "0", gr.update(choices=[], interactive=False), gr.update(choices=[], interactive=False), gr.update(interactive=False)

    def _extract_dataset_metadata(self, df: pd.DataFrame) -> Dict[str, Any]:
        rows, cols = df.shape
        quality = round((df.notna().sum().sum() / (rows * cols)) * 100, 1) if rows * cols > 0 else 0
        return {'shape': (rows, cols), 'columns': df.columns.tolist(),
                'numeric_cols': df.select_dtypes(include=np.number).columns.tolist(),
                'categorical_cols': df.select_dtypes(include=['object', 'category']).columns.tolist(),
                'datetime_cols': df.select_dtypes(include=['datetime64', 'datetime64[ns]']).columns.tolist(),
                'dtypes_head': df.head().to_string()}

    def add_plot_to_dashboard(self, state: Dict, x_col: str, y_col: Optional[str], plot_type: str) -> Tuple[Dict, List]:
        if not x_col:
            gr.Warning("Please select at least an X-axis column."); return state, state.get('dashboard_plots', [])
        df = state['df']
        title = f"{plot_type.capitalize()}: {y_col} by {x_col}" if y_col and plot_type in ['box', 'scatter'] else f"Distribution of {x_col}"
        try:
            if plot_type == 'histogram': fig = px.histogram(df, x=x_col, title=title)
            elif plot_type == 'box': fig = px.box(df, x=x_col, y=y_col, title=title)
            elif plot_type == 'scatter': fig = px.scatter(df, x=x_col, y=y_col, title=title, trendline="ols", trendline_color_override="red")
            elif plot_type == 'bar':
                counts = df[x_col].value_counts().nlargest(20)
                fig = px.bar(counts, x=counts.index, y=counts.values, title=f"Top 20 Categories for {x_col}", labels={'index': x_col, 'y': 'Count'})
            if fig:
                fig.update_layout(template="plotly_dark"); state['dashboard_plots'].append(fig); gr.Info(f"Added '{title}' to the dashboard.")
            return state, state['dashboard_plots']
        except Exception as e:
            gr.Error(f"Plotting Error: {e}"); return state, state.get('dashboard_plots', [])

    def clear_dashboard(self, state: Dict) -> Tuple[Dict, List]:
        state['dashboard_plots'] = []; gr.Info("Dashboard cleared."); return state, []

    def get_ai_suggestions(self, state: Dict, api_key: str) -> List[gr.update]:
        if not api_key: gr.Warning("API Key is required for suggestions."); return [gr.update(visible=False)]*5
        if not state: gr.Warning("Please load data first."); return [gr.update(visible=False)]*5
        metadata = state['metadata']
        prompt = f"""Based on this metadata (columns: {metadata['columns']}), generate 4 impactful analytical questions. Return ONLY a JSON list of strings."""
        try:
            genai.configure(api_key=api_key)
            suggestions = json.loads(genai.GenerativeModel('gemini-1.5-flash').generate_content(prompt).text)
            return [gr.Button(s, visible=True) for s in suggestions] + [gr.Button(visible=False)] * (5 - len(suggestions))
        except Exception as e: gr.Error(f"AI Suggestion Error: {e}"); return [gr.update(visible=False)]*5

    def handle_suggestion_click(self, question: str) -> Tuple[gr.update, ...]:
        return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), question

    def respond_to_chat(self, state: Dict, api_key: str, user_message: str, history: List) -> Any:
        if not api_key or not state:
            msg = "I need a Gemini API key and a dataset to work."; history.append((user_message, msg)); return history, *[gr.update(visible=False)]*4
        
        history.append((user_message, "Thinking... πŸ€”")); yield history, *[gr.update(visible=False)]*4
        
        metadata, prompt = state['metadata'], f"""You are 'Chief Data Scientist', an expert AI analyst...
        **Instructions:**
        1. **Analyze:** Understand the user's intent.
        2. **Method:** Choose the best method (table, value, or plot). Infer the best plot type.
        3. **Plan:** Briefly explain your plan.
        4. **Code:** Write Python code. Use `fig` for plots (with `template='plotly_dark'`) and `result_df` for tables.
        5. **Insight:** Provide a one-sentence business insight.
        6. **Respond ONLY with a single JSON object with keys: "plan", "code", "insight".**
        **Metadata:** {metadata['dtypes_head']}
        **User Question:** "{user_message}"
        """
        try:
            genai.configure(api_key=api_key)
            response_json = json.loads(genai.GenerativeModel('gemini-1.5-flash').generate_content(prompt).text.strip().replace("```json", "").replace("```", ""))
            plan, code, insight = response_json.get("plan"), response_json.get("code"), response_json.get("insight")
            stdout, fig, df_result, error = self._safe_exec(code, {'df': state['df'], 'px': px, 'pd': pd})
            
            history[-1] = (user_message, f"**Plan:** {plan}")
            explanation = f"**Insight:** {insight}"
            if stdout: explanation += f"\n\n**Console Output:**\n```\n{stdout}\n```"
            if error: gr.Error(f"AI Code Execution Failed: {error}")
            
            yield (history, gr.update(visible=bool(explanation), value=explanation), gr.update(visible=bool(code), value=code),
                   gr.update(visible=bool(fig), value=fig), gr.update(visible=bool(df_result is not None), value=df_result))
        except Exception as e:
            history[-1] = (user_message, f"I encountered an error. Please rephrase your question. (Error: {e})")
            yield history, *[gr.update(visible=False)]*4
            
    def _safe_exec(self, code_string: str, local_vars: Dict) -> Tuple[Any, ...]:
        output_buffer = io.StringIO()
        try:
            with redirect_stdout(output_buffer): exec(code_string, globals(), local_vars)
            return output_buffer.getvalue(), local_vars.get('fig'), local_vars.get('result_df'), None
        except Exception as e: return None, None, None, str(e)

if __name__ == "__main__":
    app = DataExplorerApp()
    app.launch()