vashu2425 commited on
Commit
2278b61
·
verified ·
1 Parent(s): ba7a5fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -1939
app.py CHANGED
@@ -1935,1942 +1935,4 @@ def main():
1935
  st.rerun()
1936
 
1937
  if __name__ == "__main__":
1938
- main()
1939
-
1940
-
1941
-
1942
-
1943
-
1944
-
1945
-
1946
-
1947
-
1948
-
1949
-
1950
-
1951
- # """
1952
- # AI-Powered EDA & Feature Engineering Assistant
1953
-
1954
- # This application enables users to upload a CSV dataset, and utilizes LLMs to analyze
1955
- # the dataset to provide EDA and feature engineering recommendations.
1956
- # """
1957
-
1958
- # import streamlit as st
1959
- # import pandas as pd
1960
- # import os
1961
- # import base64
1962
- # from io import BytesIO
1963
- # from dotenv import load_dotenv
1964
- # from typing import Dict, List, Any, Optional
1965
- # import time
1966
- # import logging
1967
- # import plotly.express as px
1968
- # import numpy as np
1969
-
1970
- # # Import local modules
1971
- # from eda_analysis import DatasetAnalyzer
1972
- # from llm_inference import LLMInference
1973
-
1974
- # # Configure logging
1975
- # logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
1976
- # logger = logging.getLogger(__name__)
1977
-
1978
- # # Load environment variables
1979
- # load_dotenv()
1980
-
1981
- # # Set page configuration - must be the first Streamlit command
1982
- # st.set_page_config(
1983
- # page_title="AI-Powered EDA & Feature Engineering Assistant",
1984
- # page_icon="📊",
1985
- # layout="wide",
1986
- # initial_sidebar_state="expanded"
1987
- # )
1988
-
1989
- # # Initialize our classes
1990
- # @st.cache_resource
1991
- # def get_llm_inference():
1992
- # try:
1993
- # return LLMInference()
1994
- # except Exception as e:
1995
- # st.error(f"Error initializing LLM inference: {str(e)}")
1996
- # return None
1997
-
1998
- # llm_inference = get_llm_inference()
1999
-
2000
- # # Session state initialization
2001
- # if "dataset_analyzer" not in st.session_state:
2002
- # st.session_state.dataset_analyzer = DatasetAnalyzer()
2003
-
2004
- # if "dataset_loaded" not in st.session_state:
2005
- # st.session_state.dataset_loaded = False
2006
-
2007
- # if "dataset_info" not in st.session_state:
2008
- # st.session_state.dataset_info = {}
2009
-
2010
- # if "visualizations" not in st.session_state:
2011
- # st.session_state.visualizations = {}
2012
-
2013
- # if "eda_insights" not in st.session_state:
2014
- # st.session_state.eda_insights = ""
2015
-
2016
- # if "feature_engineering_recommendations" not in st.session_state:
2017
- # st.session_state.feature_engineering_recommendations = ""
2018
-
2019
- # if "data_quality_insights" not in st.session_state:
2020
- # st.session_state.data_quality_insights = ""
2021
-
2022
- # if "active_tab" not in st.session_state:
2023
- # st.session_state.active_tab = "welcome"
2024
-
2025
- # # Add new functions to support the updated UI
2026
- # def initialize_session_state():
2027
- # """Initialize session state variables needed for the application"""
2028
- # # Initialize session variables with appropriate defaults
2029
- # if "chat_history" not in st.session_state:
2030
- # st.session_state.chat_history = []
2031
-
2032
- # # For dataframe and related variables, ensure proper initialization
2033
- # # df should not be in session_state until a proper DataFrame is loaded
2034
- # if "descriptive_stats" not in st.session_state:
2035
- # st.session_state.descriptive_stats = None
2036
-
2037
- # if "selected_columns" not in st.session_state:
2038
- # st.session_state.selected_columns = []
2039
-
2040
- # if "filtered_df" not in st.session_state:
2041
- # st.session_state.filtered_df = None
2042
-
2043
- # if "ai_insights" not in st.session_state:
2044
- # st.session_state.ai_insights = None
2045
-
2046
- # if "loading_insights" not in st.session_state:
2047
- # st.session_state.loading_insights = False
2048
-
2049
- # if "selected_tab" not in st.session_state:
2050
- # st.session_state.selected_tab = 'tab-overview'
2051
-
2052
- # if "dataset_name" not in st.session_state:
2053
- # st.session_state.dataset_name = ""
2054
-
2055
- # # Logging initialization
2056
- # logger.info("Session state initialized")
2057
-
2058
- # def apply_custom_css():
2059
- # """Apply additional custom CSS that's not already in the main CSS block"""
2060
- # st.markdown("""
2061
- # <style>
2062
- # /* Base theme variables */
2063
- # :root {
2064
- # --primary: #4F46E5;
2065
- # --secondary: #06B6D4;
2066
- # --text-light: #F3F4F6;
2067
- # --text-muted: #9CA3AF;
2068
- # --bg-card: rgba(31, 41, 55, 0.7);
2069
- # --bg-dark: #111827;
2070
- # }
2071
-
2072
- # /* Global styles */
2073
- # .stApp {
2074
- # background-color: var(--bg-dark);
2075
- # color: var(--text-light);
2076
- # }
2077
-
2078
- # /* Improve sidebar styling */
2079
- # .sidebar-header {
2080
- # background: linear-gradient(90deg, var(--primary), var(--secondary));
2081
- # color: white;
2082
- # padding: 1rem;
2083
- # border-radius: 8px;
2084
- # margin-bottom: 1.5rem;
2085
- # font-size: 1.2rem;
2086
- # font-weight: 600;
2087
- # text-align: center;
2088
- # }
2089
-
2090
-
2091
- # /*
2092
- # div[data-testid="stBottomBlockContainer"] {
2093
- # background-color: #111827 !important;
2094
- # }
2095
-
2096
- # div[data-testid="stChatInput"]{
2097
- # background-color: #111827 !important;
2098
- # } */
2099
-
2100
- # /* Override the bottom chat input container */
2101
- # div.stChatFloatingInputContainer {
2102
- # background-color: #111827 !important;
2103
- # }
2104
-
2105
- # /* Override the inner chat input box */
2106
- # div.stChatInputContainer {
2107
- # background-color: #111827 !important;
2108
-
2109
- # }
2110
-
2111
- # /* Optional: Override text area background */
2112
- # textarea {
2113
- # background-color: #111827 !important;
2114
- # color: white !important;
2115
- # }
2116
-
2117
- # .sidebar-section {
2118
- # background: rgba(31, 41, 55, 0.4);
2119
- # border-radius: 8px;
2120
- # padding: 1rem;
2121
- # margin-bottom: 1.5rem;
2122
- # border: 1px solid rgba(99, 102, 241, 0.1);
2123
- # }
2124
-
2125
- # .sidebar-footer {
2126
- # text-align: center;
2127
- # padding: 1rem;
2128
- # font-size: 0.8rem;
2129
- # color: var(--text-muted);
2130
- # margin-top: 3rem;
2131
- # }
2132
-
2133
- # /* Feature Engineering Cards */
2134
- # .fe-cards-container {
2135
- # display: grid;
2136
- # grid-template-columns: repeat(2, 1fr);
2137
- # gap: 0.8rem;
2138
- # margin-top: 1rem;
2139
- # }
2140
-
2141
- # .fe-card {
2142
- # background: rgba(31, 41, 55, 0.6);
2143
- # border-radius: 8px;
2144
- # padding: 0.8rem;
2145
- # text-align: center;
2146
- # cursor: pointer;
2147
- # transition: all 0.2s ease;
2148
- # border: 1px solid rgba(99, 102, 241, 0.1);
2149
- # position: relative;
2150
- # overflow: hidden;
2151
- # }
2152
-
2153
- # .fe-card::before {
2154
- # content: '';
2155
- # position: absolute;
2156
- # top: 0;
2157
- # left: 0;
2158
- # right: 0;
2159
- # bottom: 0;
2160
- # background: linear-gradient(135deg, var(--primary), var(--secondary));
2161
- # opacity: 0;
2162
- # transition: opacity 0.3s ease;
2163
- # z-index: 0;
2164
- # }
2165
-
2166
- # .fe-card:hover::before {
2167
- # opacity: 0.1;
2168
- # }
2169
-
2170
- # .fe-card:hover {
2171
- # transform: translateY(-2px);
2172
- # box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
2173
- # border-color: rgba(99, 102, 241, 0.3);
2174
- # }
2175
-
2176
- # .fe-card-active {
2177
- # border-color: var(--primary);
2178
- # background: rgba(79, 70, 229, 0.1);
2179
- # }
2180
-
2181
- # .fe-card-icon {
2182
- # font-size: 1.8rem;
2183
- # margin-bottom: 0.3rem;
2184
- # position: relative;
2185
- # z-index: 1;
2186
- # }
2187
-
2188
- # .fe-card-title {
2189
- # font-size: 0.85rem;
2190
- # font-weight: 600;
2191
- # color: var(--text-light);
2192
- # position: relative;
2193
- # z-index: 1;
2194
- # }
2195
-
2196
- # /* Tab content styling */
2197
- # .tab-title {
2198
- # font-size: 1.8rem;
2199
- # margin-bottom: 1.5rem;
2200
- # position: relative;
2201
- # display: inline-block;
2202
- # color: var(--text-light);
2203
- # }
2204
-
2205
- # .tab-title:after {
2206
- # content: '';
2207
- # position: absolute;
2208
- # bottom: -10px;
2209
- # left: 0;
2210
- # width: 100%;
2211
- # height: 3px;
2212
- # background: linear-gradient(90deg, var(--primary) 0%, var(--secondary) 100%);
2213
- # border-radius: 3px;
2214
- # }
2215
-
2216
- # /* Navigation Tabs */
2217
- # .custom-tabs {
2218
- # display: flex;
2219
- # background: rgba(31, 41, 55, 0.6);
2220
- # border-radius: 12px;
2221
- # padding: 0.5rem;
2222
- # margin-bottom: 2rem;
2223
- # justify-content: space-between;
2224
- # overflow: hidden;
2225
- # border: 1px solid rgba(99, 102, 241, 0.1);
2226
- # }
2227
-
2228
- # .tab-item {
2229
- # flex: 1;
2230
- # text-align: center;
2231
- # padding: 0.8rem 0.5rem;
2232
- # border-radius: 8px;
2233
- # cursor: pointer;
2234
- # transition: all 0.3s ease;
2235
- # position: relative;
2236
- # z-index: 1;
2237
- # margin: 0 0.2rem;
2238
- # }
2239
-
2240
- # .tab-item.active {
2241
- # background: rgba(79, 70, 229, 0.1);
2242
- # }
2243
-
2244
- # .tab-item.active::before {
2245
- # content: '';
2246
- # position: absolute;
2247
- # bottom: 0;
2248
- # left: 10%;
2249
- # right: 10%;
2250
- # height: 3px;
2251
- # background: linear-gradient(90deg, var(--primary), var(--secondary));
2252
- # border-radius: 3px;
2253
- # }
2254
-
2255
- # .tab-item:hover {
2256
- # background: rgba(79, 70, 229, 0.05);
2257
- # }
2258
-
2259
- # .tab-icon {
2260
- # font-size: 1.5rem;
2261
- # margin-bottom: 0.3rem;
2262
- # }
2263
-
2264
- # .tab-label {
2265
- # font-size: 0.85rem;
2266
- # font-weight: 500;
2267
- # color: var(--text-light);
2268
- # }
2269
-
2270
- # .tab-content-spacer {
2271
- # height: 1rem;
2272
- # }
2273
-
2274
- # /* Card styling */
2275
- # .stats-card, .info-card, .chart-card {
2276
- # background: rgba(31, 41, 55, 0.3);
2277
- # border-radius: 10px;
2278
- # padding: 1.2rem;
2279
- # margin-bottom: 1.5rem;
2280
- # border: 1px solid rgba(99, 102, 241, 0.1);
2281
- # transition: all 0.3s ease;
2282
- # }
2283
-
2284
- # .stats-card:hover, .info-card:hover, .chart-card:hover {
2285
- # transform: translateY(-5px);
2286
- # box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2);
2287
- # border-color: rgba(99, 102, 241, 0.3);
2288
- # }
2289
-
2290
- # /* Dataset stats styling */
2291
- # .dataset-stats {
2292
- # display: flex;
2293
- # flex-wrap: wrap;
2294
- # gap: 0.8rem;
2295
- # justify-content: center;
2296
- # }
2297
-
2298
- # .stat-item {
2299
- # text-align: center;
2300
- # padding: 0.8rem;
2301
- # background: rgba(31, 41, 55, 0.6);
2302
- # border-radius: 8px;
2303
- # min-width: 80px;
2304
- # border: 1px solid rgba(99, 102, 241, 0.2);
2305
- # }
2306
-
2307
- # .stat-value {
2308
- # font-size: 1.5rem;
2309
- # font-weight: 700;
2310
- # color: var(--primary);
2311
- # }
2312
-
2313
- # .stat-label {
2314
- # font-size: 0.8rem;
2315
- # color: var(--text-muted);
2316
- # margin-top: 0.3rem;
2317
- # }
2318
-
2319
- # /* Chart styling */
2320
- # .chart-container {
2321
- # margin-top: 1.5rem;
2322
- # }
2323
-
2324
- # .chart-card h3 {
2325
- # font-size: 1.2rem;
2326
- # margin-bottom: 1rem;
2327
- # color: var(--text-light);
2328
- # }
2329
-
2330
- # .stat-summary {
2331
- # display: grid;
2332
- # grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
2333
- # gap: 0.5rem;
2334
- # margin-top: 1rem;
2335
- # }
2336
-
2337
- # .stat-pair {
2338
- # display: flex;
2339
- # justify-content: space-between;
2340
- # padding: 0.3rem 0.5rem;
2341
- # background: rgba(31, 41, 55, 0.4);
2342
- # border-radius: 4px;
2343
- # font-size: 0.9rem;
2344
- # }
2345
-
2346
- # .stat-pair span {
2347
- # color: var(--text-muted);
2348
- # }
2349
-
2350
- # .stat-pair strong {
2351
- # color: var(--text-light);
2352
- # }
2353
-
2354
- # /* Filter container */
2355
- # .filter-container {
2356
- # background: rgba(31, 41, 55, 0.3);
2357
- # border-radius: 10px;
2358
- # padding: 1.2rem;
2359
- # margin-bottom: 1.5rem;
2360
- # border: 1px solid rgba(99, 102, 241, 0.1);
2361
- # }
2362
-
2363
- # /* AI Insights styling */
2364
- # .insights-container {
2365
- # margin-top: 1rem;
2366
- # }
2367
-
2368
- # .insights-category {
2369
- # margin-top: 0.5rem;
2370
- # }
2371
-
2372
- # .insight-card {
2373
- # background: rgba(31, 41, 55, 0.3);
2374
- # border-radius: 10px;
2375
- # padding: 1.2rem;
2376
- # margin-bottom: 1rem;
2377
- # border: 1px solid rgba(99, 102, 241, 0.1);
2378
- # display: flex;
2379
- # align-items: flex-start;
2380
- # }
2381
-
2382
- # .insight-content {
2383
- # display: flex;
2384
- # align-items: flex-start;
2385
- # gap: 1rem;
2386
- # }
2387
-
2388
- # .insight-icon {
2389
- # font-size: 1.5rem;
2390
- # margin-top: 0.1rem;
2391
- # }
2392
-
2393
- # .insight-text {
2394
- # flex: 1;
2395
- # line-height: 1.5;
2396
- # }
2397
-
2398
- # .generate-insights-container {
2399
- # display: flex;
2400
- # justify-content: center;
2401
- # align-items: center;
2402
- # margin: 3rem 0;
2403
- # }
2404
-
2405
- # .placeholder-card {
2406
- # background: rgba(31, 41, 55, 0.3);
2407
- # border-radius: 15px;
2408
- # padding: 2rem;
2409
- # text-align: center;
2410
- # border: 1px solid rgba(99, 102, 241, 0.1);
2411
- # max-width: 500px;
2412
- # margin: 0 auto;
2413
- # }
2414
-
2415
- # .placeholder-icon {
2416
- # font-size: 3rem;
2417
- # margin-bottom: 1rem;
2418
- # animation: float 3s ease-in-out infinite;
2419
- # }
2420
-
2421
- # .placeholder-text {
2422
- # color: var(--text-muted);
2423
- # line-height: 1.6;
2424
- # margin-bottom: 1.5rem;
2425
- # }
2426
-
2427
- # .loading-container {
2428
- # display: flex;
2429
- # justify-content: center;
2430
- # margin: 2rem 0;
2431
- # }
2432
-
2433
- # .loading-pulse {
2434
- # width: 80px;
2435
- # height: 80px;
2436
- # border-radius: 50%;
2437
- # background: linear-gradient(to right, var(--primary), var(--secondary));
2438
- # animation: pulse-animation 1.5s ease infinite;
2439
- # }
2440
-
2441
- # @keyframes pulse-animation {
2442
- # 0% {
2443
- # transform: scale(0.6);
2444
- # opacity: 0.5;
2445
- # }
2446
- # 50% {
2447
- # transform: scale(1);
2448
- # opacity: 1;
2449
- # }
2450
- # 100% {
2451
- # transform: scale(0.6);
2452
- # opacity: 0.5;
2453
- # }
2454
- # }
2455
-
2456
- # @keyframes float {
2457
- # 0% { transform: translateY(0px); }
2458
- # 50% { transform: translateY(-10px); }
2459
- # 100% { transform: translateY(0px); }
2460
- # }
2461
-
2462
- # /* Button styling */
2463
- # button[kind="primary"] {
2464
- # background: linear-gradient(90deg, var(--primary), var(--secondary)) !important;
2465
- # color: white !important;
2466
- # border: none !important;
2467
- # border-radius: 8px !important;
2468
- # padding: 0.6rem 1.2rem !important;
2469
- # font-weight: 600 !important;
2470
- # transition: all 0.3s ease !important;
2471
- # box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
2472
- # }
2473
-
2474
- # button[kind="primary"]:hover {
2475
- # transform: translateY(-2px) !important;
2476
- # box-shadow: 0 6px 10px rgba(0, 0, 0, 0.15) !important;
2477
- # }
2478
-
2479
- # button[kind="secondary"] {
2480
- # background: rgba(79, 70, 229, 0.1) !important;
2481
- # color: var(--text-light) !important;
2482
- # border: 1px solid rgba(79, 70, 229, 0.3) !important;
2483
- # border-radius: 8px !important;
2484
- # padding: 0.6rem 1.2rem !important;
2485
- # font-weight: 600 !important;
2486
- # transition: all 0.3s ease !important;
2487
- # }
2488
-
2489
- # button[kind="secondary"]:hover {
2490
- # background: rgba(79, 70, 229, 0.2) !important;
2491
- # transform: translateY(-2px) !important;
2492
- # }
2493
-
2494
- # /* Override Streamlit default button styles */
2495
- # .stButton>button {
2496
- # background: linear-gradient(90deg, var(--primary), var(--secondary)) !important;
2497
- # color: white !important;
2498
- # border: none !important;
2499
- # border-radius: 8px !important;
2500
- # padding: 0.6rem 1.2rem !important;
2501
- # font-weight: 600 !important;
2502
- # transition: all 0.3s ease !important;
2503
- # box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
2504
- # width: 100%;
2505
- # }
2506
-
2507
- # .stButton>button:hover {
2508
- # transform: translateY(-2px) !important;
2509
- # box-shadow: 0 6px 10px rgba(0, 0, 0, 0.15) !important;
2510
- # }
2511
-
2512
- # /* Chat interface styling */
2513
- # .chat-interface-container {
2514
- # padding: 1rem 0;
2515
- # margin-bottom: 100px;
2516
- # position: relative;
2517
- # }
2518
-
2519
- # .chat-messages {
2520
- # display: flex;
2521
- # flex-direction: column;
2522
- # gap: 15px;
2523
- # margin-bottom: 20px;
2524
- # }
2525
-
2526
- # .chat-message-user, .chat-message-ai {
2527
- # padding: 12px 16px;
2528
- # border-radius: 12px;
2529
- # max-width: 80%;
2530
- # box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
2531
- # }
2532
-
2533
- # .chat-message-user {
2534
- # align-self: flex-end;
2535
- # background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
2536
- # color: white;
2537
- # border-bottom-right-radius: 0;
2538
- # margin-left: auto;
2539
- # }
2540
-
2541
- # .chat-message-ai {
2542
- # align-self: flex-start;
2543
- # background: var(--bg-card);
2544
- # color: var(--text-light);
2545
- # border-bottom-left-radius: 0;
2546
- # margin-right: auto;
2547
- # }
2548
-
2549
- # .chat-input-container {
2550
- # display: flex;
2551
- # align-items: center;
2552
- # gap: 10px;
2553
- # margin-top: 1.5rem;
2554
- # }
2555
-
2556
- # .chat-suggestions {
2557
- # display: flex;
2558
- # flex-wrap: wrap;
2559
- # gap: 10px;
2560
- # margin: 1.5rem 0;
2561
- # }
2562
-
2563
- # .chat-suggestion {
2564
- # background: rgba(99, 102, 241, 0.1);
2565
- # border: 1px solid rgba(99, 102, 241, 0.3);
2566
- # border-radius: 30px;
2567
- # padding: 8px 15px;
2568
- # font-size: 0.9rem;
2569
- # color: var(--text-light);
2570
- # cursor: pointer;
2571
- # transition: all 0.3s ease;
2572
- # display: inline-block;
2573
- # margin-bottom: 8px;
2574
- # }
2575
-
2576
- # .chat-suggestion:hover {
2577
- # background: rgba(99, 102, 241, 0.2);
2578
- # transform: translateY(-2px);
2579
- # }
2580
-
2581
- # /* Expander styling */
2582
- # .st-expander {
2583
- # background: rgba(31, 41, 55, 0.2) !important;
2584
- # border-radius: 8px !important;
2585
- # margin-bottom: 1rem !important;
2586
- # border: 1px solid rgba(99, 102, 241, 0.1) !important;
2587
- # }
2588
-
2589
- # /* Streamlit widget styling */
2590
- # div[data-testid="stForm"] {
2591
- # background: rgba(31, 41, 55, 0.2) !important;
2592
- # border-radius: 10px !important;
2593
- # padding: 1rem !important;
2594
- # border: 1px solid rgba(99, 102, 241, 0.1) !important;
2595
- # }
2596
-
2597
- # .stSelectbox>div>div {
2598
- # background: rgba(31, 41, 55, 0.4) !important;
2599
- # border: 1px solid rgba(99, 102, 241, 0.2) !important;
2600
- # border-radius: 8px !important;
2601
- # }
2602
-
2603
- # .stTextInput>div>div>input {
2604
- # background: rgba(31, 41, 55, 0.4) !important;
2605
- # border: 1px solid rgba(99, 102, 241, 0.2) !important;
2606
- # border-radius: 8px !important;
2607
- # color: var(--text-light) !important;
2608
- # padding: 1rem !important;
2609
- # }
2610
-
2611
- # /* Streamlit multiselect dropdown styling */
2612
- # div[data-baseweb="popover"] {
2613
- # background: var(--bg-dark) !important;
2614
- # border: 1px solid rgba(99, 102, 241, 0.2) !important;
2615
- # border-radius: 8px !important;
2616
- # }
2617
-
2618
- # div[data-baseweb="menu"] {
2619
- # background: var(--bg-dark) !important;
2620
- # }
2621
-
2622
- # div[role="listbox"] {
2623
- # background: var(--bg-dark) !important;
2624
- # }
2625
-
2626
- # /* Fix for the upload button */
2627
- # .stFileUploader > div {
2628
- # display: flex;
2629
- # flex-direction: column;
2630
- # align-items: center;
2631
- # }
2632
-
2633
- # .stFileUploader > div > button {
2634
- # background: linear-gradient(90deg, var(--primary), var(--secondary)) !important;
2635
- # color: white !important;
2636
- # border: none !important;
2637
- # width: 100%;
2638
- # margin-top: 1rem;
2639
- # }
2640
-
2641
- # /* Fix for tab content spacing */
2642
- # .tab-content {
2643
- # margin-top: 2rem;
2644
- # padding: 1rem;
2645
- # background: rgba(31, 41, 55, 0.2);
2646
- # border-radius: 10px;
2647
- # border: 1px solid rgba(99, 102, 241, 0.1);
2648
- # }
2649
- # </style>
2650
- # """, unsafe_allow_html=True)
2651
-
2652
- # def generate_ai_insights():
2653
- # """Generate AI-powered insights about the dataset"""
2654
- # # Make sure we have a dataframe to analyze
2655
- # if 'df' not in st.session_state:
2656
- # logger.warning("Cannot generate AI insights: No dataframe in session state")
2657
- # return {}
2658
-
2659
- # df = st.session_state.df
2660
- # insights = {}
2661
-
2662
- # # Try to use the LLM for insights generation first
2663
- # try:
2664
- # if llm_inference is not None:
2665
- # # Create dataset_info dictionary for LLM
2666
- # num_rows, num_cols = df.shape
2667
- # num_numerical = len(df.select_dtypes(include=['number']).columns)
2668
- # num_categorical = len(df.select_dtypes(include=['object', 'category']).columns)
2669
- # num_missing = df.isnull().sum().sum()
2670
-
2671
- # # Format missing values for better readability
2672
- # missing_cols = df.isnull().sum()[df.isnull().sum() > 0]
2673
- # missing_values = {}
2674
- # for col in missing_cols.index:
2675
- # count = missing_cols[col]
2676
- # percent = round(count / len(df) * 100, 2)
2677
- # missing_values[col] = (count, percent)
2678
-
2679
- # # Get numerical columns and their correlations if applicable
2680
- # num_cols = df.select_dtypes(include=['number']).columns
2681
- # correlations = "No numerical columns to calculate correlations."
2682
- # if len(num_cols) > 1:
2683
- # # Calculate correlations
2684
- # corr_matrix = df[num_cols].corr()
2685
- # # Get top correlations (absolute values)
2686
- # corr_pairs = []
2687
- # for i in range(len(num_cols)):
2688
- # for j in range(i):
2689
- # val = corr_matrix.iloc[i, j]
2690
- # if abs(val) > 0.5: # Only show strong correlations
2691
- # corr_pairs.append((num_cols[i], num_cols[j], val))
2692
-
2693
- # # Sort by absolute correlation and format
2694
- # if corr_pairs:
2695
- # corr_pairs.sort(key=lambda x: abs(x[2]), reverse=True)
2696
- # formatted_corrs = []
2697
- # for col1, col2, val in corr_pairs[:5]: # Top 5
2698
- # formatted_corrs.append(f"{col1} and {col2}: {val:.3f}")
2699
- # correlations = "\n".join(formatted_corrs)
2700
-
2701
- # dataset_info = {
2702
- # "shape": f"{num_rows} rows, {num_cols} columns",
2703
- # "columns": df.columns.tolist(),
2704
- # "dtypes": {col: str(dtype) for col, dtype in df.dtypes.items()},
2705
- # "missing_values": missing_values,
2706
- # "basic_stats": df.describe().to_string(),
2707
- # "correlations": correlations,
2708
- # "sample_data": df.head(5).to_string()
2709
- # }
2710
-
2711
- # # Generate EDA insights with better error handling
2712
- # logger.info("Requesting EDA insights from LLM")
2713
- # try:
2714
- # eda_insights = llm_inference.generate_eda_insights(dataset_info)
2715
-
2716
- # if eda_insights and isinstance(eda_insights, str) and len(eda_insights) > 50:
2717
- # # Clean and format the response
2718
- # eda_insights = eda_insights.strip()
2719
- # insights["EDA Insights"] = [eda_insights]
2720
- # logger.info("Successfully generated EDA insights")
2721
- # else:
2722
- # logger.warning(f"EDA insights response was invalid: {type(eda_insights)}, length: {len(eda_insights) if isinstance(eda_insights, str) else 'N/A'}")
2723
- # except Exception as e:
2724
- # logger.error(f"Error generating EDA insights: {str(e)}")
2725
-
2726
- # # Generate feature engineering recommendations
2727
- # if "EDA Insights" in insights: # Only proceed if EDA worked
2728
- # logger.info("Requesting feature engineering recommendations from LLM")
2729
- # try:
2730
- # fe_insights = llm_inference.generate_feature_engineering_recommendations(dataset_info)
2731
-
2732
- # if fe_insights and isinstance(fe_insights, str) and len(fe_insights) > 50:
2733
- # fe_insights = fe_insights.strip()
2734
- # insights["Feature Engineering Recommendations"] = [fe_insights]
2735
- # logger.info("Successfully generated feature engineering recommendations")
2736
- # else:
2737
- # logger.warning(f"Feature engineering response was invalid: {type(fe_insights)}, length: {len(fe_insights) if isinstance(fe_insights, str) else 'N/A'}")
2738
- # except Exception as e:
2739
- # logger.error(f"Error generating feature engineering recommendations: {str(e)}")
2740
-
2741
- # # Generate data quality insights
2742
- # logger.info("Requesting data quality insights from LLM")
2743
- # try:
2744
- # dq_insights = llm_inference.generate_data_quality_insights(dataset_info)
2745
-
2746
- # if dq_insights and isinstance(dq_insights, str) and len(dq_insights) > 50:
2747
- # dq_insights = dq_insights.strip()
2748
- # insights["Data Quality Insights"] = [dq_insights]
2749
- # logger.info("Successfully generated data quality insights")
2750
- # else:
2751
- # logger.warning(f"Data quality response was invalid: {type(dq_insights)}, length: {len(dq_insights) if isinstance(dq_insights, str) else 'N/A'}")
2752
- # except Exception as e:
2753
- # logger.error(f"Error generating data quality insights: {str(e)}")
2754
-
2755
- # # If we have at least one type of insights, consider it a success
2756
- # if insights:
2757
- # # Mark that the insights are loaded
2758
- # st.session_state['loading_insights'] = False
2759
- # logger.info("Successfully generated AI insights using LLM")
2760
- # return insights
2761
-
2762
- # logger.warning("All LLM generated insights failed or were too short. Falling back to template insights.")
2763
- # else:
2764
- # logger.warning("LLM inference is not available. Falling back to template insights.")
2765
- # except Exception as e:
2766
- # logger.error(f"Error in generate_ai_insights(): {str(e)}. Falling back to template insights.")
2767
-
2768
- # # If LLM fails or is not available, generate template-based insights
2769
- # logger.info("Falling back to template-based insights generation")
2770
-
2771
- # # Add missing values insights
2772
- # missing_data = df.isnull().sum()
2773
- # missing_percent = (missing_data / len(df)) * 100
2774
- # missing_cols = missing_data[missing_data > 0]
2775
-
2776
- # missing_insights = []
2777
- # if len(missing_cols) > 0:
2778
- # missing_insights.append(f"Found {len(missing_cols)} columns with missing values.")
2779
- # for col in missing_cols.index[:3]: # Show details for top 3
2780
- # missing_insights.append(f"Column '{col}' has {missing_data[col]} missing values ({missing_percent[col]:.2f}%).")
2781
-
2782
- # if len(missing_cols) > 3:
2783
- # missing_insights.append(f"And {len(missing_cols) - 3} more columns have missing values.")
2784
-
2785
- # # Add recommendation
2786
- # if any(missing_percent > 50):
2787
- # high_missing = missing_percent[missing_percent > 50].index.tolist()
2788
- # missing_insights.append(f"Consider dropping columns with >50% missing values: {', '.join(high_missing[:3])}.")
2789
- # else:
2790
- # missing_insights.append("Consider using imputation techniques for columns with missing values.")
2791
- # else:
2792
- # missing_insights.append("No missing values found in the dataset. Great job!")
2793
-
2794
- # insights["Missing Values Analysis"] = missing_insights
2795
-
2796
- # # Add distribution insights
2797
- # num_cols = df.select_dtypes(include=['number']).columns
2798
- # dist_insights = []
2799
-
2800
- # if len(num_cols) > 0:
2801
- # for col in num_cols[:3]: # Analyze top 3 numeric columns
2802
- # # Check for skewness
2803
- # skew = df[col].skew()
2804
- # if abs(skew) > 1:
2805
- # direction = "right" if skew > 0 else "left"
2806
- # dist_insights.append(f"Column '{col}' is {direction}-skewed (skewness: {skew:.2f}). Consider log transformation.")
2807
-
2808
- # # Check for outliers using IQR
2809
- # Q1 = df[col].quantile(0.25)
2810
- # Q3 = df[col].quantile(0.75)
2811
- # IQR = Q3 - Q1
2812
- # outliers = df[(df[col] < (Q1 - 1.5 * IQR)) | (df[col] > (Q3 + 1.5 * IQR))][col].count()
2813
-
2814
- # if outliers > 0:
2815
- # pct = (outliers / len(df)) * 100
2816
- # dist_insights.append(f"Column '{col}' has {outliers} outliers ({pct:.2f}%). Consider outlier treatment.")
2817
-
2818
- # if len(num_cols) > 3:
2819
- # dist_insights.append(f"Additional {len(num_cols) - 3} numerical columns not analyzed here.")
2820
- # else:
2821
- # dist_insights.append("No numerical columns found for distribution analysis.")
2822
-
2823
- # insights["Distribution Insights"] = dist_insights
2824
-
2825
- # # Add correlation insights
2826
- # corr_insights = []
2827
- # if len(num_cols) > 1:
2828
- # # Calculate correlation
2829
- # corr_matrix = df[num_cols].corr()
2830
- # high_corr = []
2831
-
2832
- # # Find high correlations
2833
- # for i in range(len(corr_matrix.columns)):
2834
- # for j in range(i):
2835
- # if abs(corr_matrix.iloc[i, j]) > 0.7:
2836
- # high_corr.append((corr_matrix.columns[i], corr_matrix.columns[j], corr_matrix.iloc[i, j]))
2837
-
2838
- # if high_corr:
2839
- # corr_insights.append(f"Found {len(high_corr)} pairs of highly correlated features.")
2840
- # for col1, col2, corr_val in high_corr[:3]: # Show top 3
2841
- # corr_direction = "positively" if corr_val > 0 else "negatively"
2842
- # corr_insights.append(f"'{col1}' and '{col2}' are strongly {corr_direction} correlated (r={corr_val:.2f}).")
2843
-
2844
- # if len(high_corr) > 3:
2845
- # corr_insights.append(f"And {len(high_corr) - 3} more highly correlated pairs found.")
2846
-
2847
- # corr_insights.append("Consider removing some highly correlated features to reduce dimensionality.")
2848
- # else:
2849
- # corr_insights.append("No strong correlations found between features.")
2850
- # else:
2851
- # corr_insights.append("Need at least 2 numerical columns to analyze correlations.")
2852
-
2853
- # insights["Correlation Analysis"] = corr_insights
2854
-
2855
- # # Add feature engineering recommendations
2856
- # fe_insights = []
2857
-
2858
- # # Check for date columns
2859
- # date_cols = []
2860
- # for col in df.columns.tolist():
2861
- # if df[col].dtype == 'object':
2862
- # try:
2863
- # pd.to_datetime(df[col])
2864
- # date_cols.append(col)
2865
- # except:
2866
- # pass
2867
-
2868
- # if date_cols:
2869
- # fe_insights.append(f"Found {len(date_cols)} potential date columns: {', '.join(date_cols[:3])}.")
2870
- # fe_insights.append("Consider extracting year, month, day, weekday from these columns.")
2871
-
2872
- # # Check for categorical columns
2873
- # cat_cols = df.select_dtypes(include=['object']).columns
2874
- # if len(cat_cols) > 0:
2875
- # fe_insights.append(f"Found {len(cat_cols)} categorical columns.")
2876
- # fe_insights.append("Consider one-hot encoding or label encoding for categorical features.")
2877
-
2878
- # # Check for high cardinality
2879
- # high_card_cols = []
2880
- # for col in cat_cols:
2881
- # if df[col].nunique() > 10:
2882
- # high_card_cols.append((col, df[col].nunique()))
2883
-
2884
- # if high_card_cols:
2885
- # fe_insights.append(f"Some categorical columns have high cardinality:")
2886
- # for col, card in high_card_cols[:2]:
2887
- # fe_insights.append(f"Column '{col}' has {card} unique values. Consider grouping less common categories.")
2888
-
2889
- # # Suggest polynomial features if few numeric features
2890
- # if 1 < len(num_cols) < 5:
2891
- # fe_insights.append("Consider creating polynomial features or interaction terms between numerical features.")
2892
-
2893
- # insights["Feature Engineering Recommendations"] = fe_insights
2894
-
2895
- # # Add a slight delay to simulate processing
2896
- # time.sleep(1)
2897
-
2898
- # # Mark that the insights are loaded
2899
- # st.session_state['loading_insights'] = False
2900
- # logger.info("Template-based insights generation completed")
2901
-
2902
- # return insights
2903
-
2904
- # def display_chat_interface():
2905
- # """Display a chat interface for interacting with the data"""
2906
- # st.markdown('<div class="tab-content">', unsafe_allow_html=True)
2907
- # st.markdown('<h2 class="tab-title">💬 Chat with Your Data</h2>', unsafe_allow_html=True)
2908
-
2909
- # # Initialize chat history if not present
2910
- # if "chat_history" not in st.session_state:
2911
- # st.session_state.chat_history = []
2912
-
2913
- # # Make sure we have data to chat about
2914
- # if 'df' not in st.session_state or st.session_state.df is None:
2915
- # st.error("No dataset loaded. Please upload a CSV file to chat with your data.")
2916
-
2917
- # # Show a preview of chat capabilities
2918
- # st.markdown("""
2919
- # <div style="margin-top: 2rem;">
2920
- # <h3>What can I help you with?</h3>
2921
- # <p>Once you upload a dataset, you can ask questions like:</p>
2922
- # <ul>
2923
- # <li>What patterns do you see in my data?</li>
2924
- # <li>How many missing values are there?</li>
2925
- # <li>What feature engineering would you recommend?</li>
2926
- # <li>Show me the distribution of a specific column</li>
2927
- # <li>What are the correlations between features?</li>
2928
- # </ul>
2929
- # </div>
2930
- # """, unsafe_allow_html=True)
2931
-
2932
- # st.markdown('</div>', unsafe_allow_html=True)
2933
- # return
2934
-
2935
- # # Display chat history
2936
- # for message in st.session_state.chat_history:
2937
- # if message["role"] == "user":
2938
- # st.chat_message("user").write(message["content"])
2939
- # else:
2940
- # st.chat_message("assistant").write(message["content"])
2941
-
2942
- # # If no chat history, show some example questions
2943
- # if not st.session_state.chat_history:
2944
- # st.info("Ask me anything about your dataset! I can help you understand patterns, identify issues, and suggest improvements.")
2945
-
2946
- # st.markdown("### Example questions you can ask:")
2947
-
2948
- # # Create a grid of example questions using columns
2949
- # col1, col2 = st.columns(2)
2950
-
2951
- # with col1:
2952
- # example_questions = [
2953
- # "What are the key patterns in this dataset?",
2954
- # "Which columns have missing values?",
2955
- # "What kind of feature engineering would help?"
2956
- # ]
2957
-
2958
- # for i, question in enumerate(example_questions):
2959
- # if st.button(question, key=f"example_q_{i}"):
2960
- # process_chat_message(question)
2961
- # st.rerun()
2962
-
2963
- # with col2:
2964
- # more_questions = [
2965
- # "How are the numerical variables distributed?",
2966
- # "What are the strongest correlations?",
2967
- # "How can I prepare this data for modeling?"
2968
- # ]
2969
-
2970
- # for i, question in enumerate(more_questions):
2971
- # if st.button(question, key=f"example_q_{i+3}"):
2972
- # process_chat_message(question)
2973
- # st.rerun()
2974
-
2975
- # # Input area for new messages
2976
- # user_input = st.chat_input("Ask a question about your data...", key="chat_input")
2977
-
2978
- # if user_input:
2979
- # # Add user message to chat history
2980
- # process_chat_message(user_input)
2981
- # st.rerun()
2982
-
2983
- # st.markdown('</div>', unsafe_allow_html=True)
2984
-
2985
- # def display_descriptive_tab():
2986
- # st.markdown('<div class="tab-content">', unsafe_allow_html=True)
2987
- # st.markdown('<h2 class="tab-title">📊 Descriptive Statistics</h2>', unsafe_allow_html=True)
2988
-
2989
- # # Make sure we access the data from session state
2990
- # if 'df' not in st.session_state or 'descriptive_stats' not in st.session_state:
2991
- # st.error("No dataset loaded. Please upload a CSV file.")
2992
- # st.markdown('</div>', unsafe_allow_html=True)
2993
- # return
2994
-
2995
- # df = st.session_state.df
2996
- # descriptive_stats = st.session_state.descriptive_stats
2997
-
2998
- # # Display descriptive statistics in a more visually appealing way
2999
- # col1, col2 = st.columns([3, 1])
3000
-
3001
- # with col1:
3002
- # # Style the dataframe
3003
- # st.markdown('<div class="stats-card">', unsafe_allow_html=True)
3004
- # st.subheader("Numerical Summary")
3005
- # st.dataframe(descriptive_stats.style.background_gradient(cmap='Blues', axis=0)
3006
- # .format(precision=2, na_rep="Missing"), use_container_width=True)
3007
- # st.markdown('</div>', unsafe_allow_html=True)
3008
-
3009
- # with col2:
3010
- # st.markdown('<div class="info-card">', unsafe_allow_html=True)
3011
- # st.subheader("Dataset Overview")
3012
-
3013
- # # Display dataset information in a cleaner format
3014
- # total_rows = df.shape[0]
3015
- # total_cols = df.shape[1]
3016
- # numeric_cols = len(df.select_dtypes(include=['number']).columns)
3017
- # cat_cols = len(df.select_dtypes(include=['object', 'category']).columns)
3018
- # date_cols = len(df.select_dtypes(include=['datetime']).columns)
3019
-
3020
- # st.markdown(f"""
3021
- # <div class="dataset-stats">
3022
- # <div class="stat-item">
3023
- # <div class="stat-value">{total_rows:,}</div>
3024
- # <div class="stat-label">Rows</div>
3025
- # </div>
3026
- # <div class="stat-item">
3027
- # <div class="stat-value">{total_cols}</div>
3028
- # <div class="stat-label">Columns</div>
3029
- # </div>
3030
- # <div class="stat-item">
3031
- # <div class="stat-value">{numeric_cols}</div>
3032
- # <div class="stat-label">Numerical</div>
3033
- # </div>
3034
- # <div class="stat-item">
3035
- # <div class="stat-value">{cat_cols}</div>
3036
- # <div class="stat-label">Categorical</div>
3037
- # </div>
3038
- # <div class="stat-item">
3039
- # <div class="stat-value">{date_cols}</div>
3040
- # <div class="stat-label">Date/Time</div>
3041
- # </div>
3042
- # </div>
3043
- # """, unsafe_allow_html=True)
3044
- # st.markdown('</div>', unsafe_allow_html=True)
3045
-
3046
- # # Add missing values information with visualization
3047
- # st.markdown('<div class="stats-card">', unsafe_allow_html=True)
3048
- # st.subheader("Missing Values")
3049
- # col1, col2 = st.columns([2, 3])
3050
-
3051
- # with col1:
3052
- # # Calculate missing values
3053
- # missing_data = df.isnull().sum()
3054
- # missing_percent = (missing_data / len(df)) * 100
3055
- # missing_data = pd.DataFrame({
3056
- # 'Missing Values': missing_data,
3057
- # 'Percentage (%)': missing_percent.round(2)
3058
- # })
3059
- # missing_data = missing_data[missing_data['Missing Values'] > 0].sort_values('Missing Values', ascending=False)
3060
-
3061
- # if not missing_data.empty:
3062
- # st.dataframe(missing_data.style.background_gradient(cmap='Reds', subset=['Percentage (%)'])
3063
- # .format({'Percentage (%)': '{:.2f}%'}), use_container_width=True)
3064
- # else:
3065
- # st.success("No missing values found in the dataset! 🎉")
3066
-
3067
- # with col2:
3068
- # if not missing_data.empty:
3069
- # # Create a horizontal bar chart for missing values
3070
- # fig = px.bar(missing_data,
3071
- # x='Percentage (%)',
3072
- # y=missing_data.index,
3073
- # orientation='h',
3074
- # color='Percentage (%)',
3075
- # color_continuous_scale='Reds',
3076
- # title='Missing Values by Column')
3077
-
3078
- # fig.update_layout(
3079
- # height=max(350, len(missing_data) * 30),
3080
- # xaxis_title='Missing (%)',
3081
- # yaxis_title='',
3082
- # coloraxis_showscale=False,
3083
- # margin=dict(l=0, r=10, t=30, b=0)
3084
- # )
3085
-
3086
- # st.plotly_chart(fig, use_container_width=True)
3087
-
3088
- # st.markdown('</div>', unsafe_allow_html=True)
3089
- # st.markdown('</div>', unsafe_allow_html=True)
3090
-
3091
- # def display_distribution_tab():
3092
- # st.markdown('<div class="tab-content">', unsafe_allow_html=True)
3093
- # st.markdown('<h2 class="tab-title">📈 Data Distribution</h2>', unsafe_allow_html=True)
3094
-
3095
- # # Make sure we access the data from session state
3096
- # if 'df' not in st.session_state:
3097
- # st.error("No dataset loaded. Please upload a CSV file.")
3098
- # st.markdown('</div>', unsafe_allow_html=True)
3099
- # return
3100
-
3101
- # df = st.session_state.df
3102
-
3103
- # # Add filters for better UX
3104
- # st.markdown('<div class="filter-container">', unsafe_allow_html=True)
3105
- # col1, col2 = st.columns([1, 1])
3106
-
3107
- # with col1:
3108
- # chart_type = st.selectbox(
3109
- # "Select Chart Type",
3110
- # ["Histogram", "Box Plot", "Violin Plot", "Distribution Plot"],
3111
- # key="chart_type_select"
3112
- # )
3113
-
3114
- # # with col2:
3115
- # # if chart_type != "Distribution Plot":
3116
- # # column_type = "Numerical" if chart_type in ["Histogram", "Box Plot", "Violin Plot"] else "Categorical"
3117
- # # columns_to_show = df.select_dtypes(include=['number']).columns.tolist() if column_type == "Numerical" else df.select_dtypes(include=['object', 'category']).columns.tolist()
3118
-
3119
- # # selected_columns = st.multiselect(
3120
- # # f"Select {column_type} Columns to Visualize",
3121
- # # options=columns_to_show,
3122
- # # default=columns_to_show[:min(3, len(columns_to_show))],
3123
- # # key="column_select"
3124
- # # )
3125
- # # else:
3126
- # # num_cols = df.select_dtypes(include=['number']).columns.tolist()
3127
- # # selected_columns = st.multiselect(
3128
- # # "Select Numerical Columns",
3129
- # # options=num_cols,
3130
- # # default=num_cols[:min(3, len(num_cols))],
3131
- # # key="column_select"
3132
- # # )
3133
-
3134
-
3135
-
3136
- # with col2:
3137
- # if chart_type != "Distribution Plot":
3138
- # column_type = "Numerical" if chart_type in ["Histogram", "Box Plot", "Violin Plot"] else "Categorical"
3139
- # columns_to_show = list(df.select_dtypes(include=['number']).columns) if column_type == "Numerical" else list(df.select_dtypes(include=['object', 'category']).columns)
3140
-
3141
- # selected_columns = st.multiselect(
3142
- # f"Select {column_type} Columns to Visualize",
3143
- # options=columns_to_show,
3144
- # default=list(columns_to_show[:min(3, len(columns_to_show))]), # Convert to list ✅
3145
- # key="column_select"
3146
- # )
3147
- # else:
3148
- # num_cols = list(df.select_dtypes(include=['number']).columns) # Convert to list ✅
3149
- # selected_columns = st.multiselect(
3150
- # "Select Numerical Columns",
3151
- # options=num_cols,
3152
- # default=list(num_cols[:min(3, len(num_cols))]), # Convert to list ✅
3153
- # key="column_select"
3154
- # )
3155
-
3156
- # st.markdown('</div>', unsafe_allow_html=True)
3157
-
3158
- # # Display selected charts
3159
- # if selected_columns:
3160
- # st.markdown('<div class="chart-container">', unsafe_allow_html=True)
3161
-
3162
- # if chart_type == "Histogram":
3163
- # col1, col2 = st.columns([3, 1])
3164
- # with col2:
3165
- # bins = st.slider("Number of bins", min_value=5, max_value=100, value=30, key="hist_bins")
3166
- # kde = st.checkbox("Show KDE", value=True, key="show_kde")
3167
-
3168
- # with col1:
3169
- # pass
3170
-
3171
- # # Display histograms with better styling
3172
- # for column in selected_columns:
3173
- # st.markdown(f'<div class="chart-card"><h3>{column}</h3>', unsafe_allow_html=True)
3174
- # fig = px.histogram(df, x=column, nbins=bins,
3175
- # title=f"Histogram of {column}",
3176
- # marginal="box" if kde else None,
3177
- # color_discrete_sequence=['rgba(99, 102, 241, 0.7)'])
3178
-
3179
- # fig.update_layout(
3180
- # template="plotly_white",
3181
- # height=400,
3182
- # margin=dict(l=10, r=10, t=40, b=10),
3183
- # xaxis_title=column,
3184
- # yaxis_title="Frequency",
3185
- # bargap=0.1
3186
- # )
3187
-
3188
- # st.plotly_chart(fig, use_container_width=True)
3189
-
3190
- # # Show basic statistics
3191
- # stats = df[column].describe().to_dict()
3192
- # st.markdown(f"""
3193
- # <div class="stat-summary">
3194
- # <div class="stat-pair"><span>Mean:</span> <strong>{stats['mean']:.2f}</strong></div>
3195
- # <div class="stat-pair"><span>Median:</span> <strong>{stats['50%']:.2f}</strong></div>
3196
- # <div class="stat-pair"><span>Std Dev:</span> <strong>{stats['std']:.2f}</strong></div>
3197
- # <div class="stat-pair"><span>Min:</span> <strong>{stats['min']:.2f}</strong></div>
3198
- # <div class="stat-pair"><span>Max:</span> <strong>{stats['max']:.2f}</strong></div>
3199
- # </div>
3200
- # """, unsafe_allow_html=True)
3201
- # st.markdown('</div>', unsafe_allow_html=True)
3202
-
3203
- # elif chart_type == "Box Plot":
3204
- # for column in selected_columns:
3205
- # st.markdown(f'<div class="chart-card"><h3>{column}</h3>', unsafe_allow_html=True)
3206
- # fig = px.box(df, y=column, title=f"Box Plot of {column}",
3207
- # color_discrete_sequence=['rgba(99, 102, 241, 0.7)'])
3208
-
3209
- # fig.update_layout(
3210
- # template="plotly_white",
3211
- # height=400,
3212
- # margin=dict(l=10, r=10, t=40, b=10),
3213
- # yaxis_title=column
3214
- # )
3215
-
3216
- # st.plotly_chart(fig, use_container_width=True)
3217
-
3218
- # # Show outlier information
3219
- # q1 = df[column].quantile(0.25)
3220
- # q3 = df[column].quantile(0.75)
3221
- # iqr = q3 - q1
3222
- # lower_bound = q1 - 1.5 * iqr
3223
- # upper_bound = q3 + 1.5 * iqr
3224
- # outliers = df[(df[column] < lower_bound) | (df[column] > upper_bound)][column]
3225
-
3226
- # st.markdown(f"""
3227
- # <div class="stat-summary">
3228
- # <div class="stat-pair"><span>Q1 (25%):</span> <strong>{q1:.2f}</strong></div>
3229
- # <div class="stat-pair"><span>Median:</span> <strong>{df[column].median():.2f}</strong></div>
3230
- # <div class="stat-pair"><span>Q3 (75%):</span> <strong>{q3:.2f}</strong></div>
3231
- # <div class="stat-pair"><span>IQR:</span> <strong>{iqr:.2f}</strong></div>
3232
- # <div class="stat-pair"><span>Outliers:</span> <strong>{len(outliers)}</strong> ({(len(outliers)/len(df)*100):.2f}%)</div>
3233
- # </div>
3234
- # """, unsafe_allow_html=True)
3235
- # st.markdown('</div>', unsafe_allow_html=True)
3236
-
3237
- # elif chart_type == "Violin Plot":
3238
- # for column in selected_columns:
3239
- # st.markdown(f'<div class="chart-card"><h3>{column}</h3>', unsafe_allow_html=True)
3240
- # fig = px.violin(df, y=column, box=True, points="all", title=f"Violin Plot of {column}",
3241
- # color_discrete_sequence=['rgba(99, 102, 241, 0.7)'])
3242
-
3243
- # fig.update_layout(
3244
- # template="plotly_white",
3245
- # height=400,
3246
- # margin=dict(l=10, r=10, t=40, b=10),
3247
- # yaxis_title=column
3248
- # )
3249
-
3250
- # fig.update_traces(marker=dict(size=3, opacity=0.5))
3251
- # st.plotly_chart(fig, use_container_width=True)
3252
- # st.markdown('</div>', unsafe_allow_html=True)
3253
-
3254
- # elif chart_type == "Distribution Plot":
3255
- # if len(selected_columns) >= 2:
3256
- # st.markdown('<div class="chart-card">', unsafe_allow_html=True)
3257
- # chart_options = st.radio(
3258
- # "Select Distribution Plot Type",
3259
- # ["Scatter Plot", "Correlation Heatmap"],
3260
- # horizontal=True
3261
- # )
3262
-
3263
- # if chart_options == "Scatter Plot":
3264
- # col1, col2 = st.columns([3, 1])
3265
- # with col2:
3266
- # x_axis = st.selectbox("X-axis", options=selected_columns, index=0)
3267
- # y_axis = st.selectbox("Y-axis", options=selected_columns, index=min(1, len(selected_columns)-1))
3268
- # color_option = st.selectbox("Color by", options=["None"] + df.columns.tolist())
3269
-
3270
- # with col1:
3271
- # if color_option != "None":
3272
- # fig = px.scatter(df, x=x_axis, y=y_axis,
3273
- # color=color_option,
3274
- # title=f"{y_axis} vs {x_axis} (colored by {color_option})",
3275
- # opacity=0.7,
3276
- # marginal_x="histogram", marginal_y="histogram")
3277
- # else:
3278
- # fig = px.scatter(df, x=x_axis, y=y_axis,
3279
- # title=f"{y_axis} vs {x_axis}",
3280
- # opacity=0.7,
3281
- # marginal_x="histogram", marginal_y="histogram")
3282
-
3283
- # fig.update_layout(
3284
- # template="plotly_white",
3285
- # height=600,
3286
- # margin=dict(l=10, r=10, t=40, b=10),
3287
- # )
3288
-
3289
- # st.plotly_chart(fig, use_container_width=True)
3290
-
3291
- # elif chart_options == "Correlation Heatmap":
3292
- # # Calculate correlation matrix
3293
- # corr_matrix = df[selected_columns].corr()
3294
-
3295
- # # Create heatmap
3296
- # fig = px.imshow(corr_matrix,
3297
- # text_auto=".2f",
3298
- # color_continuous_scale="RdBu_r",
3299
- # zmin=-1, zmax=1,
3300
- # title="Correlation Heatmap")
3301
-
3302
- # fig.update_layout(
3303
- # template="plotly_white",
3304
- # height=600,
3305
- # margin=dict(l=10, r=10, t=40, b=10),
3306
- # )
3307
-
3308
- # st.plotly_chart(fig, use_container_width=True)
3309
-
3310
- # # Show highest correlations
3311
- # corr_df = corr_matrix.stack().reset_index()
3312
- # corr_df.columns = ['Variable 1', 'Variable 2', 'Correlation']
3313
- # corr_df = corr_df[corr_df['Variable 1'] != corr_df['Variable 2']]
3314
- # corr_df = corr_df.sort_values('Correlation', ascending=False).head(5)
3315
-
3316
- # st.markdown("##### Top 5 Highest Correlations")
3317
- # st.dataframe(corr_df.style.background_gradient(cmap='Blues')
3318
- # .format({'Correlation': '{:.2f}'}), use_container_width=True)
3319
- # st.markdown('</div>', unsafe_allow_html=True)
3320
- # else:
3321
- # st.warning("Please select at least 2 numerical columns to see distribution plots")
3322
-
3323
- # st.markdown('</div>', unsafe_allow_html=True)
3324
- # else:
3325
- # st.info("Please select at least one column to visualize")
3326
-
3327
- # st.markdown('</div>', unsafe_allow_html=True)
3328
-
3329
- # def display_ai_insights_tab():
3330
- # st.markdown('<div class="tab-content">', unsafe_allow_html=True)
3331
- # st.markdown('<h2 class="tab-title">🧠 AI-Generated Insights</h2>', unsafe_allow_html=True)
3332
-
3333
- # # Make sure we access the data from session state
3334
- # if 'df' not in st.session_state:
3335
- # st.error("No dataset loaded. Please upload a CSV file.")
3336
- # st.markdown('</div>', unsafe_allow_html=True)
3337
- # return
3338
-
3339
- # if st.session_state.get('loading_insights', False):
3340
- # with st.spinner("Generating AI insights about your data..."):
3341
- # st.markdown('<div class="loading-container"><div class="loading-pulse"></div></div>', unsafe_allow_html=True)
3342
- # time.sleep(0.1) # Small delay to ensure UI updates
3343
-
3344
- # # AI insights section
3345
- # if 'ai_insights' in st.session_state and st.session_state.ai_insights and len(st.session_state.ai_insights) > 0:
3346
- # insights = st.session_state.ai_insights
3347
-
3348
- # st.markdown('<div class="insights-container">', unsafe_allow_html=True)
3349
-
3350
- # for i, (category, insight_list) in enumerate(insights.items()):
3351
- # with st.expander(f"{category}", expanded=i < 2):
3352
- # st.markdown('<div class="insights-category">', unsafe_allow_html=True)
3353
-
3354
- # # Check if the insights are from LLM (single string) or template (list of strings)
3355
- # if len(insight_list) == 1 and isinstance(insight_list[0], str) and len(insight_list[0]) > 100:
3356
- # # This is likely an LLM-generated insight (single long string)
3357
- # st.markdown(insight_list[0])
3358
- # else:
3359
- # # Template-based insights (list of strings)
3360
- # for insight in insight_list:
3361
- # st.markdown(f"""
3362
- # <div class="insight-card">
3363
- # <div class="insight-content">
3364
- # <div class="insight-icon">💡</div>
3365
- # <div class="insight-text">{insight}</div>
3366
- # </div>
3367
- # </div>
3368
- # """, unsafe_allow_html=True)
3369
-
3370
- # st.markdown('</div>', unsafe_allow_html=True)
3371
-
3372
- # st.markdown('</div>', unsafe_allow_html=True)
3373
-
3374
- # # Add regenerate button
3375
- # st.markdown('<div style="text-align: center; margin-top: 20px;">', unsafe_allow_html=True)
3376
- # if st.button("Regenerate Insights", key="regenerate_insights"):
3377
- # st.session_state['loading_insights'] = True
3378
- # st.session_state['ai_insights'] = None
3379
- # logger.info("User requested regeneration of AI insights")
3380
- # st.rerun()
3381
- # st.markdown('</div>', unsafe_allow_html=True)
3382
- # else:
3383
- # if not st.session_state.get('loading_insights', False):
3384
- # # Show generate button if insights are not loading and not available
3385
- # st.markdown('<div class="generate-insights-container">', unsafe_allow_html=True)
3386
- # st.markdown("""
3387
- # <div class="placeholder-card">
3388
- # <div class="placeholder-icon">🧠</div>
3389
- # <div class="placeholder-text">Generate AI-powered insights about your dataset to discover patterns, anomalies, and suggestions for feature engineering.</div>
3390
- # </div>
3391
- # """, unsafe_allow_html=True)
3392
- # if st.button("Generate Insights", key="generate_insights"):
3393
- # st.session_state['loading_insights'] = True
3394
- # logger.info("User initiated AI insights generation")
3395
- # st.rerun()
3396
- # st.markdown('</div>', unsafe_allow_html=True)
3397
-
3398
- # st.markdown('</div>', unsafe_allow_html=True)
3399
-
3400
- # def display_welcome_page():
3401
- # """Display a welcome page with information about the application"""
3402
- # # Use Streamlit columns and components instead of raw HTML
3403
- # st.title("Welcome to AI-Powered EDA & Feature Engineering Assistant")
3404
-
3405
- # st.write("""
3406
- # Upload your CSV dataset and leverage the power of AI to analyze, visualize, and improve your data.
3407
- # This tool helps you understand your data better and prepare it for machine learning models.
3408
- # """)
3409
-
3410
- # # Feature cards
3411
- # st.subheader("Key Features")
3412
-
3413
- # # Use Streamlit columns to create a grid layout
3414
- # col1, col2 = st.columns(2)
3415
-
3416
- # with col1:
3417
- # st.markdown("#### 📊 Exploratory Data Analysis")
3418
- # st.write("Quickly understand your dataset with automatic statistical analysis and visualizations")
3419
-
3420
- # st.markdown("#### 🧠 AI-Powered Insights")
3421
- # st.write("Get intelligent recommendations about patterns, anomalies, and opportunities in your data")
3422
-
3423
- # st.markdown("#### ⚡ Feature Engineering")
3424
- # st.write("Transform and enhance your features to improve machine learning model performance")
3425
-
3426
- # with col2:
3427
- # st.markdown("#### 📈 Interactive Visualizations")
3428
- # st.write("Explore distributions, relationships, and outliers with dynamic charts")
3429
-
3430
- # st.markdown("#### 💬 Chat Interface")
3431
- # st.write("Ask questions about your data and get AI-powered answers in natural language")
3432
-
3433
- # st.markdown("#### 🔄 Data Transformation")
3434
- # st.write("Clean, transform, and prepare your data for modeling with guided workflows")
3435
-
3436
- # # Usage section
3437
- # st.subheader("How to use")
3438
-
3439
- # st.markdown("""
3440
- # 1. **Upload** your CSV dataset using the sidebar on the left
3441
- # 2. **Explore** automatically generated statistics and visualizations
3442
- # 3. **Generate** AI insights to better understand your data
3443
- # 4. **Chat** with AI to ask specific questions about your dataset
3444
- # 5. **Transform** your features based on recommendations
3445
- # """)
3446
-
3447
- # # # Powered by section
3448
- # # st.subheader("Powered by")
3449
- # # cols = st.columns(3)
3450
- # # with cols[0]:
3451
- # # st.markdown("**llama3-8b-8192**")
3452
- # # with cols[1]:
3453
- # # st.markdown("**Groq API**")
3454
- # # with cols[2]:
3455
- # # st.markdown("**Streamlit**")
3456
-
3457
- # # Upload prompt
3458
- # st.info("👈 Please upload a CSV file using the sidebar to get started")
3459
-
3460
- # def display_relationships_tab():
3461
- # """Display correlations and relationships between variables"""
3462
- # st.markdown('<div class="tab-content">', unsafe_allow_html=True)
3463
- # st.markdown('<h2 class="tab-title">🔄 Relationships & Correlations</h2>', unsafe_allow_html=True)
3464
-
3465
- # # Make sure we have data to visualize
3466
- # if 'df' not in st.session_state or st.session_state.df is None:
3467
- # st.error("No dataset loaded. Please upload a CSV file.")
3468
- # st.markdown('</div>', unsafe_allow_html=True)
3469
- # return
3470
-
3471
- # df = st.session_state.df
3472
-
3473
- # # Select numerical columns for correlation analysis
3474
- # num_cols = df.select_dtypes(include=['number']).columns
3475
-
3476
- # if len(num_cols) < 2:
3477
- # st.warning("At least 2 numerical columns are needed for correlation analysis.")
3478
- # st.markdown('</div>', unsafe_allow_html=True)
3479
- # return
3480
-
3481
- # # Correlation matrix heatmap
3482
- # st.subheader("Correlation Matrix")
3483
-
3484
- # # Calculate correlation
3485
- # corr_matrix = df[num_cols].corr()
3486
-
3487
- # # Create correlation heatmap
3488
- # fig = px.imshow(
3489
- # corr_matrix,
3490
- # text_auto=".2f",
3491
- # color_continuous_scale="RdBu_r",
3492
- # zmin=-1, zmax=1,
3493
- # aspect="auto",
3494
- # title="Correlation Heatmap"
3495
- # )
3496
-
3497
- # fig.update_layout(
3498
- # height=600,
3499
- # width=800,
3500
- # title_font_size=20,
3501
- # margin=dict(l=10, r=10, t=30, b=10)
3502
- # )
3503
-
3504
- # st.plotly_chart(fig, use_container_width=True)
3505
-
3506
- # # Show top correlations
3507
- # st.subheader("Top Correlations")
3508
-
3509
- # # Extract and format correlations
3510
- # corr_pairs = []
3511
- # for i in range(len(num_cols)):
3512
- # for j in range(i):
3513
- # corr_pairs.append({
3514
- # 'Feature 1': num_cols[i],
3515
- # 'Feature 2': num_cols[j],
3516
- # 'Correlation': corr_matrix.iloc[i, j]
3517
- # })
3518
-
3519
- # # Convert to dataframe and sort
3520
- # corr_df = pd.DataFrame(corr_pairs)
3521
- # sorted_corr = corr_df.sort_values('Correlation', key=abs, ascending=False).head(10)
3522
-
3523
- # # Show table with styled background
3524
- # st.dataframe(
3525
- # sorted_corr.style.background_gradient(cmap='RdBu_r', subset=['Correlation'])
3526
- # .format({'Correlation': '{:.3f}'}),
3527
- # use_container_width=True
3528
- # )
3529
-
3530
- # # Scatter plot matrix
3531
- # st.subheader("Scatter Plot Matrix")
3532
-
3533
- # # # Let user choose columns
3534
- # # selected_cols = st.multiselect(
3535
- # # "Select columns for scatter plot matrix (max 5 recommended)",
3536
- # # options=num_cols,
3537
- # # default=num_cols[:min(4, len(num_cols))]
3538
- # # )
3539
-
3540
-
3541
- # # Convert num_cols to a list before using it in multiselect
3542
- # num_cols = list(df.select_dtypes(include=['number']).columns)
3543
-
3544
- # # Ensure default selection is also a list
3545
- # selected_cols = st.multiselect(
3546
- # "Select columns for scatter plot matrix (max 5 recommended)",
3547
- # options=num_cols,
3548
- # default=list(num_cols[:min(4, len(num_cols))]) # Convert to list ✅
3549
- # )
3550
-
3551
- # if selected_cols:
3552
- # if len(selected_cols) > 5:
3553
- # st.warning("More than 5 columns may make the plot hard to read.")
3554
-
3555
- # color_col = st.selectbox("Color by", options=["None"] + df.columns.tolist())
3556
-
3557
- # # Only pass the color parameter if not "None"
3558
- # if color_col != "None":
3559
- # fig = px.scatter_matrix(
3560
- # df,
3561
- # dimensions=selected_cols,
3562
- # color=color_col,
3563
- # opacity=0.7,
3564
- # title="Scatter Plot Matrix"
3565
- # )
3566
- # else:
3567
- # fig = px.scatter_matrix(
3568
- # df,
3569
- # dimensions=selected_cols,
3570
- # opacity=0.7,
3571
- # title="Scatter Plot Matrix"
3572
- # )
3573
-
3574
- # fig.update_layout(
3575
- # height=700,
3576
- # title_font_size=18,
3577
- # margin=dict(l=10, r=10, t=30, b=10)
3578
- # )
3579
-
3580
- # st.plotly_chart(fig, use_container_width=True)
3581
-
3582
- # st.markdown('</div>', unsafe_allow_html=True)
3583
-
3584
- # def process_chat_message(user_message):
3585
- # """Process a user message in the chat interface"""
3586
- # # Add user message to chat history
3587
- # st.session_state.chat_history.append({"role": "user", "content": user_message})
3588
-
3589
- # # Generate a response from the AI
3590
- # if 'df' in st.session_state and st.session_state.df is not None:
3591
- # # Try to use LLM if available, otherwise fall back to templates
3592
- # try:
3593
- # if llm_inference is not None:
3594
- # # Create a prompt about the dataset
3595
- # df = st.session_state.df
3596
-
3597
- # # Get basic dataset info
3598
- # num_rows, num_cols = df.shape
3599
- # num_numerical = len(df.select_dtypes(include=['number']).columns)
3600
- # num_categorical = len(df.select_dtypes(include=['object', 'category']).columns)
3601
- # num_missing = df.isnull().sum().sum()
3602
- # missing_cols = df.isnull().sum()[df.isnull().sum() > 0]
3603
-
3604
- # # Format missing values for better readability
3605
- # missing_values = {}
3606
- # for col in missing_cols.index:
3607
- # count = missing_cols[col]
3608
- # percent = round(count / len(df) * 100, 2)
3609
- # missing_values[col] = (count, percent)
3610
-
3611
- # # Get correlations for numerical columns
3612
- # num_cols = df.select_dtypes(include=['number']).columns
3613
- # correlations = "No numerical columns to calculate correlations."
3614
- # if len(num_cols) > 1:
3615
- # # Calculate correlations
3616
- # corr_matrix = df[num_cols].corr()
3617
- # # Get top 5 correlations (absolute values)
3618
- # corr_pairs = []
3619
- # for i in range(len(num_cols)):
3620
- # for j in range(i):
3621
- # val = corr_matrix.iloc[i, j]
3622
- # if abs(val) > 0.5: # Only show strong correlations
3623
- # corr_pairs.append((num_cols[i], num_cols[j], val))
3624
-
3625
- # # Sort by absolute correlation and format
3626
- # if corr_pairs:
3627
- # corr_pairs.sort(key=lambda x: abs(x[2]), reverse=True)
3628
- # formatted_corrs = []
3629
- # for col1, col2, val in corr_pairs[:5]: # Top 5
3630
- # formatted_corrs.append(f"{col1} and {col2}: {val:.3f}")
3631
- # correlations = "\n".join(formatted_corrs)
3632
-
3633
- # # Create dataset_info dictionary for LLM
3634
- # dataset_info = {
3635
- # "shape": f"{num_rows} rows, {num_cols} columns",
3636
- # "columns": df.columns.tolist(),
3637
- # "dtypes": {col: str(dtype) for col, dtype in df.dtypes.items()},
3638
- # "missing_values": missing_values,
3639
- # "basic_stats": df.describe().to_string(),
3640
- # "correlations": correlations,
3641
- # "sample_data": df.head(5).to_string()
3642
- # }
3643
-
3644
- # # Generate response using LLM
3645
- # logger.info(f"Sending question to LLM: {user_message}")
3646
- # response = llm_inference.answer_dataset_question(user_message, dataset_info)
3647
-
3648
- # # Log the raw response for debugging
3649
- # logger.info(f"Raw LLM response: {response[:100]}...")
3650
-
3651
- # # If response is not empty and is a valid string
3652
- # if response and isinstance(response, str) and len(response) > 10:
3653
- # # Clean up the response if needed
3654
- # cleaned_response = response.strip()
3655
-
3656
- # # Add to chat history
3657
- # st.session_state.chat_history.append({"role": "assistant", "content": cleaned_response})
3658
- # return
3659
- # else:
3660
- # logger.warning(f"LLM response too short or invalid: {response}")
3661
- # raise Exception("LLM response too short or invalid")
3662
- # else:
3663
- # raise Exception("LLM not available")
3664
-
3665
- # except Exception as e:
3666
- # logger.warning(f"Error using LLM for chat response: {str(e)}. Falling back to templates.")
3667
- # # Fall back happens below
3668
-
3669
- # # If we're here, either there's no dataframe, LLM failed, or response was invalid
3670
- # # Use template-based responses as fallback
3671
- # if 'df' in st.session_state and st.session_state.df is not None:
3672
- # df = st.session_state.df
3673
-
3674
- # # Simple response templates
3675
- # responses = {
3676
- # "missing": f"I found {df.isnull().sum().sum()} missing values across the dataset. The columns with the most missing values are: {df.isnull().sum().sort_values(ascending=False).head(3).index.tolist()}.",
3677
- # "pattern": "Looking at the data, I can see several interesting patterns. The numerical features show varied distributions, and there might be some correlations worth exploring further.",
3678
- # "feature": "Based on the data, I'd recommend feature engineering steps like handling missing values, encoding categorical variables, and possibly creating interaction terms for highly correlated features.",
3679
- # "distribution": f"The numerical variables show different distributions. Some appear to be normally distributed while others show skewness. Let me know if you want to see visualizations for specific columns.",
3680
- # "correlation": "I detected several strong correlations in the dataset. You might want to look at the correlation heatmap in the Relationships tab for more details.",
3681
- # "prepare": "To prepare this data for modeling, I suggest: 1) Handling missing values, 2) Encoding categorical variables, 3) Feature scaling, and 4) Possibly dimensionality reduction if you have many features."
3682
- # }
3683
-
3684
- # # Simple keyword matching for demo purposes
3685
- # if "missing" in user_message.lower():
3686
- # response = responses["missing"]
3687
- # elif "pattern" in user_message.lower():
3688
- # response = responses["pattern"]
3689
- # elif "feature" in user_message.lower() or "engineering" in user_message.lower():
3690
- # response = responses["feature"]
3691
- # elif "distribut" in user_message.lower():
3692
- # response = responses["distribution"]
3693
- # elif "correlat" in user_message.lower() or "relation" in user_message.lower():
3694
- # response = responses["correlation"]
3695
- # elif "prepare" in user_message.lower() or "model" in user_message.lower():
3696
- # response = responses["prepare"]
3697
- # else:
3698
- # # Generic response
3699
- # response = "I analyzed your dataset and found some interesting insights. You can explore different aspects of your data using the tabs above. Is there anything specific you'd like to know about your data?"
3700
- # else:
3701
- # response = "Please upload a dataset first so I can analyze it and answer your questions."
3702
-
3703
- # # Add AI response to chat history
3704
- # st.session_state.chat_history.append({"role": "assistant", "content": response})
3705
-
3706
- # def main():
3707
- # """Main function to run the application"""
3708
- # # Initialize session state at the beginning
3709
- # initialize_session_state()
3710
-
3711
- # # Apply CSS styling
3712
- # apply_custom_css()
3713
-
3714
- # # Sidebar for file upload and settings
3715
- # with st.sidebar:
3716
- # st.markdown('<div class="sidebar-header">AI-Powered EDA & Feature Engineering</div>', unsafe_allow_html=True)
3717
-
3718
- # # File uploader
3719
- # st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
3720
- # st.markdown('### Upload Dataset')
3721
- # uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
3722
- # st.markdown('</div>', unsafe_allow_html=True)
3723
-
3724
- # # Load example dataset
3725
- # with st.expander("Or use an example dataset"):
3726
- # example_datasets = {
3727
- # "Iris": "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv",
3728
- # "Tips": "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv",
3729
- # "Titanic": "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/titanic.csv",
3730
- # "Diamonds": "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/diamonds.csv"
3731
- # }
3732
- # selected_example = st.selectbox("Select example dataset", list(example_datasets.keys()))
3733
- # if st.button("Load Example", key="load_example_btn"):
3734
- # try:
3735
- # # Load the selected example dataset
3736
- # df = pd.read_csv(example_datasets[selected_example])
3737
-
3738
- # # Verify we have a valid dataframe
3739
- # if df is not None and not df.empty:
3740
- # st.session_state['df'] = df
3741
- # st.session_state['descriptive_stats'] = df.describe()
3742
- # st.session_state['dataset_name'] = selected_example
3743
- # st.success(f"Loaded {selected_example} dataset!")
3744
- # else:
3745
- # st.error(f"The {selected_example} dataset appears to be empty.")
3746
- # except Exception as e:
3747
- # st.error(f"Error loading example dataset: {str(e)}")
3748
-
3749
- # # Only show these sections if a dataset is loaded
3750
- # if 'df' in st.session_state:
3751
- # # Dataset Info
3752
- # st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
3753
- # st.markdown(f'### Dataset Info: {st.session_state.get("dataset_name", "Uploaded Data")}')
3754
- # df = st.session_state.df
3755
- # # Add check to ensure df is not None before accessing shape
3756
- # if df is not None:
3757
- # st.write(f"Rows: {df.shape[0]}, Columns: {df.shape[1]}")
3758
- # else:
3759
- # st.error("Dataset is loaded but appears to be empty.")
3760
- # st.markdown('</div>', unsafe_allow_html=True)
3761
-
3762
- # # Column filters
3763
- # st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
3764
- # st.markdown('### Column Filters')
3765
- # if df is not None:
3766
- # selected_columns = st.multiselect("Select columns to analyze",
3767
- # options=df.columns.tolist(),
3768
- # default=df.columns.tolist())
3769
-
3770
- # if len(selected_columns) > 0:
3771
- # st.session_state['selected_columns'] = selected_columns
3772
- # st.session_state['filtered_df'] = df[selected_columns]
3773
- # else:
3774
- # st.session_state['selected_columns'] = df.columns.tolist()
3775
- # st.session_state['filtered_df'] = df
3776
- # st.markdown('</div>', unsafe_allow_html=True)
3777
-
3778
- # # Feature Engineering options with Streamlit buttons instead of JavaScript
3779
- # st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
3780
- # st.markdown('### Feature Engineering')
3781
-
3782
- # col1, col2 = st.columns(2)
3783
- # with col1:
3784
- # if st.button("Missing Values", key="missing_values_btn"):
3785
- # st.session_state['fe_selected'] = 'missing_values'
3786
-
3787
- # with col2:
3788
- # if st.button("Encode Categorical", key="encode_cat_btn"):
3789
- # st.session_state['fe_selected'] = 'encode_categorical'
3790
-
3791
- # col1, col2 = st.columns(2)
3792
- # with col1:
3793
- # if st.button("Scale Features", key="scale_features_btn"):
3794
- # st.session_state['fe_selected'] = 'scale_features'
3795
-
3796
- # with col2:
3797
- # if st.button("Transform", key="transform_btn"):
3798
- # st.session_state['fe_selected'] = 'transform'
3799
-
3800
- # # Display currently selected feature engineering option
3801
- # if 'fe_selected' in st.session_state:
3802
- # st.info(f"Selected: {st.session_state['fe_selected']}")
3803
-
3804
- # st.markdown('</div>', unsafe_allow_html=True)
3805
-
3806
- # # st.markdown('<div class="sidebar-footer">Powered by Hugging Face & Streamlit</div>', unsafe_allow_html=True)
3807
-
3808
- # # If data is uploaded, process it
3809
- # if uploaded_file is not None and ('df' not in st.session_state or st.session_state.get('df') is None):
3810
- # try:
3811
- # # Attempt to read the CSV file
3812
- # df = pd.read_csv(uploaded_file)
3813
-
3814
- # # Verify that we have a valid dataframe before storing in session state
3815
- # if df is not None and not df.empty:
3816
- # st.session_state['df'] = df
3817
- # st.session_state['descriptive_stats'] = df.describe()
3818
- # st.session_state['dataset_name'] = uploaded_file.name
3819
- # st.success(f"Successfully loaded dataset: {uploaded_file.name}")
3820
- # else:
3821
- # st.error("The uploaded file appears to be empty.")
3822
- # except Exception as e:
3823
- # st.error(f"Error reading CSV file: {str(e)}")
3824
-
3825
- # # Create navigation tabs using Streamlit
3826
- # st.write("### Navigation")
3827
- # tabs = ["Overview", "Distribution", "Relationships", "AI Insights", "Chat"]
3828
-
3829
- # # Create columns for each tab
3830
- # cols = st.columns(len(tabs))
3831
-
3832
- # # Handle tab selection using Streamlit buttons
3833
- # for i, tab in enumerate(tabs):
3834
- # with cols[i]:
3835
- # if st.button(tab, key=f"tab_{tab.lower()}"):
3836
- # st.session_state['selected_tab'] = f"tab-{tab.lower().replace(' ', '-')}"
3837
- # st.rerun()
3838
-
3839
- # # Show selected tab indicator
3840
- # selected_tab_name = st.session_state['selected_tab'].replace('tab-', '').replace('-', ' ').title()
3841
- # st.markdown(f"<div style='text-align: center; margin-bottom: 2rem;'>Selected: {selected_tab_name}</div>", unsafe_allow_html=True)
3842
-
3843
- # # Show welcome message if no data is uploaded
3844
- # if 'df' not in st.session_state:
3845
- # display_welcome_page()
3846
- # else:
3847
- # # Display content based on selected tab
3848
- # if st.session_state['selected_tab'] == 'tab-overview':
3849
- # display_descriptive_tab()
3850
- # elif st.session_state['selected_tab'] == 'tab-distribution':
3851
- # display_distribution_tab()
3852
- # elif st.session_state['selected_tab'] == 'tab-relationships':
3853
- # display_relationships_tab()
3854
- # elif st.session_state['selected_tab'] == 'tab-ai-insights' or st.session_state['selected_tab'] == 'tab-ai':
3855
- # display_ai_insights_tab()
3856
- # elif st.session_state['selected_tab'] == 'tab-chat':
3857
- # display_chat_interface()
3858
-
3859
- # # After all tabs are rendered, check if we have a regenerate action
3860
- # # This is processed at the end to avoid session state changes during rendering
3861
- # if (st.session_state.get('loading_insights', False) and
3862
- # ('ai_insights' not in st.session_state or st.session_state.get('ai_insights') is None)):
3863
- # logger.info("Generating AI insights at end of main function")
3864
- # try:
3865
- # st.session_state['ai_insights'] = generate_ai_insights()
3866
- # logger.info(f"Generated insights: {len(st.session_state['ai_insights'])} categories")
3867
- # st.session_state['loading_insights'] = False
3868
- # except Exception as e:
3869
- # logger.error(f"Error generating insights in main function: {str(e)}")
3870
- # st.session_state['loading_insights'] = False
3871
- # st.session_state['ai_insights'] = {} # Set to empty dict to prevent repeated failures
3872
- # finally:
3873
- # st.rerun()
3874
-
3875
- # if __name__ == "__main__":
3876
- # main()
 
1935
  st.rerun()
1936
 
1937
  if __name__ == "__main__":
1938
+ main()