Repository navigation
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
71 lines (52 loc) · 2.2 KB
/
Copy pathstreamlit_app.py
File metadata and controls
71 lines (52 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
from pipeline.rag_pipeline import RAGPipeline
from utils.config import DATA_PATH, DEFAULT_RETRIEVER, TOP_K
st.set_page_config(page_title="RAG Insight Engine", page_icon="📚", layout="wide")
@st.cache_resource(show_spinner="Initializing RAG pipeline...")
def get_pipeline(retriever_kind: str, top_k: int) -> RAGPipeline:
"""Cache one pipeline per (retriever_kind, top_k) so embeddings/DB are reused."""
return RAGPipeline(
retriever_kind=retriever_kind,
top_k=top_k,
data_path=DATA_PATH,
)
def render_sources(sources: list[dict]) -> None:
if not sources:
st.info("No sources retrieved.")
return
for index, source in enumerate(sources, start=1):
title = source.get("title") or "Untitled"
file_name = source.get("file_name") or "unknown"
page = source.get("page")
page_str = f"page {page}" if page not in (None, "") else "page N/A"
st.markdown(f"**[{index}]** `{file_name}` · {page_str} · _{title}_")
def main() -> None:
st.title("RAG Insight Engine")
st.caption("Ask questions grounded in the documents under `data/raw`.")
with st.sidebar:
st.header("Settings")
retriever_kind = st.selectbox(
"Retriever",
options=["similarity", "mmr"],
index=0 if DEFAULT_RETRIEVER == "similarity" else 1,
help="`mmr` adds diversity to retrieved chunks.",
)
top_k = st.slider("Top K chunks", min_value=1, max_value=20, value=TOP_K)
st.caption("Pipeline rebuilds only when these settings change.")
pipeline = get_pipeline(retriever_kind, top_k)
query = st.text_input(
"Question",
placeholder="e.g. What is the main contribution of the paper?",
)
submitted = st.button("Ask", type="primary", disabled=not query.strip())
if submitted:
with st.spinner("Retrieving and generating..."):
result = pipeline.ask(query)
st.subheader("Answer")
st.write(result["answer"])
st.subheader("Sources")
render_sources(result["sources"])
with st.expander("Raw structured output"):
st.json(result)
if __name__ == "__main__":
main()