File size: 11,950 Bytes
60da408
c9ba3ae
0d6622c
c9ba3ae
0d6622c
 
 
 
 
 
c9ba3ae
 
0d6622c
 
c9ba3ae
60da408
 
 
 
 
1b21942
 
c9ba3ae
 
 
 
c08faed
c9ba3ae
c08faed
60da408
b5fce9d
60da408
0d6622c
 
 
 
 
c9ba3ae
60da408
c9ba3ae
0d6622c
c9ba3ae
4b2fe64
 
0d6622c
c9ba3ae
0d6622c
60da408
 
0d6622c
60da408
 
c9ba3ae
60da408
 
 
0d6622c
60da408
b5fce9d
60da408
0d6622c
60da408
 
 
0d6622c
 
 
60da408
 
 
 
c9ba3ae
0d6622c
 
60da408
4b2fe64
 
0d6622c
c9ba3ae
60da408
c9ba3ae
60da408
 
 
c9ba3ae
0d6622c
 
60da408
c9ba3ae
0d6622c
 
c9ba3ae
0d6622c
 
 
c9ba3ae
1b21942
0d6622c
 
 
 
 
 
 
 
60da408
0d6622c
 
c9ba3ae
0d6622c
60da408
0d6622c
 
 
60da408
0d6622c
 
c9ba3ae
0d6622c
 
 
 
c9ba3ae
0d6622c
 
c9ba3ae
0d6622c
c9ba3ae
0d6622c
4b2fe64
0d6622c
 
 
1b21942
0d6622c
1b21942
0d6622c
c9ba3ae
0d6622c
 
c9ba3ae
 
 
0d6622c
 
c9ba3ae
0d6622c
 
 
 
c9ba3ae
0d6622c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9ba3ae
0d6622c
 
 
 
 
 
 
 
 
 
 
c9ba3ae
0d6622c
4b2fe64
0d6622c
 
 
60da408
 
c9ba3ae
0d6622c
c9ba3ae
0d6622c
 
 
 
 
 
 
 
 
 
 
 
 
c9ba3ae
 
60da408
0d6622c
 
 
 
 
4b2fe64
0d6622c
 
 
 
 
4b2fe64
60da408
0d6622c
 
60da408
1b21942
0d6622c
 
60da408
 
0d6622c
c9ba3ae
 
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
# -*- coding: utf-8 -*-
#
# PROJECT:      CognitiveEDA - The Adaptive Intelligence Engine
#
# DESCRIPTION:  A world-class data discovery platform that transcends static EDA.
#               It intelligently profiles datasets to unlock specialized analysis
#               modules for Time-Series, Text, and Unsupervised Learning, providing
#               a context-aware, deeply insightful user experience.
#
# SETUP:        $ pip install -r requirements.txt
#
# AUTHOR:       An MCP Expert in Data & AI Solutions
# VERSION:      4.0 (Adaptive Intelligence Engine)
# LAST-UPDATE:  2023-10-29 (Major architectural refactor for adaptive modules)

from __future__ import annotations

import warnings
import logging
import os
import sys
import importlib.util
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple

import gradio as gr
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import google.generativeai as genai

# --- Local Adaptive Modules ---
from analysis_modules import analyze_time_series, generate_word_cloud, perform_clustering

# --- Configuration & Setup (Identical to previous versions) ---
logging.basicConfig(level=logging.INFO, format='%(asctime)s - [%(levelname)s] - (%(filename)s:%(lineno)d) - %(message)s')
warnings.filterwarnings('ignore', category=FutureWarning)

class Config:
    APP_TITLE = "πŸš€ CognitiveEDA: The Adaptive Intelligence Engine"
    GEMINI_MODEL = 'gemini-1.5-flash-latest'
    CORR_THRESHOLD = 0.75
    TOP_N_CATEGORIES = 10
    MAX_UI_ROWS = 50000 # Sample large datasets for UI responsiveness

# --- Core Analysis Engine (Mostly unchanged, added context to AI prompt) ---
class DataAnalyzer:
    def __init__(self, df: pd.DataFrame):
        if not isinstance(df, pd.DataFrame): raise TypeError("Input must be a pandas DataFrame.")
        self.df = df
        self._metadata: Optional[Dict[str, Any]] = None
        logging.info(f"DataAnalyzer instantiated with DataFrame of shape: {self.df.shape}")

    @property
    def metadata(self) -> Dict[str, Any]:
        if self._metadata is None: self._metadata = self._extract_metadata()
        return self._metadata

    def _extract_metadata(self) -> Dict[str, Any]:
        # (This method remains the same as v3.2)
        rows, cols = self.df.shape
        numeric_cols = self.df.select_dtypes(include=np.number).columns.tolist()
        categorical_cols = self.df.select_dtypes(include=['object', 'category']).columns.tolist()
        datetime_cols = self.df.select_dtypes(include=['datetime64', 'datetimetz']).columns.tolist()
        text_cols = [col for col in categorical_cols if self.df[col].str.len().mean() > 50]

        high_corr_pairs = []
        if len(numeric_cols) > 1:
            corr_matrix = self.df[numeric_cols].corr().abs()
            upper_tri = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1).astype(bool))
            high_corr_series = upper_tri.stack()
            high_corr_pairs = (high_corr_series[high_corr_series > Config.CORR_THRESHOLD].reset_index().rename(columns={'level_0': 'Feature 1', 'level_1': 'Feature 2', 0: 'Correlation'}).to_dict('records'))

        return {
            'shape': (rows, cols), 'columns': self.df.columns.tolist(),
            'numeric_cols': numeric_cols, 'categorical_cols': categorical_cols,
            'datetime_cols': datetime_cols, 'text_cols': text_cols,
            'memory_usage_mb': f"{self.df.memory_usage(deep=True).sum() / 1e6:.2f}",
            'total_missing': int(self.df.isnull().sum().sum()),
            'data_quality_score': round((self.df.notna().sum().sum() / self.df.size) * 100, 2),
            'high_corr_pairs': high_corr_pairs,
        }

    def get_profiling_tables(self) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
        # (This method remains the same as v3.2)
        ...

    def get_overview_visuals(self) -> Tuple[go.Figure, go.Figure, go.Figure]:
        # (This method remains the same as v3.2)
        ...

    def generate_ai_narrative(self, api_key: str, context: Dict[str, Any]) -> str:
        """Generates a context-aware AI narrative."""
        logging.info(f"Generating AI narrative with context: {context.keys()}")
        meta = self.metadata
        data_snippet_md = self.df.head(5).to_markdown(index=False)
        
        # Dynamically build the context section of the prompt
        context_prompt = "**DATASET CONTEXT:**\n"
        if context.get('is_timeseries'):
            context_prompt += "- **Analysis Mode:** Time-Series. Focus on trends, seasonality, and stationarity.\n"
        if context.get('has_text'):
            context_prompt += "- **Analysis Mode:** Text Analysis. Note potential for NLP tasks like sentiment analysis or topic modeling.\n"
        
        prompt = f"""
        As "Cognitive Analyst," an elite AI data scientist, your task is to generate a comprehensive data discovery report.
        {context_prompt}
        - **Shape:** {meta['shape'][0]} rows, {meta['shape'][1]} columns.
        ... (rest of the prompt from v3.2)
        """
        # (API call logic remains the same)
        ...
        return "AI Narrative Placeholder" # For brevity in this example

# --- UI Creation (create_ui) ---
# Contains all Gradio component definitions and their event listeners
def create_ui():
    """Defines and builds the new adaptive Gradio user interface."""

    with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue"), title=Config.APP_TITLE) as demo:
        # State object to hold the DataAnalyzer instance
        state_analyzer = gr.State()

        # --- Header & Main Controls ---
        gr.Markdown(f"<h1>{Config.APP_TITLE}</h1>")
        gr.Markdown("Upload your data (CSV, Excel) and let the AI build a custom analysis dashboard for you.")
        with gr.Row():
            upload_button = gr.File(label="1. Upload Data File", file_types=[".csv", ".xlsx", ".xls"], scale=3)
            api_key_input = gr.Textbox(label="2. Enter Google Gemini API Key", type="password", scale=2)
            analyze_button = gr.Button("✨ Build My Dashboard", variant="primary", scale=1)

        # --- Tabbed Interface for Analysis Modules ---
        with gr.Tabs():
            # Standard Tabs (Always Visible)
            with gr.Tab("πŸ€– AI Narrative"):
                ai_report_output = gr.Markdown("### Your AI-generated report will appear here...")
                download_report_button = gr.Button("⬇️ Download Full Report", visible=False)
            with gr.Tab("πŸ“‹ Profile"):
                gr.Markdown("### **Detailed Data Profile**")
                profile_missing_df = gr.DataFrame(interactive=False, label="Missing Values")
                profile_numeric_df = gr.DataFrame(interactive=False, label="Numeric Stats")
                profile_categorical_df = gr.DataFrame(interactive=False, label="Categorical Stats")
            with gr.Tab("πŸ“Š Overview Visuals"):
                with gr.Row(): plot_types, plot_missing = gr.Plot(), gr.Plot()
                plot_correlation = gr.Plot()
            
            # Specialized, Initially Hidden Tabs
            with gr.Tab("βŒ› Time-Series Analysis", visible=False) as tab_timeseries:
                gr.Markdown("### **Decompose and Analyze Time-Series Data**")
                with gr.Row():
                    dd_ts_date = gr.Dropdown(label="Select Date/Time Column", interactive=True)
                    dd_ts_value = gr.Dropdown(label="Select Value Column", interactive=True)
                plot_ts_decomp = gr.Plot()
                md_ts_stats = gr.Markdown()
                
            with gr.Tab("πŸ“ Text Analysis", visible=False) as tab_text:
                gr.Markdown("### **Visualize High-Frequency Words**")
                dd_text_col = gr.Dropdown(label="Select Text Column", interactive=True)
                html_word_cloud = gr.HTML()
                
            with gr.Tab("🧩 Clustering (K-Means)", visible=False) as tab_cluster:
                gr.Markdown("### **Discover Latent Groups with K-Means Clustering**")
                with gr.Row():
                    num_clusters = gr.Slider(minimum=2, maximum=10, value=4, step=1, label="Number of Clusters (K)", interactive=True)
                plot_cluster = gr.Plot()
                md_cluster_summary = gr.Markdown()

        # --- Event Listeners ---
        main_outputs = [
            state_analyzer, ai_report_output, download_report_button,
            profile_missing_df, profile_numeric_df, profile_categorical_df,
            plot_types, plot_missing, plot_correlation,
            tab_timeseries, dd_ts_date, dd_ts_value,
            tab_text, dd_text_col,
            tab_cluster, num_clusters
        ]
        analyze_button.click(fn=run_full_analysis, inputs=[upload_button, api_key_input], outputs=main_outputs)

        # Listeners for specialized tabs
        ts_inputs = [state_analyzer, dd_ts_date, dd_ts_value]
        for dd in [dd_ts_date, dd_ts_value]:
            dd.change(fn=lambda a, d, v: analyze_time_series(a.df, d, v), inputs=ts_inputs, outputs=[plot_ts_decomp, md_ts_stats])
        
        dd_text_col.change(fn=lambda a, t: generate_word_cloud(a.df, t), inputs=[state_analyzer, dd_text_col], outputs=html_word_cloud)
        
        cluster_inputs = [state_analyzer, num_clusters]
        num_clusters.change(fn=lambda a, k: perform_clustering(a.df, a.metadata['numeric_cols'], k), inputs=cluster_inputs, outputs=[plot_cluster, md_cluster_summary])

    return demo

# --- Main Application Logic & Orchestration ---
def run_full_analysis(file_obj: gr.File, api_key: str) -> list:
    """The new adaptive analysis orchestrator."""
    if file_obj is None: raise gr.Error("CRITICAL: No file uploaded.")
    if not api_key: raise gr.Error("CRITICAL: Gemini API key is missing.")

    try:
        logging.info(f"Processing uploaded file: {file_obj.name}")
        df = pd.read_csv(file_obj.name) if file_obj.name.endswith('.csv') else pd.read_excel(file_obj.name)

        if len(df) > Config.MAX_UI_ROWS:
            logging.info(f"Large dataset detected ({len(df)} rows). Sampling to {Config.MAX_UI_ROWS} for UI.")
            df_display = df.sample(n=Config.MAX_UI_ROWS, random_state=42)
        else:
            df_display = df

        analyzer = DataAnalyzer(df_display)
        meta = analyzer.metadata
        
        # --- Base Analysis ---
        ai_context = {'is_timeseries': bool(meta['datetime_cols']), 'has_text': bool(meta['text_cols'])}
        # ai_report = analyzer.generate_ai_narrative(api_key, context=ai_context) # Commented out for speed
        ai_report = "AI Narrative generation is ready. Trigger on demand." # Placeholder
        missing_df, num_df, cat_df = analyzer.get_profiling_tables()
        fig_types, fig_missing, fig_corr = analyzer.get_overview_visuals()
        
        # --- Adaptive Module Configuration ---
        show_ts_tab = gr.Tab(visible=bool(meta['datetime_cols']))
        show_text_tab = gr.Tab(visible=bool(meta['text_cols']))
        show_cluster_tab = gr.Tab(visible=len(meta['numeric_cols']) > 1)

        return [
            analyzer, ai_report, gr.Button(visible=True),
            missing_df, num_df, cat_df, fig_types, fig_missing, fig_corr,
            show_ts_tab, gr.Dropdown(choices=meta['datetime_cols']), gr.Dropdown(choices=meta['numeric_cols']),
            show_text_tab, gr.Dropdown(choices=meta['text_cols']),
            show_cluster_tab, gr.Slider(visible=True) # or gr.Number
        ]
    except Exception as e:
        logging.error(f"A critical error occurred: {e}", exc_info=True)
        raise gr.Error(f"Analysis Failed! Error: {str(e)}")

def perform_pre_flight_checks():
    # (Same as v3.2)
    ...

if __name__ == "__main__":
    # perform_pre_flight_checks() # Can be commented out during active dev
    app_instance = create_ui()
    app_instance.launch(debug=True, server_name="0.0.0.0")