Wauplin HF staff commited on
Commit
52608b2
1 Parent(s): 3cc4e3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -9
app.py CHANGED
@@ -3,9 +3,8 @@ import logging
3
  import time
4
  import gradio as gr
5
  import datasets
6
- from huggingface_hub import snapshot_download, WebhooksServer, WebhookPayload
7
  from gradio_leaderboard import Leaderboard, ColumnFilter, SelectColumns
8
- from gradio_space_ci import enable_space_ci
9
 
10
  from src.display.about import (
11
  CITATION_BUTTON_LABEL,
@@ -44,8 +43,7 @@ from src.tools.plots import create_metric_plot_obj, create_plot_df, create_score
44
  # Configure logging
45
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
46
 
47
- # Start ephemeral Spaces on PRs (see config in README.md)
48
- enable_space_ci()
49
 
50
 
51
  def restart_space():
@@ -292,10 +290,41 @@ with demo:
292
 
293
  demo.queue(default_concurrency_limit=40)
294
 
295
- # Add webhooks
296
- app = WebhooksServer(ui=demo)
 
 
 
 
 
 
 
 
 
297
 
298
- @app.add_webhook
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  async def update_leaderboard(payload: WebhookPayload) -> None:
300
  if payload.repo.type == "dataset" and payload.event.action == "update":
301
  leaderboard_dataset = datasets.load_dataset(AGGREGATED_REPO, "default", split="train", cache_dir=HF_HOME)
@@ -306,7 +335,7 @@ async def update_leaderboard(payload: WebhookPayload) -> None:
306
  )
307
  leaderboard.value = leaderboard_df
308
 
309
- @app.add_webhook
310
  async def update_queue(payload: WebhookPayload) -> None:
311
  if payload.repo.type == "dataset" and payload.event.action == "update":
312
  download_dataset(QUEUE_REPO, EVAL_REQUESTS_PATH)
@@ -317,5 +346,4 @@ async def update_queue(payload: WebhookPayload) -> None:
317
  running_eval_table.value = running_eval_queue_df
318
  pending_eval_table.value = pending_eval_queue_df
319
 
320
-
321
  app.launch()
 
3
  import time
4
  import gradio as gr
5
  import datasets
6
+ from huggingface_hub import snapshot_download, WebhooksServer, WebhookPayload, RepoCard
7
  from gradio_leaderboard import Leaderboard, ColumnFilter, SelectColumns
 
8
 
9
  from src.display.about import (
10
  CITATION_BUTTON_LABEL,
 
43
  # Configure logging
44
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
45
 
46
+
 
47
 
48
 
49
  def restart_space():
 
290
 
291
  demo.queue(default_concurrency_limit=40)
292
 
293
+ # Start ephemeral Spaces on PRs (see config in README.md)
294
+ from gradio_space_ci.webhook import IS_EPHEMERAL_SPACE, SPACE_ID, configure_space_ci
295
+
296
+
297
+
298
+ def enable_space_ci_and_return_server(ui: gr.Blocks) -> WebhooksServer:
299
+ # Taken from https://huggingface.co/spaces/Wauplin/gradio-space-ci/blob/075119aee75ab5e7150bf0814eec91c83482e790/src/gradio_space_ci/webhook.py#L61
300
+ # Compared to original, this one do not monkeypatch Gradio which allows us to define more webhooks.
301
+ if SPACE_ID is None:
302
+ print("Not in a Space: Space CI disabled.")
303
+ return WebhooksServer(ui=demo)
304
 
305
+ if IS_EPHEMERAL_SPACE:
306
+ print("In an ephemeral Space: Space CI disabled.")
307
+ return WebhooksServer(ui=demo)
308
+
309
+ card = RepoCard.load(repo_id_or_path=SPACE_ID, repo_type="space")
310
+ config = card.data.get("space_ci", {})
311
+ print(f"Enabling Space CI with config from README: {config}")
312
+
313
+ return configure_space_ci(
314
+ blocks=self,
315
+ trusted_authors=config.get("trusted_authors"),
316
+ private=config.get("private", "auto"),
317
+ variables=config.get("variables", "auto"),
318
+ secrets=config.get("secrets"),
319
+ hardware=config.get("hardware"),
320
+ storage=config.get("storage"),
321
+ )
322
+
323
+ # Create webhooks server (with CI url if in Space and not ephemeral)
324
+ webhooks_server = enable_space_ci_and_return_server(ui=demo)
325
+
326
+ # Add webhooks
327
+ @webhooks_server.add_webhook
328
  async def update_leaderboard(payload: WebhookPayload) -> None:
329
  if payload.repo.type == "dataset" and payload.event.action == "update":
330
  leaderboard_dataset = datasets.load_dataset(AGGREGATED_REPO, "default", split="train", cache_dir=HF_HOME)
 
335
  )
336
  leaderboard.value = leaderboard_df
337
 
338
+ @webhooks_server.add_webhook
339
  async def update_queue(payload: WebhookPayload) -> None:
340
  if payload.repo.type == "dataset" and payload.event.action == "update":
341
  download_dataset(QUEUE_REPO, EVAL_REQUESTS_PATH)
 
346
  running_eval_table.value = running_eval_queue_df
347
  pending_eval_table.value = pending_eval_queue_df
348
 
 
349
  app.launch()