|
|
"""
|
|
|
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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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"""
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
services = {}
|
|
|
|
|
|
try:
|
|
|
|
|
|
services['optimizer'] = PerformanceOptimizer()
|
|
|
services['optimizer'].initialize_performance_optimization()
|
|
|
|
|
|
|
|
|
services['privacy'] = PrivacyService()
|
|
|
|
|
|
|
|
|
services['engagement'] = EngagementService()
|
|
|
|
|
|
|
|
|
services['pwa'] = PWAManager()
|
|
|
services['pwa'].initialize_pwa()
|
|
|
|
|
|
|
|
|
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"""
|
|
|
|
|
|
if not st.session_state.privacy_consent_given:
|
|
|
st.session_state.privacy_consent_given = True
|
|
|
|
|
|
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:
|
|
|
|
|
|
st.session_state.onboarding_completed = True
|
|
|
|
|
|
|
|
|
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"""
|
|
|
|
|
|
st.session_state.admin_mode = True
|
|
|
|
|
|
def main():
|
|
|
"""Main application entry point"""
|
|
|
try:
|
|
|
|
|
|
services = initialize_application()
|
|
|
|
|
|
if not services:
|
|
|
st.error("Failed to initialize application services. Please refresh the page.")
|
|
|
return
|
|
|
|
|
|
|
|
|
services['optimizer'].render_performance_indicator()
|
|
|
|
|
|
|
|
|
services['optimizer'].optimize_streamlit_config()
|
|
|
|
|
|
|
|
|
enable_admin_mode()
|
|
|
|
|
|
|
|
|
handle_privacy_consent(services['privacy'])
|
|
|
|
|
|
|
|
|
handle_onboarding(services['engagement'])
|
|
|
|
|
|
|
|
|
router = ActivityRouter()
|
|
|
st.session_state.router = router
|
|
|
|
|
|
|
|
|
render_admin_interface(services)
|
|
|
|
|
|
|
|
|
router.run()
|
|
|
|
|
|
|
|
|
render_admin_dashboards(services)
|
|
|
|
|
|
|
|
|
services['engagement'].render_session_summary()
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
global_error_handler.handle_error(
|
|
|
e,
|
|
|
ErrorCategory.SYSTEM,
|
|
|
ErrorSeverity.CRITICAL,
|
|
|
context={'component': 'main_application'},
|
|
|
show_user_message=True
|
|
|
)
|
|
|
|
|
|
|
|
|
st.error("๐จ Critical application error occurred. Please refresh the page.")
|
|
|
if st.button("๐ Refresh Application"):
|
|
|
st.rerun()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main() |