File size: 24,554 Bytes
7aabe91
 
 
 
 
b3908ac
4fe3afe
7aabe91
b3908ac
 
 
 
7aabe91
 
 
 
6282cbe
 
7aabe91
 
 
6282cbe
b3908ac
7aabe91
 
6a2ca60
 
 
 
 
 
 
e119d3b
6282cbe
7aabe91
 
 
 
 
 
 
6282cbe
7aabe91
 
 
 
6282cbe
7aabe91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3908ac
 
 
 
 
 
 
e119d3b
 
b3908ac
 
e119d3b
b3908ac
 
 
 
 
 
 
 
 
 
4fe3afe
 
7aabe91
4fe3afe
 
6282cbe
b3908ac
4fe3afe
b3908ac
 
 
 
6282cbe
 
4fe3afe
 
 
6282cbe
4fe3afe
 
 
 
6282cbe
4fe3afe
6282cbe
 
 
4fe3afe
 
 
 
 
7aabe91
6282cbe
7aabe91
6282cbe
4fe3afe
6282cbe
4fe3afe
 
6282cbe
 
4fe3afe
 
7aabe91
 
4fe3afe
 
 
 
 
 
 
 
 
 
7aabe91
 
 
6282cbe
 
4fe3afe
 
 
 
 
 
 
 
 
 
 
6282cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aabe91
 
b3908ac
7aabe91
 
b3908ac
 
 
 
 
7aabe91
 
 
 
 
 
 
b3908ac
7aabe91
 
 
 
 
 
 
 
b3908ac
7aabe91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afb90e6
6282cbe
7aabe91
 
 
 
 
afb90e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aabe91
 
 
afb90e6
7aabe91
 
 
 
 
afb90e6
6282cbe
 
 
b3908ac
6282cbe
 
 
afb90e6
6282cbe
b3908ac
 
 
 
6282cbe
 
 
afb90e6
6282cbe
b3908ac
 
7aabe91
afb90e6
6282cbe
 
 
 
7aabe91
 
afb90e6
7aabe91
 
 
 
6282cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aabe91
b3908ac
7aabe91
 
 
b3908ac
7aabe91
 
 
 
 
 
 
 
b3908ac
 
7aabe91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6282cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aabe91
 
b3908ac
6282cbe
7aabe91
 
 
 
b3908ac
7aabe91
b3908ac
7aabe91
 
 
b3908ac
7aabe91
 
 
6282cbe
 
 
 
 
 
 
 
 
b3908ac
7aabe91
 
 
 
 
 
 
 
 
 
 
 
 
 
b3908ac
7aabe91
 
 
b3908ac
 
 
 
 
 
7aabe91
b3908ac
 
 
 
 
 
7aabe91
6282cbe
 
b3908ac
 
 
 
 
7aabe91
6282cbe
 
 
 
 
 
b3908ac
7aabe91
b3908ac
 
 
 
 
 
7aabe91
 
b3908ac
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import os, uuid, datetime, traceback
from pathlib import Path
import html as _py_html
import pandas as pd
import gradio as gr
from huggingface_hub import hf_hub_download
from urllib.parse import unquote  # add at top

# ----------- HF DATASET CONFIG -----------
HF_DATASET_REPO = "akazemian/audio-html"   # <-- change if needed
INDEX_FILENAME  = "index.csv"
# -----------------------------------------

DB_PATH = "library.csv"
ALLOWED_EXTS = {".html"}

# Columns in DB
EXTRA_COLS = ["category", "dataset", "hf_path"]   # <-- add hf_path here
BASE_COLS  = ["id","filename","path","tags","keywords","notes","uploaded_at"]
ALL_DB_COLS = BASE_COLS + EXTRA_COLS

# Columns shown in the table (don't show hf_path)
TABLE_COLS = ["id","filename","category","dataset",
              "tags","keywords","notes","uploaded_at"]

# At top-level config
HF_INDEX_REPO_ID   = "akazemian/audio-library"  # where index.csv lives *now*
HF_INDEX_REPO_TYPE = "space"                    # <β€” it's a Space, not a dataset
INDEX_FILENAME     = "index.csv"

from huggingface_hub import hf_hub_download



# ---------- DB helpers ----------
def _load_db() -> pd.DataFrame:
    if os.path.exists(DB_PATH):
        df = pd.read_csv(DB_PATH)
        for c in ALL_DB_COLS:
            if c not in df.columns:
                df[c] = ""
        for c in ["tags","keywords","notes","category","dataset","hf_path","path","filename","id","uploaded_at"]:
            df[c] = df[c].fillna("").astype(str)
        return df[ALL_DB_COLS]
    return pd.DataFrame(columns=ALL_DB_COLS)


def _save_db(df: pd.DataFrame):
    df.to_csv(DB_PATH, index=False)

# ---------- Table normalizer ----------
def _df_from_table_value(table_value):
    cols = TABLE_COLS
    if isinstance(table_value, pd.DataFrame):
        for c in cols:
            if c not in table_value.columns:
                table_value[c] = ""
        return table_value[cols]
    if isinstance(table_value, list):
        if not table_value:
            return pd.DataFrame(columns=cols)
        first = table_value[0]
        if isinstance(first, dict):
            df = pd.DataFrame(table_value)
            for c in cols:
                if c not in df.columns:
                    df[c] = ""
            return df[cols]
        else:
            return pd.DataFrame(table_value, columns=cols)
    return pd.DataFrame(columns=cols)

# ---------- Load HF index ----------
def _load_hf_index() -> pd.DataFrame:
    """
    Download + read index.csv from the HF dataset repo.
    Required columns: id, filename, relpath, category, dataset, tags, keywords, notes, uploaded_at
    """
    local = hf_hub_download(
        repo_id=HF_INDEX_REPO_ID,
        repo_type=HF_INDEX_REPO_TYPE,
        filename=INDEX_FILENAME,
    )

    df = pd.read_csv(local)
    for c in ["id","filename","relpath","category","dataset","tags","keywords","notes","uploaded_at"]:
        if c not in df.columns:
            df[c] = ""
    # normalize types
    for c in ["id","filename","relpath","category","dataset","tags","keywords","notes","uploaded_at"]:
        df[c] = df[c].fillna("").astype(str)
    return df

# ---------- Sync by model (prefix inside HF dataset) ----------
from urllib.parse import unquote  # ensure this import exists at top

def sync_model(model_name: str):
    raw = (model_name or "").strip()
    if not raw:
        return gr.Info("Please enter a model name."), None, None, None, "", ""

    # 1) read index from HF and filter to this model prefix (accept raw or URL-decoded)
    try:
        idx = _load_hf_index()
    except Exception as e:
        traceback.print_exc()
        return gr.Info(f"Failed to load index from HF: {e}"), None, None, None, "", ""

    decoded = unquote(raw)
    rel = idx["relpath"].astype(str)
    sub = idx[ rel.str.startswith(f"{raw}/") | rel.str.startswith(f"{decoded}/") ]
    if sub.empty:
        return gr.Info(
            f"No HTML files found for model '{raw}'. "
            "Tip: if you copied from the URL, use '=' instead of '%3D'."
        ), None, None, None, "", ""

    # 2) load local DB, backfill hf_path for existing rows of this model (by filename)
    db = _load_db()
    if not db.empty:
        rel_by_fname = dict(zip(sub["filename"].astype(str), sub["relpath"].astype(str)))
        mask_model_rows = db["filename"].astype(str).isin(rel_by_fname.keys())
        if mask_model_rows.any():
            db.loc[mask_model_rows, "hf_path"] = db.loc[mask_model_rows, "filename"].map(
                lambda fn: f"hf://{HF_DATASET_REPO}/{rel_by_fname.get(str(fn), str(fn))}"
            )

    # 3) add any missing rows from HF index
    now = datetime.datetime.now().isoformat(timespec="seconds")
    existing_hf = set(db["hf_path"].astype(str))
    new_rows = []
    for _, r in sub.iterrows():
        rp = str(r["relpath"])
        hf_uri = f"hf://{HF_DATASET_REPO}/{rp}"
        if hf_uri in existing_hf:
            continue
        # If a row with same filename exists already, we updated its hf_path above; skip adding duplicate
        if not db[db["filename"].astype(str) == str(r["filename"])].empty:
            continue
        new_rows.append({
            "id": (str(r["id"]) if str(r.get("id", "")) else uuid.uuid4().hex[:8]),
            "filename": str(r["filename"]),
            "path": "",                         # local path unknown in HF flow
            "hf_path": hf_uri,
            "tags": str(r.get("tags", "")),
            "keywords": str(r.get("keywords", "")),
            "notes": str(r.get("notes", "")),
            "uploaded_at": (str(r.get("uploaded_at", "")) or now),
            "category": str(r.get("category", "")),
            "dataset": str(r.get("dataset", "")),
        })

    if new_rows:
        db = pd.concat([db, pd.DataFrame(new_rows)], ignore_index=True)

    _save_db(db)

    # Use decoded model for downstream filtering
    current_model = decoded
    # outputs: [table, tag_filter, category_filter, dataset_filter, count_md, current_model]
    return refresh_view("", [], "", "", current_model) + (current_model,)



# allow user to paste either "wavcoch_audio-preds-sr=16000" or the URL-encoded "%3D" form


# def sync_model(model_name: str):
#     """
#     Load index.csv from HF, add rows for the selected model (by relpath prefix),
#     store HF URIs in DB, and show only that model’s files.
#     """
#     model_name = (model_name or "").strip()
#     if not model_name:
#         return gr.Info("Please enter a model name."), None, None, None, ""

#     try:
#         idx = _load_hf_index()
    # except Exception as e:
    #     traceback.print_exc()
    #     return gr.Info(f"Failed to load index from HF: {e}"), None, None, None, ""

    # # rows like "{model_name}/.../file.html"
    # subset = idx[idx["relpath"].str.startswith(model_name + "/")]
    # if subset.empty:
    #     return gr.Info(f"No HTML files found for model '{model_name}' on {HF_DATASET_REPO}"), None, None, None, ""

    # df = _load_db()
    # now = datetime.datetime.now().isoformat(timespec="seconds")
    # new_rows = []

    # for _, r in subset.iterrows():
    #     relpath = r["relpath"]
    #     hub_uri = f"hf://{HF_DATASET_REPO}/{relpath}"
    #     if (df["path"] == hub_uri).any():
    #         continue
    #     new_rows.append({
    #         "id": r["id"] if r["id"] else uuid.uuid4().hex[:8],
    #         "filename": r["filename"],
    #         "path": hub_uri,                        # store HF URI
    #         "tags": r["tags"],
    #         "keywords": r["keywords"],
    #         "notes": r["notes"],
    #         "uploaded_at": r["uploaded_at"] or now,
    #         "category": r["category"],
    #         "dataset": r["dataset"]
    #     })

    # if new_rows:
    #     df = pd.concat([df, pd.DataFrame(new_rows)], ignore_index=True)
    #     _save_db(df)

    # current_model = model_name  # remember which model prefix is active
    # return refresh_view("", [], "", "", current_model) + (current_model,)

# ---------- Search / filters ----------
def refresh_view(query, tag_filters, category_filter, dataset_filter, current_model):
    df = _load_db()

    # scope to current model prefix in HF URI if provided
    if current_model:
        prefix = f"hf://{HF_DATASET_REPO}/{current_model}/"
        df = df[df["path"].astype(str).str.startswith(prefix)]

    # tag vocabulary
    all_tags = sorted({t.strip()
                       for s in df["tags"].dropna().astype(str).tolist()
                       for t in s.split(",") if t.strip()})
    all_cats   = sorted([c for c in df["category"].dropna().astype(str).unique() if c])
    all_sets   = sorted([c for c in df["dataset"].dropna().astype(str).unique() if c])

    # free-text query across filename/tags/keywords/notes/category/dataset
    if query:
        q = query.lower()
        mask = (
            df["filename"].str.lower().str.contains(q, na=False) |
            df["tags"].str.lower().str.contains(q, na=False) |
            df["keywords"].str.lower().str.contains(q, na=False) |
            df["notes"].str.lower().str.contains(q, na=False) |
            df["category"].str.lower().str.contains(q, na=False) |
            df["dataset"].str.lower().str.contains(q, na=False)
        )
        df = df[mask]

    # tag filters (AND semantics)
    for t in (tag_filters or []):
        df = df[df["tags"].astype(str).apply(
            lambda s: t in [x.strip() for x in s.split(",") if x.strip()])]

    # dropdown filters (exact match)
    if category_filter:
        df = df[df["category"] == category_filter]
    if dataset_filter:
        df = df[df["dataset"] == dataset_filter]

    df = df.sort_values("uploaded_at", ascending=False).reset_index(drop=True)
    view = df[TABLE_COLS].copy()
    count_text = f"**Showing {len(view)} file(s)**"

    return (
        view,
        gr.update(choices=all_tags),
        gr.update(choices=[""] + all_cats,   value=category_filter or ""),
        gr.update(choices=[""] + all_sets,   value=dataset_filter or ""),
        count_text
    )

# ---------- Preview ----------
def _iframe_from_html_string(raw_html: str, height_px: int = 720) -> str:
    srcdoc = raw_html.replace("&", "&amp;").replace('"', "&quot;")
    return f'<iframe style="width:100%;height:{height_px}px;border:1px solid #ddd;border-radius:8px;" srcdoc="{srcdoc}"></iframe>'

def select_row(evt: gr.SelectData, table_value, source_mode):
    try:
        view = _df_from_table_value(table_value)
        if view.empty:
            return "<em>No rows.</em>", ""

        # --- resolve row_idx robustly ---
        row_idx = None

        # 1) Preferred: evt.index (int or [int, ...])
        ix = getattr(evt, "index", None)
        if isinstance(ix, int):
            row_idx = ix
        elif isinstance(ix, (list, tuple)) and ix and isinstance(ix[0], int):
            row_idx = ix[0]

        # 2) Fallbacks: evt.value may be a dict with id, or a list (row values)
        if row_idx is None:
            val = getattr(evt, "value", None)
            if isinstance(val, dict) and "id" in val:
                hits = view.index[view["id"] == val["id"]].tolist()
                if hits:
                    row_idx = hits[0]
            elif isinstance(val, list) and len(val) >= 1:
                # assume first column is id
                hits = view.index[view["id"] == val[0]].tolist()
                if hits:
                    row_idx = hits[0]

        # 3) Last resort: default to first row
        if row_idx is None:
            row_idx = 0

        # bounds check
        if not (0 <= row_idx < len(view)):
            return "<em>Invalid selection.</em>", ""

        row = view.iloc[row_idx]
        sel_id = row["id"]

        # --- look up the full record from DB ---
        db = _load_db()
        rec = db[db["id"] == sel_id]
        if rec.empty:
            return "<em>Could not find file for this row.</em>", ""

        # --- choose source: HF vs Local ---
        use_hf = (str(source_mode).upper() == "HF")
        path_str = rec["hf_path"].values[0] if use_hf else rec["path"].values[0]
        path_str = str(path_str or "")

        if not path_str:
            return "<em>No path available for this source.</em>", f"πŸ“„ {row['filename']}"

        # HF dataset URI β†’ lazy download then iframe from raw HTML
        if path_str.startswith("hf://"):
            _, rest = path_str.split("hf://", 1)
            repo_id, relpath = rest.split("/", 1)
            local_path = hf_hub_download(repo_id=repo_id, repo_type="dataset", filename=relpath)
            raw_html = Path(local_path).read_text(encoding="utf-8")
            iframe = _iframe_from_html_string(raw_html, height_px=720)
            return iframe, f"πŸ“„ {row['filename']}"

        # Direct HTTP URL (CDN) β†’ iframe src
        if path_str.startswith("http"):
            iframe = f'<iframe style="width:100%;height:720px;border:1px solid #ddd;border-radius:8px;" src="{_py_html.escape(path_str)}"></iframe>'
            return iframe, f"πŸ“„ {row['filename']}"

        # Local file fallback
        p = Path(path_str)
        if not p.exists():
            return f"<em>File not found:</em> <code>{_py_html.escape(str(p))}</code>", f"πŸ“„ {row['filename']}"
        raw_html = p.read_text(encoding="utf-8")
        iframe = _iframe_from_html_string(raw_html, height_px=720)
        return iframe, f"πŸ“„ {row['filename']}"

    except Exception as e:
        traceback.print_exc()
        return f"<pre>Failed to render (see terminal):\n{_py_html.escape(str(e))}</pre>", ""

# def select_row(evt: gr.SelectData, table_value):
#     try:
#         view = _df_from_table_value(table_value)
#         if view.empty:
#             return "<em>No rows.</em>", ""
#         # resolve row
#         row_idx = None
#         ix = getattr(evt, "index", None)
#         if isinstance(ix, int):
#             row_idx = ix
#         elif isinstance(ix, (list, tuple)) and ix and isinstance(ix[0], int):
#             row_idx = ix[0]
#         if row_idx is None:
#             val = getattr(evt, "value", None)
#             if isinstance(val, dict) and "id" in val:
#                 hits = view.index[view["id"] == val["id"]].tolist()
#                 if hits: row_idx = hits[0]
#             elif isinstance(val, list) and len(val) >= 1:
#                 hits = view.index[view["id"] == val[0]].tolist()
#                 if hits: row_idx = hits[0]
#         if row_idx is None or not (0 <= row_idx < len(view)):
#             return "<em>Invalid selection.</em>", ""

#         row = view.iloc[row_idx]
#         sel_id = row["id"]

#         db = _load_db()
#         rec = db[db["id"] == sel_id]
#         if rec.empty:
#             return "<em>Could not find file for this row.</em>", ""

#         path_str = rec["path"].values[0]

#         # Hub-backed path β†’ lazy download
#         if str(path_str).startswith("hf://"):
#             _, rest = path_str.split("hf://", 1)
#             repo_id, relpath = rest.split("/", 1)
#             local_path = hf_hub_download(repo_id=repo_id, repo_type="dataset", filename=relpath)
#             raw_html = Path(local_path).read_text(encoding="utf-8")
#         elif str(path_str).startswith("http"):
#             # if you ever swap to CDN URLs, iframe the URL directly
#             iframe = f'<iframe style="width:100%;height:720px;border:1px solid #ddd;border-radius:8px;" src="{_py_html.escape(path_str)}"></iframe>'
#             return iframe, f"πŸ“„ {row['filename']}"
#         else:
#             # local file fallback (not used for HF flow, kept for compatibility)
#             p = Path(path_str)
#             if not p.exists():
#                 return f"<em>File not found:</em> <code>{_py_html.escape(str(p))}</code>", f"πŸ“„ {row['filename']}"
#             raw_html = p.read_text(encoding="utf-8")

#         iframe = _iframe_from_html_string(raw_html, height_px=720)
#         return iframe, f"πŸ“„ {row['filename']}"
#     except Exception as e:
#         traceback.print_exc()
#         return f"<pre>Failed to render (see terminal):\n{_py_html.escape(str(e))}</pre>", ""

# ---------- Save edits ----------
def save_edits(edited_table, current_model):
    if edited_table is None or not len(edited_table):
        return gr.Info("Nothing to save.")
    df_db = _load_db()
    editable_cols = ["category","dataset","tags","keywords","notes"]
    for c in editable_cols:
        edited_table[c] = edited_table[c].fillna("").astype(str)
    for _, row in edited_table.iterrows():
        i = df_db.index[df_db["id"] == row["id"]]
        if len(i):
            for c in editable_cols:
                df_db.at[i[0], c] = row[c]
    _save_db(df_db)
    # return refreshed table only (respect current_model scope)
    return refresh_view("", [], "", "", current_model)[0]

# -------------------- UI --------------------
# CSS that targets only the three buttons via elem_id
custom_css = """
/* scope styles to only these 3 components */
#sync-btn button,
#refresh-btn button,
#save-btn button,
#sync-btn .gr-button,
#refresh-btn .gr-button,
#save-btn .gr-button,
#sync-btn [role="button"],
#refresh-btn [role="button"],
#save-btn [role="button"] {
  background: #f97316 !important;   /* orange-500 */
  border-color: #f97316 !important;
  color: #fff !important;
}

/* hover/active */
#sync-btn button:hover,
#refresh-btn button:hover,
#save-btn button:hover,
#sync-btn .gr-button:hover,
#refresh-btn .gr-button:hover,
#save-btn .gr-button:hover,
#sync-btn [role="button"]:hover,
#refresh-btn [role="button"]:hover,
#save-btn [role="button"]:hover {
  background: #ea580c !important;   /* orange-600 */
  border-color: #ea580c !important;
}

/* (optional) also set CSS vars in case theme uses them */
#sync-btn, #refresh-btn, #save-btn {
  --button-primary-background-fill: #f97316;
  --button-primary-background-fill-hover: #ea580c;
  --button-text-color: #fff;
}
"""

# with gr.Blocks(title="Audio HTML Library", css=custom_css) as demo:
#     gr.Markdown("## 🎧 Audio Reconstruction Reports β€” sync β€’ search β€’ view")
#     current_model = gr.State("")  # remembers active model prefix inside HF repo
#     source_mode = gr.State("HF")  # default


#     with gr.Row():
#         with gr.Column(scale=1):
#             # Choose model & sync
#             gr.Markdown(f"**Model prefix on HF dataset:** `{HF_DATASET_REPO}/<model_name>/...`")
#             model_in = gr.Textbox(label="Model name", placeholder="e.g., WavCochV8192")
#             sync_btn = gr.Button("Sync this model", elem_id="sync-btn")

#             # Search & filters
#             gr.Markdown("---\n**Search & filter**")
#             query = gr.Textbox(label="Keyword search (filename/tags/notes/category/dataset)", placeholder="type to search…")
#             tag_filter = gr.CheckboxGroup(choices=[], label="Filter by tags (AND)")
#             category_filter = gr.Dropdown(choices=[], label="Category")
#             dataset_filter  = gr.Dropdown(choices=[], label="Dataset")
#             refresh_btn = gr.Button("Refresh", elem_id="refresh-btn")

#         with gr.Column(scale=2):
#             # Count of current view
#             count_md = gr.Markdown("**Showing 0 file(s)**")
#             gr.Markdown("**Library** (click a row to preview; edit cells and Save)")
#             table = gr.Dataframe(
#                 headers=TABLE_COLS,
#                 datatype=["str"] * len(TABLE_COLS),
#                 interactive=True,
#                 wrap=True,
#                 row_count=(0, "dynamic"),
#                 col_count=(len(TABLE_COLS), "fixed")
#             )
#             with gr.Row():
#                 save_btn = gr.Button("Save Edits", elem_id="save-btn")
#             preview_label = gr.Markdown("")
#             preview_html = gr.HTML("")

#     # wiring: sync (also sets current_model)
#     sync_btn.click(
#         sync_model,
#         [model_in],
#         [table, tag_filter, category_filter, dataset_filter, count_md, current_model]
#     )

#     # wiring: refresh + live filters (respect current_model)
#     refresh_btn.click(
#         refresh_view,
#         [query, tag_filter, category_filter, dataset_filter, current_model],
#         [table, tag_filter, category_filter, dataset_filter, count_md]
#     )

#     for comp in (query, tag_filter, category_filter, dataset_filter):
#         comp.change(
#             refresh_view,
#             [query, tag_filter, category_filter, dataset_filter, current_model],
#             [table, tag_filter, category_filter, dataset_filter, count_md]
#         )

#     table.select(select_row, [table], [preview_html, preview_label])
#     save_btn.click(save_edits, [table, current_model], [table])

#     # initial load (no model yet)
#     demo.load(
#         refresh_view,
#         [query, tag_filter, category_filter, dataset_filter, current_model],
#         [table, tag_filter, category_filter, dataset_filter, count_md]
#     )

# if __name__ == "__main__":
#     demo.launch(share=True)  # auth optional

with gr.Blocks(title="Audio HTML Library", css=custom_css) as demo:
    gr.Markdown("## 🎧 Audio Reconstruction Reports β€” sync β€’ search β€’ view")
    current_model = gr.State("")  # remembers active model prefix inside HF repo
    source_mode = gr.State("HF")  # default

    with gr.Row():
        with gr.Column(scale=1):
            # Choose model & sync
            gr.Markdown(f"**Model prefix on HF dataset:** `{HF_DATASET_REPO}/<model_name>/...`")
            model_in = gr.Textbox(label="Model name", placeholder="e.g., WavCochV8192")
            sync_btn = gr.Button("Sync this model", elem_id="sync-btn")

            # Search & filters
            gr.Markdown("---\n**Search & filter**")
            query = gr.Textbox(label="Keyword search (filename/tags/notes/category/dataset)", placeholder="type to search…")
            tag_filter = gr.CheckboxGroup(choices=[], label="Filter by tags (AND)")
            category_filter = gr.Dropdown(choices=[], label="Category")
            dataset_filter  = gr.Dropdown(choices=[], label="Dataset")

            # πŸ”½ Step 5: Source toggle (HF vs Local)
            mode_radio = gr.Radio(
                choices=["HF", "Local"],
                value="HF",
                label="Source",
                info="Preview from HF dataset or local disk"
            )

            refresh_btn = gr.Button("Refresh", elem_id="refresh-btn")

        with gr.Column(scale=2):
            # Count of current view
            count_md = gr.Markdown("**Showing 0 file(s)**")
            gr.Markdown("**Library** (click a row to preview; edit cells and Save)")
            table = gr.Dataframe(
                headers=TABLE_COLS,
                datatype=["str"] * len(TABLE_COLS),
                interactive=True,
                wrap=True,
                row_count=(0, "dynamic"),
                col_count=(len(TABLE_COLS), "fixed")
            )
            with gr.Row():
                save_btn = gr.Button("Save Edits", elem_id="save-btn")
            preview_label = gr.Markdown("")
            preview_html = gr.HTML("")

    # wiring: sync (also sets current_model)
    sync_btn.click(
        sync_model,
        [model_in],
        [table, tag_filter, category_filter, dataset_filter, count_md, current_model]
    )

    # wiring: refresh + live filters (respect current_model)
    refresh_btn.click(
        refresh_view,
        [query, tag_filter, category_filter, dataset_filter, current_model],
        [table, tag_filter, category_filter, dataset_filter, count_md]
    )

    # Trigger refresh when any filter OR source mode changes
    for comp in (query, tag_filter, category_filter, dataset_filter, mode_radio):
        comp.change(
            refresh_view,
            [query, tag_filter, category_filter, dataset_filter, current_model],
            [table, tag_filter, category_filter, dataset_filter, count_md]
        )

    # Keep source_mode state in sync with the radio
    mode_radio.change(lambda x: x, [mode_radio], [source_mode])

    # Pass source_mode into select_row so it can choose hf_path vs path
    table.select(select_row, [table, source_mode], [preview_html, preview_label])

    save_btn.click(save_edits, [table, current_model], [table])

    # initial load (no model yet)
    demo.load(
        refresh_view,
        [query, tag_filter, category_filter, dataset_filter, current_model],
        [table, tag_filter, category_filter, dataset_filter, count_md]
    )

if __name__ == "__main__":
    demo.launch(share=True)  # auth optional