Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,115 @@
|
|
1 |
-
#
|
2 |
|
3 |
# -*- coding: utf-8 -*-
|
4 |
#
|
5 |
# PROJECT: CognitiveEDA v5.0 - The QuantumLeap Intelligence Platform
|
6 |
#
|
7 |
-
# DESCRIPTION:
|
8 |
-
#
|
9 |
-
# same Gradio Blocks context.
|
10 |
-
#
|
11 |
-
# SETUP: $ pip install -r requirements.txt
|
12 |
-
#
|
13 |
-
# AUTHOR: An MCP & PhD Expert in Data & AI Solutions
|
14 |
-
# VERSION: 5.1 (Context-Aware Edition: Architectural Fix)
|
15 |
-
# LAST-UPDATE: 2023-10-30 (Corrected Gradio context handling)
|
16 |
|
17 |
-
import warnings
|
18 |
-
import logging
|
19 |
import gradio as gr
|
20 |
-
|
21 |
-
# Import the UI layout and the callback LOGIC functions
|
22 |
-
from ui.layout import create_main_layout
|
23 |
-
from ui import callbacks
|
24 |
from core.config import settings
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
format='%(asctime)s - [%(levelname)s] - (%(filename)s:%(lineno)d) - %(message)s'
|
30 |
-
)
|
31 |
-
warnings.filterwarnings('ignore', category=FutureWarning)
|
32 |
|
|
|
33 |
|
34 |
-
|
|
|
35 |
"""
|
36 |
-
|
37 |
-
|
38 |
-
logging.info(f"Starting {settings.APP_TITLE}")
|
39 |
-
|
40 |
-
# Create the top-level Blocks context. All UI and events will be defined here.
|
41 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo"), title=settings.APP_TITLE) as demo:
|
42 |
-
# 1. Build the UI from the layout module.
|
43 |
-
# This function now only creates the components and returns them.
|
44 |
-
components = create_main_layout()
|
45 |
-
|
46 |
-
# 2. Register all event handlers (callbacks) within the same context.
|
47 |
-
# This is the correct pattern. We attach listeners to the components
|
48 |
-
# that were just created.
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
analysis_complete_event = components["analyze_button"].click(
|
54 |
-
fn=callbacks.run_full_analysis,
|
55 |
-
inputs=[components["upload_button"]],
|
56 |
-
outputs=[components["state_analyzer"]]
|
57 |
-
)
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
# The outputs dictionary keys must match the component dictionary keys
|
64 |
-
outputs=list(components.values())
|
65 |
-
)
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
outputs=[components["plot_cluster"], components["plot_elbow"], components["md_cluster_summary"]]
|
72 |
-
)
|
73 |
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
#
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
main()
|
|
|
1 |
+
# ui/layout.py
|
2 |
|
3 |
# -*- coding: utf-8 -*-
|
4 |
#
|
5 |
# PROJECT: CognitiveEDA v5.0 - The QuantumLeap Intelligence Platform
|
6 |
#
|
7 |
+
# DESCRIPTION: This module defines the entire Gradio UI structure. It is now
|
8 |
+
# corrected to return only the dictionary of components.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
10 |
import gradio as gr
|
|
|
|
|
|
|
|
|
11 |
from core.config import settings
|
12 |
|
13 |
+
def create_main_layout() -> dict:
|
14 |
+
"""
|
15 |
+
Defines the Gradio UI structure and returns a dictionary of its components.
|
|
|
|
|
|
|
16 |
|
17 |
+
This function is designed to be called within a `gr.Blocks` context.
|
18 |
|
19 |
+
Returns:
|
20 |
+
A dictionary mapping component names to their Gradio component objects.
|
21 |
"""
|
22 |
+
# State object to hold the DataAnalyzer instance
|
23 |
+
state_analyzer = gr.State()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# --- Header ---
|
26 |
+
gr.Markdown(f"<h1>{settings.APP_TITLE}</h1>")
|
27 |
+
gr.Markdown("A world-class data discovery platform that provides a complete suite of EDA tools and intelligently unlocks specialized analysis modules.")
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# --- Input Row ---
|
30 |
+
with gr.Row():
|
31 |
+
upload_button = gr.File(label="1. Upload Data File (CSV, Excel)", file_types=[".csv", ".xlsx"], scale=3)
|
32 |
+
analyze_button = gr.Button("β¨ Generate Intelligence Report", variant="primary", scale=1)
|
|
|
|
|
|
|
33 |
|
34 |
+
# --- Main Tabs ---
|
35 |
+
with gr.Tabs():
|
36 |
+
with gr.Tab("π€ AI-Powered Strategy Report", id="tab_ai"):
|
37 |
+
ai_report_output = gr.Markdown("### Your AI-generated report will appear here after analysis...")
|
|
|
|
|
38 |
|
39 |
+
with gr.Tab("π Data Profile", id="tab_profile"):
|
40 |
+
with gr.Accordion("Missing Values Report", open=False):
|
41 |
+
profile_missing_df = gr.DataFrame()
|
42 |
+
with gr.Accordion("Numeric Features Summary", open=True):
|
43 |
+
profile_numeric_df = gr.DataFrame()
|
44 |
+
with gr.Accordion("Categorical Features Summary", open=True):
|
45 |
+
profile_categorical_df = gr.DataFrame()
|
46 |
|
47 |
+
with gr.Tab("π Overview Visuals", id="tab_overview"):
|
48 |
+
with gr.Row():
|
49 |
+
plot_types = gr.Plot()
|
50 |
+
plot_missing = gr.Plot()
|
51 |
+
plot_correlation = gr.Plot()
|
52 |
+
|
53 |
+
with gr.Tab("π¨ Interactive Explorer", id="tab_explorer"):
|
54 |
+
gr.Markdown("### Univariate Analysis")
|
55 |
+
with gr.Row():
|
56 |
+
dd_hist_col = gr.Dropdown(label="Select Column for Histogram", interactive=True)
|
57 |
+
plot_histogram = gr.Plot()
|
58 |
+
gr.Markdown("### Bivariate Analysis")
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column(scale=1):
|
61 |
+
dd_scatter_x = gr.Dropdown(label="X-Axis (Numeric)", interactive=True)
|
62 |
+
dd_scatter_y = gr.Dropdown(label="Y-Axis (Numeric)", interactive=True)
|
63 |
+
dd_scatter_color = gr.Dropdown(label="Color By (Optional)", interactive=True)
|
64 |
+
with gr.Column(scale=2):
|
65 |
+
plot_scatter = gr.Plot()
|
66 |
+
|
67 |
+
with gr.Tab("π§© Clustering (K-Means)", id="tab_cluster", visible=False) as tab_cluster:
|
68 |
+
with gr.Row():
|
69 |
+
with gr.Column(scale=1):
|
70 |
+
num_clusters = gr.Slider(minimum=2, maximum=10, value=3, step=1, label="Number of Clusters (K)", interactive=True)
|
71 |
+
md_cluster_summary = gr.Markdown()
|
72 |
+
with gr.Column(scale=2):
|
73 |
+
plot_cluster = gr.Plot()
|
74 |
+
plot_elbow = gr.Plot()
|
75 |
+
|
76 |
+
# Add other tabs as needed (Time-Series, Text)
|
77 |
+
tab_timeseries = gr.Tab("β Time-Series Analysis", id="tab_timeseries", visible=False)
|
78 |
+
tab_text = gr.Tab("π Text Analysis", id="tab_text", visible=False)
|
79 |
|
80 |
+
# Collect all components into a dictionary for easy access
|
81 |
+
components = {
|
82 |
+
# State
|
83 |
+
"state_analyzer": state_analyzer,
|
84 |
+
# Inputs
|
85 |
+
"upload_button": upload_button,
|
86 |
+
"analyze_button": analyze_button,
|
87 |
+
# AI Tab
|
88 |
+
"ai_report_output": ai_report_output,
|
89 |
+
# Profile Tab
|
90 |
+
"profile_missing_df": profile_missing_df,
|
91 |
+
"profile_numeric_df": profile_numeric_df,
|
92 |
+
"profile_categorical_df": profile_categorical_df,
|
93 |
+
# Overview Tab
|
94 |
+
"plot_types": plot_types,
|
95 |
+
"plot_missing": plot_missing,
|
96 |
+
"plot_correlation": plot_correlation,
|
97 |
+
# Explorer Tab
|
98 |
+
"dd_hist_col": dd_hist_col,
|
99 |
+
"plot_histogram": plot_histogram,
|
100 |
+
"dd_scatter_x": dd_scatter_x,
|
101 |
+
"dd_scatter_y": dd_scatter_y,
|
102 |
+
"dd_scatter_color": dd_scatter_color,
|
103 |
+
"plot_scatter": plot_scatter,
|
104 |
+
# Conditional Tabs
|
105 |
+
"tab_timeseries": tab_timeseries,
|
106 |
+
"tab_text": tab_text,
|
107 |
+
"tab_cluster": tab_cluster,
|
108 |
+
# Clustering Tab Components
|
109 |
+
"num_clusters": num_clusters,
|
110 |
+
"md_cluster_summary": md_cluster_summary,
|
111 |
+
"plot_cluster": plot_cluster,
|
112 |
+
"plot_elbow": plot_elbow,
|
113 |
+
}
|
114 |
|
115 |
+
return components
|
|