""" Corpus Collection Engine - Main Streamlit Application AI-powered app for collecting diverse data on Indian languages, history, and culture """ import streamlit as st import sys import os # Add the parent directory to Python path for imports sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from corpus_collection_engine.activities.activity_router import ActivityRouter from corpus_collection_engine.utils.performance_optimizer import PerformanceOptimizer from corpus_collection_engine.utils.error_handler import global_error_handler, ErrorCategory, ErrorSeverity from corpus_collection_engine.services.privacy_service import PrivacyService from corpus_collection_engine.services.engagement_service import EngagementService from corpus_collection_engine.pwa.pwa_manager import PWAManager from corpus_collection_engine.utils.performance_dashboard import PerformanceDashboard # Configure Streamlit page st.set_page_config( page_title="Corpus Collection Engine", page_icon="๐Ÿ‡ฎ๐Ÿ‡ณ", layout="wide", initial_sidebar_state="expanded" ) def initialize_application(): """Initialize all application services and components""" # Initialize session state for global app management if 'app_initialized' not in st.session_state: st.session_state.app_initialized = False st.session_state.privacy_consent_given = False st.session_state.onboarding_completed = False st.session_state.admin_mode = False # Initialize services services = {} try: # Performance optimization services['optimizer'] = PerformanceOptimizer() services['optimizer'].initialize_performance_optimization() # Privacy service services['privacy'] = PrivacyService() # Engagement service services['engagement'] = EngagementService() # PWA manager services['pwa'] = PWAManager() services['pwa'].initialize_pwa() # Performance dashboard (for admin mode) services['performance_dashboard'] = PerformanceDashboard() st.session_state.app_initialized = True return services except Exception as e: global_error_handler.handle_error( e, ErrorCategory.SYSTEM, ErrorSeverity.HIGH, context={'component': 'app_initialization'}, show_user_message=True ) return {} def render_admin_interface(services): """Render admin interface for monitoring and management""" if not st.session_state.get('admin_mode', False): return with st.sidebar.expander("๐Ÿ”ง Admin Panel"): st.markdown("**System Monitoring**") if st.button("๐Ÿ“Š Performance Dashboard"): st.session_state.show_performance_dashboard = True if st.button("๐Ÿšจ Error Dashboard"): st.session_state.show_error_dashboard = True if st.button("๐Ÿ“ˆ Analytics Dashboard"): st.session_state.show_analytics_dashboard = True st.markdown("**System Actions**") if st.button("๐Ÿงน Clear Cache"): st.cache_data.clear() st.success("Cache cleared!") if st.button("๐Ÿ”„ Reset Session"): for key in list(st.session_state.keys()): if key not in ['app_initialized']: del st.session_state[key] st.success("Session reset!") st.rerun() def render_admin_dashboards(services): """Render admin dashboards when requested""" if st.session_state.get('show_performance_dashboard', False): st.markdown("---") services['performance_dashboard'].render_dashboard() if st.button("โŒ Close Performance Dashboard"): st.session_state.show_performance_dashboard = False st.rerun() if st.session_state.get('show_error_dashboard', False): st.markdown("---") global_error_handler.render_error_dashboard() if st.button("โŒ Close Error Dashboard"): st.session_state.show_error_dashboard = False st.rerun() if st.session_state.get('show_analytics_dashboard', False): st.markdown("---") if 'router' in st.session_state: router = st.session_state.router if hasattr(router, 'analytics_service'): router.analytics_service.render_analytics_dashboard() if st.button("โŒ Close Analytics Dashboard"): st.session_state.show_analytics_dashboard = False st.rerun() def handle_privacy_consent(privacy_service): """Handle privacy consent flow - Auto-consent for public deployment""" # Auto-consent for Hugging Face Spaces deployment if not st.session_state.privacy_consent_given: st.session_state.privacy_consent_given = True # Initialize privacy service without requiring explicit consent privacy_service.initialize_privacy_management() def handle_onboarding(engagement_service): """Handle user onboarding flow - Optional for public deployment""" if not st.session_state.onboarding_completed and st.session_state.privacy_consent_given: # Auto-complete onboarding for public deployment st.session_state.onboarding_completed = True # Show optional welcome message in sidebar with st.sidebar: st.success("๐ŸŽ‰ Welcome to Corpus Collection Engine!") st.markdown("Help preserve Indian cultural heritage through AI!") if st.button("โ„น๏ธ Show Quick Guide"): st.session_state.show_quick_guide = True def enable_admin_mode(): """Enable admin mode for Hugging Face Spaces deployment""" # Admin mode is always enabled for public deployment st.session_state.admin_mode = True def main(): """Main application entry point""" try: # Initialize application services services = initialize_application() if not services: st.error("Failed to initialize application services. Please refresh the page.") return # Show performance indicator services['optimizer'].render_performance_indicator() # Apply Streamlit-specific optimizations services['optimizer'].optimize_streamlit_config() # Enable admin mode for public deployment enable_admin_mode() # Handle privacy consent handle_privacy_consent(services['privacy']) # Handle onboarding handle_onboarding(services['engagement']) # Initialize activity router router = ActivityRouter() st.session_state.router = router # Store for admin access # Render admin interface render_admin_interface(services) # Run main application router.run() # Render admin dashboards if requested render_admin_dashboards(services) # Show engagement features services['engagement'].render_session_summary() except Exception as e: # Handle critical application errors global_error_handler.handle_error( e, ErrorCategory.SYSTEM, ErrorSeverity.CRITICAL, context={'component': 'main_application'}, show_user_message=True ) # Show fallback interface st.error("๐Ÿšจ Critical application error occurred. Please refresh the page.") if st.button("๐Ÿ”„ Refresh Application"): st.rerun() if __name__ == "__main__": main()