# pyrefly: ignore [missing-import] import streamlit as st import requests import pandas as pd import json # pyrefly: ignore [missing-import] import plotly.io as pio import os # pyrefly: ignore [missing-import] from dotenv import load_dotenv load_dotenv() st.set_page_config( page_title="Autonomous Data Analyst", page_icon="๐Ÿค–", layout="wide", initial_sidebar_state="expanded" ) st.markdown(""" """, unsafe_allow_html=True) API_BASE_URL = "http://localhost:8000/api/v1" # --- Session state init --- defaults = { 'file_path': None, 'cleaned_file_path': None, 'schema': None, 'active_file': None, 'summary_stats': None, 'automl_results': None, 'ai_insights': None, 'model_insights': None, 'chat_history': [], 'gemini_key_ok': None } for k, v in defaults.items(): if k not in st.session_state: st.session_state[k] = v # --- Sidebar --- with st.sidebar: st.markdown("## ๐Ÿค– Autonomous Data Analyst") st.markdown("---") page = st.radio("Navigate", [ "๐Ÿ  Home", "๐Ÿ“‚ Upload & Ingestion", "๐Ÿงน Data Cleaning", "๐Ÿ“Š EDA Dashboard", "๐Ÿ”ฌ Statistical Tests", "โš™๏ธ AutoML Studio", "๐Ÿง  AI Insights", "๐Ÿ’ฌ Chat with Data", "๐Ÿ“„ Report Generator" ], label_visibility="collapsed") st.markdown("---") # API Key status gemini_key = os.getenv("GEMINI_API_KEY", "") if gemini_key and gemini_key != "your-gemini-api-key": st.success("โœ… Gemini API Key Set") else: st.error("โŒ GEMINI_API_KEY missing in .env") if st.session_state.active_file: st.info(f"๐Ÿ“ Active: `{os.path.basename(st.session_state.active_file)}`") else: st.warning("๐Ÿ’ก Upload a dataset to begin") st.markdown('
Autonomous Data Analyst
', unsafe_allow_html=True) st.markdown('
AI-Powered End-to-End Data Science Platform
', unsafe_allow_html=True) # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # HOME # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ if page == "๐Ÿ  Home": c1, c2, c3, c4 = st.columns(4) cards = [ ("๐Ÿ“‚", "Upload CSV/Excel/JSON"), ("๐Ÿงน", "Auto Data Cleaning"), ("๐Ÿ“Š", "EDA & Visualizations"), ("โš™๏ธ", "AutoML Pipeline"), ] for col, (icon, label) in zip([c1, c2, c3, c4], cards): with col: st.markdown(f'
{icon}
{label}
', unsafe_allow_html=True) st.markdown("
", unsafe_allow_html=True) c5, c6, c7, c8 = st.columns(4) cards2 = [("๐Ÿง ", "AI Insights (Gemini)"), ("๐Ÿ’ฌ", "Chat with Data"), ("๐Ÿ“„", "Report Generation"), ("๐Ÿ”ฌ", "Statistical Tests")] for col, (icon, label) in zip([c5, c6, c7, c8], cards2): with col: st.markdown(f'
{icon}
{label}
', unsafe_allow_html=True) st.markdown("---") st.markdown("### ๐Ÿš€ Quick Start") st.markdown("1. **Upload** your CSV/Excel/JSON dataset\n2. **Clean** the data with one click\n3. **Explore** with auto-generated charts\n4. **Train** ML models automatically\n5. **Ask** questions in plain English\n6. **Download** the full report") # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # UPLOAD # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ elif page == "๐Ÿ“‚ Upload & Ingestion": st.header("๐Ÿ“‚ Data Ingestion") uploaded_file = st.file_uploader("Upload your dataset", type=["csv", "xlsx", "xls", "json"]) if uploaded_file is not None: with st.spinner("Uploading and analyzing dataset..."): files = {"file": (uploaded_file.name, uploaded_file.getvalue(), uploaded_file.type)} try: resp = requests.post(f"{API_BASE_URL}/upload", files=files) if resp.status_code == 200: data = resp.json() st.success("โœ… File uploaded successfully!") st.session_state.file_path = data["file_path"] st.session_state.active_file = data["file_path"] st.session_state.schema = data["schema"] st.session_state.cleaned_file_path = None else: st.error(f"Error: {resp.text}") except Exception as e: st.error(f"Cannot connect to backend API at {API_BASE_URL}. Is the server running? Error: {e}") if st.session_state.schema: schema = st.session_state.schema st.subheader("๐Ÿ“‹ Dataset Overview") c1, c2, c3, c4, c5 = st.columns(5) metrics = [ ("Rows", f"{schema['num_rows']:,}"), ("Columns", schema['num_cols']), ("Numerical", len(schema['numerical_cols'])), ("Categorical", len(schema['categorical_cols'])), ("Duplicates", schema.get('duplicate_rows', 0)), ] for col, (label, val) in zip([c1, c2, c3, c4, c5], metrics): with col: st.markdown(f'
{label}
{val}
', unsafe_allow_html=True) st.markdown("
", unsafe_allow_html=True) st.subheader("๐Ÿ—‚๏ธ Column Details") col_data = [] for col_name, info in schema["columns"].items(): col_data.append({ "Column": col_name, "Type": info["dtype"], "Unique Values": info["unique_count"], "Missing": info["missing_count"], "Missing %": f"{info['missing_percentage']:.1f}%" }) st.dataframe(pd.DataFrame(col_data), use_container_width=True) # Dataset preview st.subheader("๐Ÿ‘๏ธ Data Preview") try: from app.utils.data_ingestion import load_dataset df_preview = load_dataset(st.session_state.active_file) st.dataframe(df_preview.head(20), use_container_width=True) except Exception: st.info("Run backend server to see preview.") # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # DATA CLEANING # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ elif page == "๐Ÿงน Data Cleaning": st.header("๐Ÿงน Automated Data Cleaning") if not st.session_state.file_path: st.warning("โš ๏ธ Please upload a dataset first.") else: col1, col2 = st.columns(2) with col1: missing_val_strategy = st.selectbox("Missing Value Strategy", ["mean", "median", "most_frequent", "knn", "drop"]) encode_strategy = st.selectbox("Categorical Encoding", ["label", "onehot"]) with col2: scale_strategy = st.selectbox("Feature Scaling", ["standard", "minmax"]) outlier_method = st.selectbox("Outlier Detection", ["iqr", "zscore"]) remove_dupes = st.checkbox("Remove Duplicate Rows", value=True) if st.button("๐Ÿš€ Run Cleaning Engine", use_container_width=True): with st.spinner("Cleaning dataset..."): payload = { "file_path": st.session_state.file_path, "missing_value_strategy": missing_val_strategy, "encode_strategy": encode_strategy, "scale_strategy": scale_strategy, "outlier_method": outlier_method, "remove_duplicates": remove_dupes } try: resp = requests.post(f"{API_BASE_URL}/clean", json=payload) if resp.status_code == 200: data = resp.json() st.success(f"โœ… Cleaned! Rows after cleaning: **{data['rows_after_cleaning']:,}**") st.session_state.cleaned_file_path = data["cleaned_file_path"] st.session_state.active_file = data["cleaned_file_path"] st.session_state.schema = data["schema"] st.balloons() # Download button with open(data["cleaned_file_path"], "rb") as f: st.download_button("โฌ‡๏ธ Download Cleaned CSV", f, file_name="cleaned_dataset.csv", mime="text/csv") else: st.error(f"Error: {resp.text}") except Exception as e: st.error(f"Connection error: {e}") # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # EDA DASHBOARD # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ elif page == "๐Ÿ“Š EDA Dashboard": st.header("๐Ÿ“Š Exploratory Data Analysis") if not st.session_state.active_file: st.warning("โš ๏ธ Please upload a dataset first.") else: if st.button("๐Ÿ” Generate EDA Dashboard", use_container_width=True): with st.spinner("Generating visualizations..."): try: # Summary resp_sum = requests.post(f"{API_BASE_URL}/eda/summary", json={"file_path": st.session_state.active_file}) if resp_sum.status_code == 200: st.session_state.summary_stats = resp_sum.json()["summary"] # Plots resp_plots = requests.post(f"{API_BASE_URL}/eda/plots", json={"file_path": st.session_state.active_file}) if resp_plots.status_code == 200: plots_data = resp_plots.json() if plots_data.get("correlation_matrix"): st.subheader("๐Ÿ”ฅ Correlation Matrix") st.plotly_chart(pio.from_json(plots_data["correlation_matrix"]), use_container_width=True) dist_plots = plots_data.get("distribution_plots", {}) if dist_plots: st.subheader("๐Ÿ“ˆ Distributions") cols = st.columns(2) for i, (col_name, fig_json) in enumerate(dist_plots.items()): with cols[i % 2]: st.plotly_chart(pio.from_json(fig_json), use_container_width=True) cat_plots = plots_data.get("categorical_plots", {}) if cat_plots: st.subheader("๐Ÿ“Š Categorical Features") cols = st.columns(2) for i, (col_name, fig_json) in enumerate(cat_plots.items()): with cols[i % 2]: st.plotly_chart(pio.from_json(fig_json), use_container_width=True) box_plots = plots_data.get("box_plots", {}) if box_plots: st.subheader("๐Ÿ“ฆ Boxplots (Outlier View)") cols = st.columns(2) for i, (col_name, fig_json) in enumerate(box_plots.items()): with cols[i % 2]: st.plotly_chart(pio.from_json(fig_json), use_container_width=True) else: st.error(f"Failed to generate plots: {resp_plots.text}") except Exception as e: st.error(f"Connection error: {e}") # Show summary table if available if st.session_state.summary_stats: st.subheader("๐Ÿ“‹ Numerical Summary") num_records = st.session_state.summary_stats.get("numerical", []) if num_records: st.dataframe(pd.DataFrame(num_records), use_container_width=True) # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # STATISTICAL TESTS # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ elif page == "๐Ÿ”ฌ Statistical Tests": st.header("๐Ÿ”ฌ Statistical Tests") if not st.session_state.active_file: st.warning("โš ๏ธ Please upload a dataset first.") else: schema = st.session_state.schema target_col = None if schema: all_cols = list(schema["columns"].keys()) target_col = st.selectbox("Optional: Select Target Column for group tests", ["(None)"] + all_cols) if target_col == "(None)": target_col = None if st.button("๐Ÿงช Run Statistical Tests", use_container_width=True): with st.spinner("Running statistical tests..."): try: payload = {"file_path": st.session_state.active_file} if target_col: payload["target_column"] = target_col resp = requests.post(f"{API_BASE_URL}/eda/statistical_tests", json=payload) if resp.status_code == 200: tests = resp.json()["statistical_tests"] if tests.get("normality_tests"): st.subheader("๐Ÿ“ Normality Tests (Shapiro-Wilk)") norm_data = [] for col, res in tests["normality_tests"].items(): norm_data.append({"Column": col, "Statistic": res["statistic"], "P-Value": res["p_value"], "Normal?": "โœ… Yes" if res["is_normal"] else "โŒ No", "Interpretation": res["interpretation"]}) st.dataframe(pd.DataFrame(norm_data), use_container_width=True) if tests.get("chi_square_tests"): st.subheader("๐Ÿ“Š Chi-Square Tests") chi_data = [] for col, res in tests["chi_square_tests"].items(): chi_data.append({"Column": col, "Chi2": res["chi2_statistic"], "P-Value": res["p_value"], "Significant?": "โœ… Yes" if res["is_significant"] else "โŒ No", "Interpretation": res["interpretation"]}) st.dataframe(pd.DataFrame(chi_data), use_container_width=True) if tests.get("t_tests"): st.subheader("๐Ÿ“ T-Tests (Group Comparison)") t_data = [] for col, res in tests["t_tests"].items(): t_data.append({"Column": col, "T-Statistic": res["t_statistic"], "P-Value": res["p_value"], "Significant?": "โœ… Yes" if res["is_significant"] else "โŒ No", "Interpretation": res["interpretation"]}) st.dataframe(pd.DataFrame(t_data), use_container_width=True) else: st.error(f"Error: {resp.text}") except Exception as e: st.error(f"Connection error: {e}") # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # AUTOML # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ elif page == "โš™๏ธ AutoML Studio": st.header("โš™๏ธ Automated Machine Learning") if not st.session_state.active_file: st.warning("โš ๏ธ Please upload a dataset first.") elif not st.session_state.schema: st.warning("โš ๏ธ Schema not loaded. Re-upload your dataset.") else: schema = st.session_state.schema all_cols = list(schema["columns"].keys()) target_col = st.selectbox("๐ŸŽฏ Select Target Variable", all_cols) problem_override = st.selectbox("Problem Type Override (optional)", ["Auto Detect", "Binary Classification", "Multi-Class Classification", "Regression", "Clustering"]) st.info("โฑ๏ธ AutoML compares multiple models. This may take 2-5 minutes.") if st.button("๐Ÿš€ Start AutoML Pipeline", type="primary", use_container_width=True): with st.spinner("Training models... Please wait."): payload = { "file_path": st.session_state.active_file, "target_column": target_col, "problem_type": None if problem_override == "Auto Detect" else problem_override } try: resp = requests.post(f"{API_BASE_URL}/automl/train", json=payload, timeout=600) if resp.status_code == 200: data = resp.json() st.session_state.automl_results = data st.success(f"โœ… Pipeline complete! Problem Type: **{data['problem_type']}**") else: st.error(f"AutoML failed: {resp.text}") except Exception as e: st.error(f"Error: {e}") if st.session_state.automl_results: data = st.session_state.automl_results st.subheader("๐Ÿ† Model Leaderboard") st.dataframe(pd.DataFrame(data["leaderboard"]), use_container_width=True) st.info(f"๐Ÿ’พ Best model saved: `{data['best_model_path']}`") if data.get("confusion_matrix"): st.subheader("๐ŸŽฏ Confusion Matrix") st.plotly_chart(pio.from_json(data["confusion_matrix"]), use_container_width=True) if data.get("roc_curve"): st.subheader("๐Ÿ“ˆ ROC Curve") st.plotly_chart(pio.from_json(data["roc_curve"]), use_container_width=True) # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # AI INSIGHTS # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ elif page == "๐Ÿง  AI Insights": st.header("๐Ÿง  AI-Powered Insights") if not st.session_state.active_file: st.warning("โš ๏ธ Please upload a dataset first.") else: col1, col2 = st.columns(2) with col1: if st.button("๐Ÿ“Š Generate EDA Insights", use_container_width=True): if not st.session_state.summary_stats: with st.spinner("Fetching summary..."): try: resp = requests.post(f"{API_BASE_URL}/eda/summary", json={"file_path": st.session_state.active_file}) if resp.status_code == 200: st.session_state.summary_stats = resp.json()["summary"] except Exception as e: st.error(f"Error: {e}") if st.session_state.summary_stats: with st.spinner("Analyzing with Gemini AI..."): try: resp = requests.post(f"{API_BASE_URL}/insights/eda", json={"summary_stats": st.session_state.summary_stats}) if resp.status_code == 200: st.session_state.ai_insights = resp.json()["insights"] else: st.error(f"Error: {resp.json().get('detail', resp.text)}") except Exception as e: st.error(f"Error: {e}") with col2: if st.session_state.automl_results: if st.button("๐Ÿค– Generate Model Insights", use_container_width=True): with st.spinner("Analyzing model results with Gemini..."): try: data = st.session_state.automl_results resp = requests.post(f"{API_BASE_URL}/insights/model", json={ "leaderboard": data["leaderboard"], "problem_type": data["problem_type"] }) if resp.status_code == 200: st.session_state.model_insights = resp.json()["insights"] else: st.error(f"Error: {resp.json().get('detail', resp.text)}") except Exception as e: st.error(f"Error: {e}") else: st.info("Run AutoML first to generate model insights.") if st.session_state.ai_insights: st.subheader("๐Ÿ“Š EDA Insights") st.markdown(st.session_state.ai_insights) if st.session_state.model_insights: st.subheader("๐Ÿค– Model Insights") st.markdown(st.session_state.model_insights) # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # CHAT WITH DATA # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ elif page == "๐Ÿ’ฌ Chat with Data": st.header("๐Ÿ’ฌ Natural Language Querying") if not st.session_state.active_file: st.warning("โš ๏ธ Please upload a dataset first.") else: st.write("Ask questions about your dataset in plain English.") # Display chat history for msg in st.session_state.chat_history: if msg["role"] == "user": st.markdown(f'
๐Ÿ‘ค You: {msg["content"]}
', unsafe_allow_html=True) else: st.markdown(f'
๐Ÿค– AI:
{msg["content"]}
', unsafe_allow_html=True) col1, col2 = st.columns([4, 1]) with col1: query = st.text_input("Your question:", placeholder="e.g. What is the average age? Show top 5 rows. Which column has most nulls?", key="chat_input") with col2: if st.button("๐Ÿ—‘๏ธ Clear", use_container_width=True): st.session_state.chat_history = [] st.rerun() if st.button("๐Ÿ’ฌ Ask AI", use_container_width=True) and query: with st.spinner("Thinking..."): try: payload = { "file_path": st.session_state.active_file, "query": query, "chat_history": st.session_state.chat_history } resp = requests.post(f"{API_BASE_URL}/insights/query", json=payload) if resp.status_code == 200: answer = resp.json()["response"] st.session_state.chat_history.append({"role": "user", "content": query}) st.session_state.chat_history.append({"role": "assistant", "content": answer}) st.rerun() else: st.error(f"Error: {resp.json().get('detail', resp.text)}") except Exception as e: st.error(f"Connection error: {e}") # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # REPORT GENERATOR # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ elif page == "๐Ÿ“„ Report Generator": st.header("๐Ÿ“„ Automated Report Generation") if not st.session_state.active_file: st.warning("โš ๏ธ Please upload a dataset first.") else: st.write("Generate a comprehensive HTML report with all findings.") st.info("The report includes: Dataset Overview, EDA Statistics, AI Insights, and AutoML Leaderboard (if available).") if st.button("๐Ÿ“„ Generate Full Report", type="primary", use_container_width=True): with st.spinner("Generating report..."): payload = { "file_path": st.session_state.active_file, "ai_insights": st.session_state.ai_insights or "", "leaderboard": st.session_state.automl_results["leaderboard"] if st.session_state.automl_results else [], "problem_type": st.session_state.automl_results["problem_type"] if st.session_state.automl_results else "", "model_insights": st.session_state.model_insights or "" } try: resp = requests.post(f"{API_BASE_URL}/reports/generate", json=payload) if resp.status_code == 200: data = resp.json() st.success(f"โœ… Report generated!") html_bytes = data["html_content"].encode("utf-8") st.download_button( label="โฌ‡๏ธ Download HTML Report", data=html_bytes, file_name="autonomous_analyst_report.html", mime="text/html", use_container_width=True ) with st.expander("๐Ÿ‘๏ธ Preview Report"): st.components.v1.html(data["html_content"], height=700, scrolling=True) else: st.error(f"Error: {resp.text}") except Exception as e: st.error(f"Connection error: {e}")