pdmdp commited on
Commit
de1f347
·
verified ·
1 Parent(s): d30532e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -115
app.py CHANGED
@@ -17,15 +17,10 @@ from finalign.neural import NeuralSearch
17
  from finalign.preparation import finish_index, prepare_batch
18
  from finalign.presentation import (
19
  CSS,
 
20
  details_html,
21
  esc,
22
- method_header,
23
- pipeline_count,
24
- rank_map,
25
- results_html,
26
- score_name,
27
- state_html,
28
- status_html,
29
  summary_html,
30
  )
31
 
@@ -80,25 +75,31 @@ def refresh_page():
80
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=True))
81
 
82
 
83
- def pipeline_label(keywords, hyde, lexical, rerank):
84
- options = Options(keywords, hyde, lexical, rerank).to_dict()
85
- return gr.update(label=f"FinAlign pipeline · {pipeline_count(options)}")
 
 
 
 
 
 
 
 
86
 
87
 
88
  def gpu_failure(error):
89
- """Turn a ZeroGPU or model failure into an inline state; BM25 results stay visible."""
90
  message = str(getattr(error, "message", "") or error).strip()[:300]
91
  lowered = message.lower()
92
  if any(word in lowered for word in ("zerogpu", "quota", "exceeded", "runs limit")):
93
  return ("quota", "GPU quota reached",
94
- "The BM25 ranking is complete. FinAlign needs a ZeroGPU slot, and the "
95
- "free allowance for this visitor is used up. Signed-in Hugging Face users receive "
96
- f"a larger daily allowance; otherwise, try again after it resets.<br><small>{esc(message)}</small>",
97
- "GPU quota reached")
98
  return ("error", "FinAlign could not finish this search",
99
  "The BM25 ranking is complete. Try again in a moment; the Space logs contain details."
100
- + (f"<br><small>{esc(message)}</small>" if message else ""),
101
- "failed")
102
 
103
 
104
  def prepare(query, keywords, hyde, lexical, rerank):
@@ -106,26 +107,22 @@ def prepare(query, keywords, hyde, lexical, rerank):
106
  skip = gr.update()
107
  INDEX.refresh_dense()
108
  if not INDEX.ready:
109
- return (None, skip, skip, skip,
110
- state_html("error", "The search index is not ready yet.",
111
- "The Space owner is still preparing the passage embeddings."), skip, skip)
112
  try:
113
  query = validate_query(query)
114
  except ValueError as error:
115
- return (None, skip, skip, skip, state_html("error", esc(error)), skip, skip)
116
  scores, elapsed = LEXICAL.search(query)
117
  options = Options(keywords, hyde, lexical, rerank)
118
  known = INDEX.known_query(query)
119
  state = {"query": query, "lexical": scores, "lexical_ms": elapsed, "options": options.to_dict(),
120
  "started": started}
121
- lexical_ranking = ranked(scores)
122
- return (state,
123
- results_html(INDEX, lexical_ranking, known=known, method="BM25", scale="BM25"),
124
- state_html("loading", "Waiting for a GPU slot…",
125
- "BM25 is complete. FinAlign runs on a shared ZeroGPU and may wait in a queue."),
126
- method_header("FinAlign", options.to_dict()),
127
- status_html([("BM25", "done", f"{elapsed:,.0f} ms"), ("FinAlign", "waiting", "waiting for GPU")]),
128
- summary_html(query, known, lexical_ranking),
129
  details_html())
130
 
131
 
@@ -136,42 +133,42 @@ def neural_search(state):
136
 
137
  def search(state):
138
  if not state:
139
- return (gr.update(),) * 5
140
- bm25 = ("BM25", "done", f"{state['lexical_ms']:,.0f} ms")
 
 
141
  try:
142
  result = neural_search(state)
143
  except Exception as error: # Shown inline instead of a toast and per-component error badges.
144
  logging.exception("FinAlign search failed")
145
- kind, title, body, label = gpu_failure(error)
146
- return (gr.update(), state_html(kind, title, body), status_html([bm25, ("FinAlign", "error", label)]),
147
- gr.update(), gr.update())
148
- known = INDEX.known_query(state["query"])
149
- lexical, neural = ranked(state["lexical"]), result["ranking"]
150
  timings = {"BM25": state["lexical_ms"], **result["timings_ms"]}
151
  elapsed = sum(timings.values()) / 1000
152
  server_elapsed = time.perf_counter() - state["started"]
153
- finalign_ms = sum(result["timings_ms"].values())
154
- note = (f"{elapsed:.2f} s processing · {server_elapsed:.2f} s on the server, including GPU wait · "
155
- "full FiQA corpus, top 10 shown")
156
- return (results_html(INDEX, lexical, known=known, method="BM25", scale="BM25",
157
- other=rank_map(neural), other_name="FinAlign"),
158
- results_html(INDEX, neural, result["passages"], known, method="FinAlign",
159
- scale=score_name(state["options"]), other=rank_map(lexical), other_name="BM25"),
160
- status_html([bm25, ("FinAlign", "done", f"{finalign_ms:,.0f} ms")], note),
161
- summary_html(state["query"], known, lexical, neural),
162
  details_html(timings, result["variants"], elapsed, server_elapsed))
163
 
164
 
165
  def make_app():
166
  with gr.Blocks(title="FinAlign · Financial document retrieval", analytics_enabled=False) as app:
167
- gr.HTML('<header id="fa-masthead"><div class="fa-title-row"><h1>FinAlign</h1>'
168
- '<nav aria-label="Project links"><a href="https://github.com/pdmdp/FinAlign">Code &amp; report</a>'
 
169
  '<a href="https://huggingface.co/datasets/BeIR/fiqa">Dataset</a></nav></div>'
 
170
  '<p class="fa-lede">Ask a financial question in English and compare two top-10 rankings of '
171
  'FiQA documents: BM25 keyword search and FinAlign, a BGE passage retriever with optional '
172
  'query expansion, score fusion and reranking.</p>'
173
- f'<p class="fa-meta">FiQA / BEIR · {INDEX.manifest["documents"]:,} documents · '
174
- f'{INDEX.manifest["passages"]:,} passages · English</p></header>', padding=False)
 
175
  with gr.Group(visible=not INDEX.ready):
176
  gr.Markdown("### Prepare the passage index")
177
  setup_status = gr.Markdown(
@@ -182,51 +179,51 @@ def make_app():
182
  build_button = gr.Button("Prepare / continue index", variant="primary")
183
  archive_download = gr.File(label="Save the completed index", visible=False, interactive=False)
184
  build_state = gr.State()
185
- with gr.Column(visible=INDEX.ready) as search_panel:
186
- with gr.Column(variant="panel", elem_id="fa-search"):
187
- query = gr.Textbox(label="Question", placeholder="Why does a bond’s price fall when interest rates rise?",
188
- lines=1, max_lines=4, max_length=600, elem_id="fa-query")
189
- with gr.Row(elem_id="fa-actions"):
190
- with gr.Row(elem_id="fa-examples"):
191
- gr.Markdown("FiQA questions:", elem_classes="fa-examples-label")
192
- # Fixed questions from the dev split; selected for readable wording, not scores.
193
- example_ids = {"1952", "1718", "469"}
194
- example_texts = [r["query"] for r in INDEX.queries["dev"] if r["qid"] in example_ids]
195
- example_buttons = [(gr.Button(text, size="sm", variant="secondary"), text)
196
- for text in example_texts]
197
- submit = gr.Button("Compare rankings", variant="primary", elem_id="fa-submit")
198
- with gr.Accordion("FinAlign pipeline · BGE only", open=False, elem_id="fa-pipeline") as pipeline:
199
- gr.Markdown("Each option changes only the FinAlign ranking. Turning all four on reproduces "
200
- "the final pipeline from the course notebook.", elem_classes="fa-pipeline-intro")
201
- with gr.Row(elem_id="fa-options"):
202
- keywords = gr.Checkbox(label="Technical keywords · Qwen", value=False,
203
- info="Adds financial terms, tickers and metrics as an extra query.",
204
- interactive=not ENCODER_ONLY)
205
- hyde = gr.Checkbox(label="Hypothetical expansion · Qwen", value=False,
206
- info="Writes a short hypothetical answer, used as an extra query.",
207
- interactive=not ENCODER_ONLY)
208
- lexical = gr.Checkbox(label="Blend BM25 with BGE", value=False,
209
- info="Adds normalized BM25 and BGE document scores, equal weight.")
210
- rerank = gr.Checkbox(label="Rerank top 50 · monoT5", value=False,
211
- info="Rescores the first 50 candidates with monoT5.",
212
- interactive=not ENCODER_ONLY)
213
- with gr.Row(elem_id="fa-presets"):
214
- bge_only = gr.Button("BGE only", size="sm", variant="secondary")
215
- all_four = gr.Button("All four options", size="sm", variant="secondary")
216
- with gr.Column(elem_id="fa-overview"):
217
- status = gr.HTML('<p class="fa-fine">Enter a question or choose a FiQA question. BM25 appears '
218
- 'first; FinAlign follows when a GPU slot is free.</p>', padding=False)
219
- summary = gr.HTML("", padding=False)
220
- with gr.Row(equal_height=False, elem_id="fa-columns"):
221
- with gr.Column(variant="panel", elem_id="fa-col-bm25", elem_classes="fa-col"):
222
- gr.HTML(method_header("BM25"), padding=False)
223
- baseline = gr.HTML(state_html("idle", "The BM25 top 10 will appear here."), padding=False)
224
- with gr.Column(variant="panel", elem_id="fa-col-finalign", elem_classes="fa-col"):
225
- finalign_header = gr.HTML(method_header("FinAlign", Options().to_dict()), padding=False)
226
- neural = gr.HTML(state_html("idle", "The FinAlign top 10 will appear here."), padding=False)
227
- with gr.Accordion("Run details · latency and query variants", open=False):
228
  details = gr.HTML(details_html(), padding=False)
229
- with gr.Accordion("About the experiment", open=False):
230
  gr.Markdown(
231
  "BGE retrieves up to 400 non-overlapping, 250-word passages per query variant. "
232
  "Variant scores are normalized and combined (original 0.70, keywords 0.15, "
@@ -256,38 +253,25 @@ def make_app():
256
  queue=False, api_visibility="private")
257
 
258
  options = [keywords, hyde, lexical, rerank]
259
- for box in options:
260
- box.change(pipeline_label, options, pipeline, queue=False, api_visibility="private")
261
  bge_only.click(lambda: [False] * 4, None, options, queue=False, api_visibility="private")
262
  full = not ENCODER_ONLY
263
  all_four.click(lambda: [full, full, True, full], None, options, queue=False, api_visibility="private")
264
 
265
  state = gr.State()
266
  inputs = [query, *options]
267
- prep_outputs = [state, baseline, neural, finalign_header, status, summary, details]
268
- search_outputs = [baseline, neural, status, summary, details]
269
-
270
- # The skeleton and status line replace Gradio's overlay while FinAlign waits for a GPU.
271
- shared = {"concurrency_limit": 1, "concurrency_id": "search", "api_visibility": "private"}
272
- gr.on([submit.click, query.submit], prepare, inputs, prep_outputs,
273
- show_progress="minimal", show_progress_on=baseline, **shared).success(
274
- search, state, search_outputs, show_progress="hidden", **shared)
275
  # An example fills the question and runs the comparison in one click.
276
  for button, text in example_buttons:
277
  button.click(lambda text=text: text, None, query, queue=False, api_visibility="private").then(
278
- prepare, inputs, prep_outputs, show_progress="minimal", show_progress_on=baseline, **shared).success(
279
- search, state, search_outputs, show_progress="hidden", **shared)
280
  return app
281
 
282
 
283
  if __name__ == "__main__":
284
- make_app().queue(max_size=12).launch(
285
- theme=gr.themes.Base(primary_hue="blue", neutral_hue="slate",
286
- font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"]).set(
287
- # AA contrast for secondary text and white-on-blue button labels in both themes.
288
- body_text_color_subdued="*neutral_600", body_text_color_subdued_dark="*neutral_400",
289
- button_primary_background_fill="*primary_600", button_primary_background_fill_dark="*primary_600",
290
- button_primary_background_fill_hover="*primary_700",
291
- button_primary_background_fill_hover_dark="*primary_700",
292
- button_primary_text_color="white", button_primary_text_color_dark="white"),
293
- css=CSS, show_error=False, ssr_mode=False)
 
17
  from finalign.preparation import finish_index, prepare_batch
18
  from finalign.presentation import (
19
  CSS,
20
+ board_html,
21
  details_html,
22
  esc,
23
+ notice_html,
 
 
 
 
 
 
24
  summary_html,
25
  )
26
 
 
75
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=True))
76
 
77
 
78
+ def theme():
79
+ """The Space's original palette (slate and blue), with contrast raised to AA in both modes."""
80
+ return gr.themes.Base(primary_hue="blue", neutral_hue="slate").set(
81
+ body_text_color_subdued="*neutral_600", body_text_color_subdued_dark="*neutral_400",
82
+ button_primary_background_fill="*primary_600", button_primary_background_fill_dark="*primary_600",
83
+ button_primary_background_fill_hover="*primary_700", button_primary_background_fill_hover_dark="*primary_700",
84
+ button_primary_text_color="white", button_primary_text_color_dark="white",
85
+ button_secondary_background_fill="transparent", button_secondary_background_fill_dark="transparent",
86
+ button_secondary_background_fill_hover="transparent", button_secondary_background_fill_hover_dark="transparent",
87
+ checkbox_background_color_selected="*primary_600", checkbox_background_color_selected_dark="*primary_500",
88
+ checkbox_border_color_selected="*primary_600", checkbox_border_color_selected_dark="*primary_500")
89
 
90
 
91
  def gpu_failure(error):
92
+ """Turn a ZeroGPU or model failure into an inline notice; BM25 results stay visible."""
93
  message = str(getattr(error, "message", "") or error).strip()[:300]
94
  lowered = message.lower()
95
  if any(word in lowered for word in ("zerogpu", "quota", "exceeded", "runs limit")):
96
  return ("quota", "GPU quota reached",
97
+ "The BM25 ranking is complete. FinAlign needs a ZeroGPU slot, and the free allowance "
98
+ "for this visitor is used up. Signed-in Hugging Face users receive a larger daily "
99
+ f"allowance; otherwise, try again after it resets.<small>{esc(message)}</small>")
 
100
  return ("error", "FinAlign could not finish this search",
101
  "The BM25 ranking is complete. Try again in a moment; the Space logs contain details."
102
+ + (f"<small>{esc(message)}</small>" if message else ""))
 
103
 
104
 
105
  def prepare(query, keywords, hyde, lexical, rerank):
 
107
  skip = gr.update()
108
  INDEX.refresh_dense()
109
  if not INDEX.ready:
110
+ return (None, notice_html("error", "The search index is not ready yet.",
111
+ "The Space owner is still preparing the passage embeddings."), skip, skip, skip)
 
112
  try:
113
  query = validate_query(query)
114
  except ValueError as error:
115
+ return (None, notice_html("error", esc(error)), skip, skip, skip)
116
  scores, elapsed = LEXICAL.search(query)
117
  options = Options(keywords, hyde, lexical, rerank)
118
  known = INDEX.known_query(query)
119
  state = {"query": query, "lexical": scores, "lexical_ms": elapsed, "options": options.to_dict(),
120
  "started": started}
121
+ ranking = ranked(scores)
122
+ return (state, "",
123
+ summary_html(query, known, ranking, state="waiting", lexical_ms=elapsed),
124
+ board_html(INDEX, query, ranking, known=known, options=options.to_dict(), state="waiting",
125
+ lexical_ms=elapsed),
 
 
 
126
  details_html())
127
 
128
 
 
133
 
134
  def search(state):
135
  if not state:
136
+ return (gr.update(),) * 3
137
+ query, options = state["query"], state["options"]
138
+ known = INDEX.known_query(query)
139
+ lexical = ranked(state["lexical"])
140
  try:
141
  result = neural_search(state)
142
  except Exception as error: # Shown inline instead of a toast and per-component error badges.
143
  logging.exception("FinAlign search failed")
144
+ return (summary_html(query, known, lexical, state="failed", lexical_ms=state["lexical_ms"]),
145
+ board_html(INDEX, query, lexical, known=known, options=options, state="failed",
146
+ lexical_ms=state["lexical_ms"], notice=gpu_failure(error)),
147
+ gr.update())
148
+ neural = result["ranking"]
149
  timings = {"BM25": state["lexical_ms"], **result["timings_ms"]}
150
  elapsed = sum(timings.values()) / 1000
151
  server_elapsed = time.perf_counter() - state["started"]
152
+ return (summary_html(query, known, lexical, neural, state="done", lexical_ms=state["lexical_ms"],
153
+ timings=result["timings_ms"], server_s=server_elapsed),
154
+ board_html(INDEX, query, lexical, neural, result["passages"], known, options, state="done",
155
+ lexical_ms=state["lexical_ms"], neural_ms=sum(result["timings_ms"].values())),
 
 
 
 
 
156
  details_html(timings, result["variants"], elapsed, server_elapsed))
157
 
158
 
159
  def make_app():
160
  with gr.Blocks(title="FinAlign · Financial document retrieval", analytics_enabled=False) as app:
161
+ gr.HTML('<header id="fa-masthead" class="fa"><div class="fa-top"><span class="fa-kicker">Financial document '
162
+ 'retrieval</span><nav aria-label="Project links">'
163
+ '<a href="https://github.com/pdmdp/FinAlign">Code &amp; report</a>'
164
  '<a href="https://huggingface.co/datasets/BeIR/fiqa">Dataset</a></nav></div>'
165
+ '<h1>FinAlign</h1>'
166
  '<p class="fa-lede">Ask a financial question in English and compare two top-10 rankings of '
167
  'FiQA documents: BM25 keyword search and FinAlign, a BGE passage retriever with optional '
168
  'query expansion, score fusion and reranking.</p>'
169
+ f'<ul class="fa-meta"><li><b>{INDEX.manifest["documents"]:,}</b> documents</li>'
170
+ f'<li><b>{INDEX.manifest["passages"]:,}</b> passages</li><li>FiQA / BEIR</li>'
171
+ '<li>English</li></ul></header>', padding=False)
172
  with gr.Group(visible=not INDEX.ready):
173
  gr.Markdown("### Prepare the passage index")
174
  setup_status = gr.Markdown(
 
179
  build_button = gr.Button("Prepare / continue index", variant="primary")
180
  archive_download = gr.File(label="Save the completed index", visible=False, interactive=False)
181
  build_state = gr.State()
182
+ with gr.Column(visible=INDEX.ready, elem_id="fa-app") as search_panel:
183
+ with gr.Row(elem_id="fa-searchbar"):
184
+ query = gr.Textbox(label="Question", show_label=False, container=False,
185
+ placeholder="Why does a bond’s price fall when interest rates rise?",
186
+ lines=1, max_lines=4, max_length=600, elem_id="fa-query", scale=1)
187
+ submit = gr.Button("Compare rankings", variant="primary", elem_id="fa-submit", scale=0,
188
+ min_width=176)
189
+ with gr.Row(elem_id="fa-examples"):
190
+ gr.HTML('<div class="fa"><span class="fa-label">Try a FiQA question</span></div>', padding=False,
191
+ elem_classes="fa-inline")
192
+ # Fixed questions from the dev split; selected for readable wording, not scores.
193
+ example_ids = {"1952", "1718", "469"}
194
+ example_texts = [r["query"] for r in INDEX.queries["dev"] if r["qid"] in example_ids]
195
+ example_buttons = [(gr.Button(text, size="sm", variant="secondary"), text)
196
+ for text in example_texts]
197
+ notice = gr.HTML("", padding=False, elem_id="fa-notice-slot")
198
+ with gr.Column(elem_id="fa-pipe"):
199
+ with gr.Row(elem_id="fa-pipe-head"):
200
+ gr.HTML('<div class="fa"><span class="fa-label">FinAlign pipeline</span><span class="fa-pipe-hint">'
201
+ 'Switch stages on to see what each one changes. BM25 is not affected.</span></div>',
202
+ padding=False, elem_classes="fa-inline")
203
+ bge_only = gr.Button("BGE only", size="sm", variant="secondary", scale=0, min_width=0)
204
+ all_four = gr.Button("All stages", size="sm", variant="secondary", scale=0, min_width=0)
205
+ with gr.Row(elem_id="fa-stages"):
206
+ keywords = gr.Checkbox(label="Keyword expansion", value=False,
207
+ info="Qwen adds financial terms as an extra query.",
208
+ interactive=not ENCODER_ONLY, elem_classes="fa-stage")
209
+ hyde = gr.Checkbox(label="Hypothetical answer", value=False,
210
+ info="Qwen drafts an answer, searched as an extra query.",
211
+ interactive=not ENCODER_ONLY, elem_classes="fa-stage")
212
+ gr.HTML('<div class="fa fa-stage-fixed"><span class="fa-stage-title">BGE passage search'
213
+ '</span><span class="fa-stage-info">Ranks each document by its '
214
+ 'best 250-word passage.</span></div>', padding=False, elem_classes="fa-stage-core")
215
+ lexical = gr.Checkbox(label="BM25 fusion", value=False,
216
+ info="Adds normalized BM25 and BGE scores, equal weight.",
217
+ elem_classes="fa-stage")
218
+ rerank = gr.Checkbox(label="monoT5 rerank", value=False,
219
+ info="Rescores the first 50 candidates.",
220
+ interactive=not ENCODER_ONLY, elem_classes="fa-stage")
221
+ summary = gr.HTML("", padding=False, elem_id="fa-summary-slot")
222
+ board = gr.HTML(board_html(INDEX), padding=False, elem_id="fa-board-slot")
223
+ with gr.Accordion("Run details · latency by stage and query variants", open=False,
224
+ elem_classes="fa-accordion"):
225
  details = gr.HTML(details_html(), padding=False)
226
+ with gr.Accordion("About the experiment", open=False, elem_classes="fa-accordion"):
227
  gr.Markdown(
228
  "BGE retrieves up to 400 non-overlapping, 250-word passages per query variant. "
229
  "Variant scores are normalized and combined (original 0.70, keywords 0.15, "
 
253
  queue=False, api_visibility="private")
254
 
255
  options = [keywords, hyde, lexical, rerank]
 
 
256
  bge_only.click(lambda: [False] * 4, None, options, queue=False, api_visibility="private")
257
  full = not ENCODER_ONLY
258
  all_four.click(lambda: [full, full, True, full], None, options, queue=False, api_visibility="private")
259
 
260
  state = gr.State()
261
  inputs = [query, *options]
262
+ prep_outputs = [state, notice, summary, board, details]
263
+ search_outputs = [summary, board, details]
264
+ # The board's own skeleton and status replace Gradio's overlay while FinAlign waits for a GPU.
265
+ shared = {"concurrency_limit": 1, "concurrency_id": "search", "api_visibility": "private",
266
+ "show_progress": "hidden"}
267
+ gr.on([submit.click, query.submit], prepare, inputs, prep_outputs, **shared).success(
268
+ search, state, search_outputs, **shared)
 
269
  # An example fills the question and runs the comparison in one click.
270
  for button, text in example_buttons:
271
  button.click(lambda text=text: text, None, query, queue=False, api_visibility="private").then(
272
+ prepare, inputs, prep_outputs, **shared).success(search, state, search_outputs, **shared)
 
273
  return app
274
 
275
 
276
  if __name__ == "__main__":
277
+ make_app().queue(max_size=12).launch(theme=theme(), css=CSS, show_error=False, ssr_mode=False)