cakiki commited on
Commit
1a2fbfc
·
1 Parent(s): d6cb72b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -27,8 +27,12 @@ def result_html(result, meta):
27
  f"<div><details><summary>{result[:250]}...</summary><p>{result[250:]}</p></details></div><br><hr><br>"
28
  )
29
 
30
- def format_results(results):
31
- return "\n".join([result_html(result, meta) for result,meta in zip(results[TEXT_FIELD], results[METADATA_FIELD])])
 
 
 
 
32
 
33
  def page_0(query):
34
  untokenized_query = query
@@ -36,13 +40,13 @@ def page_0(query):
36
  hits = searcher.search(query, k=NUM_PAGES*RESULTS_PER_PAGE)
37
  ix = [int(hit.docid) for hit in hits]
38
  results = ds.select(ix).shard(num_shards=NUM_PAGES, index=0, contiguous=True) # no need to shard. split ix in batches instead. (would make sense if results was cacheable)
39
- results = format_results(results)
40
  return results, [ix], gr.update(visible=True), untokenized_query
41
 
42
  def page_i(i, ix, query):
43
  ix = ix[0]
44
  results = ds.select(ix).shard(num_shards=NUM_PAGES, index=i, contiguous=True)
45
- results = format_results(results)
46
  return results, [ix], query
47
 
48
  with gr.Blocks(css="#b {min-width:15px;background:transparent;border:white;box-shadow:none;}") as demo: #
 
27
  f"<div><details><summary>{result[:250]}...</summary><p>{result[250:]}</p></details></div><br><hr><br>"
28
  )
29
 
30
+ def format_results(results, query):
31
+ text_content = results[TEXT_FIELD]
32
+ query_words = query.split()
33
+ for word in query_words:
34
+ text_content = [text.replace(word, f"<b>{word}</b>") for text in text_content]
35
+ return "\n".join([result_html(result, meta) for result,meta in zip(text_content, results[METADATA_FIELD])])
36
 
37
  def page_0(query):
38
  untokenized_query = query
 
40
  hits = searcher.search(query, k=NUM_PAGES*RESULTS_PER_PAGE)
41
  ix = [int(hit.docid) for hit in hits]
42
  results = ds.select(ix).shard(num_shards=NUM_PAGES, index=0, contiguous=True) # no need to shard. split ix in batches instead. (would make sense if results was cacheable)
43
+ results = format_results(results, untokenized_query)
44
  return results, [ix], gr.update(visible=True), untokenized_query
45
 
46
  def page_i(i, ix, query):
47
  ix = ix[0]
48
  results = ds.select(ix).shard(num_shards=NUM_PAGES, index=i, contiguous=True)
49
+ results = format_results(results, query)
50
  return results, [ix], query
51
 
52
  with gr.Blocks(css="#b {min-width:15px;background:transparent;border:white;box-shadow:none;}") as demo: #