fix problems
Browse files
app.py
CHANGED
|
@@ -3,9 +3,9 @@ import chromadb
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
import uuid
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
|
| 10 |
st.set_page_config(
|
| 11 |
page_title="Semantic Search Engine",
|
|
@@ -13,9 +13,9 @@ st.set_page_config(
|
|
| 13 |
layout="wide"
|
| 14 |
)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
# CSS
|
| 18 |
-
#
|
| 19 |
|
| 20 |
st.markdown("""
|
| 21 |
<style>
|
|
@@ -24,26 +24,32 @@ st.markdown("""
|
|
| 24 |
padding-top: 1rem;
|
| 25 |
}
|
| 26 |
|
| 27 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
padding: 1rem;
|
| 29 |
border-radius: 12px;
|
| 30 |
-
border: 1px solid #
|
| 31 |
margin-bottom: 10px;
|
| 32 |
}
|
| 33 |
|
| 34 |
</style>
|
| 35 |
""", unsafe_allow_html=True)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
#
|
| 39 |
-
#
|
| 40 |
|
| 41 |
st.title("π Semantic Search Engine")
|
| 42 |
-
st.caption(
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
#
|
| 46 |
-
#
|
| 47 |
|
| 48 |
@st.cache_resource
|
| 49 |
def load_model():
|
|
@@ -53,9 +59,9 @@ def load_model():
|
|
| 53 |
|
| 54 |
model = load_model()
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
#
|
| 58 |
-
#
|
| 59 |
|
| 60 |
client = chromadb.PersistentClient(
|
| 61 |
path="./chroma_db"
|
|
@@ -65,9 +71,9 @@ collection = client.get_or_create_collection(
|
|
| 65 |
name="documents"
|
| 66 |
)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
#
|
| 70 |
-
#
|
| 71 |
|
| 72 |
with st.sidebar:
|
| 73 |
|
|
@@ -83,12 +89,12 @@ with st.sidebar:
|
|
| 83 |
st.markdown("---")
|
| 84 |
|
| 85 |
st.info(
|
| 86 |
-
"Semantic Search compares meanings
|
| 87 |
)
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
#
|
| 91 |
-
#
|
| 92 |
|
| 93 |
st.markdown("## π Database Statistics")
|
| 94 |
|
|
@@ -106,16 +112,16 @@ with col2:
|
|
| 106 |
"MiniLM-L6-v2"
|
| 107 |
)
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
#
|
| 111 |
-
#
|
| 112 |
|
| 113 |
st.markdown("---")
|
| 114 |
st.markdown("## π₯ Add Documents")
|
| 115 |
|
| 116 |
documents = st.text_area(
|
| 117 |
-
"Enter
|
| 118 |
-
height=
|
| 119 |
placeholder="""
|
| 120 |
Python is a programming language.
|
| 121 |
FastAPI is used to build APIs.
|
|
@@ -153,20 +159,20 @@ if st.button("πΎ Store Documents"):
|
|
| 153 |
)
|
| 154 |
|
| 155 |
st.success(
|
| 156 |
-
f"{len(docs)}
|
| 157 |
)
|
| 158 |
|
| 159 |
st.rerun()
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
#
|
| 163 |
-
#
|
| 164 |
|
| 165 |
st.markdown("---")
|
| 166 |
-
st.markdown("## π Search
|
| 167 |
|
| 168 |
query = st.text_input(
|
| 169 |
-
"Enter
|
| 170 |
placeholder="How can I build an API?"
|
| 171 |
)
|
| 172 |
|
|
@@ -178,7 +184,7 @@ if st.button(
|
|
| 178 |
if collection.count() == 0:
|
| 179 |
|
| 180 |
st.error(
|
| 181 |
-
"No documents
|
| 182 |
)
|
| 183 |
|
| 184 |
elif not query.strip():
|
|
@@ -190,7 +196,7 @@ if st.button(
|
|
| 190 |
else:
|
| 191 |
|
| 192 |
with st.spinner(
|
| 193 |
-
"Searching
|
| 194 |
):
|
| 195 |
|
| 196 |
query_embedding = model.encode(
|
|
@@ -210,25 +216,33 @@ if st.button(
|
|
| 210 |
docs = results["documents"][0]
|
| 211 |
distances = results["distances"][0]
|
| 212 |
|
| 213 |
-
st.markdown("
|
|
|
|
| 214 |
|
| 215 |
for rank, (doc, distance) in enumerate(
|
| 216 |
zip(docs, distances),
|
| 217 |
start=1
|
| 218 |
):
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
with st.expander(
|
| 226 |
-
f"#{rank} |
|
| 227 |
):
|
| 228 |
st.write(doc)
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
st.markdown("---")
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
import uuid
|
| 5 |
|
| 6 |
+
# ==========================================
|
| 7 |
+
# PAGE CONFIG
|
| 8 |
+
# ==========================================
|
| 9 |
|
| 10 |
st.set_page_config(
|
| 11 |
page_title="Semantic Search Engine",
|
|
|
|
| 13 |
layout="wide"
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# ==========================================
|
| 17 |
+
# CUSTOM CSS
|
| 18 |
+
# ==========================================
|
| 19 |
|
| 20 |
st.markdown("""
|
| 21 |
<style>
|
|
|
|
| 24 |
padding-top: 1rem;
|
| 25 |
}
|
| 26 |
|
| 27 |
+
.block-container {
|
| 28 |
+
padding-top: 2rem;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
.result-box {
|
| 32 |
padding: 1rem;
|
| 33 |
border-radius: 12px;
|
| 34 |
+
border: 1px solid #333;
|
| 35 |
margin-bottom: 10px;
|
| 36 |
}
|
| 37 |
|
| 38 |
</style>
|
| 39 |
""", unsafe_allow_html=True)
|
| 40 |
|
| 41 |
+
# ==========================================
|
| 42 |
+
# TITLE
|
| 43 |
+
# ==========================================
|
| 44 |
|
| 45 |
st.title("π Semantic Search Engine")
|
| 46 |
+
st.caption(
|
| 47 |
+
"Search documents using semantic similarity powered by Hugging Face embeddings."
|
| 48 |
+
)
|
| 49 |
|
| 50 |
+
# ==========================================
|
| 51 |
+
# LOAD MODEL
|
| 52 |
+
# ==========================================
|
| 53 |
|
| 54 |
@st.cache_resource
|
| 55 |
def load_model():
|
|
|
|
| 59 |
|
| 60 |
model = load_model()
|
| 61 |
|
| 62 |
+
# ==========================================
|
| 63 |
+
# CHROMADB
|
| 64 |
+
# ==========================================
|
| 65 |
|
| 66 |
client = chromadb.PersistentClient(
|
| 67 |
path="./chroma_db"
|
|
|
|
| 71 |
name="documents"
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# ==========================================
|
| 75 |
+
# SIDEBAR
|
| 76 |
+
# ==========================================
|
| 77 |
|
| 78 |
with st.sidebar:
|
| 79 |
|
|
|
|
| 89 |
st.markdown("---")
|
| 90 |
|
| 91 |
st.info(
|
| 92 |
+
"Semantic Search compares meanings instead of matching exact keywords."
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# ==========================================
|
| 96 |
+
# DATABASE STATS
|
| 97 |
+
# ==========================================
|
| 98 |
|
| 99 |
st.markdown("## π Database Statistics")
|
| 100 |
|
|
|
|
| 112 |
"MiniLM-L6-v2"
|
| 113 |
)
|
| 114 |
|
| 115 |
+
# ==========================================
|
| 116 |
+
# DOCUMENT INPUT
|
| 117 |
+
# ==========================================
|
| 118 |
|
| 119 |
st.markdown("---")
|
| 120 |
st.markdown("## π₯ Add Documents")
|
| 121 |
|
| 122 |
documents = st.text_area(
|
| 123 |
+
"Enter documents (one document per line)",
|
| 124 |
+
height=220,
|
| 125 |
placeholder="""
|
| 126 |
Python is a programming language.
|
| 127 |
FastAPI is used to build APIs.
|
|
|
|
| 159 |
)
|
| 160 |
|
| 161 |
st.success(
|
| 162 |
+
f"{len(docs)} document(s) stored successfully."
|
| 163 |
)
|
| 164 |
|
| 165 |
st.rerun()
|
| 166 |
|
| 167 |
+
# ==========================================
|
| 168 |
+
# SEARCH SECTION
|
| 169 |
+
# ==========================================
|
| 170 |
|
| 171 |
st.markdown("---")
|
| 172 |
+
st.markdown("## π Search")
|
| 173 |
|
| 174 |
query = st.text_input(
|
| 175 |
+
"Enter your search query",
|
| 176 |
placeholder="How can I build an API?"
|
| 177 |
)
|
| 178 |
|
|
|
|
| 184 |
if collection.count() == 0:
|
| 185 |
|
| 186 |
st.error(
|
| 187 |
+
"No documents available. Add documents first."
|
| 188 |
)
|
| 189 |
|
| 190 |
elif not query.strip():
|
|
|
|
| 196 |
else:
|
| 197 |
|
| 198 |
with st.spinner(
|
| 199 |
+
"Searching similar documents..."
|
| 200 |
):
|
| 201 |
|
| 202 |
query_embedding = model.encode(
|
|
|
|
| 216 |
docs = results["documents"][0]
|
| 217 |
distances = results["distances"][0]
|
| 218 |
|
| 219 |
+
st.markdown("---")
|
| 220 |
+
st.markdown("## π Search Results")
|
| 221 |
|
| 222 |
for rank, (doc, distance) in enumerate(
|
| 223 |
zip(docs, distances),
|
| 224 |
start=1
|
| 225 |
):
|
| 226 |
|
| 227 |
+
# Relevance Label
|
| 228 |
+
if distance < 0.7:
|
| 229 |
+
relevance = "π’ Highly Relevant"
|
| 230 |
+
elif distance < 1.2:
|
| 231 |
+
relevance = "π‘ Relevant"
|
| 232 |
+
else:
|
| 233 |
+
relevance = "π΄ Weak Match"
|
| 234 |
|
| 235 |
with st.expander(
|
| 236 |
+
f"#{rank} | {relevance}"
|
| 237 |
):
|
| 238 |
st.write(doc)
|
| 239 |
|
| 240 |
+
st.caption(
|
| 241 |
+
f"Distance Score: {distance:.4f}"
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
# ==========================================
|
| 245 |
+
# FOOTER
|
| 246 |
+
# ==========================================
|
| 247 |
|
| 248 |
st.markdown("---")
|