import streamlit as st
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from rag_pipeline import VectorStore
import json
from datetime import datetime
from pathlib import Path
st.set_page_config(
page_title="Research Assistant",
page_icon="📚",
layout="wide"
)
st.title("📚 Personal Research Assistant")
st.markdown("Semantic search across your research library")
# Initialize
vector_store = VectorStore(use_local=True)
# Get stats
try:
stats = vector_store.get_collection_stats()
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Chunks", stats.get("total_chunks", 0))
with col2:
# Count query logs
log_dir = "./data/query_logs"
total_queries = 0
if os.path.exists(log_dir):
for file in Path(log_dir).glob("*.jsonl"):
with open(file) as f:
total_queries += len(f.readlines())
st.metric("Total Queries", total_queries)
with col3:
# Average query time
avg_time = 0
if os.path.exists(log_dir) and total_queries > 0:
times = []
for file in Path(log_dir).glob("*.jsonl"):
with open(file) as f:
for line in f:
data = json.loads(line)
times.append(data.get("query_time_ms", 0))
avg_time = sum(times) / len(times) if times else 0
st.metric("Avg Query Time", f"{avg_time:.0f}ms")
# Recent queries
st.subheader("Recent Queries")
if os.path.exists(log_dir):
recent = []
for file in sorted(Path(log_dir).glob("*.jsonl"), reverse=True):
with open(file) as f:
for line in f:
recent.append(json.loads(line))
if len(recent) >= 10:
break
if len(recent) >= 10:
break
if recent:
for q in recent[:10]:
st.text(f"🔍 {q['query']} ({q['query_time_ms']:.0f}ms)")
else:
st.info("No queries yet. Use the Search Test page!")
except Exception as e:
st.error(f"Error loading stats: {e}")
st.sidebar.page_link("pages/search_test.py", label="🔍 Search")
st.sidebar.page_link("pages/metrics_dashboard.py", label="📊 Metrics")
st.sidebar.page_link("pages/document_library.py", label="📚 Library")
st.sidebar.page_link("pages/upload_documents.py", label="📤 Upload")
st.markdown("---")
st.markdown("Navigate using sidebar → Search Test, Metrics, or Document Library")