-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp_streamlit.py
More file actions
112 lines (103 loc) · 2.82 KB
/
Copy pathApp_streamlit.py
File metadata and controls
112 lines (103 loc) · 2.82 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# streamlit_app.py
import streamlit as st
from rag_engine import RAGEngineW
import numpy as np
# -------------------------
# Load RAG Engine
# -------------------------
@st.cache_resource
def load_rag():
return RAGEngineW()
rag = load_rag()
# -------------------------
# Page Config
# -------------------------
st.set_page_config(
page_title="📄 RAG Q&A",
page_icon="🧠",
layout="wide",
initial_sidebar_state="expanded"
)
# -------------------------
# Custom CSS for styling
# -------------------------
st.markdown("""
<style>
body {
background: linear-gradient(to right, #FF4E50, #FC913A, #9D50BB);
font-family: 'Segoe UI', sans-serif;
color: white;
}
h1, h2 {
font-weight: bold;
}
h1 {
text-align: center;
font-size: 42px;
margin-bottom: 10px;
}
h2 {
font-size: 24px;
}
.stButton>button {
background: linear-gradient(to right, #FF4E50, #FC913A, #9D50BB);
color: white;
font-size: 16px;
font-weight: bold;
padding: 10px 25px;
border-radius: 10px;
border: none;
transition: transform 0.2s;
}
.stButton>button:hover {
transform: scale(1.05);
}
.stTextInput>div>div>input {
border-radius: 10px;
padding: 12px;
font-size: 16px;
}
.stSpinner>div {
color: white;
}
.stCheckbox>div>label {
color: white;
font-weight: bold;
}
</style>
""", unsafe_allow_html=True)
# -------------------------
# Title & Description
# -------------------------
st.title("📄 RAG Document Q&A")
st.subheader("Ask questions and get answers directly from the web page!")
# -------------------------
# Sidebar Info (Scraping notice)
# -------------------------
with st.sidebar:
st.header("ℹ️ Info")
st.write("This RAG pipeline is built on a **single scraped page** from:")
st.markdown("[Getting Started - VS Code Docs](https://code.visualstudio.com/docs/getstarted/getting-started)")
# -------------------------
# Question Input
# -------------------------
st.markdown("### ❓ Ask a Question")
question = st.text_input("Type your question here:")
if st.button("Get Answer"):
if question.strip() == "":
st.warning("Please enter a question!")
else:
with st.spinner("Fetching answer... 🧠"):
# For single page, you can fix top_k = 2 or any number
answer = rag.ask(question, top_k=3)
st.markdown("### ✅ Answer")
st.success(answer)
# Optional: show retrieved context
show_context = st.checkbox("Show retrieved context")
if show_context:
st.markdown("### 📚 Retrieved Context")
query_vector = np.array(rag.embedding_model.embed_query(question), dtype="float32").reshape(1, -1)
context = "\n\n".join(
rag.docstore[rag.mapping[i]] for i in rag.index.search(query_vector, top_k=3)[1][0] if i != -1
)
st.info(context)