Parsa Kzr commited on
Commit
f39b7b1
·
1 Parent(s): 59de00b

style: demo's output formatting without breaking API tests

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -23,6 +23,7 @@ from nlp4web_codebase.ir.data_loaders.dm import Document
23
  from nlp4web_codebase.ir.models import BaseRetriever
24
  from nlp4web_codebase.ir.data_loaders import Split
25
  from scipy.sparse._csc import csc_matrix
 
26
 
27
 
28
  # ----------------- PRE SETUP ----------------- #
@@ -570,7 +571,8 @@ def search(query: str, index_dir: str = index_dir) -> List[Hit]: # , topk:int =
570
  return hits
571
 
572
 
573
- '''
 
574
  # Function for formatted display of results
575
  def format_hits_md(hits: List[Hit]) -> str:
576
  if not hits:
@@ -584,6 +586,8 @@ def format_hits_md(hits: List[Hit]) -> str:
584
  f"* Text: {hit['text'] or 'No text available.'}\n"
585
  )
586
  return "\n".join(formatted)
 
 
587
  # to return pure json data as a list of json objects
588
  def format_hits_json(hits: List[Hit]):
589
  if not hits:
@@ -592,27 +596,41 @@ def format_hits_json(hits: List[Hit]):
592
  # json format
593
  for hit in hits:
594
  formatted.append(
595
- {
596
- "cid": hit['cid'],
597
- "score": hit['score'],
598
- "text": hit['text'] or ''
599
- }
600
  )
601
  return formatted
602
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603
  # Gradio wrapper
604
- def interface_search(query: str) -> str: # , topk: int = 10
605
  """Wrapper for Gradio interface to call search function and format results."""
606
  try:
607
- hits = search(query) # , topk=topk
608
- return format_hits_json(hits) # [json, md]
609
  except Exception as e:
610
  return f"Error: {str(e)}"
611
- '''
612
 
613
  # app interface
614
  demo = gr.Interface(
615
- fn=search, # interface_search to format Markdown or JSON
616
  inputs=[
617
  gr.Textbox(label="Search Query", placeholder="Type your search query"),
618
  # gr.Number(label="Number of Results (Top-k)", value=10),
 
23
  from nlp4web_codebase.ir.models import BaseRetriever
24
  from nlp4web_codebase.ir.data_loaders import Split
25
  from scipy.sparse._csc import csc_matrix
26
+ import json
27
 
28
 
29
  # ----------------- PRE SETUP ----------------- #
 
571
  return hits
572
 
573
 
574
+ # Since the test cases limit using Non-Interface or TextBox as output, ->
575
+ # -> these are obselete for passing tests, but better formatted.
576
  # Function for formatted display of results
577
  def format_hits_md(hits: List[Hit]) -> str:
578
  if not hits:
 
586
  f"* Text: {hit['text'] or 'No text available.'}\n"
587
  )
588
  return "\n".join(formatted)
589
+
590
+
591
  # to return pure json data as a list of json objects
592
  def format_hits_json(hits: List[Hit]):
593
  if not hits:
 
596
  # json format
597
  for hit in hits:
598
  formatted.append(
599
+ {"cid": hit["cid"], "score": hit["score"], "text": hit["text"] or ""}
 
 
 
 
600
  )
601
  return formatted
602
 
603
+
604
+ def format_hits_jsonstr(hits: List[Hit]):
605
+ if not hits:
606
+ return
607
+ formatted = "["
608
+ for hit in hits:
609
+ formatted += (
610
+ json.dumps(
611
+ {"cid": hit["cid"], "score": hit["score"], "text": hit["text"] or ""},
612
+ separators=(",", ":"),
613
+ indent=4,
614
+ )
615
+ + ",\n"
616
+ )
617
+ formatted = formatted[:-2] + "]"
618
+ return formatted
619
+
620
+
621
  # Gradio wrapper
622
+ def interface_search(query: str) -> str: # , topk: int = 10
623
  """Wrapper for Gradio interface to call search function and format results."""
624
  try:
625
+ hits = search(query) # , topk=topk
626
+ return format_hits_jsonstr(hits) # [jsonstr, json, md]
627
  except Exception as e:
628
  return f"Error: {str(e)}"
629
+
630
 
631
  # app interface
632
  demo = gr.Interface(
633
+ fn=interface_search, # interface_search to format Markdown or JSON
634
  inputs=[
635
  gr.Textbox(label="Search Query", placeholder="Type your search query"),
636
  # gr.Number(label="Number of Results (Top-k)", value=10),