|
| 1 | +import streamlit as st |
| 2 | + |
| 3 | +from interface.core.config import ( |
| 4 | + load_config, |
| 5 | + get_data_sources_registry, |
| 6 | + update_datahub_server, |
| 7 | + update_vectordb_settings, |
| 8 | + update_data_source_mode, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +def render_sidebar_data_source_selector(config=None) -> None: |
| 13 | + if config is None: |
| 14 | + config = load_config() |
| 15 | + |
| 16 | + registry = get_data_sources_registry() |
| 17 | + |
| 18 | + st.sidebar.markdown("### 데이터 소스") |
| 19 | + enable_data_source = st.sidebar.checkbox( |
| 20 | + "데이터 소스 적용", value=True, key="enable_data_source" |
| 21 | + ) |
| 22 | + if not enable_data_source: |
| 23 | + return |
| 24 | + |
| 25 | + mode_index = 0 if (config.data_source_mode or "datahub").lower() == "datahub" else 1 |
| 26 | + selected_mode = st.sidebar.radio( |
| 27 | + "소스 종류", options=["DataHub", "VectorDB"], index=mode_index, horizontal=True |
| 28 | + ) |
| 29 | + |
| 30 | + if selected_mode == "DataHub": |
| 31 | + datahub_names = [s.name for s in registry.datahub] |
| 32 | + if not datahub_names: |
| 33 | + st.sidebar.warning( |
| 34 | + "등록된 DataHub가 없습니다. 설정 > 데이터 소스에서 추가하세요." |
| 35 | + ) |
| 36 | + return |
| 37 | + dh_name = st.sidebar.selectbox( |
| 38 | + "DataHub 인스턴스", options=datahub_names, key="sidebar_dh_select" |
| 39 | + ) |
| 40 | + if st.sidebar.button("소스 적용", key="sidebar_apply_dh"): |
| 41 | + selected = next((s for s in registry.datahub if s.name == dh_name), None) |
| 42 | + if selected is None: |
| 43 | + st.sidebar.error("선택한 DataHub를 찾을 수 없습니다.") |
| 44 | + return |
| 45 | + try: |
| 46 | + update_datahub_server(config, selected.url) |
| 47 | + # DataHub 선택 시, FAISS 경로가 정의되어 있으면 기본 VectorDB 로케이션으로도 반영 |
| 48 | + if selected.faiss_path: |
| 49 | + try: |
| 50 | + update_vectordb_settings( |
| 51 | + config, |
| 52 | + vectordb_type="faiss", |
| 53 | + vectordb_location=selected.faiss_path, |
| 54 | + ) |
| 55 | + except Exception as e: |
| 56 | + st.sidebar.warning(f"FAISS 경로 적용 경고: {e}") |
| 57 | + update_data_source_mode(config, "datahub") |
| 58 | + st.sidebar.success(f"DataHub 적용됨: {selected.name}") |
| 59 | + except Exception as e: |
| 60 | + st.sidebar.error(f"적용 실패: {e}") |
| 61 | + else: |
| 62 | + vdb_names = [s.name for s in registry.vectordb] |
| 63 | + if not vdb_names: |
| 64 | + st.sidebar.warning( |
| 65 | + "등록된 VectorDB가 없습니다. 설정 > 데이터 소스에서 추가하세요." |
| 66 | + ) |
| 67 | + return |
| 68 | + vdb_name = st.sidebar.selectbox( |
| 69 | + "VectorDB 인스턴스", options=vdb_names, key="sidebar_vdb_select" |
| 70 | + ) |
| 71 | + if st.sidebar.button("소스 적용", key="sidebar_apply_vdb"): |
| 72 | + selected = next((s for s in registry.vectordb if s.name == vdb_name), None) |
| 73 | + if selected is None: |
| 74 | + st.sidebar.error("선택한 VectorDB를 찾을 수 없습니다.") |
| 75 | + return |
| 76 | + try: |
| 77 | + update_vectordb_settings( |
| 78 | + config, |
| 79 | + vectordb_type=selected.type, |
| 80 | + vectordb_location=selected.location, |
| 81 | + ) |
| 82 | + update_data_source_mode(config, "vectordb") |
| 83 | + st.sidebar.success(f"VectorDB 적용됨: {selected.name}") |
| 84 | + except Exception as e: |
| 85 | + st.sidebar.error(f"적용 실패: {e}") |
0 commit comments