Spaces:
Runtime error
Runtime error
Simple search engine gui development
#1
by CAntoniadis - opened
- web_gui/streamlit.py +96 -42
web_gui/streamlit.py
CHANGED
|
@@ -8,6 +8,9 @@ from sentence_transformers import SentenceTransformer
|
|
| 8 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
# --- Configuration & Constants ---
|
| 12 |
PAGE_TITLE = "Manga & TV Assistant"
|
| 13 |
DATA_DIRECTORY = "corpus"
|
|
@@ -27,6 +30,17 @@ SYSTEM_PROMPT = HYDE_SYSTEM_PROMPT =(
|
|
| 27 |
|
| 28 |
st.set_page_config(page_title=PAGE_TITLE, layout="wide")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# --- Helper Functions: Data Loading & Processing ---
|
| 32 |
|
|
@@ -398,45 +412,85 @@ def render_chat_interface():
|
|
| 398 |
|
| 399 |
|
| 400 |
# --- Main Application Execution ---
|
| 401 |
-
|
| 402 |
-
initialize_session_state()
|
| 403 |
-
|
| 404 |
-
if st.session_state['doc_embeddings'] is None:
|
| 405 |
-
process_documents_and_embed()
|
| 406 |
-
|
| 407 |
-
render_sidebar()
|
| 408 |
-
msg_container = render_chat_interface()
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
st.
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
|
| 11 |
+
###### Import search engine from directory
|
| 12 |
+
from simple_search_engine.search_engine import SimpleSearchEngine
|
| 13 |
+
|
| 14 |
# --- Configuration & Constants ---
|
| 15 |
PAGE_TITLE = "Manga & TV Assistant"
|
| 16 |
DATA_DIRECTORY = "corpus"
|
|
|
|
| 30 |
|
| 31 |
st.set_page_config(page_title=PAGE_TITLE, layout="wide")
|
| 32 |
|
| 33 |
+
###### Added - Radio button to switch between simple search engine and RAG agent
|
| 34 |
+
def render_mode_selector():
|
| 35 |
+
with st.sidebar:
|
| 36 |
+
st.header("Mode")
|
| 37 |
+
mode = st.radio(
|
| 38 |
+
"Select application mode",
|
| 39 |
+
["RAG Chat", "TF-IDF Search"],
|
| 40 |
+
key="app_mode")
|
| 41 |
+
st.divider()
|
| 42 |
+
return mode
|
| 43 |
+
#####
|
| 44 |
|
| 45 |
# --- Helper Functions: Data Loading & Processing ---
|
| 46 |
|
|
|
|
| 412 |
|
| 413 |
|
| 414 |
# --- Main Application Execution ---
|
| 415 |
+
###### Replaced below and indented once to the right - Mode check
|
| 416 |
+
#initialize_session_state()
|
| 417 |
+
|
| 418 |
+
#if st.session_state['doc_embeddings'] is None:
|
| 419 |
+
# process_documents_and_embed()
|
| 420 |
+
|
| 421 |
+
#render_sidebar()
|
| 422 |
+
#msg_container = render_chat_interface()
|
| 423 |
+
######
|
| 424 |
+
|
| 425 |
+
mode = render_mode_selector()
|
| 426 |
+
|
| 427 |
+
if mode == "RAG Chat":
|
| 428 |
+
initialize_session_state()
|
| 429 |
+
|
| 430 |
+
if st.session_state['doc_embeddings'] is None:
|
| 431 |
+
process_documents_and_embed()
|
| 432 |
+
|
| 433 |
+
render_sidebar()
|
| 434 |
+
msg_container = render_chat_interface()
|
| 435 |
+
|
| 436 |
+
if prompt := st.chat_input("Ask a question..."):
|
| 437 |
+
msg_container.chat_message("user", avatar=":material/psychology_alt:").markdown(prompt)
|
| 438 |
+
|
| 439 |
+
col1, col2 = msg_container.columns(2)
|
| 440 |
+
|
| 441 |
+
with col1:
|
| 442 |
+
with st.spinner("Standard RAG..."):
|
| 443 |
+
sim_results = find_similar_context(prompt)
|
| 444 |
+
if sim_results['max_similarity'] > st.session_state['similarity_threshold']:
|
| 445 |
+
std_response, _ = generate_rag_response(prompt, sim_results['sentence_indices'], sim_results['sources'])
|
| 446 |
+
else:
|
| 447 |
+
std_response = f"Low similarity ({sim_results['max_similarity']:.2f}). No relevant info found."
|
| 448 |
+
st.markdown("### Standard RAG")
|
| 449 |
+
st.markdown(std_response)
|
| 450 |
+
|
| 451 |
+
with col2:
|
| 452 |
+
with st.spinner("HyDE processing..."):
|
| 453 |
+
hyde_response, hyde_hypothetical, hyde_sim_results, hyde_score = run_hyde_process(prompt)
|
| 454 |
+
st.markdown("### HyDE Response")
|
| 455 |
+
st.markdown(hyde_response)
|
| 456 |
+
|
| 457 |
+
# Save history
|
| 458 |
+
st.session_state['chat_history'].append({"role": "user", "content": prompt})
|
| 459 |
+
st.session_state['chat_history'].append({"role": "assistant", "content": std_response, "type": "normal"})
|
| 460 |
+
st.session_state['chat_history'].append({"role": "assistant", "content": hyde_response, "type": "hyde"})
|
| 461 |
+
|
| 462 |
+
# Update Session State for Sidebar
|
| 463 |
+
st.session_state['last_std_sources'] = sim_results.get('sources', set())
|
| 464 |
+
st.session_state['last_hyde_sources'] = hyde_sim_results.get('sources', set())
|
| 465 |
+
st.session_state['last_hyde_hypothetical'] = hyde_hypothetical
|
| 466 |
+
|
| 467 |
+
# Rerun to update sidebar immediately
|
| 468 |
+
st.rerun()
|
| 469 |
+
|
| 470 |
+
###### Added TF-IDF logic
|
| 471 |
+
if mode == "TF-IDF Search":
|
| 472 |
+
if "tfidf_engine" not in st.session_state:
|
| 473 |
+
engine = SimpleSearchEngine(corpus_dir="corpus")
|
| 474 |
+
engine.build_index()
|
| 475 |
+
st.session_state["tfidf_engine"] = engine
|
| 476 |
+
st.title("TF-IDF Search Engine")
|
| 477 |
+
|
| 478 |
+
query = st.text_input("Search",placeholder="Search the One Piece corpus like Google…")
|
| 479 |
+
|
| 480 |
+
if query:
|
| 481 |
+
results = st.session_state["tfidf_engine"].search(query, top_k=10)
|
| 482 |
+
|
| 483 |
+
if not results:
|
| 484 |
+
st.info("No results found.")
|
| 485 |
+
else:
|
| 486 |
+
st.caption(f"Showing {len(results)} results")
|
| 487 |
+
|
| 488 |
+
for i, r in enumerate(results, start=1):
|
| 489 |
+
st.markdown(f"### {i}. {r['title']}")
|
| 490 |
+
st.caption(f"Relevance score: {r['score']:.4f}")
|
| 491 |
+
|
| 492 |
+
if r.get("url"):
|
| 493 |
+
st.markdown(r["url"])
|
| 494 |
+
|
| 495 |
+
st.write(r["snippet"])
|
| 496 |
+
st.divider()
|