# Main application entry point import hmac import os import logging from pathlib import Path import streamlit as st import base64 from typing import List, Dict, Any, Tuple, Optional from config import ( GROQ_API_KEY, APP_PASSWORD, LLM_MODELS, DEFAULT_LLM_MODEL, UI_TEXTS, DATABASE_CONFIG, ) from document_processor import extract_pdf_pages, extract_pdf_from_path, process_documents, chunks_to_store_format from vector_store import get_vector_store, reset_vector_store from retrieval import get_retrieval_engine, reset_retrieval_engine from llm_generator import get_llm_generator, reset_llm_generator from embeddings import reset_embedder from integration import QueryHandler, QueryMode, QueryResult, get_query_handler, reset_query_handler from utils import get_metrics, record_query logger = logging.getLogger(__name__) st.set_page_config( page_title=UI_TEXTS["title"], layout="wide", initial_sidebar_state="collapsed" ) STYLES_PATH = Path(__file__).parent / "static" / "styles.css" BASE_DOCUMENTS_PATH = Path(__file__).parent.parent / "documents" @st.cache_resource def init_database_components() -> Dict[str, Any]: """Initialize database components if enabled. Cached to run once per session.""" if not DATABASE_CONFIG.get("enabled", False): return {"status": "disabled", "message": "Database integration disabled"} try: from database import init_database, get_database_status result = init_database() if result is None: return {"status": "disabled", "message": "Database initialization returned None"} if result.get("status") == "error": logger.error(f"Database init error: {result.get('message')}") return result logger.info(f"Database initialized: {result.get('status')}") return result except Exception as e: logger.error(f"Database initialization failed: {e}") return {"status": "error", "message": str(e)} def get_database_status_info() -> Dict[str, Any]: """Get current database status for UI display.""" if not DATABASE_CONFIG.get("enabled", False): return {"enabled": False, "status": "disabled"} try: from database import get_database_status status = get_database_status() return {"enabled": True, **status} except Exception as e: return {"enabled": True, "status": "error", "message": str(e)} def load_css() -> str: """Load CSS from external file.""" if STYLES_PATH.exists(): return STYLES_PATH.read_text() return "" def apply_custom_css(): """Apply custom CSS styling.""" css = load_css() if css: st.markdown(f"", unsafe_allow_html=True) def check_password() -> bool: """Verify user password using timing-safe comparison.""" if "password_correct" not in st.session_state: st.session_state.password_correct = False if st.session_state.password_correct: return True if not APP_PASSWORD: return True password = st.text_input("Sifre", type="password", key="password_input") if password: if hmac.compare_digest(password, APP_PASSWORD): st.session_state.password_correct = True st.rerun() else: st.error("Yanlis sifre") return False return False def init_session_state(): """Initialize all session state variables.""" defaults = { "documents_processed": False, "chunk_count": 0, "chat_history": [], "embedder_loaded": False, "sources": [], "selected_model": DEFAULT_LLM_MODEL, "use_reranking": True, "base_docs_loaded": False, "loaded_documents": [], "query_mode": QueryMode.AUTO, "database_enabled": DATABASE_CONFIG.get("enabled", False), "database_initialized": False, "database_tables": [], "agent_available": False, } for key, value in defaults.items(): if key not in st.session_state: st.session_state[key] = value def get_base_documents() -> List[Path]: """Get list of base PDF documents from documents folder.""" if not BASE_DOCUMENTS_PATH.exists(): return [] return list(BASE_DOCUMENTS_PATH.glob("*.pdf")) def render_pdf_viewer(): """Render a PDF viewer for documents in the base documents folder.""" st.header("Doküman Görüntüleyici") docs = get_base_documents() if not docs: st.info("Documents klasöründe PDF bulunamadı.") return # Prefer deterministic order docs = sorted(docs, key=lambda p: p.name.lower()) selected_doc = st.selectbox( "PDF Seç", docs, format_func=lambda p: p.name ) try: pdf_bytes = selected_doc.read_bytes() base64_pdf = base64.b64encode(pdf_bytes).decode("utf-8") pdf_display = f""" """ st.markdown(pdf_display, unsafe_allow_html=True) except Exception as e: st.error(f"PDF görüntüleme hatası: {e}") def load_default_documents(): """Load default documents from documents folder into vector store.""" if st.session_state.get("base_docs_loaded", False): return base_docs = get_base_documents() if not base_docs: st.session_state.base_docs_loaded = True return vector_store = get_vector_store() stats = vector_store.get_collection_stats() # If documents already exist in store, restore session state from persisted data if stats["points_count"] > 0: st.session_state.base_docs_loaded = True st.session_state.documents_processed = True st.session_state.chunk_count = stats["points_count"] return total_chunks = 0 loaded_docs = [] for doc_path in base_docs: pages = extract_pdf_from_path(str(doc_path), doc_path.name) chunks = process_documents(pages) if chunks: texts, metadatas = chunks_to_store_format(chunks) vector_store.add_documents(texts, metadatas) total_chunks += len(chunks) loaded_docs.append({"name": doc_path.name, "chunks": len(chunks), "pages": len(pages)}) st.session_state.base_docs_loaded = True st.session_state.documents_processed = total_chunks > 0 st.session_state.chunk_count = total_chunks st.session_state.loaded_documents = loaded_docs def process_uploaded_files(files) -> int: """Process uploaded PDF files and add to vector store.""" if not files: return 0 vector_store = get_vector_store() total_chunks = 0 # Get already loaded sources to check for duplicates loaded_sources = vector_store.get_loaded_sources() for file in files: doc_name = file.name # Skip if document already exists if doc_name in loaded_sources: st.warning(f"'{doc_name}' already exists in the database, skipping.") continue pages = extract_pdf_pages(file, doc_name) chunks = process_documents(pages) if chunks: texts, metadatas = chunks_to_store_format(chunks) vector_store.add_documents(texts, metadatas) total_chunks += len(chunks) loaded_sources.add(doc_name) # Track newly added doc st.success(f"Added '{doc_name}': {len(chunks)} chunks") return total_chunks def reset_application(): reset_vector_store() reset_embedder() reset_retrieval_engine() reset_llm_generator() reset_query_handler() from integration.entity_context import reset_entity_store from integration.context_manager import reset_context_manager reset_entity_store() reset_context_manager() st.session_state.documents_processed = False st.session_state.chunk_count = 0 st.session_state.chat_history = [] st.session_state.sources = [] st.session_state.base_docs_loaded = False st.session_state.loaded_documents = [] st.session_state.query_mode = QueryMode.AUTO def render_settings_tab(): """Render the settings and document management in a tab (formerly sidebar).""" st.header("Sistem Ayarları ve Dokümanlar") col_settings, col_docs = st.columns([1, 2]) with col_settings: st.subheader("Model Ayarları") if not GROQ_API_KEY: st.error(UI_TEXTS["error_api_key"]) st.markdown(f"**{UI_TEXTS['model_select']}**") model_names = list(LLM_MODELS.keys()) current_model_id = st.session_state.get("selected_model", DEFAULT_LLM_MODEL) current_index = 0 for i, name in enumerate(model_names): if LLM_MODELS[name] == current_model_id: current_index = i break selected_name = st.selectbox( "Model", model_names, index=current_index, label_visibility="collapsed" ) st.session_state.selected_model = LLM_MODELS[selected_name] st.markdown("---") st.markdown(f"**{UI_TEXTS['rerank_toggle']}**") st.session_state.use_reranking = st.toggle( "Rerank Aktif", value=st.session_state.use_reranking, help=UI_TEXTS["rerank_help"] ) if st.session_state.use_reranking: st.success(UI_TEXTS["search_advanced"]) else: st.info(UI_TEXTS["search_standard"]) st.markdown("---") st.markdown(f"**{UI_TEXTS['data_source_label']}**") # Build mode options based on what's available mode_options = { UI_TEXTS["data_source_auto"]: QueryMode.AUTO, UI_TEXTS["data_source_documents"]: QueryMode.DOCUMENTS, } # Only show database modes if database is initialized if st.session_state.get("database_initialized", False): mode_options[UI_TEXTS["data_source_database"]] = QueryMode.DATABASE mode_options[UI_TEXTS["data_source_hybrid"]] = QueryMode.HYBRID selected_mode_name = st.selectbox( "Kaynak", list(mode_options.keys()), index=0, label_visibility="collapsed", ) st.session_state.query_mode = mode_options[selected_mode_name] # Database status indicator if st.session_state.get("database_enabled", False): if st.session_state.get("database_initialized", False): st.success("Veritabanı: Bağlı") tables = st.session_state.get("database_tables", []) if tables: with st.expander("Tablolar", expanded=False): for table in tables: st.text(f"- {table}") else: st.warning("Veritabanı: Bağlantı başarısız") else: st.info("Veritabanı: Devre dışı") st.markdown("---") if st.button("Tüm Sistemi Sıfırla (Reset)", type="secondary", use_container_width=True): reset_application() st.rerun() with col_docs: st.subheader("Doküman Yönetimi") uploaded_files = st.file_uploader( UI_TEXTS["upload_label"], type=["pdf"], accept_multiple_files=True, help=UI_TEXTS["upload_help"] ) if uploaded_files: if st.button(UI_TEXTS["process_button"], type="primary"): with st.spinner(UI_TEXTS["processing"]): chunk_count = process_uploaded_files(uploaded_files) st.session_state.chunk_count += chunk_count st.session_state.documents_processed = True st.success(UI_TEXTS["processing_complete"].format(count=len(uploaded_files))) st.divider() if st.session_state.documents_processed: st.metric("Toplam Vektör Parçacığı (Chunks)", st.session_state.chunk_count) if st.session_state.loaded_documents: with st.expander("Yüklü Doküman Listesi", expanded=True): for doc in st.session_state.loaded_documents: st.text(f"📄 {doc['name']} ({doc['chunks']} chunks)") def generate_response( question: str, use_reranking: bool, chat_history: List[Dict[str, str]], model_id: str = DEFAULT_LLM_MODEL, query_mode: QueryMode = QueryMode.DOCUMENTS, ) -> Tuple[str, List[Dict[str, Any]], Dict[str, Any]]: handler = get_query_handler() result = handler.execute(question, mode=query_mode, chat_history=chat_history, model_id=model_id) record_query( query=question, mode=result.mode.value, response_time=result.execution_time, success=result.success, error=result.error, ) debug_info = { "mode": result.mode.value, "execution_time": result.execution_time, "intent": result.intent, "sql_query": result.sql_query, "db_results_count": len(result.db_results) if result.db_results else 0, "success": result.success, "model_id": model_id, "routing": result.routing_metadata, "is_database": result.mode in (QueryMode.DATABASE, QueryMode.HYBRID) and result.db_results, } sources = result.sources if result.sources else [] return result.response, sources, debug_info def render_database_tab(): import pandas as pd from database.db_adapter import get_database_adapter st.header("Veritabanı") if not DATABASE_CONFIG.get("enabled", False): st.info("Veritabanı entegrasyonu kapalı (DATABASE_CONFIG.enabled=false).") return try: adapter = get_database_adapter() if adapter is None or not adapter.is_connected: st.warning("Veritabanı bağlantısı kurulamadı.") return table_rows = adapter.execute_query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name" ) tables = [r["name"] for r in table_rows] if not tables: st.info("Veritabanında tablo bulunamadı.") return col1, col2 = st.columns([2, 1]) with col1: selected_table = st.selectbox("Tablo seç", tables) with col2: limit = st.number_input("Limit", min_value=10, max_value=2000, value=200, step=50) if selected_table not in tables: st.error("Geçersiz tablo seçimi.") return try: count_rows = adapter.execute_query( f"SELECT COUNT(*) AS cnt FROM {selected_table}" ) if count_rows: st.caption(f"Toplam kayıt: {int(count_rows[0]['cnt'])}") except Exception: pass rows = adapter.execute_query( f"SELECT * FROM {selected_table} LIMIT :lim", {"lim": int(limit)} ) if rows: df = pd.DataFrame(rows) st.dataframe(df, use_container_width=True) else: st.info("Tabloda kayıt bulunamadı.") except Exception as e: st.error(f"DB görüntüleme hatası: {e}") def render_sources(sources: List[Dict[str, Any]], use_reranking: bool): """Render source citations in expandable section.""" if not sources: return with st.expander(UI_TEXTS["sources_label"], expanded=False): for i, src in enumerate(sources, 1): st.markdown(f'