File size: 16,707 Bytes
839b969
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""
RunLocalAI catalog + leaderboard β€” HuggingFace Space.

Two live, read-only surfaces over the RunLocalAI corpus:

  1. πŸ† Benchmark leaderboard β€” reproducible quality scores
     (HumanEval+, MBPP+, TurkishMMLU) ranked per benchmark, each row
     carrying provenance: run log, reproduction command, first-party /
     community status. Source: GET /api/v1/quality-benchmarks.

  2. πŸ› οΈ Model catalog β€” every open-weight model worth running locally,
     with license tone, params, context, and vendor. Source:
     GET /api/v1/models.

Both endpoints are public, keyless, CC-BY-4.0. The catalog at
runlocalai.co is the source of truth β€” this Space is a discovery surface
for the HuggingFace community. Click any model name to read the full
operator-grade page.
"""

import gradio as gr
import pandas as pd
import requests

SITE_URL = "https://www.runlocalai.co"
MODELS_URL = f"{SITE_URL}/api/v1/models"
QB_URL = f"{SITE_URL}/api/v1/quality-benchmarks"
API_LIMIT = 200  # /api/v1/models caps at 200; rows come pre-sorted by popularity

# ─────────────────────────────────────────────────────────────────────────
# Leaderboard (tab 1) β€” GET /api/v1/quality-benchmarks
# ─────────────────────────────────────────────────────────────────────────

STATUS_DISPLAY = {
    "first-party": "⭐ First-party",
    "community": "πŸ‘₯ Community",
    "verified": "βœ… Verified",
    "pending": "⏳ Pending",
}

LB_COLS = ["Rank", "Benchmark", "Model", "Params (B)", "Quant", "Runtime", "Score", "Status", "Proof", "Tested"]
LB_DATATYPES = ["str", "str", "markdown", "number", "str", "str", "number", "str", "markdown", "str"]
ALL_BENCHMARKS = "All benchmarks"


def fetch_leaderboard():
    """Fetch quality-benchmark runs. Returns (runs_df, benchmarks_meta)."""
    try:
        r = requests.get(QB_URL, timeout=30)
        r.raise_for_status()
        payload = r.json()
    except Exception as exc:  # noqa: BLE001
        return pd.DataFrame({"Error": [f"Could not fetch leaderboard: {exc}"]}), {}

    benchmarks = {b.get("slug"): b for b in payload.get("benchmarks", []) if isinstance(b, dict)}
    runs = payload.get("runs", []) or []
    if not runs:
        return pd.DataFrame({"Status": ["No benchmark runs yet"]}), benchmarks

    records = []
    for run in runs:
        if not isinstance(run, dict):
            continue
        bslug = run.get("benchmark") or ""
        bdef = benchmarks.get(bslug, {})
        bname = bdef.get("name") or bslug or "β€”"

        mslug = run.get("model_slug") or ""
        mname = run.get("model_name") or mslug or "β€”"
        model_link = f"[{mname}]({SITE_URL}/models/{mslug})" if mslug else mname

        score = run.get("score")
        score_val = round(float(score), 1) if isinstance(score, (int, float)) else None

        log_url = run.get("test_run_log_url") or ""
        proof = f"[run log]({log_url})" if log_url else "β€”"

        status = run.get("submission_status") or ""
        tested = (run.get("tested_at") or "")[:10]

        records.append({
            "Benchmark": bname,
            "Model": model_link,
            "Params (B)": run.get("model_params_b"),
            "Quant": run.get("quant") or "β€”",
            "Runtime": run.get("runtime") or "β€”",
            "Score": score_val,
            "Status": STATUS_DISPLAY.get(status, status or "β€”"),
            "Proof": proof,
            "Tested": tested or "β€”",
            "_benchmark": bname,
            "_score_raw": float(score) if isinstance(score, (int, float)) else -1.0,
        })

    return pd.DataFrame(records), benchmarks


def leaderboard_view(df: pd.DataFrame, benchmark_label: str) -> pd.DataFrame:
    """Filter to a benchmark (or all), rank by score within each, add medals."""
    if "_score_raw" not in df.columns:  # error / empty passthrough
        return df
    out = df.copy()
    if benchmark_label and benchmark_label != ALL_BENCHMARKS:
        out = out[out["_benchmark"] == benchmark_label]
    if out.empty:
        return pd.DataFrame({"Status": ["No runs for this benchmark yet"]})

    out = out.sort_values(["_benchmark", "_score_raw"], ascending=[True, False])
    ranks = out.groupby("_benchmark")["_score_raw"].rank(ascending=False, method="min").astype(int)
    medal = {1: "πŸ₯‡", 2: "πŸ₯ˆ", 3: "πŸ₯‰"}
    out["Rank"] = [medal.get(int(rk), str(int(rk))) for rk in ranks]
    return out[LB_COLS].reset_index(drop=True)


def benchmark_blurb(benchmarks: dict) -> str:
    """Render the 'what these benchmarks measure' note from API metadata."""
    if not benchmarks:
        return ""
    lines = ["### What these scores mean\n"]
    for b in benchmarks.values():
        name = b.get("name", "")
        metric = b.get("metric", {}) or {}
        unit = f"{metric.get('label', '')} {metric.get('unit', '')}".strip()
        src = b.get("source", {}) or {}
        authors = src.get("authors", "")
        url = src.get("url", "")
        cats = ", ".join(b.get("categories", []) or [])
        src_link = f"[dataset]({url})" if url else ""
        lines.append(f"- **{name}** β€” {unit} Β· _{cats}_ Β· {authors} {src_link}".rstrip())
    lines.append(
        "\nEvery run is **measured first-party** on real consumer hardware and carries a public "
        "run log + a one-line reproduction command. "
        f"Methodology: [runlocalai.co/benchmarks/methodology]({SITE_URL}/benchmarks/methodology)."
    )
    return "\n".join(lines)


# ─────────────────────────────────────────────────────────────────────────
# Catalog (tab 2) β€” GET /api/v1/models
# ─────────────────────────────────────────────────────────────────────────

MODALITY_DISPLAY = {
    "text": "πŸ’¬ Text",
    "vision": "πŸ‘οΈ Vision",
    "audio": "πŸŽ™οΈ Audio",
    "video": "πŸŽ₯ Video",
    "embedding": "πŸ“ Embedding",
    "rerank": "πŸ” Rerank",
    "image-gen": "🎨 Image-gen",
}

CAT_HIDDEN = ["_modality_raw", "_params_raw", "_commercial_raw", "_family"]
CAT_DATATYPES = ["markdown", "str", "str", "str", "str", "str", "str", "markdown", "number", "number"]


def fetch_catalog() -> pd.DataFrame:
    """Fetch the latest model catalog. Falls back gracefully on error."""
    try:
        r = requests.get(MODELS_URL, params={"limit": API_LIMIT}, timeout=30)
        if r.status_code == 401:
            return pd.DataFrame(
                {"Error": ["Catalog API requires a key right now. "
                           "Browse the full catalog at runlocalai.co/models"]}
            )
        r.raise_for_status()
        payload = r.json()
        if isinstance(payload, dict):
            rows = payload.get("data") or payload.get("models") or []
        elif isinstance(payload, list):
            rows = payload
        else:
            rows = []
    except Exception as exc:  # noqa: BLE001
        return pd.DataFrame({"Error": [f"Could not fetch catalog: {exc}"]})

    if not rows:
        return pd.DataFrame({"Status": ["Catalog is empty"]})

    records = []
    for m in rows:
        if not isinstance(m, dict):
            continue
        modalities = m.get("modalities") or ["text"]
        modality = modalities[0] if isinstance(modalities, list) and modalities else "text"

        params_b = m.get("parameter_count_b") or 0
        if params_b and params_b < 1:
            params_label = f"{int(round(params_b * 1000))}M"
        elif params_b:
            params_label = f"{params_b}B"
        else:
            params_label = "β€”"

        commercial = "βœ… Yes" if m.get("license_commercial_ok") else "⚠️ Restricted"
        license_short = (m.get("license") or "β€”")[:24]

        ctx = m.get("context_length") or 0
        ctx_label = f"{int(ctx / 1024)}K" if ctx >= 1024 else (f"{ctx}" if ctx > 0 else "β€”")

        slug = m.get("slug", "") or ""
        name = m.get("name") or slug or "β€”"
        name_link = f"[{name}]({SITE_URL}/models/{slug})" if slug else name

        hf_repo = m.get("hf_repo") or ""
        hf_link = f"[hf.co/{hf_repo}](https://huggingface.co/{hf_repo})" if hf_repo else "β€”"

        rating = m.get("our_rating_score")
        rating_val = round(float(rating), 1) if isinstance(rating, (int, float)) else 0.0

        records.append({
            "Model": name_link,
            "Modality": MODALITY_DISPLAY.get(modality, modality),
            "Params": params_label,
            "Context": ctx_label,
            "License": license_short,
            "Commercial": commercial,
            "Vendor": m.get("vendor") or "β€”",
            "HuggingFace": hf_link,
            "Rating": rating_val,
            "Popularity": m.get("popularity_score") or 0,
            "_modality_raw": modality,
            "_params_raw": float(params_b) if params_b else 0.0,
            "_commercial_raw": bool(m.get("license_commercial_ok")),
            "_family": m.get("family") or "other",
        })

    if not records:
        return pd.DataFrame({"Status": ["Catalog is empty"]})

    df = pd.DataFrame(records)
    return df.sort_values("Popularity", ascending=False).reset_index(drop=True)


def apply_filters(df, modality, commercial_only, max_params, family, search):
    if not all(c in df.columns for c in CAT_HIDDEN):  # error / status passthrough
        return df
    out = df.copy()
    if modality and modality != "All":
        out = out[out["_modality_raw"] == modality]
    if commercial_only:
        out = out[out["_commercial_raw"]]
    if max_params and max_params < 200:  # 200 = no cap
        out = out[out["_params_raw"] <= max_params]
    if family and family != "All":
        out = out[out["_family"] == family]
    if search:
        s = search.lower().strip()
        mask = (
            out["Model"].str.lower().str.contains(s, na=False)
            | out["Vendor"].str.lower().str.contains(s, na=False)
            | out["HuggingFace"].str.lower().str.contains(s, na=False)
        )
        out = out[mask]
    return out.drop(columns=CAT_HIDDEN)


# ─────────────────────────────────────────────────────────────────────────
# Initial data load
# ─────────────────────────────────────────────────────────────────────────

LB_DATA, BENCHMARKS = fetch_leaderboard()
CATALOG = fetch_catalog()

benchmark_options = [ALL_BENCHMARKS] + sorted(
    {b.get("name") for b in BENCHMARKS.values() if b.get("name")}
)
modality_options = ["All"] + sorted({m for m in CATALOG.get("_modality_raw", []) if isinstance(m, str)})
family_options = ["All"] + sorted({f for f in CATALOG.get("_family", []) if isinstance(f, str)})

# ─────────────────────────────────────────────────────────────────────────
# UI
# ─────────────────────────────────────────────────────────────────────────

with gr.Blocks(
    title="RunLocalAI β€” local AI leaderboard & catalog",
    theme=gr.themes.Soft(primary_hue="amber", neutral_hue="slate"),
) as demo:
    gr.Markdown(
        f"""
        # πŸ› οΈ RunLocalAI β€” local AI leaderboard & catalog

        Reproducible benchmark scores and the full open-weight model catalog for running AI on
        **your own hardware**. Every benchmark is measured first-party with a public run log and a
        one-line reproduction command β€” no vibes, no leaderboard laundering.

        Source of truth: **[runlocalai.co]({SITE_URL})** Β· Data license: **CC-BY-4.0** Β·
        Click any model name for the full operator-grade page.
        """
    )

    with gr.Tabs():
        # ── Tab 1: Leaderboard ────────────────────────────────────────────
        with gr.Tab("πŸ† Benchmark leaderboard"):
            gr.Markdown(
                "Ranked, reproducible quality scores on real consumer GPUs. "
                "Pick a benchmark to see the head-to-head ranking."
            )
            benchmark_dd = gr.Dropdown(
                benchmark_options, value=ALL_BENCHMARKS, label="Benchmark", interactive=True
            )
            lb_table = gr.Dataframe(
                value=leaderboard_view(LB_DATA, ALL_BENCHMARKS),
                interactive=False,
                wrap=True,
                datatype=LB_DATATYPES,
            )
            lb_refresh = gr.Button("πŸ” Refresh leaderboard", variant="secondary")
            gr.Markdown(benchmark_blurb(BENCHMARKS))

            def _lb_filter(label):
                return leaderboard_view(LB_DATA, label)

            def _lb_refresh(label):
                global LB_DATA, BENCHMARKS
                LB_DATA, BENCHMARKS = fetch_leaderboard()
                return leaderboard_view(LB_DATA, label)

            benchmark_dd.change(_lb_filter, inputs=benchmark_dd, outputs=lb_table)
            lb_refresh.click(_lb_refresh, inputs=benchmark_dd, outputs=lb_table)

        # ── Tab 2: Catalog ────────────────────────────────────────────────
        with gr.Tab("πŸ› οΈ Model catalog"):
            gr.Markdown(
                "Every open-weight model worth running locally β€” LLMs, embeddings, rerankers, "
                "ASR, TTS, diffusion, vision encoders β€” with license tone and VRAM math."
            )
            with gr.Row():
                modality_dd = gr.Dropdown(modality_options, value="All", label="Modality", interactive=True)
                family_dd = gr.Dropdown(family_options, value="All", label="Family", interactive=True)
                max_params_slider = gr.Slider(
                    minimum=0.1, maximum=200, value=200, step=0.5,
                    label="Max params (B). 200 = no cap.",
                )
            with gr.Row():
                search_box = gr.Textbox(
                    label="Search (model / vendor / hf repo)",
                    placeholder="qwen, kokoro, gemma, deepseek …",
                )
                commercial_only = gr.Checkbox(label="Commercial-license only", value=False)

            cat_table = gr.Dataframe(
                value=apply_filters(CATALOG, "All", False, 200, "All", ""),
                interactive=False,
                wrap=True,
                datatype=CAT_DATATYPES,
            )
            cat_refresh = gr.Button("πŸ” Refresh catalog", variant="secondary")

            cat_inputs = [modality_dd, commercial_only, max_params_slider, family_dd, search_box]

            def _cat_filter(mod, com, mp, fam, search):
                return apply_filters(CATALOG, mod, com, mp, fam, search)

            def _cat_refresh(mod, com, mp, fam, search):
                global CATALOG
                CATALOG = fetch_catalog()
                return apply_filters(CATALOG, mod, com, mp, fam, search)

            for ctrl in cat_inputs:
                ctrl.change(_cat_filter, inputs=cat_inputs, outputs=cat_table)
            cat_refresh.click(_cat_refresh, inputs=cat_inputs, outputs=cat_table)

    gr.Markdown(
        f"""
        ---
        **Catalog hubs:**
        [Small LMs]({SITE_URL}/small-language-models) Β·
        [Embeddings]({SITE_URL}/embeddings) Β·
        [Audio]({SITE_URL}/audio) Β·
        [Image]({SITE_URL}/image-models) Β·
        [Coding]({SITE_URL}/coding-models) Β·
        [Turkish]({SITE_URL}/turkish-models) Β·
        [Benchmarks]({SITE_URL}/benchmarks)

        **Machine-readable:**
        [models]({MODELS_URL}) Β·
        [quality-benchmarks]({QB_URL}) Β·
        [OpenAPI]({SITE_URL}/api/v2/openapi)

        Data licensed **CC-BY-4.0** β€” attribute to runlocalai.co with a link.
        """
    )

if __name__ == "__main__":
    demo.launch()