Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from typing import TypedDict
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
class Hit(TypedDict):
|
| 6 |
+
cid: str
|
| 7 |
+
score: float
|
| 8 |
+
text: str
|
| 9 |
+
|
| 10 |
+
demo: Optional[gr.Interface] = None # Assign your gradio demo to this variable
|
| 11 |
+
return_type = List[Hit]
|
| 12 |
+
|
| 13 |
+
## YOUR_CODE_STARTS_HERE
|
| 14 |
+
def retrieve(query: str, topk: int=10) -> return_type:
|
| 15 |
+
ranking = bm25_retriever.retrieve(query=query, topk=3)
|
| 16 |
+
hits = []
|
| 17 |
+
for cid, score in ranking.items():
|
| 18 |
+
text = bm25_retriever.index.doc_texts[bm25_retriever.index.cid2docid[cid]]
|
| 19 |
+
hits.append({"cid": cid, "score": score, "text": text})
|
| 20 |
+
return hits
|
| 21 |
+
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=retrieve,
|
| 24 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter your query here..."),
|
| 25 |
+
outputs="json",
|
| 26 |
+
title="CSC BM25 Retriever",
|
| 27 |
+
description="Retrieve documents based on the query using CSC BM25 Retriever",
|
| 28 |
+
examples=[
|
| 29 |
+
["What are the differences between immunodeficiency and autoimmune diseases?"],
|
| 30 |
+
["What are the causes of immunodeficiency?"],
|
| 31 |
+
["What are the symptoms of immunodeficiency?"],
|
| 32 |
+
]
|
| 33 |
+
)
|
| 34 |
+
## YOUR_CODE_ENDS_HERE
|
| 35 |
+
demo.launch()
|