Spaces:
Sleeping
Sleeping
File size: 4,805 Bytes
713507e a4b35cf 713507e a4b35cf 713507e fda2118 713507e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | from __future__ import annotations
import json
import re
from datetime import datetime, timezone
from pathlib import Path
import gradio as gr
import pandas as pd
from leaderboarder.config import Settings
from leaderboarder.deploy import deploy_to_hf_space, generate_space_artifacts
from leaderboarder.pipeline import LeaderboardPipeline
WORKDIR = Path("/tmp/leaderboarder-builder-runs")
WORKDIR.mkdir(parents=True, exist_ok=True)
def slugify(text: str) -> str:
value = re.sub(r"[^a-zA-Z0-9]+", "-", (text or "").strip().lower()).strip("-")
return value or "benchmark"
def resolve_owner(hf_token: str) -> str:
from huggingface_hub import HfApi
whoami = HfApi(token=hf_token).whoami()
return whoami.get("name") or ""
def run_build_and_deploy(
benchmark_input: str,
explicit_space_id: str,
max_citations: int,
read_citations: int,
private_space: bool,
):
benchmark_input = (benchmark_input or "").strip()
if not benchmark_input:
return "Missing benchmark input.", "", pd.DataFrame()
settings = Settings.from_env()
if not settings.openrouter_api_key:
return (
"Missing OPENROUTER_API_KEY secret in this builder space.",
"",
pd.DataFrame(),
)
if not settings.hf_token:
return "Missing HF_TOKEN secret in this builder space.", "", pd.DataFrame()
pipeline = LeaderboardPipeline(settings=settings)
run_id = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S")
base_slug = slugify(benchmark_input)
output_dir = WORKDIR / f"{base_slug}-{run_id}"
try:
summary = pipeline.run(
input_value=benchmark_input,
output_dir=str(output_dir),
max_citations=int(max_citations),
read_citations=int(read_citations),
)
except Exception as exc:
return f"Build failed: {exc}", "", pd.DataFrame()
owner = resolve_owner(settings.hf_token)
if not owner:
return "Could not resolve HF account owner.", "", pd.DataFrame()
if explicit_space_id.strip():
target_space = explicit_space_id.strip()
else:
target_space = f"{owner}/leaderboarder-{slugify(summary.benchmark_name)}"
generate_space_artifacts(Path(summary.output_dir), summary.benchmark_name)
try:
deployed_url = deploy_to_hf_space(
output_dir=Path(summary.output_dir),
space_id=target_space,
hf_token=settings.hf_token,
private=bool(private_space),
)
except Exception as exc:
return f"Deployment failed: {exc}", json.dumps(summary.to_dict(), indent=2, ensure_ascii=False), pd.DataFrame()
csv_path = Path(summary.output_dir) / "leaderboard.csv"
preview = pd.read_csv(csv_path).head(25) if csv_path.exists() else pd.DataFrame()
status = (
f"Built benchmark: {summary.benchmark_name}\n"
f"Rows: {summary.final_rows}\n"
f"Space: {target_space}\n"
f"URL: {deployed_url}"
)
return status, json.dumps(summary.to_dict(), indent=2, ensure_ascii=False), preview
with gr.Blocks(title="Leaderboarder Builder") as demo:
gr.Markdown("# Leaderboarder Builder")
gr.Markdown(
"Input a benchmark URL or benchmark name/query. "
"The app extracts leaderboard rows, expands with related works, and deploys a new Hugging Face Space."
)
gr.Markdown(
"Required HF Space secrets: `OPENROUTER_API_KEY`, `HF_TOKEN`."
)
benchmark_input = gr.Textbox(
label="Benchmark input",
placeholder="https://arxiv.org/abs/... or benchmark name",
)
explicit_space_id = gr.Textbox(
label="Target Space ID (optional)",
placeholder="username/space-name",
)
with gr.Row():
max_citations = gr.Slider(
label="Max citation candidates",
minimum=10,
maximum=500,
value=150,
step=10,
)
read_citations = gr.Slider(
label="Citations to read deeply",
minimum=5,
maximum=100,
value=30,
step=1,
)
private_space = gr.Checkbox(label="Create private Space", value=False)
run_btn = gr.Button("Create and Deploy Leaderboard")
status_out = gr.Textbox(label="Status", lines=6)
summary_out = gr.Code(label="Pipeline summary", language="json")
preview_out = gr.Dataframe(label="Leaderboard preview", wrap=False, row_count=(20, "fixed"))
run_btn.click(
fn=run_build_and_deploy,
inputs=[
benchmark_input,
explicit_space_id,
max_citations,
read_citations,
private_space,
],
outputs=[status_out, summary_out, preview_out],
)
demo.queue(max_size=8).launch()
|