Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,18 +10,80 @@ MODEL_ID = "KaLM-Embedding/KaLM-Reranker-V1-Nano"
|
|
| 10 |
DEFAULT_INSTRUCTION = "Given a query, retrieve documents that answer the query."
|
| 11 |
MAX_DOCS = 20
|
| 12 |
MAX_DOC_CHARS = 4000
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
docs = docs[:MAX_DOCS]
|
| 19 |
docs = [doc[:MAX_DOC_CHARS] for doc in docs]
|
| 20 |
-
return docs
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
@spaces.GPU
|
| 24 |
-
def rerank(query: str, documents: str, instruction: str, chunk_size: int):
|
| 25 |
reranker = KaLMReranker(
|
| 26 |
MODEL_ID,
|
| 27 |
device=None,
|
|
@@ -34,12 +96,6 @@ def rerank(query: str, documents: str, instruction: str, chunk_size: int):
|
|
| 34 |
|
| 35 |
query = query.strip()
|
| 36 |
instruction = instruction.strip() or DEFAULT_INSTRUCTION
|
| 37 |
-
docs = parse_documents(documents)
|
| 38 |
-
|
| 39 |
-
if not query:
|
| 40 |
-
return [], "Please input a query."
|
| 41 |
-
if not docs:
|
| 42 |
-
return [], "Please input at least one candidate document."
|
| 43 |
|
| 44 |
try:
|
| 45 |
rankings = reranker.rank(
|
|
@@ -64,49 +120,67 @@ def rerank(query: str, documents: str, instruction: str, chunk_size: int):
|
|
| 64 |
return [], f"Error during reranking: {repr(error)}"
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
| 67 |
with gr.Blocks(title="KaLM-Reranker-V1 Demo") as demo:
|
| 68 |
gr.Markdown(
|
| 69 |
-
|
| 70 |
# KaLM-Reranker-V1 Demo
|
| 71 |
**KaLM-Reranker-V1** is a fast but not late-interaction reranker for compressed document reranking.
|
| 72 |
Input a query and several candidate documents. The demo returns relevance scores and reranked results.
|
| 73 |
-
**Document format:** separate candidate documents with `{DOC_SEPARATOR}` (three dashes on its own line).
|
| 74 |
"""
|
| 75 |
)
|
| 76 |
|
| 77 |
with gr.Row():
|
|
|
|
| 78 |
with gr.Column(scale=1):
|
| 79 |
-
|
| 80 |
label="Query",
|
| 81 |
value="What is the capital of China?",
|
| 82 |
lines=3,
|
| 83 |
)
|
| 84 |
-
|
| 85 |
label="Instruction",
|
| 86 |
value=DEFAULT_INSTRUCTION,
|
| 87 |
lines=2,
|
| 88 |
)
|
| 89 |
-
|
| 90 |
label="Chunk Size",
|
| 91 |
choices=[2, 4, 8, 16],
|
| 92 |
value=4,
|
| 93 |
interactive=True,
|
| 94 |
)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
"Shanghai is a major city in China.\n"
|
| 103 |
-
"---\n"
|
| 104 |
-
"Paris is the capital of France."
|
| 105 |
-
),
|
| 106 |
-
lines=14,
|
| 107 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
submit = gr.Button("Rerank", variant="primary")
|
| 109 |
|
|
|
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
output_table = gr.Dataframe(
|
| 112 |
headers=["Rank", "Corpus ID", "Score", "Document"],
|
|
@@ -115,74 +189,48 @@ Input a query and several candidate documents. The demo returns relevance scores
|
|
| 115 |
)
|
| 116 |
output_summary = gr.Markdown()
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
submit.click(
|
| 119 |
fn=rerank,
|
| 120 |
-
inputs=[
|
| 121 |
outputs=[output_table, output_summary],
|
| 122 |
)
|
| 123 |
|
| 124 |
-
gr.Examples(
|
| 125 |
-
examples=[
|
| 126 |
-
[
|
| 127 |
-
"What is the capital of China?",
|
| 128 |
-
(
|
| 129 |
-
"The capital of China is Beijing.\n"
|
| 130 |
-
"---\n"
|
| 131 |
-
"Gravity attracts bodies toward one another.\n"
|
| 132 |
-
"---\n"
|
| 133 |
-
"Paris is the capital of France."
|
| 134 |
-
),
|
| 135 |
-
DEFAULT_INSTRUCTION,
|
| 136 |
-
4,
|
| 137 |
-
],
|
| 138 |
-
[
|
| 139 |
-
"Which model is suitable for efficient reranking?",
|
| 140 |
-
(
|
| 141 |
-
"KaLM-Reranker-V1-Nano is designed for efficient reranking.\n"
|
| 142 |
-
"---\n"
|
| 143 |
-
"Large language models are often expensive for reranking.\n"
|
| 144 |
-
"---\n"
|
| 145 |
-
"Image classifiers are used for visual recognition."
|
| 146 |
-
),
|
| 147 |
-
DEFAULT_INSTRUCTION,
|
| 148 |
-
4,
|
| 149 |
-
],
|
| 150 |
-
[
|
| 151 |
-
"What is KaLM-Reranker-V1 designed for?",
|
| 152 |
-
(
|
| 153 |
-
"KaLM-Reranker-V1 is a reranker for compressed document reranking.\n"
|
| 154 |
-
"---\n"
|
| 155 |
-
"KaLM-Embedding is a general-purpose embedding model.\n"
|
| 156 |
-
"---\n"
|
| 157 |
-
"Weather forecasting predicts future weather conditions."
|
| 158 |
-
),
|
| 159 |
-
DEFAULT_INSTRUCTION,
|
| 160 |
-
4,
|
| 161 |
-
],
|
| 162 |
-
],
|
| 163 |
-
inputs=[query, documents, instruction, chunk_size],
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
gr.Markdown(
|
| 167 |
"""
|
| 168 |
## Citation
|
| 169 |
-
|
| 170 |
If you find this demo useful, please cite:
|
| 171 |
-
|
| 172 |
```bibtex
|
| 173 |
@misc{zhao2026kalmrerankerv1,
|
| 174 |
-
title={KaLM-Reranker-V1: Fast but Not Late Interaction for Compressed Document Reranking},
|
| 175 |
author={Xinping Zhao and Jiaxin Xu and Ziqi Dai and Xin Zhang and Shouzheng Huang and Danyu Tang and Xinshuo Hu and Meishan Zhang and Baotian Hu and Min Zhang},
|
| 176 |
year={2026},
|
| 177 |
eprint={2606.22807},
|
| 178 |
archivePrefix={arXiv},
|
| 179 |
primaryClass={cs.CL},
|
| 180 |
-
url={https://arxiv.org/abs/2606.22807},
|
| 181 |
-
}
|
| 182 |
```
|
| 183 |
"""
|
| 184 |
)
|
| 185 |
-
|
| 186 |
-
|
| 187 |
if __name__ == "__main__":
|
| 188 |
demo.launch()
|
|
|
|
| 10 |
DEFAULT_INSTRUCTION = "Given a query, retrieve documents that answer the query."
|
| 11 |
MAX_DOCS = 20
|
| 12 |
MAX_DOC_CHARS = 4000
|
| 13 |
+
INIT_DOCS = 4
|
| 14 |
+
|
| 15 |
+
EXAMPLES = [
|
| 16 |
+
{
|
| 17 |
+
"label": "Capital of China",
|
| 18 |
+
"query": "What is the capital of China?",
|
| 19 |
+
"docs": [
|
| 20 |
+
"The capital of China is Beijing.",
|
| 21 |
+
"Gravity attracts bodies toward one another.",
|
| 22 |
+
"Paris is the capital of France.",
|
| 23 |
+
],
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"label": "Efficient Reranking",
|
| 27 |
+
"query": "Which model is suitable for efficient reranking?",
|
| 28 |
+
"docs": [
|
| 29 |
+
"KaLM-Reranker-V1-Nano is designed for efficient reranking.",
|
| 30 |
+
"Large language models are often expensive for reranking.",
|
| 31 |
+
"Image classifiers are used for visual recognition.",
|
| 32 |
+
],
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"label": "KaLM-Reranker Design",
|
| 36 |
+
"query": "What is KaLM-Reranker-V1 designed for?",
|
| 37 |
+
"docs": [
|
| 38 |
+
"KaLM-Reranker-V1 is a reranker for compressed document reranking.",
|
| 39 |
+
"KaLM-Embedding is a general-purpose embedding model.",
|
| 40 |
+
"Weather forecasting predicts future weather conditions.",
|
| 41 |
+
],
|
| 42 |
+
},
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _make_vis_updates(count: int):
|
| 47 |
+
return [gr.update(visible=(i < count)) for i in range(MAX_DOCS)]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def add_doc(count: int):
|
| 51 |
+
count = min(count + 1, MAX_DOCS)
|
| 52 |
+
return _make_vis_updates(count) + [count]
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def remove_doc(count: int):
|
| 56 |
+
count = max(count - 1, 1)
|
| 57 |
+
return _make_vis_updates(count) + [count]
|
| 58 |
|
| 59 |
|
| 60 |
+
def load_example(example_idx):
|
| 61 |
+
if example_idx is None:
|
| 62 |
+
query_upd = gr.update(value="")
|
| 63 |
+
doc_upds = [gr.update(value="") for _ in range(MAX_DOCS)]
|
| 64 |
+
return [query_upd] + doc_upds + [INIT_DOCS]
|
| 65 |
+
ex = EXAMPLES[example_idx]
|
| 66 |
+
docs = ex["docs"]
|
| 67 |
+
count = len(docs)
|
| 68 |
+
query_upd = gr.update(value=ex["query"])
|
| 69 |
+
doc_upds = [
|
| 70 |
+
gr.update(value=docs[i] if i < count else "", visible=(i < count))
|
| 71 |
+
for i in range(MAX_DOCS)
|
| 72 |
+
]
|
| 73 |
+
return [query_upd] + doc_upds + [count]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@spaces.GPU
|
| 77 |
+
def rerank(query: str, instruction: str, chunk_size: int, *doc_texts: str):
|
| 78 |
+
docs = [doc.strip() for doc in doc_texts if doc and doc.strip()]
|
| 79 |
docs = docs[:MAX_DOCS]
|
| 80 |
docs = [doc[:MAX_DOC_CHARS] for doc in docs]
|
|
|
|
| 81 |
|
| 82 |
+
if not query.strip():
|
| 83 |
+
return [], "Please input a query."
|
| 84 |
+
if not docs:
|
| 85 |
+
return [], "Please input at least one candidate document."
|
| 86 |
|
|
|
|
|
|
|
| 87 |
reranker = KaLMReranker(
|
| 88 |
MODEL_ID,
|
| 89 |
device=None,
|
|
|
|
| 96 |
|
| 97 |
query = query.strip()
|
| 98 |
instruction = instruction.strip() or DEFAULT_INSTRUCTION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
try:
|
| 101 |
rankings = reranker.rank(
|
|
|
|
| 120 |
return [], f"Error during reranking: {repr(error)}"
|
| 121 |
|
| 122 |
|
| 123 |
+
# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 124 |
+
|
| 125 |
with gr.Blocks(title="KaLM-Reranker-V1 Demo") as demo:
|
| 126 |
gr.Markdown(
|
| 127 |
+
"""
|
| 128 |
# KaLM-Reranker-V1 Demo
|
| 129 |
**KaLM-Reranker-V1** is a fast but not late-interaction reranker for compressed document reranking.
|
| 130 |
Input a query and several candidate documents. The demo returns relevance scores and reranked results.
|
|
|
|
| 131 |
"""
|
| 132 |
)
|
| 133 |
|
| 134 |
with gr.Row():
|
| 135 |
+
# ββ Left column: inputs ββ
|
| 136 |
with gr.Column(scale=1):
|
| 137 |
+
query_input = gr.Textbox(
|
| 138 |
label="Query",
|
| 139 |
value="What is the capital of China?",
|
| 140 |
lines=3,
|
| 141 |
)
|
| 142 |
+
instruction_input = gr.Textbox(
|
| 143 |
label="Instruction",
|
| 144 |
value=DEFAULT_INSTRUCTION,
|
| 145 |
lines=2,
|
| 146 |
)
|
| 147 |
+
chunk_size_input = gr.Dropdown(
|
| 148 |
label="Chunk Size",
|
| 149 |
choices=[2, 4, 8, 16],
|
| 150 |
value=4,
|
| 151 |
interactive=True,
|
| 152 |
)
|
| 153 |
+
|
| 154 |
+
# Example selector
|
| 155 |
+
example_selector = gr.Dropdown(
|
| 156 |
+
label="Load Example",
|
| 157 |
+
choices=[ex["label"] for ex in EXAMPLES],
|
| 158 |
+
value=None,
|
| 159 |
+
interactive=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
)
|
| 161 |
+
|
| 162 |
+
gr.Markdown("### Candidate Documents")
|
| 163 |
+
doc_count = gr.State(value=INIT_DOCS)
|
| 164 |
+
doc_textboxes: List[gr.Textbox] = []
|
| 165 |
+
|
| 166 |
+
with gr.Column() as doc_list:
|
| 167 |
+
for i in range(MAX_DOCS):
|
| 168 |
+
visible = i < INIT_DOCS
|
| 169 |
+
tb = gr.Textbox(
|
| 170 |
+
label=f"Document {i + 1}",
|
| 171 |
+
lines=2,
|
| 172 |
+
placeholder=f"Enter document {i + 1}...",
|
| 173 |
+
visible=visible,
|
| 174 |
+
)
|
| 175 |
+
doc_textboxes.append(tb)
|
| 176 |
+
|
| 177 |
+
with gr.Row():
|
| 178 |
+
add_btn = gr.Button("+ Add Document", size="sm")
|
| 179 |
+
remove_btn = gr.Button("- Remove Last", size="sm")
|
| 180 |
+
|
| 181 |
submit = gr.Button("Rerank", variant="primary")
|
| 182 |
|
| 183 |
+
# ββ Right column: outputs ββ
|
| 184 |
with gr.Column(scale=1):
|
| 185 |
output_table = gr.Dataframe(
|
| 186 |
headers=["Rank", "Corpus ID", "Score", "Document"],
|
|
|
|
| 189 |
)
|
| 190 |
output_summary = gr.Markdown()
|
| 191 |
|
| 192 |
+
# ββ Events ββ
|
| 193 |
+
add_btn.click(
|
| 194 |
+
fn=add_doc,
|
| 195 |
+
inputs=[doc_count],
|
| 196 |
+
outputs=doc_textboxes + [doc_count],
|
| 197 |
+
)
|
| 198 |
+
remove_btn.click(
|
| 199 |
+
fn=remove_doc,
|
| 200 |
+
inputs=[doc_count],
|
| 201 |
+
outputs=doc_textboxes + [doc_count],
|
| 202 |
+
)
|
| 203 |
+
example_selector.change(
|
| 204 |
+
fn=load_example,
|
| 205 |
+
inputs=[example_selector],
|
| 206 |
+
outputs=[query_input] + doc_textboxes + [doc_count],
|
| 207 |
+
)
|
| 208 |
submit.click(
|
| 209 |
fn=rerank,
|
| 210 |
+
inputs=[query_input, instruction_input, chunk_size_input] + doc_textboxes,
|
| 211 |
outputs=[output_table, output_summary],
|
| 212 |
)
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
gr.Markdown(
|
| 215 |
"""
|
| 216 |
## Citation
|
| 217 |
+
|
| 218 |
If you find this demo useful, please cite:
|
| 219 |
+
|
| 220 |
```bibtex
|
| 221 |
@misc{zhao2026kalmrerankerv1,
|
| 222 |
+
title={KaLM-Reranker-V1: Fast but Not Late Interaction for Compressed Document Reranking},
|
| 223 |
author={Xinping Zhao and Jiaxin Xu and Ziqi Dai and Xin Zhang and Shouzheng Huang and Danyu Tang and Xinshuo Hu and Meishan Zhang and Baotian Hu and Min Zhang},
|
| 224 |
year={2026},
|
| 225 |
eprint={2606.22807},
|
| 226 |
archivePrefix={arXiv},
|
| 227 |
primaryClass={cs.CL},
|
| 228 |
+
url={https://arxiv.org/abs/2606.22807},
|
| 229 |
+
}
|
| 230 |
```
|
| 231 |
"""
|
| 232 |
)
|
| 233 |
+
|
| 234 |
+
|
| 235 |
if __name__ == "__main__":
|
| 236 |
demo.launch()
|