mgbam commited on
Commit
05e596d
Β·
verified Β·
1 Parent(s): dc6813f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -28
app.py CHANGED
@@ -2,16 +2,17 @@
2
 
3
  # -*- coding: utf-8 -*-
4
  #
5
- # PROJECT: CognitiveEDA v5.7 - The QuantumLeap Intelligence Platform
6
  #
7
- # DESCRIPTION: Main application entry point. This version adds UI components
8
- # for cluster profiling to the Clustering tab.
 
9
  #
10
  # SETUP: $ pip install -r requirements.txt
11
  #
12
  # AUTHOR: An MCP & PhD Expert in Data & AI Solutions
13
- # VERSION: 5.7 (Cluster Profiling Edition)
14
- # LAST-UPDATE: 2023-10-31 (Integrated cluster persona analysis)
15
 
16
  import warnings
17
  import logging
@@ -38,7 +39,6 @@ def main():
38
  upload_button = gr.File(label="1. Upload Data File", file_types=[".csv", ".xlsx"], scale=3)
39
  analyze_button = gr.Button("✨ Generate Intelligence Report", variant="primary", scale=1)
40
  with gr.Tabs():
41
- # ... (Other tabs remain the same)
42
  with gr.Tab("πŸ€– AI-Powered Strategy Report"):
43
  ai_report_output = gr.Markdown("### Your AI-generated report will appear here...")
44
  with gr.Tab("πŸ“‹ Data Profile"):
@@ -52,24 +52,29 @@ def main():
52
  plot_histogram = gr.Plot()
53
  with gr.Row():
54
  with gr.Column(scale=1):
55
- dd_scatter_x, dd_scatter_y, dd_scatter_color = gr.Dropdown(label="X-Axis", interactive=True), gr.Dropdown(label="Y-Axis", interactive=True), gr.Dropdown(label="Color By", interactive=True)
56
- with gr.Column(scale=2): plot_scatter = gr.Plot()
 
 
 
57
 
58
- # --- MODIFIED CLUSTERING TAB ---
59
  with gr.Tab("🧩 Clustering (K-Means)", visible=False) as tab_cluster:
 
 
60
  with gr.Row():
61
- with gr.Column(scale=1):
62
- num_clusters = gr.Slider(minimum=2, maximum=10, value=5, step=1, label="Number of Clusters (K)", interactive=True)
63
- md_cluster_summary = gr.Markdown("Methodology summary will appear here.")
64
- with gr.Column(scale=2):
65
- plot_cluster = gr.Plot(label="PCA Visualization")
66
 
67
  gr.Markdown("---")
68
- gr.Markdown("## Cluster Profile Analysis")
69
  with gr.Row():
70
  with gr.Column(scale=1):
 
71
  md_cluster_personas = gr.Markdown("Detailed cluster personas will appear here...")
72
  with gr.Column(scale=2):
 
73
  plot_cluster_profile = gr.Plot(label="Cluster Profile Visualization")
74
 
75
  gr.Markdown("---")
@@ -78,14 +83,16 @@ def main():
78
 
79
  tab_timeseries, tab_text = gr.Tab("βŒ› Time-Series", visible=False), gr.Tab("πŸ“ Text", visible=False)
80
 
 
81
  main_outputs = [
82
  ai_report_output, profile_missing_df, profile_numeric_df, profile_categorical_df,
83
  plot_types, plot_missing, plot_correlation,
84
  dd_hist_col, dd_scatter_x, dd_scatter_y, dd_scatter_color,
85
- tab_timeseries, tab_text, tab_cluster
 
86
  ]
87
 
88
- # 2. REGISTER EVENT HANDLERS
89
  analysis_complete_event = analyze_button.click(
90
  fn=callbacks.run_initial_analysis,
91
  inputs=[upload_button],
@@ -102,18 +109,23 @@ def main():
102
  for dropdown in [dd_scatter_x, dd_scatter_y, dd_scatter_color]:
103
  dropdown.change(fn=callbacks.create_scatterplot, inputs=scatter_inputs, outputs=[plot_scatter])
104
 
105
- # --- MODIFIED CLUSTERING CALLBACK WIRING ---
106
- num_clusters.change(
107
- fn=callbacks.update_clustering,
108
- inputs=[state_analyzer, num_clusters],
109
- outputs=[
110
- plot_cluster,
111
- plot_elbow,
112
- md_cluster_summary,
113
- md_cluster_personas,
114
- plot_cluster_profile
115
- ]
116
  )
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  demo.launch(debug=False, server_name="0.0.0.0")
119
 
 
2
 
3
  # -*- coding: utf-8 -*-
4
  #
5
+ # PROJECT: CognitiveEDA v5.9 - The QuantumLeap Intelligence Platform
6
  #
7
+ # DESCRIPTION: Main application entry point. This version implements a fully
8
+ # generic, data-agnostic stratification system, allowing users
9
+ # to dynamically filter and analyze any high-cardinality feature.
10
  #
11
  # SETUP: $ pip install -r requirements.txt
12
  #
13
  # AUTHOR: An MCP & PhD Expert in Data & AI Solutions
14
+ # VERSION: 5.9 (Generic Stratification Engine)
15
+ # LAST-UPDATE: 2023-11-01 (Abstracted stratification controls)
16
 
17
  import warnings
18
  import logging
 
39
  upload_button = gr.File(label="1. Upload Data File", file_types=[".csv", ".xlsx"], scale=3)
40
  analyze_button = gr.Button("✨ Generate Intelligence Report", variant="primary", scale=1)
41
  with gr.Tabs():
 
42
  with gr.Tab("πŸ€– AI-Powered Strategy Report"):
43
  ai_report_output = gr.Markdown("### Your AI-generated report will appear here...")
44
  with gr.Tab("πŸ“‹ Data Profile"):
 
52
  plot_histogram = gr.Plot()
53
  with gr.Row():
54
  with gr.Column(scale=1):
55
+ dd_scatter_x = gr.Dropdown(label="X-Axis", interactive=True)
56
+ dd_scatter_y = gr.Dropdown(label="Y-Axis", interactive=True)
57
+ dd_scatter_color = gr.Dropdown(label="Color By", interactive=True)
58
+ with gr.Column(scale=2):
59
+ plot_scatter = gr.Plot()
60
 
61
+ # --- GENERALIZED CLUSTERING TAB ---
62
  with gr.Tab("🧩 Clustering (K-Means)", visible=False) as tab_cluster:
63
+ gr.Markdown("### Stratified Analysis Control Panel")
64
+ gr.Markdown("Use this to control for a dominant categorical variable. First, select a feature to stratify by (e.g., 'Region', 'Product_Category'), then select a specific value to analyze.")
65
  with gr.Row():
66
+ dd_stratify_by_col = gr.Dropdown(label="1. Stratify By Feature", interactive=True, scale=2)
67
+ dd_stratify_by_value = gr.Dropdown(label="2. Filter By Value", interactive=True, scale=2)
68
+ num_clusters = gr.Slider(minimum=2, maximum=10, value=4, step=1, label="Number of Clusters (K)", interactive=True, scale=1)
 
 
69
 
70
  gr.Markdown("---")
71
+ gr.Markdown("## Segmentation Visualization & Profile")
72
  with gr.Row():
73
  with gr.Column(scale=1):
74
+ md_cluster_summary = gr.Markdown("Methodology summary will appear here.")
75
  md_cluster_personas = gr.Markdown("Detailed cluster personas will appear here...")
76
  with gr.Column(scale=2):
77
+ plot_cluster = gr.Plot(label="PCA Visualization")
78
  plot_cluster_profile = gr.Plot(label="Cluster Profile Visualization")
79
 
80
  gr.Markdown("---")
 
83
 
84
  tab_timeseries, tab_text = gr.Tab("βŒ› Time-Series", visible=False), gr.Tab("πŸ“ Text", visible=False)
85
 
86
+ # 2. DEFINE OUTPUT LISTS
87
  main_outputs = [
88
  ai_report_output, profile_missing_df, profile_numeric_df, profile_categorical_df,
89
  plot_types, plot_missing, plot_correlation,
90
  dd_hist_col, dd_scatter_x, dd_scatter_y, dd_scatter_color,
91
+ tab_timeseries, tab_text, tab_cluster,
92
+ dd_stratify_by_col
93
  ]
94
 
95
+ # 3. REGISTER EVENT HANDLERS
96
  analysis_complete_event = analyze_button.click(
97
  fn=callbacks.run_initial_analysis,
98
  inputs=[upload_button],
 
109
  for dropdown in [dd_scatter_x, dd_scatter_y, dd_scatter_color]:
110
  dropdown.change(fn=callbacks.create_scatterplot, inputs=scatter_inputs, outputs=[plot_scatter])
111
 
112
+ # Chained callback for the stratification dropdowns
113
+ dd_stratify_by_col.change(
114
+ fn=callbacks.update_filter_dropdown,
115
+ inputs=[state_analyzer, dd_stratify_by_col],
116
+ outputs=[dd_stratify_by_value]
 
 
 
 
 
 
117
  )
118
+
119
+ # Clustering callback now listens to changes on ALL THREE controls
120
+ cluster_inputs = [state_analyzer, dd_stratify_by_col, dd_stratify_by_value, num_clusters]
121
+ cluster_outputs = [plot_cluster, plot_elbow, md_cluster_summary, md_cluster_personas, plot_cluster_profile]
122
+
123
+ for control in [dd_stratify_by_col, dd_stratify_by_value, num_clusters]:
124
+ control.change(
125
+ fn=callbacks.update_stratified_clustering,
126
+ inputs=cluster_inputs,
127
+ outputs=cluster_outputs
128
+ )
129
 
130
  demo.launch(debug=False, server_name="0.0.0.0")
131