File size: 30,368 Bytes
c3ab38e
 
 
 
 
 
 
 
ddeba5b
 
c3ab38e
 
 
 
 
 
 
 
 
ddeba5b
c3ab38e
 
 
 
e7aa85e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddeba5b
e7aa85e
ddeba5b
c3ab38e
e7aa85e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
e7aa85e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddeba5b
e7aa85e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
e7aa85e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddeba5b
 
 
 
 
c3ab38e
ddeba5b
 
 
 
 
 
c3ab38e
ddeba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
ddeba5b
 
c3ab38e
ddeba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
e7aa85e
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
 
e7aa85e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddeba5b
e7aa85e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
 
ddeba5b
 
c3ab38e
 
 
ddeba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
 
ddeba5b
c3ab38e
ddeba5b
c3ab38e
ddeba5b
 
 
 
 
 
 
c3ab38e
ddeba5b
 
 
c3ab38e
ddeba5b
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
 
ddeba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
ddeba5b
c3ab38e
 
 
ddeba5b
 
c3ab38e
ddeba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
ddeba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
ddeba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ab38e
ddeba5b
 
 
 
 
 
 
c3ab38e
 
 
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
import streamlit as st
import pandas as pd
from typing import Dict, List, Optional, Any
from pydantic import BaseModel, Field
import base64
import io
import matplotlib.pyplot as plt
import seaborn as sns
from abc import ABC, abstractmethod
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.stattools import adfuller
from langchain.prompts import PromptTemplate
from groq import Groq
import os
import numpy as np
from scipy.stats import ttest_ind, f_oneway
import json

# Initialize Groq Client
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

# ---------------------- Base Classes and Schemas ---------------------------
class ResearchInput(BaseModel):
    """Base schema for research tool inputs"""
    data_key: str = Field(..., description="Session state key containing DataFrame")
    columns: Optional[List[str]] = Field(None, description="List of columns to analyze")

class TemporalAnalysisInput(ResearchInput):
    """Schema for temporal analysis"""
    time_col: str = Field(..., description="Name of timestamp column")
    value_col: str = Field(..., description="Name of value column to analyze")

class HypothesisInput(ResearchInput):
    """Schema for hypothesis testing"""
    group_col: str = Field(..., description="Categorical column defining groups")
    value_col: str = Field(..., description="Numerical column to compare")

class ModelTrainingInput(ResearchInput):
    """Schema for model training"""
    target_col: str = Field(..., description="Name of target column")

class DataAnalyzer(ABC):
    """Abstract base class for data analysis modules"""
    @abstractmethod
    def invoke(self, **kwargs) -> Dict[str, Any]:
        pass

# ---------------------- Concrete Analyzer Implementations ---------------------------
class AdvancedEDA(DataAnalyzer):
    """Comprehensive Exploratory Data Analysis"""
    def invoke(self, data: pd.DataFrame, **kwargs) -> Dict[str, Any]:
        try:
            analysis = {
                "dimensionality": {
                    "rows": len(data),
                    "columns": list(data.columns),
                    "memory_usage": f"{data.memory_usage().sum() / 1e6:.2f} MB"
                },
                "statistical_profile": data.describe(percentiles=[.25, .5, .75]).to_dict(),
                "temporal_analysis": {
                    "date_ranges": {
                        col: {
                            "min": data[col].min(),
                            "max": data[col].max()
                        } for col in data.select_dtypes(include='datetime').columns
                    }
                },
                "data_quality": {
                    "missing_values": data.isnull().sum().to_dict(),
                    "duplicates": data.duplicated().sum(),
                    "cardinality": {
                        col: data[col].nunique() for col in data.columns
                    }
                }
            }
            return analysis
        except Exception as e:
            return {"error": f"EDA Failed: {str(e)}"}

class DistributionVisualizer(DataAnalyzer):
    """Distribution visualizations"""
    def invoke(self, data: pd.DataFrame, columns: List[str], **kwargs) -> str:
      try:
          plt.figure(figsize=(12, 6))
          for i, col in enumerate(columns, 1):
              plt.subplot(1, len(columns), i)
              sns.histplot(data[col], kde=True, stat="density")
              plt.title(f'Distribution of {col}', fontsize=10)
              plt.xticks(fontsize=8)
              plt.yticks(fontsize=8)
          plt.tight_layout()
        
          buf = io.BytesIO()
          plt.savefig(buf, format='png', dpi=300, bbox_inches='tight')
          plt.close()
          return base64.b64encode(buf.getvalue()).decode()
      except Exception as e:
          return f"Visualization Error: {str(e)}"

class TemporalAnalyzer(DataAnalyzer):
    """Time series analysis"""
    def invoke(self, data: pd.DataFrame, time_col: str, value_col: str, **kwargs) -> Dict[str, Any]:
        try:
            ts_data = data.set_index(pd.to_datetime(data[time_col]))[value_col]
            decomposition = seasonal_decompose(ts_data, period=365)
            
            plt.figure(figsize=(12, 8))
            decomposition.plot()
            plt.tight_layout()
            
            buf = io.BytesIO()
            plt.savefig(buf, format='png')
            plt.close()
            plot_data = base64.b64encode(buf.getvalue()).decode()
            
            return {
                "trend_statistics": {
                    "stationarity": adfuller(ts_data)[1],
                    "seasonality_strength": max(decomposition.seasonal)
                },
                "visualization": plot_data
            }
        except Exception as e:
            return {"error": f"Temporal Analysis Failed: {str(e)}"}

class HypothesisTester(DataAnalyzer):
    """Statistical hypothesis testing"""
    def invoke(self, data: pd.DataFrame, group_col: str, value_col: str, **kwargs) -> Dict[str, Any]:
      try:
        groups = data[group_col].unique()
      
        if len(groups) < 2:
            return {"error": "Insufficient groups for comparison"}
        
        if len(groups) == 2:
            group_data = [data[data[group_col] == g][value_col] for g in groups]
            stat, p = ttest_ind(*group_data)
            test_type = "Independent t-test"
        else:
          group_data = [data[data[group_col] == g][value_col] for g in groups]
          stat, p = f_oneway(*group_data)
          test_type = "ANOVA"
        
        return {
            "test_type": test_type,
            "test_statistic": stat,
            "p_value": p,
            "effect_size": {
              "cohens_d": abs(group_data[0].mean() - group_data[1].mean())/np.sqrt(
                    (group_data[0].var() + group_data[1].var())/2
              ) if len(groups) == 2 else None
            },
            "interpretation": self.interpret_p_value(p)
        }
      except Exception as e:
        return {"error": f"Hypothesis Testing Failed: {str(e)}"}

    def interpret_p_value(self, p: float) -> str:
      if p < 0.001: return "Very strong evidence against H0"
      elif p < 0.01: return "Strong evidence against H0"
      elif p < 0.05: return "Evidence against H0"
      elif p < 0.1: return "Weak evidence against H0"
      else: return "No significant evidence against H0"

class LogisticRegressionTrainer(DataAnalyzer):
    """Logistic Regression Model Trainer"""
    def invoke(self, data: pd.DataFrame, target_col: str, columns: List[str], **kwargs) -> Dict[str, Any]:
      try:
        X = data[columns]
        y = data[target_col]
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
        model = LogisticRegression(max_iter=1000)
        model.fit(X_train, y_train)
        y_pred = model.predict(X_test)
        accuracy = accuracy_score(y_test, y_pred)
        return {
          "model_type": "Logistic Regression",
           "accuracy": accuracy,
           "model_params": model.get_params()
         }
      except Exception as e:
         return {"error": f"Logistic Regression Model Error: {str(e)}"}

# ---------------------- Groq Research Agent ---------------------------

class GroqResearcher:
    """Advanced AI Research Engine using Groq"""
    def __init__(self, model_name="mixtral-8x7b-32768"):
        self.model_name = model_name
        self.system_template = """You are a senior data scientist at a research institution. 
        Analyze this dataset with rigorous statistical methods and provide academic-quality insights:
        {dataset_info}
        
        User Question: {query}
        
        Required Format:
        - Executive Summary (1 paragraph)
        - Methodology (bullet points)
        - Key Findings (numbered list)
        - Limitations
        - Recommended Next Steps"""

    def research(self, query: str, data: pd.DataFrame) -> str:
        """Conduct academic-level analysis using Groq"""
        try:
            dataset_info = f"""
            Dataset Dimensions: {data.shape}
            Variables: {', '.join(data.columns)}
            Temporal Coverage: {data.select_dtypes(include='datetime').columns.tolist()}
            Missing Values: {data.isnull().sum().to_dict()}
            """
            
            prompt = PromptTemplate.from_template(self.system_template).format(
                dataset_info=dataset_info,
                query=query
            )
            
            completion = client.chat.completions.create(
                messages=[
                    {"role": "system", "content": "You are a research AI assistant"},
                    {"role": "user", "content": prompt}
                ],
                model=self.model_name,
                temperature=0.2,
                max_tokens=4096,
                stream=False
            )
            
            return completion.choices[0].message.content
        
        except Exception as e:
            return f"Research Error: {str(e)}"
# ---------------------- Business Logic Layer ---------------------------
class BusinessRule(BaseModel):
  name: str
  condition: str
  action: str

class BusinessRulesEngine():
    def __init__(self):
       self.rules: Dict[str, BusinessRule] = {}
    
    def add_rule(self, rule: BusinessRule):
        self.rules[rule.name] = rule

    def execute_rules(self, data: pd.DataFrame):
        results = {}
        for rule_name, rule in self.rules.items():
            try:
              if eval(rule.condition, {}, {"df":data}):
                results[rule_name] = {"rule_matched": True, "action": rule.action}
              else:
                results[rule_name] = {"rule_matched": False, "action": None}
            except Exception as e:
                 results[rule_name] = {"rule_matched": False, "error": str(e)}
        return results

class KPI(BaseModel):
  name: str
  calculation: str
  threshold: Optional[float] = None
  
class KPIMonitoring():
  def __init__(self):
    self.kpis : Dict[str, KPI] = {}
    
  def add_kpi(self, kpi:KPI):
    self.kpis[kpi.name] = kpi
    
  def calculate_kpis(self, data: pd.DataFrame):
      results = {}
      for kpi_name, kpi in self.kpis.items():
          try:
            results[kpi_name] = eval(kpi.calculation, {}, {"df": data})
          except Exception as e:
              results[kpi_name] = {"error": str(e)}
      return results

class ForecastingEngine(ABC):
    @abstractmethod
    def predict(self, data: pd.DataFrame, **kwargs) -> pd.DataFrame:
         pass

class SimpleForecasting(ForecastingEngine):
  def predict(self, data: pd.DataFrame, period: int = 7, **kwargs) -> pd.DataFrame:
      #Placeholder for actual forecasting
      return pd.DataFrame({"forecast":[f"Forecast for the next {period} days"]})
# ---------------------- Insights and Reporting Layer ---------------------------
class AutomatedInsights():
    def __init__(self):
      self.analyses : Dict[str, DataAnalyzer] = {
        "EDA": AdvancedEDA(),
        "temporal": TemporalAnalyzer(),
        "distribution": DistributionVisualizer(),
        "hypothesis": HypothesisTester(),
        "model": LogisticRegressionTrainer()
      }

    def generate_insights(self, data: pd.DataFrame, analysis_names: List[str], **kwargs):
       results = {}
       for name in analysis_names:
           if name in self.analyses:
             analyzer = self.analyses[name]
             results[name] = analyzer.invoke(data=data, **kwargs)
           else:
               results[name] = {"error": "Analysis not found"}
       return results

class Dashboard():
    def __init__(self):
        self.layout: Dict[str,str] = {}
    
    def add_visualisation(self, vis_name: str, vis_type: str):
        self.layout[vis_name] = vis_type
  
    def display_dashboard(self, data_dict: Dict[str,pd.DataFrame]):
      st.header("Dashboard")
      for vis_name, vis_type in self.layout.items():
          st.subheader(vis_name)
          if vis_type == "table":
              if vis_name in data_dict:
                st.table(data_dict[vis_name])
              else:
                st.write("Data Not Found")
          elif vis_type == "plot":
            if vis_name in data_dict:
              df = data_dict[vis_name]
              if len(df.columns) > 1:
                fig = plt.figure()
                sns.lineplot(data=df)
                st.pyplot(fig)
              else:
                 st.write("Please have more than 1 column")
            else:
                 st.write("Data not found")
class AutomatedReports():
  def __init__(self):
     self.report_definition: Dict[str,str] = {}

  def create_report_definition(self, report_name: str, definition: str):
       self.report_definition[report_name] = definition
  
  def generate_report(self, report_name: str, data:Dict[str, pd.DataFrame]):
       if report_name not in self.report_definition:
          return {"error":"Report name not found"}
       st.header(f"Report : {report_name}")
       st.write(f"Report Definition: {self.report_definition[report_name]}")
       for df_name, df in data.items():
         st.subheader(f"Data: {df_name}")
         st.table(df)

# ---------------------- Data Acquisition Layer ---------------------------
class DataSource(ABC):
    """Base class for data sources."""
    @abstractmethod
    def connect(self) -> None:
        """Connect to the data source."""
        pass
    
    @abstractmethod
    def fetch_data(self, query: str, **kwargs) -> pd.DataFrame:
         """Fetch the data based on a specific query."""
         pass


class CSVDataSource(DataSource):
    """Data source for CSV files."""
    def __init__(self, file_path: str):
        self.file_path = file_path
        self.data: Optional[pd.DataFrame] = None
    
    def connect(self):
        self.data = pd.read_csv(self.file_path)
    
    def fetch_data(self, query: str = None, **kwargs) -> pd.DataFrame:
      if self.data is None:
        raise Exception("No connection is made, call connect()")
      return self.data
    
class DatabaseSource(DataSource):
   def __init__(self, connection_string: str, database_type: str):
        self.connection_string = connection_string
        self.database_type = database_type
        self.connection = None
    
   def connect(self):
     if self.database_type.lower() == "sql":
        #Placeholder for the actual database connection
        self.connection = "Connected to SQL Database"
     else:
        raise Exception(f"Database type '{self.database_type}' is not supported")
      
   def fetch_data(self, query: str, **kwargs) -> pd.DataFrame:
        if self.connection is None:
            raise Exception("No connection is made, call connect()")
        #Placeholder for the data fetching
        return pd.DataFrame({"result":[f"Fetched data based on query: {query}"]})
     

class DataIngestion:
    def __init__(self):
      self.sources : Dict[str, DataSource] = {}
    
    def add_source(self, source_name: str, source: DataSource):
         self.sources[source_name] = source
    
    def ingest_data(self, source_name: str, query: str = None, **kwargs) -> pd.DataFrame:
       if source_name not in self.sources:
         raise Exception(f"Source '{source_name}' not found")
       source = self.sources[source_name]
       source.connect()
       return source.fetch_data(query, **kwargs)
       
class DataModel(BaseModel):
  name : str
  kpis : List[str] = Field(default_factory=list)
  dimensions : List[str] = Field(default_factory=list)
  custom_calculations : Optional[Dict[str, str]] = None
  relations: Optional[Dict[str,str]] = None #Example {table1: table2}
  
  def to_json(self):
    return json.dumps(self.dict())
  
  @staticmethod
  def from_json(json_str):
    return DataModel(**json.loads(json_str))
  
class DataModelling():
    def __init__(self):
      self.models : Dict[str, DataModel] = {}
    
    def add_model(self, model:DataModel):
       self.models[model.name] = model
    
    def get_model(self, model_name: str) -> DataModel:
      if model_name not in self.models:
          raise Exception(f"Model '{model_name}' not found")
      return self.models[model_name]
# ---------------------- Main Streamlit Application ---------------------------
def main():
    st.set_page_config(page_title="AI BI Automation Platform", layout="wide")
    st.title("πŸš€ AI-Powered Business Intelligence Automation Platform")

    # Session State
    if 'data' not in st.session_state:
        st.session_state.data = {}  # store pd.DataFrame under a name
    if 'data_ingestion' not in st.session_state:
      st.session_state.data_ingestion = DataIngestion()
    if 'data_modelling' not in st.session_state:
      st.session_state.data_modelling = DataModelling()
    if 'business_rules' not in st.session_state:
      st.session_state.business_rules = BusinessRulesEngine()
    if 'kpi_monitoring' not in st.session_state:
       st.session_state.kpi_monitoring = KPIMonitoring()
    if 'forecasting_engine' not in st.session_state:
       st.session_state.forecasting_engine = SimpleForecasting()
    if 'automated_insights' not in st.session_state:
      st.session_state.automated_insights = AutomatedInsights()
    if 'dashboard' not in st.session_state:
       st.session_state.dashboard = Dashboard()
    if 'automated_reports' not in st.session_state:
      st.session_state.automated_reports = AutomatedReports()
    if 'researcher' not in st.session_state:
        st.session_state.researcher = GroqResearcher()
  

    # Sidebar for Data Management
    with st.sidebar:
        st.header("βš™οΈ Data Management")
        data_source_selection = st.selectbox("Select Data Source Type",["CSV","SQL Database"])
        if data_source_selection == "CSV":
           uploaded_file = st.file_uploader("Upload research dataset (CSV)", type=["csv"])
           if uploaded_file:
              source_name = st.text_input("Data Source Name")
              if source_name:
                try:
                    csv_source = CSVDataSource(file_path=uploaded_file)
                    st.session_state.data_ingestion.add_source(source_name,csv_source)
                    st.success(f"Uploaded {uploaded_file.name}")
                except Exception as e:
                  st.error(f"Error loading dataset: {e}")
        elif data_source_selection == "SQL Database":
          conn_str = st.text_input("Enter connection string for SQL DB")
          if conn_str:
            source_name = st.text_input("Data Source Name")
            if source_name:
              try:
                 sql_source = DatabaseSource(connection_string=conn_str, database_type="sql")
                 st.session_state.data_ingestion.add_source(source_name, sql_source)
                 st.success(f"Added SQL DB Source {source_name}")
              except Exception as e:
                 st.error(f"Error loading database source {e}")


        if st.button("Ingest Data"):
           if st.session_state.data_ingestion.sources:
               source_name_to_fetch = st.selectbox("Select Data Source to Ingest", list(st.session_state.data_ingestion.sources.keys()))
               query = st.text_area("Optional Query to Fetch data")
               if source_name_to_fetch:
                    with st.spinner("Ingesting data..."):
                      try:
                          data = st.session_state.data_ingestion.ingest_data(source_name_to_fetch, query)
                          st.session_state.data[source_name_to_fetch] = data
                          st.success(f"Ingested data from {source_name_to_fetch}")
                      except Exception as e:
                        st.error(f"Ingestion failed: {e}")
           else:
             st.error("No data source added, please add data source")

    if st.session_state.data:
        col1, col2 = st.columns([1, 3])

        with col1:
            st.subheader("Dataset Metadata")
            
            data_source_keys = list(st.session_state.data.keys())
            selected_data_key = st.selectbox("Select Dataset", data_source_keys)
            
            if selected_data_key:
                data = st.session_state.data[selected_data_key]
                st.json({
                    "Variables": list(data.columns),
                    "Time Range": {
                        col: {
                            "min": data[col].min(),
                            "max": data[col].max()
                        } for col in data.select_dtypes(include='datetime').columns
                    },
                    "Size": f"{data.memory_usage().sum() / 1e6:.2f} MB"
                })
        with col2:
            analysis_tab, business_logic_tab, insights_tab, reports_tab, custom_research_tab = st.tabs([
              "Data Analysis",
              "Business Logic",
               "Insights",
               "Reports",
              "Custom Research"
              ])

            with analysis_tab:
                if selected_data_key:
                    analysis_type = st.selectbox("Select Analysis Mode", [
                     "Exploratory Data Analysis",
                     "Temporal Pattern Analysis",
                      "Comparative Statistics",
                      "Distribution Analysis",
                       "Train Logistic Regression Model"
                    ])
                    data = st.session_state.data[selected_data_key]
                    if analysis_type == "Exploratory Data Analysis":
                        analyzer = AdvancedEDA()
                        eda_result = analyzer.invoke(data=data)
                        st.subheader("Data Quality Report")
                        st.json(eda_result)

                    elif analysis_type == "Temporal Pattern Analysis":
                        time_col = st.selectbox("Temporal Variable",
                            data.select_dtypes(include='datetime').columns)
                        value_col = st.selectbox("Analysis Variable",
                            data.select_dtypes(include=np.number).columns)

                        if time_col and value_col:
                          analyzer = TemporalAnalyzer()
                          result = analyzer.invoke(data=data, time_col=time_col, value_col=value_col)
                          if "visualization" in result:
                              st.image(f"data:image/png;base64,{result['visualization']}")
                          st.json(result)

                    elif analysis_type == "Comparative Statistics":
                        group_col = st.selectbox("Grouping Variable",
                             data.select_dtypes(include='category').columns)
                        value_col = st.selectbox("Metric Variable",
                             data.select_dtypes(include=np.number).columns)

                        if group_col and value_col:
                          analyzer = HypothesisTester()
                          result = analyzer.invoke(data=data, group_col=group_col, value_col=value_col)
                          st.subheader("Statistical Test Results")
                          st.json(result)

                    elif analysis_type == "Distribution Analysis":
                        num_cols = data.select_dtypes(include=np.number).columns.tolist()
                        selected_cols = st.multiselect("Select Variables", num_cols)
                        if selected_cols:
                           analyzer = DistributionVisualizer()
                           img_data = analyzer.invoke(data=data, columns=selected_cols)
                           st.image(f"data:image/png;base64,{img_data}")
                       
                    elif analysis_type == "Train Logistic Regression Model":
                       num_cols = data.select_dtypes(include=np.number).columns.tolist()
                       target_col = st.selectbox("Select Target Variable",
                                                   data.columns.tolist())
                       selected_cols = st.multiselect("Select Feature Variables", num_cols)
                       if selected_cols and target_col:
                         analyzer = LogisticRegressionTrainer()
                         result = analyzer.invoke(data=data, target_col=target_col, columns=selected_cols)
                         st.subheader("Logistic Regression Model Results")
                         st.json(result)
            with business_logic_tab:
              st.header("Business Logic")
              st.subheader("Data Modelling")
              model_name = st.text_input("Enter the name of the model")
              
              if model_name:
                kpis = st.text_input("Enter KPIs (comma-separated)")
                dimensions = st.text_input("Enter Dimensions (comma-separated)")
                custom_calculations = st.text_area("Custom calculations (JSON format), use {'df': DataFrame}")
                relations = st.text_area("Relations (JSON format), use {'table1': 'table2'}")
                if st.button("Add Data Model"):
                 try:
                  custom_calculations_dict = None if not custom_calculations else json.loads(custom_calculations)
                  relations_dict = None if not relations else json.loads(relations)
                  model = DataModel(name=model_name,
                                     kpis= [kpi.strip() for kpi in kpis.split(',')] if kpis else [],
                                   dimensions=[dim.strip() for dim in dimensions.split(',')] if dimensions else [],
                                   custom_calculations= custom_calculations_dict,
                                   relations = relations_dict)
                  st.session_state.data_modelling.add_model(model)
                  st.success(f"Added data model {model_name}")
                 except Exception as e:
                  st.error(f"Error creating data model: {e}")

              st.subheader("Business Rules")
              rule_name = st.text_input("Enter Rule Name")
              condition = st.text_area("Enter Rule Condition (use 'df' for data frame), Example df['sales'] > 100")
              action = st.text_area("Enter Action to be Taken on Rule Match")
              if st.button("Add Business Rule"):
                try:
                 rule = BusinessRule(name=rule_name, condition=condition, action=action)
                 st.session_state.business_rules.add_rule(rule)
                 st.success("Added Business Rule")
                except Exception as e:
                  st.error(f"Error in rule definition: {e}")
                
              st.subheader("KPI Definition")
              kpi_name = st.text_input("Enter KPI name")
              kpi_calculation = st.text_area("Enter KPI calculation (use 'df' for data frame), Example df['revenue'].sum()")
              threshold = st.text_input("Enter Threshold for KPI")
              if st.button("Add KPI"):
                  try:
                    threshold_value = float(threshold) if threshold else None
                    kpi = KPI(name=kpi_name, calculation=kpi_calculation, threshold=threshold_value)
                    st.session_state.kpi_monitoring.add_kpi(kpi)
                    st.success(f"Added KPI {kpi_name}")
                  except Exception as e:
                      st.error(f"Error creating KPI: {e}")
              if selected_data_key:
                   data = st.session_state.data[selected_data_key]
                   if st.button("Execute Business Rules"):
                      with st.spinner("Executing Business Rules.."):
                        result = st.session_state.business_rules.execute_rules(data)
                        st.json(result)
                   if st.button("Calculate KPIs"):
                       with st.spinner("Calculating KPIs..."):
                         result = st.session_state.kpi_monitoring.calculate_kpis(data)
                         st.json(result)

            with insights_tab:
              if selected_data_key:
                  data = st.session_state.data[selected_data_key]
                  available_analysis = ["EDA", "temporal", "distribution", "hypothesis", "model"]
                  selected_analysis = st.multiselect("Select Analysis", available_analysis)
                  if st.button("Generate Automated Insights"):
                    with st.spinner("Generating Insights"):
                        results = st.session_state.automated_insights.generate_insights(data, analysis_names=selected_analysis)
                        st.json(results)
            
            with reports_tab:
                 st.header("Reports")
                 report_name = st.text_input("Report Name")
                 report_def = st.text_area("Report definition")
                 if st.button("Create Report Definition"):
                   st.session_state.automated_reports.create_report_definition(report_name, report_def)
                   st.success("Report definition created")
                 if selected_data_key:
                   data = st.session_state.data
                   if st.button("Generate Report"):
                     with st.spinner("Generating Report..."):
                       report = st.session_state.automated_reports.generate_report(report_name, data)

            with custom_research_tab:
                research_query = st.text_area("Enter Research Question:", height=150,
                    placeholder="E.g., 'What factors are most predictive of X outcome?'")

                if st.button("Execute Custom Research"):
                    with st.spinner("Conducting rigorous analysis..."):
                        if selected_data_key:
                            data = st.session_state.data[selected_data_key]
                            result = st.session_state.researcher.research(
                                research_query, data
                            )
                            st.markdown("## Research Findings")
                            st.markdown(result)

if __name__ == "__main__":
    main()