"""Query ranker UI component and logic."""
import gradio as gr
import logging
from src.core.logging_config import get_logger
from src.rag.vector_store import vector_store_manager
from src.rag import document_ingestion_service
logger = get_logger(__name__)
def handle_query_search(query, method, k_value):
"""Handle query search and return formatted results."""
if not query or not query.strip():
return """
"""]
# Add results
for result in results:
rank_emoji = ["🥇", "🥈", "🥉"][result["rank"] - 1] if result["rank"] <= 3 else f"#{result['rank']}"
# Escape content for safe HTML inclusion and JavaScript
escaped_content = result['content'].replace('"', '"').replace("'", "'").replace('\n', '\\n')
# Build score info - always show confidence, only show score for similarity search
score_info_parts = [f"""
{result['confidence_icon']} {result['confidence']}
"""]
# Only add score value if we have real scores (similarity search)
if result.get('has_score', False):
score_info_parts.append(f'
🎯 {result["score"]}')
score_info_html = f"""
{''.join(score_info_parts)}
"""
html_parts.append(f"""
""")
html_parts.append("
")
return "".join(html_parts)
def get_ranker_status():
"""Get current ranker system status."""
try:
# Get collection info
collection_info = vector_store_manager.get_collection_info()
document_count = collection_info.get("document_count", 0)
# Get available methods
available_methods = ["similarity", "mmr", "bm25", "hybrid"]
# Check if system is ready
ingestion_status = document_ingestion_service.get_ingestion_status()
system_ready = ingestion_status.get('system_ready', False)
status_html = f"""