| import streamlit as st |
| import sys |
| import os |
| import pandas as pd |
| import time |
|
|
| |
| st.set_page_config( |
| page_title="AutoML", |
| page_icon="πΈ", |
| layout="wide", |
| initial_sidebar_state="expanded", |
| menu_items={"Get Help": None, "Report a bug": None, "About": None}, |
| ) |
|
|
| |
| sys.path.extend([ |
| os.path.dirname(os.path.abspath(__file__)), |
| os.path.join(os.path.dirname(os.path.abspath(__file__)), "src") |
| ]) |
|
|
| |
| from src.ui.loading import show_loading_state |
| |
| from src.ui.css import load_css |
|
|
| |
| load_css() |
|
|
| |
| @st.cache_resource(ttl=3600) |
| def load_components(): |
| """Cache component imports to avoid reloading on every rerun""" |
| from src import ( |
| show_footer, |
| visualize_data, |
| show_welcome_page, |
| show_overview_page, |
| clean_csv, |
| model_training_tab, |
| display_ai_insights, |
| display_model_evaluation |
| ) |
| return (show_footer, visualize_data, |
| show_welcome_page, show_overview_page, clean_csv, |
| model_training_tab, display_ai_insights, display_model_evaluation) |
|
|
| |
| @st.cache_data(ttl=86400) |
| def render_header(): |
| """Cache static header HTML""" |
| return """ |
| <div class='app-header' style='padding: 1rem 0; margin-bottom: 2rem; text-align: center;'> |
| <h1 class='app-title' style='margin: 0;'>AutoML</h1> |
| <p class='app-tagline' style='margin-top: 0;'>Automated Machine Learning Made Simple.</p> |
| </div> |
| """ |
|
|
| |
| @st.cache_data(ttl=3600) |
| def load_default_data(): |
| """Load and cache the default dataset""" |
| try: |
| return pd.read_csv("laptop_data.csv") |
| except Exception as e: |
| st.error(f"β Error loading default dataset: {str(e)}") |
| return None |
|
|
| |
| def measure_time(func): |
| """Decorator to measure execution time of functions""" |
| def wrapper(*args, **kwargs): |
| start_time = time.time() |
| result = func(*args, **kwargs) |
| end_time = time.time() |
| execution_time = end_time - start_time |
| if execution_time > 1.0: |
| print(f"β±οΈ {func.__name__} took {execution_time:.2f} seconds to execute") |
| return result |
| return wrapper |
|
|
| @measure_time |
| def main(): |
| """Optimized main function for Streamlit AutoML app""" |
| |
| if "initialized" not in st.session_state: |
| |
| with st.container(): |
| show_loading_state() |
| |
| |
| st.empty().markdown("<style>#root > div:nth-child(1) > div > div > div > div > section > div {padding: 0rem;}</style>", unsafe_allow_html=True) |
| |
| |
| components = load_components() |
| (show_footer, visualize_data, |
| show_welcome_page, show_overview_page, clean_csv, |
| model_training_tab, display_ai_insights, display_model_evaluation) = components |
| |
| try: |
| |
| default_df = load_default_data() |
| if default_df is not None: |
| cleaned_df, insights = clean_csv(default_df) |
| |
| |
| st.session_state.update({ |
| "df": cleaned_df, |
| "insights": insights, |
| "components": components, |
| "initialized": True, |
| "current_tab_index": 0 |
| }) |
| |
| |
| st.rerun() |
| else: |
| st.error("β Failed to load default dataset") |
| return |
| |
| except Exception as e: |
| st.error(f"β Error during initialization: {str(e)}") |
| return |
|
|
| |
| if "initialized" in st.session_state: |
| components = st.session_state.components |
| (show_footer, visualize_data, |
| show_welcome_page, show_overview_page, clean_csv, |
| model_training_tab, display_ai_insights, display_model_evaluation) = components |
| |
| |
| st.markdown(render_header(), unsafe_allow_html=True) |
| |
| |
| TAB_NAMES = ["π Welcome", "π Overview", "π Visualization", |
| "π€ Model Training", "π‘ Insights", "π Test Results"] |
| |
| |
| if "current_tab_index" not in st.session_state: |
| st.session_state.current_tab_index = 0 |
| |
| |
| tab_index = st.tabs(TAB_NAMES) |
| |
| |
| with tab_index[0]: |
| show_welcome_page() |
| |
| with tab_index[1]: |
| show_overview_page() |
| |
| with tab_index[2]: |
| visualize_data(st.session_state.df) |
| |
| with tab_index[3]: |
| model_training_tab(st.session_state.df) |
| |
| with tab_index[4]: |
| display_ai_insights() |
| |
| with tab_index[5]: |
| display_model_evaluation() |
|
|
| show_footer() |
|
|
| if __name__ == "__main__": |
| main() |
|
|