|
import gradio as gr |
|
from typing import TypedDict |
|
|
|
class Hit(TypedDict): |
|
cid: str |
|
score: float |
|
text: str |
|
|
|
demo: Optional[gr.Interface] = None |
|
return_type = List[Hit] |
|
|
|
def search_sciq(query: str) -> List[Hit]: |
|
results = bm25_retriever.retrieve(query) |
|
|
|
|
|
hits = [] |
|
for cid, score in results.items(): |
|
index = bm25_retriever.index.cid2docid[cid] |
|
text = bm25_retriever.index.doc_texts[index] |
|
hits.append(Hit(cid=cid, score=score, text=text)) |
|
|
|
return hits |
|
|
|
|
|
demo = gr.Interface( |
|
fn=search_sciq, |
|
inputs=gr.Textbox(label="Enter your query"), |
|
outputs=gr.JSON(label="Top 10 Results"), |
|
description="BM25 Search Engine Demo on SciQ Dataset" |
|
) |
|
demo.launch() |
|
|