CoolFace
Apppublic

Maffo1408/PPML-Explorer

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py57 linesDownload Raw Back to root
1from datasets import load_dataset
2from huggingface_hub import hf_hub_download
3import gradio as gr, json, pandas as pd
4
5import logging, pprint, json
6
7logging.basicConfig(level=logging.WARNING)
8
9DS_ID = "Maffo1408/PPML"
10SCHEME_FILE = "scheme2bib.json"   # side-car mapping
11
12# ------ load table + BibTeX sidecar -----------------------------------------
13df = load_dataset(DS_ID, split="train").to_pandas()
14bib_path = hf_hub_download(
15    repo_id=DS_ID,
16    filename=SCHEME_FILE,
17    repo_type="dataset"
18)
19scheme2bib = json.load(open(bib_path))
20
21# ------ callback ------------------------------------------------------------
22
23def show_bibtex(evt: gr.SelectData):
24    """
25    evt.value  – value of the clicked cell            (unused here)
26    evt.index  – (row, col) tuple  on most 5.x builds
27    evt.row    – int row index      on newer 5.3x builds
28    """
29
30    # --- 1. determine which attribute Gradio provided ----------------------
31    row_idx = getattr(evt, "row", None)              # ✅ new line
32    if row_idx is None and evt.index is not None:
33        row_idx = evt.index[0]                       # first element of (row, col)
34
35    if row_idx is None:
36        return "No row selected."
37
38    # --- 2. first column always holds the scheme name ----------------------
39    scheme = df.iloc[row_idx, 0]                     # ✅ use position, not label
40
41    # --- 3. look up BibTeX --------------------------------------------------
42    return scheme2bib.get(str(scheme), "BibTeX not found")
43
44# ------ UI ------------------------------------------------------------------
45with gr.Blocks(css="code{font-size:0.8rem}") as demo:
46    gr.Markdown("# 🛡️ PPML Techniques Explorer")
47    grid = gr.Dataframe(value=df, interactive=True)
48    bibbox = gr.Textbox(
49        value="Click a row to see BibTeX ↓",
50        lines=15,
51        label="BibTeX",
52        show_copy_button=True,   # handy copy-to-clipboard icon
53    )
54    grid.select(show_bibtex, outputs=bibbox)
55
56demo.launch()
57