ShiniChien commited on
Commit
b88ba62
·
verified ·
1 Parent(s): 140b006

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -10
app.py CHANGED
@@ -113,13 +113,19 @@ def clear_all():
113
 
114
 
115
  # ── Tab 2: Destiny Search ────────────────────────────────────────────────────
116
- def destiny_search(query: str, filter_1word: bool, filter_2words: bool, top_k: int):
117
  if not query.strip():
118
- return build_search_html([], query, error="⚠️ Please enter a name to search for your destiny!")
119
 
120
- query_vec = model.encode(query.strip(), convert_to_tensor=False, device=device).tolist()
 
 
 
 
 
 
121
 
122
- # Build metadata filter
123
  if filter_1word and filter_2words:
124
  meta_filter = {"num_words": {"$in": [1, 2]}}
125
  elif filter_1word:
@@ -127,20 +133,33 @@ def destiny_search(query: str, filter_1word: bool, filter_2words: bool, top_k: i
127
  elif filter_2words:
128
  meta_filter = {"num_words": {"$eq": 2}}
129
  else:
130
- meta_filter = None # no filter
131
 
132
  query_kwargs = dict(
133
  vector=query_vec,
134
- top_k=top_k,
135
  include_metadata=True,
136
  )
 
137
  if meta_filter:
138
  query_kwargs["filter"] = meta_filter
139
 
140
  response = index.query(**query_kwargs)
141
  matches = response.get("matches", [])
142
 
143
- return build_search_html(matches, query)
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
 
146
  def build_search_html(matches: list, query: str, error: str = "") -> str:
@@ -210,7 +229,7 @@ def build_search_html(matches: list, query: str, error: str = "") -> str:
210
 
211
 
212
  def clear_search():
213
- return "", gr.update(value=""), True, True, 10
214
 
215
 
216
  # ── CSS ──────────────────────────────────────────────────────────────────────
@@ -437,6 +456,12 @@ with gr.Blocks(css=custom_css, title="LatentLove 💕") as demo:
437
  lines=1,
438
  )
439
 
 
 
 
 
 
 
440
  with gr.Row():
441
  cb_1word = gr.Checkbox(label="1-word names", value=True)
442
  cb_2words = gr.Checkbox(label="2-word names", value=True)
@@ -488,14 +513,14 @@ with gr.Blocks(css=custom_css, title="LatentLove 💕") as demo:
488
 
489
  search_btn.click(
490
  fn=destiny_search,
491
- inputs=[search_query, cb_1word, cb_2words, top_k_slider],
492
  outputs=[search_results],
493
  )
494
 
495
  clear_search_btn.click(
496
  fn=clear_search,
497
  inputs=[],
498
- outputs=[search_query, search_results, cb_1word, cb_2words, top_k_slider],
499
  )
500
 
501
 
 
113
 
114
 
115
  # ── Tab 2: Destiny Search ────────────────────────────────────────────────────
116
+ def destiny_search(query: str, dob: str, filter_1word: bool, filter_2words: bool, top_k: int):
117
  if not query.strip():
118
+ return build_search_html([], query, error="⚠️ Please enter a name to search!")
119
 
120
+ # Combine name + dob
121
+ query_text = query.strip()
122
+ dob = dob.strip()
123
+ if dob:
124
+ query_text = f"{query_text} {dob}"
125
+
126
+ query_vec = model.encode(query_text, convert_to_tensor=False, device=device).tolist()
127
 
128
+ # Metadata filter
129
  if filter_1word and filter_2words:
130
  meta_filter = {"num_words": {"$in": [1, 2]}}
131
  elif filter_1word:
 
133
  elif filter_2words:
134
  meta_filter = {"num_words": {"$eq": 2}}
135
  else:
136
+ meta_filter = None
137
 
138
  query_kwargs = dict(
139
  vector=query_vec,
140
+ top_k=top_k + 5, # small buffer to compensate filtering
141
  include_metadata=True,
142
  )
143
+
144
  if meta_filter:
145
  query_kwargs["filter"] = meta_filter
146
 
147
  response = index.query(**query_kwargs)
148
  matches = response.get("matches", [])
149
 
150
+ # 🔥 Remove input name from results
151
+ query_clean = query.strip().lower()
152
+ filtered_matches = []
153
+
154
+ for m in matches:
155
+ text = m.get("metadata", {}).get("text", "").strip().lower()
156
+ if text != query_clean:
157
+ filtered_matches.append(m)
158
+
159
+ # Trim back to top_k
160
+ filtered_matches = filtered_matches[:top_k]
161
+
162
+ return build_search_html(filtered_matches, query)
163
 
164
 
165
  def build_search_html(matches: list, query: str, error: str = "") -> str:
 
229
 
230
 
231
  def clear_search():
232
+ return "", "", gr.update(value=""), True, True, 10
233
 
234
 
235
  # ── CSS ──────────────────────────────────────────────────────────────────────
 
456
  lines=1,
457
  )
458
 
459
+ search_dob = gr.Textbox(
460
+ label="Date of Birth (optional)",
461
+ placeholder="dd/mm/yyyy",
462
+ lines=1,
463
+ )
464
+
465
  with gr.Row():
466
  cb_1word = gr.Checkbox(label="1-word names", value=True)
467
  cb_2words = gr.Checkbox(label="2-word names", value=True)
 
513
 
514
  search_btn.click(
515
  fn=destiny_search,
516
+ inputs=[search_query, search_dob, cb_1word, cb_2words, top_k_slider],
517
  outputs=[search_results],
518
  )
519
 
520
  clear_search_btn.click(
521
  fn=clear_search,
522
  inputs=[],
523
+ outputs=[search_query, search_dob, search_results, cb_1word, cb_2words, top_k_slider],
524
  )
525
 
526