aikacl commited on
Commit
2b2c2cb
·
verified ·
1 Parent(s): 6a5a93e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -17
app.py CHANGED
@@ -10,17 +10,18 @@ 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
  def parse_documents(text: str) -> List[str]:
16
- docs = [doc.strip() for doc in text.split("\n\n") if doc.strip()]
17
  docs = docs[:MAX_DOCS]
18
  docs = [doc[:MAX_DOC_CHARS] for doc in docs]
19
  return docs
20
 
21
 
22
  @spaces.GPU
23
- def rerank(query: str, documents: str, instruction: str):
24
  reranker = KaLMReranker(
25
  MODEL_ID,
26
  device=None,
@@ -28,7 +29,7 @@ def rerank(query: str, documents: str, instruction: str):
28
  batch_size=4,
29
  query_max_length=512,
30
  max_length=1024,
31
- chunk_size=4,
32
  )
33
 
34
  query = query.strip()
@@ -56,7 +57,7 @@ def rerank(query: str, documents: str, instruction: str):
56
 
57
  summary = (
58
  f"Reranked {len(docs)} documents with "
59
- f"`{MODEL_ID}`. Higher score means more relevant."
60
  )
61
  return table, summary
62
  except Exception as error:
@@ -65,11 +66,11 @@ def rerank(query: str, documents: str, instruction: str):
65
 
66
  with gr.Blocks(title="KaLM-Reranker-V1 Demo") as demo:
67
  gr.Markdown(
68
- """
69
  # KaLM-Reranker-V1 Demo
70
  **KaLM-Reranker-V1** is a fast but not late-interaction reranker for compressed document reranking.
71
  Input a query and several candidate documents. The demo returns relevance scores and reranked results.
72
- **Document format:** separate candidate documents with one blank line.
73
  """
74
  )
75
 
@@ -85,12 +86,21 @@ Input a query and several candidate documents. The demo returns relevance scores
85
  value=DEFAULT_INSTRUCTION,
86
  lines=2,
87
  )
 
 
 
 
 
 
88
  documents = gr.Textbox(
89
  label="Candidate Documents",
90
  value=(
91
- "The capital of China is Beijing.\n\n"
92
- "Gravity attracts bodies toward one another.\n\n"
93
- "Shanghai is a major city in China.\n\n"
 
 
 
94
  "Paris is the capital of France."
95
  ),
96
  lines=14,
@@ -107,7 +117,7 @@ Input a query and several candidate documents. The demo returns relevance scores
107
 
108
  submit.click(
109
  fn=rerank,
110
- inputs=[query, documents, instruction],
111
  outputs=[output_table, output_summary],
112
  )
113
 
@@ -116,32 +126,41 @@ Input a query and several candidate documents. The demo returns relevance scores
116
  [
117
  "What is the capital of China?",
118
  (
119
- "The capital of China is Beijing.\n\n"
120
- "Gravity attracts bodies toward one another.\n\n"
 
 
121
  "Paris is the capital of France."
122
  ),
123
  DEFAULT_INSTRUCTION,
 
124
  ],
125
  [
126
  "Which model is suitable for efficient reranking?",
127
  (
128
- "KaLM-Reranker-V1-Nano is designed for efficient reranking.\n\n"
129
- "Large language models are often expensive for reranking.\n\n"
 
 
130
  "Image classifiers are used for visual recognition."
131
  ),
132
  DEFAULT_INSTRUCTION,
 
133
  ],
134
  [
135
  "What is KaLM-Reranker-V1 designed for?",
136
  (
137
- "KaLM-Reranker-V1 is a reranker for compressed document reranking.\n\n"
138
- "KaLM-Embedding is a general-purpose embedding model.\n\n"
 
 
139
  "Weather forecasting predicts future weather conditions."
140
  ),
141
  DEFAULT_INSTRUCTION,
 
142
  ],
143
  ],
144
- inputs=[query, documents, instruction],
145
  )
146
 
147
  gr.Markdown(
 
10
  DEFAULT_INSTRUCTION = "Given a query, retrieve documents that answer the query."
11
  MAX_DOCS = 20
12
  MAX_DOC_CHARS = 4000
13
+ DOC_SEPARATOR = "---"
14
 
15
 
16
  def parse_documents(text: str) -> List[str]:
17
+ docs = [doc.strip() for doc in text.split(DOC_SEPARATOR) if doc.strip()]
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,
 
29
  batch_size=4,
30
  query_max_length=512,
31
  max_length=1024,
32
+ chunk_size=chunk_size,
33
  )
34
 
35
  query = query.strip()
 
57
 
58
  summary = (
59
  f"Reranked {len(docs)} documents with "
60
+ f"`{MODEL_ID}` (chunk_size={chunk_size}). Higher score means more relevant."
61
  )
62
  return table, summary
63
  except Exception as error:
 
66
 
67
  with gr.Blocks(title="KaLM-Reranker-V1 Demo") as demo:
68
  gr.Markdown(
69
+ f"""
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
 
 
86
  value=DEFAULT_INSTRUCTION,
87
  lines=2,
88
  )
89
+ chunk_size = gr.Dropdown(
90
+ label="Chunk Size",
91
+ choices=[2, 4, 8, 16],
92
+ value=4,
93
+ interactive=True,
94
+ )
95
  documents = gr.Textbox(
96
  label="Candidate Documents",
97
  value=(
98
+ "The capital of China is Beijing.\n"
99
+ "---\n"
100
+ "Gravity attracts bodies toward one another.\n"
101
+ "---\n"
102
+ "Shanghai is a major city in China.\n"
103
+ "---\n"
104
  "Paris is the capital of France."
105
  ),
106
  lines=14,
 
117
 
118
  submit.click(
119
  fn=rerank,
120
+ inputs=[query, documents, instruction, chunk_size],
121
  outputs=[output_table, output_summary],
122
  )
123
 
 
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(