Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,162 +2,98 @@
|
|
| 2 |
|
| 3 |
# -*- coding: utf-8 -*-
|
| 4 |
#
|
| 5 |
-
# PROJECT: CognitiveEDA v5.
|
| 6 |
#
|
| 7 |
-
# DESCRIPTION: Main application entry point. This definitive version
|
| 8 |
-
#
|
| 9 |
-
#
|
| 10 |
-
# previous startup errors.
|
| 11 |
#
|
| 12 |
# SETUP: $ pip install -r requirements.txt
|
| 13 |
#
|
| 14 |
# AUTHOR: An MCP & PhD Expert in Data & AI Solutions
|
| 15 |
-
# VERSION: 5.
|
| 16 |
-
# LAST-UPDATE: 2023-10-30 (
|
| 17 |
|
| 18 |
import warnings
|
| 19 |
import logging
|
| 20 |
import gradio as gr
|
| 21 |
|
| 22 |
-
# The callback LOGIC is still neatly separated
|
| 23 |
from ui import callbacks
|
| 24 |
from core.config import settings
|
| 25 |
|
| 26 |
-
# --- Configuration & Setup ---
|
| 27 |
logging.basicConfig(
|
| 28 |
level=logging.INFO,
|
| 29 |
format='%(asctime)s - [%(levelname)s] - (%(filename)s:%(lineno)d) - %(message)s'
|
| 30 |
)
|
| 31 |
warnings.filterwarnings('ignore', category=FutureWarning)
|
| 32 |
|
| 33 |
-
|
| 34 |
def main():
|
| 35 |
-
"""
|
| 36 |
-
Primary function to build, wire up, and launch the Gradio application.
|
| 37 |
-
"""
|
| 38 |
logging.info(f"Starting {settings.APP_TITLE}")
|
| 39 |
|
| 40 |
-
# The 'with' block creates the Gradio 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 |
|
| 43 |
-
#
|
| 44 |
-
# 1. DEFINE THE UI LAYOUT DIRECTLY WITHIN THE MAIN SCRIPT
|
| 45 |
-
# This is the most robust pattern and resolves all context-related errors.
|
| 46 |
-
# ======================================================================
|
| 47 |
-
|
| 48 |
-
# State object to hold the DataAnalyzer instance
|
| 49 |
state_analyzer = gr.State()
|
| 50 |
-
|
| 51 |
-
# --- Header ---
|
| 52 |
gr.Markdown(f"<h1>{settings.APP_TITLE}</h1>")
|
| 53 |
-
gr.Markdown("A world-class data discovery platform that provides a complete suite of EDA tools and intelligently unlocks specialized analysis modules.")
|
| 54 |
-
|
| 55 |
-
# --- Input Row ---
|
| 56 |
with gr.Row():
|
| 57 |
-
upload_button = gr.File(label="1. Upload Data File
|
| 58 |
analyze_button = gr.Button("β¨ Generate Intelligence Report", variant="primary", scale=1)
|
| 59 |
-
|
| 60 |
-
# --- Main Tabs ---
|
| 61 |
with gr.Tabs():
|
| 62 |
-
with gr.Tab("π€ AI-Powered Strategy Report"
|
| 63 |
-
ai_report_output = gr.Markdown("### Your AI-generated report will appear here
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
with gr.Accordion("Numeric Features Summary", open=True):
|
| 69 |
-
profile_numeric_df = gr.DataFrame()
|
| 70 |
-
with gr.Accordion("Categorical Features Summary", open=True):
|
| 71 |
-
profile_categorical_df = gr.DataFrame()
|
| 72 |
-
|
| 73 |
-
with gr.Tab("π Overview Visuals", id="tab_overview"):
|
| 74 |
-
with gr.Row():
|
| 75 |
-
plot_types = gr.Plot()
|
| 76 |
-
plot_missing = gr.Plot()
|
| 77 |
plot_correlation = gr.Plot()
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
dd_scatter_x = gr.Dropdown(label="X-Axis (Numeric)", interactive=True)
|
| 88 |
-
dd_scatter_y = gr.Dropdown(label="Y-Axis (Numeric)", interactive=True)
|
| 89 |
-
dd_scatter_color = gr.Dropdown(label="Color By (Optional)", interactive=True)
|
| 90 |
-
with gr.Column(scale=2):
|
| 91 |
-
plot_scatter = gr.Plot()
|
| 92 |
-
|
| 93 |
-
with gr.Tab("π§© Clustering (K-Means)", id="tab_cluster", visible=False) as tab_cluster:
|
| 94 |
with gr.Row():
|
| 95 |
with gr.Column(scale=1):
|
| 96 |
-
num_clusters = gr.Slider(minimum=2, maximum=10, value=3, step=1, label="
|
| 97 |
md_cluster_summary = gr.Markdown()
|
| 98 |
-
with gr.Column(scale=2):
|
| 99 |
-
plot_cluster = gr.Plot()
|
| 100 |
plot_elbow = gr.Plot()
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
#
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
"tab_timeseries": tab_timeseries, "tab_text": tab_text, "tab_cluster": tab_cluster,
|
| 114 |
-
"num_clusters": num_clusters, "md_cluster_summary": md_cluster_summary,
|
| 115 |
-
"plot_cluster": plot_cluster, "plot_elbow": plot_elbow,
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
# ======================================================================
|
| 119 |
-
# 2. REGISTER EVENT HANDLERS
|
| 120 |
-
# Now that components is a guaranteed dictionary, this will work.
|
| 121 |
-
# ======================================================================
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
analysis_complete_event =
|
| 125 |
fn=callbacks.run_initial_analysis,
|
| 126 |
-
inputs=[
|
| 127 |
-
outputs=[
|
| 128 |
)
|
| 129 |
analysis_complete_event.then(
|
| 130 |
fn=callbacks.generate_reports_and_visuals,
|
| 131 |
-
inputs=[
|
| 132 |
-
outputs=components
|
| 133 |
)
|
| 134 |
|
| 135 |
-
# --- Interactive
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
outputs=[
|
| 140 |
-
)
|
| 141 |
-
scatter_inputs = [
|
| 142 |
-
components["state_analyzer"], components["dd_scatter_x"],
|
| 143 |
-
components["dd_scatter_y"], components["dd_scatter_color"]
|
| 144 |
-
]
|
| 145 |
-
for dropdown in [components["dd_scatter_x"], components["dd_scatter_y"], components["dd_scatter_color"]]:
|
| 146 |
-
dropdown.change(
|
| 147 |
-
fn=callbacks.create_scatterplot, inputs=scatter_inputs, outputs=[components["plot_scatter"]]
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
# --- Specialized Module Callbacks ---
|
| 151 |
-
components["num_clusters"].change(
|
| 152 |
-
fn=callbacks.update_clustering,
|
| 153 |
-
inputs=[components["state_analyzer"], components["num_clusters"]],
|
| 154 |
-
outputs=[components["plot_cluster"], components["plot_elbow"], components["md_cluster_summary"]]
|
| 155 |
-
)
|
| 156 |
|
| 157 |
-
# 3. Launch the application server
|
| 158 |
demo.launch(debug=False, server_name="0.0.0.0")
|
| 159 |
|
| 160 |
-
|
| 161 |
-
# --- Application Entry Point ---
|
| 162 |
if __name__ == "__main__":
|
| 163 |
main()
|
|
|
|
| 2 |
|
| 3 |
# -*- coding: utf-8 -*-
|
| 4 |
#
|
| 5 |
+
# PROJECT: CognitiveEDA v5.5 - The QuantumLeap Intelligence Platform
|
| 6 |
#
|
| 7 |
+
# DESCRIPTION: Main application entry point. This definitive version correctly
|
| 8 |
+
# handles multiple outputs by aligning with Gradio's API, passing
|
| 9 |
+
# a list of components to the `outputs` parameter.
|
|
|
|
| 10 |
#
|
| 11 |
# SETUP: $ pip install -r requirements.txt
|
| 12 |
#
|
| 13 |
# AUTHOR: An MCP & PhD Expert in Data & AI Solutions
|
| 14 |
+
# VERSION: 5.5 (Final API-Compliant Edition)
|
| 15 |
+
# LAST-UPDATE: 2023-10-30 (Corrected multiple output handling)
|
| 16 |
|
| 17 |
import warnings
|
| 18 |
import logging
|
| 19 |
import gradio as gr
|
| 20 |
|
|
|
|
| 21 |
from ui import callbacks
|
| 22 |
from core.config import settings
|
| 23 |
|
|
|
|
| 24 |
logging.basicConfig(
|
| 25 |
level=logging.INFO,
|
| 26 |
format='%(asctime)s - [%(levelname)s] - (%(filename)s:%(lineno)d) - %(message)s'
|
| 27 |
)
|
| 28 |
warnings.filterwarnings('ignore', category=FutureWarning)
|
| 29 |
|
|
|
|
| 30 |
def main():
|
|
|
|
|
|
|
|
|
|
| 31 |
logging.info(f"Starting {settings.APP_TITLE}")
|
| 32 |
|
|
|
|
| 33 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo"), title=settings.APP_TITLE) as demo:
|
| 34 |
|
| 35 |
+
# 1. DEFINE THE UI LAYOUT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
state_analyzer = gr.State()
|
|
|
|
|
|
|
| 37 |
gr.Markdown(f"<h1>{settings.APP_TITLE}</h1>")
|
|
|
|
|
|
|
|
|
|
| 38 |
with gr.Row():
|
| 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"):
|
| 45 |
+
profile_missing_df, profile_numeric_df, profile_categorical_df = gr.DataFrame(), gr.DataFrame(), gr.DataFrame()
|
| 46 |
+
with gr.Tab("π Overview Visuals"):
|
| 47 |
+
with gr.Row(): plot_types, plot_missing = gr.Plot(), gr.Plot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
plot_correlation = gr.Plot()
|
| 49 |
+
with gr.Tab("π¨ Interactive Explorer"):
|
| 50 |
+
with gr.Row():
|
| 51 |
+
dd_hist_col = gr.Dropdown(label="Select Column for Histogram", interactive=True)
|
| 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 |
+
with gr.Tab("π§© Clustering (K-Means)", visible=False) as tab_cluster:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
with gr.Row():
|
| 59 |
with gr.Column(scale=1):
|
| 60 |
+
num_clusters = gr.Slider(minimum=2, maximum=10, value=3, step=1, label="K", interactive=True)
|
| 61 |
md_cluster_summary = gr.Markdown()
|
| 62 |
+
with gr.Column(scale=2): plot_cluster = gr.Plot()
|
|
|
|
| 63 |
plot_elbow = gr.Plot()
|
| 64 |
+
tab_timeseries, tab_text = gr.Tab("β Time-Series", visible=False), gr.Tab("π Text", visible=False)
|
| 65 |
+
|
| 66 |
+
# 2. DEFINE THE OUTPUTS LIST
|
| 67 |
+
# This is the critical change. We create an explicit list of components
|
| 68 |
+
# that will be updated by the main analysis function.
|
| 69 |
+
# The order here MUST match the order of the returned tuple in the callback.
|
| 70 |
+
main_outputs = [
|
| 71 |
+
ai_report_output, profile_missing_df, profile_numeric_df, profile_categorical_df,
|
| 72 |
+
plot_types, plot_missing, plot_correlation,
|
| 73 |
+
dd_hist_col, dd_scatter_x, dd_scatter_y, dd_scatter_color,
|
| 74 |
+
tab_timeseries, tab_text, tab_cluster
|
| 75 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# 3. REGISTER EVENT HANDLERS
|
| 78 |
+
analysis_complete_event = analyze_button.click(
|
| 79 |
fn=callbacks.run_initial_analysis,
|
| 80 |
+
inputs=[upload_button],
|
| 81 |
+
outputs=[state_analyzer]
|
| 82 |
)
|
| 83 |
analysis_complete_event.then(
|
| 84 |
fn=callbacks.generate_reports_and_visuals,
|
| 85 |
+
inputs=[state_analyzer],
|
| 86 |
+
outputs=main_outputs # Pass the LIST of components, not a dictionary.
|
| 87 |
)
|
| 88 |
|
| 89 |
+
# --- Other Interactive Callbacks ---
|
| 90 |
+
dd_hist_col.change(fn=callbacks.create_histogram, inputs=[state_analyzer, dd_hist_col], outputs=[plot_histogram])
|
| 91 |
+
scatter_inputs = [state_analyzer, dd_scatter_x, dd_scatter_y, dd_scatter_color]
|
| 92 |
+
for dropdown in [dd_scatter_x, dd_scatter_y, dd_scatter_color]:
|
| 93 |
+
dropdown.change(fn=callbacks.create_scatterplot, inputs=scatter_inputs, outputs=[plot_scatter])
|
| 94 |
+
num_clusters.change(fn=callbacks.update_clustering, inputs=[state_analyzer, num_clusters], outputs=[plot_cluster, plot_elbow, md_cluster_summary])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
|
|
|
| 96 |
demo.launch(debug=False, server_name="0.0.0.0")
|
| 97 |
|
|
|
|
|
|
|
| 98 |
if __name__ == "__main__":
|
| 99 |
main()
|