"""Overview page — catalog header + pipeline coverage + top-tags-across-catalog. Reads only the cached summary JSONs + the per-track JSON list (also cached). Pure rendering — no inference happens here. """ from __future__ import annotations from typing import Any import streamlit as st from sync_pilot.dashboard.data_loader import ( load_summary, load_tracks, parse_iso, total_audio_minutes, ) from sync_pilot.dashboard.plots import catalog_top_tags_chart, language_distribution_chart _CATEGORY_TITLES = { "genre": "Top genres", "mood": "Top moods", "instrument": "Top instruments", "vocal": "Top vocal configurations", } def _stat_cell(label: str, value: str, sub: str = "") -> str: """Render one statistic block in the pipeline-coverage grid.""" sub_html = f'
{sub}
' if sub else "" return ( '
' f'
{label}
' f'
{value}
' f"{sub_html}" "
" ) def _format_latency_ms(values: list[float]) -> str: if not values: return "—" return f"{sum(values) / len(values):.0f} ms" def _format_timestamp(ts: str | None) -> str: dt = parse_iso(ts) if not dt: return "—" return dt.strftime("%Y-%m-%d %H:%M UTC") def _pipeline_row( *, name: str, summary: dict[str, Any], extra_stats: list[tuple[str, str, str]] | None = None, ) -> None: """One row of the coverage grid: name + four stats + last-run timestamp.""" counts = summary.get("counts", {}) or {} timing = summary.get("timing", {}) or {} run = summary.get("run", {}) or {} ok = counts.get("tracks_succeeded", 0) + counts.get("tracks_skipped_existing", 0) failed = counts.get("tracks_failed", 0) mean = timing.get("mean_track_sec") mean_str = f"{mean:.1f}s" if isinstance(mean, (int, float)) else "—" last_run = _format_timestamp(run.get("finished_at") or run.get("started_at")) pipeline_version = run.get("pipeline_version") or run.get("pipeline_version_suffix") or "—" cols = st.columns([2, 1.4, 1.4, 1.4, 2.2]) cols[0].markdown( f"**{name}** \n" f"{pipeline_version}", unsafe_allow_html=True, ) cols[1].markdown( _stat_cell("Coverage", f"{ok}", f"{failed} failed" if failed else "no failures"), unsafe_allow_html=True, ) cols[2].markdown(_stat_cell("Mean / track", mean_str, ""), unsafe_allow_html=True) if extra_stats: label, value, sub = extra_stats[0] cols[3].markdown(_stat_cell(label, value, sub), unsafe_allow_html=True) else: cols[3].markdown(_stat_cell("—", "—", ""), unsafe_allow_html=True) cols[4].markdown(_stat_cell("Last run", last_run, ""), unsafe_allow_html=True) def render() -> None: """Render the overview page into the current Streamlit script context.""" tracks = load_tracks() tagging = load_summary("tagging") clap = load_summary("clap") desc = load_summary("description") trans = load_summary("transcription") # ----------------------------------------------------------------- # Catalog header # ----------------------------------------------------------------- st.markdown('
Catalog
', unsafe_allow_html=True) st.title("Median Müzik · sync-licensing pilot") total_min = total_audio_minutes(tracks) n_tracks = len(tracks) n_with_desc = sum(1 for t in tracks if t.get("description")) n_with_lyrics = sum(1 for t in tracks if (t.get("lyrics") or "").strip()) header_cols = st.columns(4) header_cols[0].markdown( _stat_cell("Tracks", str(n_tracks), "eligible from 84-row manifest"), unsafe_allow_html=True, ) header_cols[1].markdown( _stat_cell("Audio", f"{total_min:.0f} min", f"{total_min / 60:.1f} h total"), unsafe_allow_html=True, ) header_cols[2].markdown( _stat_cell("With description", str(n_with_desc), "v0.5 LLM synthesis"), unsafe_allow_html=True, ) header_cols[3].markdown( _stat_cell("With lyrics", str(n_with_lyrics), "Whisper-large-v3-turbo"), unsafe_allow_html=True, ) st.markdown("---") # ----------------------------------------------------------------- # Pipeline coverage grid # ----------------------------------------------------------------- st.subheader("Pipeline coverage") st.caption( "One row per inference stage. Coverage = tracks with stage output " "(succeeded + skipped-existing, both count as 'data on disk')." ) desc_tokens = desc.get("tokens", {}) or {} cost_usd = desc_tokens.get("estimated_cost_usd") cost_str = f"${cost_usd:.3f}" if isinstance(cost_usd, (int, float)) else "—" trans_counts = trans.get("counts", {}) or {} trans_timing = trans.get("timing", {}) or {} rtf = trans_timing.get("overall_realtime_factor") rtf_str = f"{rtf:.1f}×" if isinstance(rtf, (int, float)) else "—" _pipeline_row( name="MAEST tagging", summary=tagging, extra_stats=[("Model", "MAEST-30s", "Discogs taxonomy")], ) _pipeline_row( name="CLAP zero-shot", summary=clap, extra_stats=[("Model", "CLAP-htsat", "TR-prompt vocab")], ) _pipeline_row( name="Description (v0.5)", summary=desc, extra_stats=[("LLM cost", cost_str, "DeepSeek via OpenRouter")], ) _pipeline_row( name="Lyrics (Whisper)", summary=trans, extra_stats=[ ( "Realtime", rtf_str, f"{trans_counts.get('tracks_hallucination_truncated', 0)} truncated", ) ], ) st.markdown("---") # ----------------------------------------------------------------- # Top-tags-across-catalog grid (the visual "what's the catalog about?") # ----------------------------------------------------------------- st.subheader("What the catalog looks like") st.caption( f"Top tags across all {n_tracks} tracks, split by source after the " "same display precision policy used on track pages. Indigo = MAEST, " "purple = MuQ probes, amber = PaSST promoted probes, blue = taxonomy " "adapter, violet = human-review probe tags, sage = lyrics themes." ) grid_rows = st.columns(2) for i, cat in enumerate(["genre", "mood"]): with grid_rows[i]: fig = catalog_top_tags_chart( tracks, category=cat, title=_CATEGORY_TITLES[cat] ) st.plotly_chart(fig, width="stretch", key=f"overview-tag-chart-{cat}") grid_rows2 = st.columns(2) for i, cat in enumerate(["instrument", "vocal"]): with grid_rows2[i]: fig = catalog_top_tags_chart( tracks, category=cat, title=_CATEGORY_TITLES[cat] ) st.plotly_chart(fig, width="stretch", key=f"overview-tag-chart-{cat}") st.markdown("---") # ----------------------------------------------------------------- # Lyrics summary card # ----------------------------------------------------------------- st.subheader("Lyrics layer") lyrics_cols = st.columns([1, 1, 2]) empty_count = len(trans.get("empty_lyrics_track_ids", []) or []) halluc_count = len(trans.get("hallucination_truncations", []) or []) lyrics_cols[0].markdown( _stat_cell( "Tracks with lyrics", str(n_with_lyrics), f"{empty_count} confirmed empty" ), unsafe_allow_html=True, ) lyrics_cols[1].markdown( _stat_cell( "Hallucination truncations", str(halluc_count), "post-strip safety net", ), unsafe_allow_html=True, ) with lyrics_cols[2]: fig = language_distribution_chart( trans.get("language_distribution", {}) or {}, title="Detected language (Whisper)", ) st.plotly_chart( fig, width="stretch", key="overview-language-distribution" )