Spaces:
Sleeping
Sleeping
Download src/components/sidebar.py from faseehsandhu/Digitalytics: direct link, hf CLI and curl.
- Browser
- Download file 5.39 kB
-
https://huggingface.co/spaces/faseehsandhu/Digitalytics/resolve/main/src/components/sidebar.py
- Command line
-
hf download hf://spaces/faseehsandhu/Digitalytics/src/components/sidebar.py
-
curl -L -o sidebar.py https://huggingface.co/spaces/faseehsandhu/Digitalytics/resolve/main/src/components/sidebar.py
5.39 kB
| import streamlit as st | |
| # Toggle to show/hide the Clear Retrieval Cache control. | |
| # Set to True when you want to re-enable the button. | |
| ALLOW_CLEAR_CACHE_BUTTON = False | |
| class Sidebar: | |
| def __init__(self, data_service=None): | |
| self.data_service = data_service | |
| def display(self): | |
| st.sidebar.title("Data Dictionary Chatbot") | |
| # Show loaded dictionaries | |
| if self.data_service: | |
| num_dicts = len(self.data_service.data_dictionaries) | |
| st.sidebar.metric("Loaded Dictionaries", num_dicts) | |
| if num_dicts > 0: | |
| st.sidebar.subheader("Available Dictionaries") | |
| for dict_name in self.data_service.data_dictionaries.keys(): | |
| st.sidebar.text(f"π {dict_name}") | |
| st.sidebar.header("Navigation") | |
| # Define navigation options that map to actual views | |
| nav_options = { | |
| "π¬ Chat": "Chat", | |
| "πΊοΈ Area Insights": "Area Insights", | |
| "π€ Upload Dictionary": "Manage Dictionaries", | |
| "βΉοΈ About": "About" | |
| } | |
| # Get current view or default to Chat | |
| current_view = st.session_state.get('current_view', 'Chat') | |
| # Find the current option label based on the view | |
| current_option = None | |
| for label, view in nav_options.items(): | |
| if view == current_view: | |
| current_option = label | |
| break | |
| # Create radio buttons with proper default | |
| option_labels = list(nav_options.keys()) | |
| default_index = option_labels.index(current_option) if current_option else 0 | |
| selected_option = st.sidebar.radio( | |
| "Select an option:", | |
| option_labels, | |
| index=default_index | |
| ) | |
| # Update the session state based on selection | |
| selected_view = nav_options[selected_option] | |
| if st.session_state.get('current_view') != selected_view: | |
| st.session_state.current_view = selected_view | |
| st.rerun() | |
| # Retrieval / Cache Status Section | |
| retrieval = st.session_state.get('retrieval_service') | |
| st.sidebar.markdown("---") | |
| st.sidebar.subheader("Retrieval / Cache Status") | |
| if retrieval: | |
| status = retrieval.get_cache_status() | |
| col1, col2 = st.sidebar.columns(2) | |
| col1.metric("Chunks", status['chunks_indexed']) | |
| # Use session state for real-time embedding count (updates during queries) | |
| embeddings_cached = st.session_state.get('embedding_cache_size', status['embeddings_cached']) | |
| col2.metric("Embeddings", embeddings_cached) | |
| st.sidebar.caption(f"Embeddings Enabled: {'β ' if status['embeddings_enabled'] else 'β'}") | |
| st.sidebar.caption(f"Corpus Sig: `{status['corpus_sig']}`") | |
| # Controls | |
| st.sidebar.subheader("Retrieval Settings") | |
| top_k = st.sidebar.slider("Top-k retrieved", min_value=5, max_value=50, value=25, step=5, help="How many relevant snippets to retrieve") | |
| disable_embeddings = st.sidebar.checkbox("Disable embeddings (lexical only)", value=False, help="Speeds up retrieval, less semantic accuracy") | |
| ensure_diversity = st.sidebar.checkbox("Ensure cross-dataset diversity", value=True, help="Distributes results across all relevant dictionaries (critical for 100+ datasets)") | |
| st.session_state['retrieval_top_k'] = top_k | |
| st.session_state['retrieval_disable_embeddings'] = disable_embeddings | |
| st.session_state['retrieval_ensure_diversity'] = ensure_diversity | |
| if ALLOW_CLEAR_CACHE_BUTTON: | |
| if st.sidebar.button("Clear Retrieval Cache"): | |
| retrieval.clear_cache() | |
| st.sidebar.info("Cache cleared. It will rebuild on next data query.") | |
| st.rerun() | |
| if st.sidebar.button("Force Reindex"): | |
| retrieval.index() | |
| st.sidebar.success("Reindex complete.") | |
| st.rerun() | |
| if st.sidebar.button("Warm-Up Embeddings"): | |
| with st.spinner("Pre-loading embeddings for better accuracy..."): | |
| warmed = retrieval.warm_embeddings_cache(sample_size=100) | |
| if warmed > 0: | |
| st.sidebar.success(f"β Pre-loaded {warmed} embeddings") | |
| else: | |
| st.sidebar.info("All sample embeddings already cached") | |
| st.rerun() | |
| else: | |
| st.sidebar.caption("Retrieval service not initialized yet.") | |
| # Quality & Feedback Metrics | |
| st.sidebar.markdown("---") | |
| st.sidebar.subheader("Quality Metrics") | |
| feedback_collector = st.session_state.get('feedback_collector') | |
| if feedback_collector: | |
| stats = feedback_collector.get_feedback_stats() | |
| if stats.get('total', 0) > 0: | |
| st.sidebar.metric("Total Feedback", stats['total']) | |
| st.sidebar.metric("Avg Rating", f"{stats['avg_rating']:.1f}/5.0") | |
| st.sidebar.metric("Helpful %", f"{stats['useful_pct']:.0f}%") | |
| else: | |
| st.sidebar.info("π No feedback yet. Help improve by rating responses!") | |
| return selected_view |