-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
117 lines (88 loc) · 3.67 KB
/
Copy pathapp.py
File metadata and controls
117 lines (88 loc) · 3.67 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
113
114
115
116
117
import streamlit as st
import requests
import html
st.set_page_config(
page_title="VectorVault – Semantic Search Engine",
layout="wide",
page_icon="🔍"
)
st.markdown("""
<h1 style="text-align:center; color:#4A90E2;">
🔍 VectorVault – Semantic Document Search
</h1>
<p style="text-align:center; font-size:18px;">
Search across your documents using embeddings, FAISS, and semantic similarity.
</p>
""", unsafe_allow_html=True)
st.sidebar.header("⚙️ Settings")
top_k = st.sidebar.slider(
"Number of results",
min_value=1,
max_value=20,
value=5
)
show_preview = st.sidebar.checkbox("Show preview text", value=True)
show_keywords = st.sidebar.checkbox("Show matched keywords", value=True)
show_similarity_bar = st.sidebar.checkbox("Show similarity bars", value=True)
st.sidebar.markdown("---")
st.sidebar.caption("Built by Ash Nicolus")
query = st.text_input(
"Enter your search query",
placeholder="e.g., machine learning basics, quantum physics, climate change..."
)
search_button = st.button("Search 🔎")
def highlight_keywords(text, keywords):
for kw in keywords:
text = text.replace(kw, f"<mark style='background-color:#FFF59D'>{kw}</mark>")
return text
if search_button:
if not query.strip():
st.warning("⚠️ Please enter a query to search.")
else:
payload = {"query": query, "top_k": top_k}
try:
response = requests.post("http://localhost:8000/search", json=payload)
data = response.json()
results = data.get("results", [])
if len(results) == 0:
st.error("No results found!")
else:
st.success(f"Found {len(results)} matching documents.")
# ----------------------------
# DISPLAY RESULTS BEAUTIFULLY
# ----------------------------
for item in results:
doc_id = item["doc_id"]
score = round(item["score"], 4)
preview = html.escape(item["preview"])
explanation = item["explanation"]
overlap_words = explanation["overlap_keywords"]
overlap_ratio = round(explanation["overlap_ratio"], 3)
# CARD LAYOUT
with st.container():
st.markdown(f"""
<div style="padding:20px; margin-bottom:20px; border-radius:10px; background-color:#F9FAFB; border:1px solid #E5E7EB;">
<h3 style="margin-bottom:5px;">📄 {doc_id}</h3>
<p style="color:#6B7280; margin-top:-5px;">
Similarity Score: <b>{score}</b>
</p>
</div>
""", unsafe_allow_html=True)
# SIMILARITY BAR
if show_similarity_bar:
st.progress(min(max(score, 0.0), 1.0))
# PREVIEW TEXT
if show_preview:
st.markdown("**Preview**")
if show_keywords:
highlighted = highlight_keywords(preview, overlap_words)
st.markdown(f"<p>{highlighted}</p>", unsafe_allow_html=True)
else:
st.write(preview)
# EXPLANATION BLOCK
with st.expander("🧠 Why this document was matched?"):
st.write(f"**Matched Keywords:** {overlap_words}")
st.write(f"**Overlap Ratio:** {overlap_ratio}")
st.markdown("---")
except Exception as e:
st.error(f"❌ Error communicating with API: {str(e)}")