CAntoniadis commited on
Commit
a5694fd
·
verified ·
1 Parent(s): 2cc3d75

Changes to UI

Browse files

Changed response to be at 200 characters
Removed snippet from tfidf
def generate_search_summary
### 🔍 Summary
cached_search_summary

Files changed (1) hide show
  1. web_gui/streamlit.py +62 -4
web_gui/streamlit.py CHANGED
@@ -22,7 +22,7 @@ EMBEDDING_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
22
  # System Instructions
23
  SYSTEM_PROMPT = HYDE_SYSTEM_PROMPT =(
24
  "You are a helpful assistant that answers questions about a manga and tv-series called One Piece. "
25
- "Be brief and concise. Provide your answers in 100 words or less."
26
  )
27
  # HYDE_SYSTEM_PROMPT = (
28
  # "You are a helpful assistant that generates a hypothetical answer to the user's question. "
@@ -35,6 +35,24 @@ st.set_page_config(page_title=PAGE_TITLE, layout="wide")
35
  @st.cache_resource
36
  def load_embedding_model():
37
  return SentenceTransformer(EMBEDDING_MODEL_NAME, device="cpu")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  #####
39
 
40
  ###### Added - Radio button to switch between simple search engine and RAG agent
@@ -49,6 +67,37 @@ def render_mode_selector():
49
  return mode
50
  #####
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  # --- Helper Functions: Data Loading & Processing ---
53
 
54
  def load_texts_from_directory(base_dir: str) -> Dict[str, Dict]:
@@ -488,12 +537,22 @@ if mode == "TF-IDF Search":
488
  query = st.text_input("Search",placeholder="Search the One Piece corpus like Google…")
489
 
490
  if query:
491
- results = st.session_state["tfidf_engine"].search(query, top_k=10)
 
 
 
 
 
 
 
 
 
 
492
 
493
  if not results:
494
  st.info("No results found.")
495
  else:
496
- st.caption(f"Showing {len(results)} results")
497
 
498
  for i, r in enumerate(results, start=1):
499
  st.markdown(f"### {i}. {r['title']}")
@@ -502,5 +561,4 @@ if mode == "TF-IDF Search":
502
  if r.get("url"):
503
  st.markdown(r["url"])
504
 
505
- st.write(r["snippet"])
506
  st.divider()
 
22
  # System Instructions
23
  SYSTEM_PROMPT = HYDE_SYSTEM_PROMPT =(
24
  "You are a helpful assistant that answers questions about a manga and tv-series called One Piece. "
25
+ "Be brief and concise. Provide your answers in 200 words or less."
26
  )
27
  # HYDE_SYSTEM_PROMPT = (
28
  # "You are a helpful assistant that generates a hypothetical answer to the user's question. "
 
35
  @st.cache_resource
36
  def load_embedding_model():
37
  return SentenceTransformer(EMBEDDING_MODEL_NAME, device="cpu")
38
+
39
+ @st.cache_data(show_spinner=False)
40
+ def cached_search_summary(
41
+ query: str,
42
+ title_url_pairs: List[Tuple[str, str]]
43
+ ) -> str:
44
+ """
45
+ Cached wrapper for AI search summaries.
46
+ Converts hashable (title, url) pairs back into dicts.
47
+ """
48
+
49
+ results = [
50
+ {"title": title, "url": url}
51
+ for title, url in title_url_pairs
52
+ ]
53
+
54
+ return generate_search_summary(query, results)
55
+
56
  #####
57
 
58
  ###### Added - Radio button to switch between simple search engine and RAG agent
 
67
  return mode
68
  #####
69
 
70
+ def generate_search_summary(query: str, results: List[Dict]) -> str:
71
+ """
72
+ Generates a short AI summary for TF-IDF search results.
73
+ Uses only titles + URLs (not internal cleaned text).
74
+ """
75
+
76
+ if not results:
77
+ return ""
78
+
79
+ sources_text = "\n".join(
80
+ f"- {r['title']} ({r['url']})"
81
+ for r in results
82
+ if r.get("url")
83
+ )
84
+
85
+ prompt = (
86
+ "You are a helpful assistant summarizing search results.\n\n"
87
+ f"User search query: {query}\n\n"
88
+ "Here are relevant pages:\n"
89
+ f"{sources_text}\n\n"
90
+ "Write a short, high-level summary (2–4 sentences) of what the user "
91
+ "is likely looking for. Do not list episodes. Be factual and concise."
92
+ )
93
+
94
+ messages = [
95
+ {"role": "system", "content": SYSTEM_PROMPT},
96
+ {"role": "user", "content": prompt}
97
+ ]
98
+
99
+ return query_llm(messages, max_tokens=150)
100
+
101
  # --- Helper Functions: Data Loading & Processing ---
102
 
103
  def load_texts_from_directory(base_dir: str) -> Dict[str, Dict]:
 
537
  query = st.text_input("Search",placeholder="Search the One Piece corpus like Google…")
538
 
539
  if query:
540
+ results = st.session_state["tfidf_engine"].search(query, top_k=5)
541
+
542
+ if results:
543
+ with st.spinner("Generating summary..."):
544
+ summary = cached_search_summary(query,
545
+ [(r["title"], r.get("url")) for r in results])
546
+
547
+ if summary:
548
+ st.markdown("### 🔍 Summary")
549
+ st.write(summary)
550
+ st.divider()
551
 
552
  if not results:
553
  st.info("No results found.")
554
  else:
555
+ st.caption("Top 5 relevant pages")
556
 
557
  for i, r in enumerate(results, start=1):
558
  st.markdown(f"### {i}. {r['title']}")
 
561
  if r.get("url"):
562
  st.markdown(r["url"])
563
 
 
564
  st.divider()