NathanRoll commited on
Commit
9fd8a8f
·
verified ·
1 Parent(s): 8808f60

Speed initial records load and hide Gradio chrome

Browse files
Files changed (1) hide show
  1. app.py +82 -14
app.py CHANGED
@@ -33,6 +33,31 @@ HYDRATE_STATUS = maybe_hydrate_from_dataset(DATA_DIR)
33
  STORE = SolutionStore(DATA_DIR)
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  CSS = """
37
  :root {
38
  color-scheme: light;
@@ -188,6 +213,24 @@ gradio-app,
188
  color: #fff !important;
189
  }
190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  .section {
192
  padding: 30px 0 8px;
193
  }
@@ -1250,14 +1293,14 @@ def esc(value: Any) -> str:
1250
 
1251
 
1252
  def setup_title(setup: str) -> str:
1253
- for record in STORE.load_reference_records():
1254
  if record.get("setup") == setup and record.get("title"):
1255
  return str(record["title"])
1256
  return setup
1257
 
1258
 
1259
  def setup_updated(setup: str) -> str:
1260
- for record in STORE.load_reference_records():
1261
  if record.get("setup") != setup:
1262
  continue
1263
  meta = record.get("friedman_reference")
@@ -1268,8 +1311,18 @@ def setup_updated(setup: str) -> str:
1268
  return ""
1269
 
1270
 
 
1271
  def setup_choices() -> list[str]:
1272
- choices = [setup for setup in STORE.setups() if setup != "All"]
 
 
 
 
 
 
 
 
 
1273
  return sorted(choices, key=lambda setup: (setup_title(setup).lower(), setup.lower()))
1274
 
1275
 
@@ -1363,7 +1416,7 @@ def author_credit_html(author: str) -> str:
1363
 
1364
  def existing_author_choices() -> list[str]:
1365
  seen: dict[str, str] = {}
1366
- for record in [*STORE.load_reference_records(), *STORE.load_records()]:
1367
  name = display_author(record)
1368
  normalized = name.strip()
1369
  if not normalized or normalized.lower() in {"unknown", "anonymous"}:
@@ -1473,9 +1526,10 @@ def source_link(record: dict[str, Any]) -> str:
1473
  return f'<a href="{esc(url)}" target="_blank" rel="noopener noreferrer">page</a>'
1474
 
1475
 
 
1476
  def coordinate_records_by_case() -> dict[str, dict[str, Any]]:
1477
  best: dict[str, dict[str, Any]] = {}
1478
- for record in STORE.load_records():
1479
  if not record.get("verified") or not record.get("solution_path"):
1480
  continue
1481
  case = str(record.get("case") or "")
@@ -1690,10 +1744,10 @@ def record_card(record: dict[str, Any], coordinates: dict[str, dict[str, Any]])
1690
 
1691
 
1692
  def setup_summary(setup: str) -> dict[str, Any]:
1693
- records = STORE.public_records(setup, show_all=False)
1694
  coordinate_count = sum(
1695
  1
1696
- for record in STORE.load_records()
1697
  if record.get("verified") and record.get("solution_path") and record.get("setup") == setup
1698
  )
1699
  n_values = [int(r.get("n", 0)) for r in records if r.get("n") is not None]
@@ -1942,7 +1996,7 @@ def browse_page_for_request(request: gr.Request):
1942
  setup = str(query.get("family") or "").strip()
1943
  if setup in setup_choices():
1944
  return family_page_html(setup)
1945
- return directory_html()
1946
 
1947
 
1948
  def condensed_case_links(cases: list[str]) -> str:
@@ -1970,7 +2024,7 @@ def condensed_case_links(cases: list[str]) -> str:
1970
  def leaderboard_html() -> str:
1971
  rows: dict[str, dict[str, Any]] = {}
1972
  coordinates = coordinate_records_by_case()
1973
- for record in STORE.public_records(show_all=False):
1974
  author = display_author(record)
1975
  if author.strip().lower() == "trivial":
1976
  continue
@@ -2095,6 +2149,14 @@ def supported_gradio_kwargs(component: Any, **kwargs: Any) -> dict[str, Any]:
2095
  return {key: value for key, value in kwargs.items() if key in parameters}
2096
 
2097
 
 
 
 
 
 
 
 
 
2098
  def preview_html(solution: dict[str, Any]) -> str:
2099
  return f"""
2100
  <div class="preview-card">
@@ -2135,8 +2197,7 @@ def submit_solution(
2135
  pretty = json.dumps(stored, indent=2, sort_keys=True)
2136
  sync_status = record.get("sync_status", "dataset sync disabled")
2137
  message = result_markdown(result) + f"\n\nSaved as record `{record['id']}`.\n\nDataset sync: `{sync_status}`."
2138
- family_page_html.cache_clear()
2139
- directory_html.cache_clear()
2140
  return (
2141
  message,
2142
  preview_html(stored),
@@ -2158,10 +2219,10 @@ def prewarm_pages() -> None:
2158
  family_page_html(setup)
2159
 
2160
 
2161
- with gr.Blocks(title="Packing Benchmark") as demo:
2162
  with gr.Tabs():
2163
  with gr.Tab("Home"):
2164
- browse_page = gr.HTML(loading_html())
2165
 
2166
  with gr.Tab("Leaderboard"):
2167
  leaderboard_page = gr.HTML(leaderboard_html())
@@ -2245,4 +2306,11 @@ with gr.Blocks(title="Packing Benchmark") as demo:
2245
 
2246
 
2247
  if __name__ == "__main__":
2248
- demo.launch(theme=THEME, css=CSS, js=APP_JS, server_name="0.0.0.0", ssr_mode=False)
 
 
 
 
 
 
 
 
33
  STORE = SolutionStore(DATA_DIR)
34
 
35
 
36
+ @lru_cache(maxsize=1)
37
+ def cached_reference_records() -> list[dict[str, Any]]:
38
+ return STORE.load_reference_records()
39
+
40
+
41
+ @lru_cache(maxsize=1)
42
+ def cached_verified_records() -> list[dict[str, Any]]:
43
+ return STORE.load_records()
44
+
45
+
46
+ @lru_cache(maxsize=64)
47
+ def cached_public_records(setup: str = "", show_all: bool = False) -> list[dict[str, Any]]:
48
+ return STORE.public_records(setup or None, show_all=show_all)
49
+
50
+
51
+ def clear_page_caches() -> None:
52
+ cached_reference_records.cache_clear()
53
+ cached_verified_records.cache_clear()
54
+ cached_public_records.cache_clear()
55
+ setup_choices.cache_clear()
56
+ coordinate_records_by_case.cache_clear()
57
+ family_page_html.cache_clear()
58
+ directory_html.cache_clear()
59
+
60
+
61
  CSS = """
62
  :root {
63
  color-scheme: light;
 
213
  color: #fff !important;
214
  }
215
 
216
+ footer,
217
+ .gradio-container footer,
218
+ .gradio-container .footer,
219
+ .gradio-container .built-with,
220
+ .gradio-container .api-docs,
221
+ .gradio-container a[href*="view=api"],
222
+ .gradio-container a[href*="/api"],
223
+ .gradio-container button[aria-label="Settings"],
224
+ .gradio-container button[title="Settings"],
225
+ .gradio-container button[aria-label="More"],
226
+ .gradio-container button[aria-label="More options"],
227
+ .gradio-container button[title="More"],
228
+ .gradio-container button[title="More options"],
229
+ .gradio-container [data-testid="settings-button"],
230
+ .gradio-container [data-testid="more-button"] {
231
+ display: none !important;
232
+ }
233
+
234
  .section {
235
  padding: 30px 0 8px;
236
  }
 
1293
 
1294
 
1295
  def setup_title(setup: str) -> str:
1296
+ for record in cached_reference_records():
1297
  if record.get("setup") == setup and record.get("title"):
1298
  return str(record["title"])
1299
  return setup
1300
 
1301
 
1302
  def setup_updated(setup: str) -> str:
1303
+ for record in cached_reference_records():
1304
  if record.get("setup") != setup:
1305
  continue
1306
  meta = record.get("friedman_reference")
 
1311
  return ""
1312
 
1313
 
1314
+ @lru_cache(maxsize=96)
1315
  def setup_choices() -> list[str]:
1316
+ choices = {
1317
+ str(record["setup"])
1318
+ for record in cached_reference_records()
1319
+ if record.get("setup")
1320
+ }
1321
+ choices.update(
1322
+ str(record["setup"])
1323
+ for record in cached_verified_records()
1324
+ if record.get("verified") and record.get("setup")
1325
+ )
1326
  return sorted(choices, key=lambda setup: (setup_title(setup).lower(), setup.lower()))
1327
 
1328
 
 
1416
 
1417
  def existing_author_choices() -> list[str]:
1418
  seen: dict[str, str] = {}
1419
+ for record in [*cached_reference_records(), *cached_verified_records()]:
1420
  name = display_author(record)
1421
  normalized = name.strip()
1422
  if not normalized or normalized.lower() in {"unknown", "anonymous"}:
 
1526
  return f'<a href="{esc(url)}" target="_blank" rel="noopener noreferrer">page</a>'
1527
 
1528
 
1529
+ @lru_cache(maxsize=1)
1530
  def coordinate_records_by_case() -> dict[str, dict[str, Any]]:
1531
  best: dict[str, dict[str, Any]] = {}
1532
+ for record in cached_verified_records():
1533
  if not record.get("verified") or not record.get("solution_path"):
1534
  continue
1535
  case = str(record.get("case") or "")
 
1744
 
1745
 
1746
  def setup_summary(setup: str) -> dict[str, Any]:
1747
+ records = cached_public_records(setup, False)
1748
  coordinate_count = sum(
1749
  1
1750
+ for record in cached_verified_records()
1751
  if record.get("verified") and record.get("solution_path") and record.get("setup") == setup
1752
  )
1753
  n_values = [int(r.get("n", 0)) for r in records if r.get("n") is not None]
 
1996
  setup = str(query.get("family") or "").strip()
1997
  if setup in setup_choices():
1998
  return family_page_html(setup)
1999
+ return gr.update()
2000
 
2001
 
2002
  def condensed_case_links(cases: list[str]) -> str:
 
2024
  def leaderboard_html() -> str:
2025
  rows: dict[str, dict[str, Any]] = {}
2026
  coordinates = coordinate_records_by_case()
2027
+ for record in cached_public_records("", False):
2028
  author = display_author(record)
2029
  if author.strip().lower() == "trivial":
2030
  continue
 
2149
  return {key: value for key, value in kwargs.items() if key in parameters}
2150
 
2151
 
2152
+ def supported_call_kwargs(callable_obj: Any, **kwargs: Any) -> dict[str, Any]:
2153
+ try:
2154
+ parameters = inspect.signature(callable_obj).parameters
2155
+ except (TypeError, ValueError):
2156
+ return {}
2157
+ return {key: value for key, value in kwargs.items() if key in parameters}
2158
+
2159
+
2160
  def preview_html(solution: dict[str, Any]) -> str:
2161
  return f"""
2162
  <div class="preview-card">
 
2197
  pretty = json.dumps(stored, indent=2, sort_keys=True)
2198
  sync_status = record.get("sync_status", "dataset sync disabled")
2199
  message = result_markdown(result) + f"\n\nSaved as record `{record['id']}`.\n\nDataset sync: `{sync_status}`."
2200
+ clear_page_caches()
 
2201
  return (
2202
  message,
2203
  preview_html(stored),
 
2219
  family_page_html(setup)
2220
 
2221
 
2222
+ with gr.Blocks(**supported_gradio_kwargs(gr.Blocks, title="Packing Benchmark", analytics_enabled=False)) as demo:
2223
  with gr.Tabs():
2224
  with gr.Tab("Home"):
2225
+ browse_page = gr.HTML(directory_html())
2226
 
2227
  with gr.Tab("Leaderboard"):
2228
  leaderboard_page = gr.HTML(leaderboard_html())
 
2306
 
2307
 
2308
  if __name__ == "__main__":
2309
+ demo.launch(
2310
+ theme=THEME,
2311
+ css=CSS,
2312
+ js=APP_JS,
2313
+ server_name="0.0.0.0",
2314
+ ssr_mode=False,
2315
+ **supported_call_kwargs(demo.launch, show_api=False),
2316
+ )