Spaces:
Sleeping
Sleeping
Download src/app.py from faseehsandhu/Digitalytics: direct link, hf CLI and curl.
- Browser
- Download file 6.22 kB
-
https://huggingface.co/spaces/faseehsandhu/Digitalytics/resolve/main/src/app.py
- Command line
-
hf download hf://spaces/faseehsandhu/Digitalytics/src/app.py
-
curl -L -o app.py https://huggingface.co/spaces/faseehsandhu/Digitalytics/resolve/main/src/app.py
6.22 kB
| import streamlit as st | |
| from components.chat_interface import ChatInterface | |
| from components.file_uploader import FileUploader | |
| from components.sidebar import Sidebar | |
| from components.area_insights_interface import AreaInsightsInterface | |
| from services.data_dictionary_service import DataDictionaryService | |
| from services.zip_resolution_service import ZipResolutionService | |
| from services.offline_dataset_service import OfflineDatasetService | |
| from utils.logging_config import setup_logging, get_logger | |
| from utils.rate_limiter import RateLimiter | |
| from utils.feedback import FeedbackCollector | |
| # Initialize logging once | |
| if 'logger_initialized' not in st.session_state: | |
| setup_logging() | |
| st.session_state.logger_initialized = True | |
| logger = get_logger(__name__) | |
| def main(): | |
| logger.info("Application started") | |
| st.set_page_config(page_title="Data Dictionary Chatbot", layout="wide") | |
| # Initialize session state | |
| if 'current_view' not in st.session_state: | |
| st.session_state.current_view = "Chat" | |
| # Initialize rate limiter and feedback | |
| if 'rate_limiter' not in st.session_state: | |
| st.session_state.rate_limiter = RateLimiter(max_requests_per_minute=60) | |
| if 'feedback_collector' not in st.session_state: | |
| st.session_state.feedback_collector = FeedbackCollector() | |
| # Initialize data dictionary service (once) | |
| if 'data_service' not in st.session_state: | |
| data_service = DataDictionaryService() | |
| st.session_state.data_service = data_service | |
| else: | |
| data_service = st.session_state.data_service | |
| # Show page title early | |
| st.title("Data Dictionary Chatbot") | |
| # Auto-load default dictionaries on startup | |
| if 'dictionaries_loaded' not in st.session_state: | |
| placeholder = st.empty() | |
| with placeholder.container(): | |
| with st.spinner("⏳ Loading default data dictionaries..."): | |
| default_count = data_service.load_default_dictionaries() | |
| user_count = data_service.load_user_dictionaries() | |
| st.session_state.dictionaries_loaded = True | |
| # Show success message (outside spinner) | |
| total = default_count + user_count | |
| if total > 0: | |
| st.success(f"✅ Successfully loaded {total} data dictionar{'y' if total == 1 else 'ies'} ({default_count} default, {user_count} user-uploaded)") | |
| else: | |
| st.info("ℹ️ No data dictionaries found. You can upload your own using the uploader below.") | |
| # Initialize offline dataset service for area insights (after data service is ready) | |
| if 'offline_dataset_service' not in st.session_state: | |
| logger.info("Initializing OfflineDatasetService (first time)") | |
| st.session_state.offline_dataset_service = OfflineDatasetService(data_dictionary_service=data_service) | |
| else: | |
| logger.debug("OfflineDatasetService already initialized, using cached instance") | |
| # Initialize ZIP resolution service for area insights | |
| if 'zip_resolution_service' not in st.session_state: | |
| st.session_state.zip_resolution_service = ZipResolutionService() | |
| # Initialize retrieval service (needed for both Chat and Area Insights) | |
| if 'retrieval_service' not in st.session_state: | |
| from services.retrieval_service import RetrievalService | |
| st.session_state.retrieval_service = RetrievalService(data_service) | |
| logger.info("Retrieval service initialized") | |
| # Sidebar for navigation | |
| sidebar = Sidebar(data_service) | |
| current_view = sidebar.display() | |
| # Display content based on current view | |
| if current_view == "Chat": | |
| # Chat interface component | |
| chat_interface = ChatInterface(data_service) | |
| chat_interface.display() | |
| elif current_view == "Area Insights": | |
| # Area insights interface component (cached in session state) | |
| if 'area_insights_interface' not in st.session_state: | |
| st.session_state.area_insights_interface = AreaInsightsInterface( | |
| data_service, | |
| st.session_state.retrieval_service, | |
| st.session_state.zip_resolution_service, | |
| st.session_state.offline_dataset_service | |
| ) | |
| st.session_state.area_insights_interface.display() | |
| elif current_view == "Manage Dictionaries": | |
| st.header("Manage Data Dictionaries") | |
| # File uploader component | |
| file_uploader = FileUploader(data_service) | |
| file_uploader.display() | |
| # Display existing dictionaries | |
| st.subheader("Existing Dictionaries") | |
| dictionaries = data_service.get_all_dictionaries() | |
| if dictionaries: | |
| for dict_info in dictionaries: | |
| with st.expander(f"📄 {dict_info['name']}"): | |
| st.write(f"Type: {dict_info.get('type', 'Unknown')}") | |
| st.write(f"Status: {'Active' if dict_info.get('active', True) else 'Inactive'}") | |
| if dict_info.get('type') == 'user': | |
| if st.button("Delete", key=f"del_{dict_info['name']}"): | |
| data_service.delete_dictionary(dict_info['name']) | |
| st.rerun() | |
| else: | |
| st.info("No dictionaries available. Upload some using the form above.") | |
| else: # About view | |
| st.header("About This Application") | |
| st.markdown(""" | |
| This chatbot application helps you analyze data dictionaries and find relevant columns | |
| for your machine learning use cases. | |
| ### Features | |
| - Upload and manage data dictionaries (PDF, Excel, CSV) | |
| - AI-powered column suggestions | |
| - Interactive chat interface | |
| - Support for multiple dictionaries | |
| ### How to Use | |
| 1. Upload your data dictionaries using the 'Manage Dictionaries' view | |
| 2. Switch to the 'Chat' view | |
| 3. Describe your use case to get relevant column suggestions | |
| 4. Use the sidebar to enable/disable specific dictionaries | |
| ### Supported File Types | |
| - PDF (.pdf) | |
| - Excel (.xlsx, .xls) | |
| - CSV (.csv) | |
| """) | |
| if __name__ == "__main__": | |
| main() |