Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,64 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
from search_utils import SemanticSearch
|
3 |
|
4 |
@st.cache_resource
|
5 |
def init_search_system():
|
6 |
-
search_system = SemanticSearch(
|
7 |
search_system.initialize_system()
|
8 |
return search_system
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
col1, col2 = st.columns([3, 1])
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
if query:
|
24 |
with st.spinner("Searching through documents..."):
|
25 |
-
|
26 |
-
threshold_results = search_system.search_with_threshold(query, top_k, threshold)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
use_container_width=True,
|
32 |
-
hide_index=True
|
33 |
-
)
|
34 |
-
|
35 |
-
st.subheader(f"Filtered (>{threshold:.0%} similarity)")
|
36 |
-
if not threshold_results.empty:
|
37 |
-
st.dataframe(
|
38 |
-
threshold_results.style.format({'similarity': "{:.2%}"}),
|
39 |
-
use_container_width=True,
|
40 |
-
hide_index=True
|
41 |
)
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
st.
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
import streamlit as st
|
3 |
from search_utils import SemanticSearch
|
4 |
|
5 |
@st.cache_resource
|
6 |
def init_search_system():
|
7 |
+
search_system = SemanticSearch()
|
8 |
search_system.initialize_system()
|
9 |
return search_system
|
10 |
|
11 |
+
def format_source(url):
|
12 |
+
"""Convert URL to clickable link"""
|
13 |
+
return f'<a href="{url}" target="_blank" style="text-decoration: none;">π Source</a>'
|
14 |
+
|
15 |
+
st.set_page_config(
|
16 |
+
page_title="Semantic Search Engine",
|
17 |
+
page_icon="π",
|
18 |
+
layout="wide"
|
19 |
+
)
|
20 |
|
21 |
+
search_system = init_search_system()
|
|
|
22 |
|
23 |
+
# Custom CSS for better link display
|
24 |
+
st.markdown("""
|
25 |
+
<style>
|
26 |
+
a.source-link {
|
27 |
+
color: #1a73e8 !important;
|
28 |
+
text-decoration: none !important;
|
29 |
+
transition: opacity 0.2s;
|
30 |
+
}
|
31 |
+
a.source-link:hover {
|
32 |
+
opacity: 0.7;
|
33 |
+
text-decoration: none !important;
|
34 |
+
}
|
35 |
+
</style>
|
36 |
+
""", unsafe_allow_html=True)
|
37 |
|
38 |
+
# Search interface
|
39 |
+
st.title("π Semantic Search Engine")
|
40 |
+
query = st.text_input("Search knowledge base:", placeholder="Enter your question...")
|
41 |
|
42 |
if query:
|
43 |
with st.spinner("Searching through documents..."):
|
44 |
+
results = search_system.search(query, 5)
|
|
|
45 |
|
46 |
+
# Format sources as links
|
47 |
+
results['source'] = results['source'].apply(
|
48 |
+
lambda x: f'<a class="source-link" href="{x}" target="_blank">π View Source</a>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
)
|
50 |
+
|
51 |
+
# Display results with HTML
|
52 |
+
st.markdown("### Search Results")
|
53 |
+
for _, row in results.iterrows():
|
54 |
+
with st.container(border=True):
|
55 |
+
st.markdown(f"**{row['title']}**")
|
56 |
+
st.markdown(row['summary'])
|
57 |
+
st.markdown(row['source'], unsafe_allow_html=True)
|
58 |
+
st.progress(float(row['similarity']))
|
59 |
+
|
60 |
+
# Sidebar information
|
61 |
+
with st.sidebar:
|
62 |
+
st.markdown("### System Status")
|
63 |
+
st.metric("Loaded Documents", f"{len(search_system.metadata_mgr.shard_map):,}")
|
64 |
+
st.metric("Index Shards", len(search_system.index_shards))
|