Spaces:
Running
Running
Download dashboard/ui.py from sayakpaul/diffusers-dashboard: direct link, hf CLI and curl.
- Browser
- Download file 19.6 kB
-
https://huggingface.co/spaces/sayakpaul/diffusers-dashboard/resolve/main/dashboard/ui.py
- Command line
-
hf download hf://spaces/sayakpaul/diffusers-dashboard/dashboard/ui.py
-
curl -L -o ui.py https://huggingface.co/spaces/sayakpaul/diffusers-dashboard/resolve/main/dashboard/ui.py
19.6 kB
| """Diffusers views in the LeRobot Project Pulse visual style.""" | |
| from __future__ import annotations | |
| from datetime import datetime, timedelta | |
| from html import escape | |
| from pathlib import Path | |
| import gradio as gr | |
| import pandas as pd | |
| from dashboard.charts import ( | |
| area_chart, | |
| contributor_growth_chart, | |
| country_bubble_chart, | |
| multiple_history_chart, | |
| organization_adoption_chart, | |
| pypi_chart, | |
| star_history_chart, | |
| task_mix_chart, | |
| top_contributors_chart, | |
| top_organizations_chart, | |
| ) | |
| from dashboard.collect import load_snapshot | |
| CSS = Path(__file__).with_name("reference_style.css").read_text(encoding="utf-8") + """ | |
| .metric-delta {font-size:.95rem; margin-top:2px; font-weight:650;} | |
| .metric-delta.positive {color:#16a34a;} .metric-delta.negative {color:#dc2626;} | |
| .metric-delta.neutral {color:#6b7280;} | |
| .publisher-directory a {color:var(--viz-2); text-decoration:none;} | |
| .publisher-directory table {width:100%; border-collapse:collapse;} | |
| .publisher-directory td,.publisher-directory th {padding:10px;border-bottom:1px solid var(--border-color-primary);text-align:left;} | |
| @media(max-width:760px){.hero-copy{display:block}.freshness-pill{margin-top:18px;width:fit-content}} | |
| """ | |
| def fmt(value: int | None) -> str: | |
| return f"{value:,}" if value is not None else "Unavailable" | |
| def data(snapshot: dict, name: str) -> dict: | |
| return snapshot.get("sources", {}).get(name, {}).get("data") or {} | |
| def header(snapshot: dict) -> str: | |
| timestamp = snapshot.get("collected_at") | |
| try: | |
| label = datetime.fromisoformat(timestamp).strftime("Snapshot · %d %b %Y, %H:%M UTC") if timestamp else "No snapshot yet" | |
| except ValueError: | |
| label = f"Snapshot · {timestamp}" | |
| return ( | |
| '<section class="dashboard-hero"><div class="hero-eyebrow">OPEN-SOURCE IMAGE GENERATION ADOPTION</div>' | |
| '<div class="hero-copy"><div><h1>Diffusers Project Pulse</h1>' | |
| '<p>Signals from the Hub, GitHub, PyPI and the community—collected into one shared view.</p>' | |
| '</div><div class="freshness-pill"><span></span>' + escape(label) + "</div></div></section>" | |
| ) | |
| def metric_card(label: str, value: int | None, delta: str = "", detail: str = "", window: str = "past 3 months") -> str: | |
| if delta in ("+0.0%", "-0.0%"): | |
| delta = "no change" | |
| trend_class = "positive" if delta.startswith("+") else "negative" if delta.startswith("-") else "neutral" | |
| arrow = "▲" if trend_class == "positive" else "▼" if trend_class == "negative" else "" | |
| trend = (f'<div class="metric-delta {trend_class}">{arrow} {escape(delta)}</div>' | |
| f'<div class="metric-trend-label">{escape(window)}</div>') if delta else ( | |
| '<div class="metric-trend-label">Trend begins with the next snapshot</div>') | |
| return (f'<div class="metric-card" title="{escape(detail, quote=True)}">' | |
| f'<div class="metric-label">{escape(label)}</div><div class="metric-value">{fmt(value)}</div>{trend}</div>') | |
| def card_row(*cards: str) -> str: | |
| return '<div class="metric-row">' + "".join(cards) + "</div>" | |
| def source_health(snapshot: dict) -> str: | |
| if not snapshot: | |
| return '<div class="callout">Source status is available after the first scheduled snapshot.</div>' | |
| rows = [] | |
| for name, title in (("models", "Hugging Face Hub models"), ("github", "GitHub"), | |
| ("pypi", "PyPI Stats"), ("organizations", "Curated organizations")): | |
| item = snapshot.get("sources", {}).get(name, {}) | |
| status = item.get("status", "unavailable") | |
| label = "Available" if status == "ok" else "Previous snapshot" if status == "stale" else "Unavailable" | |
| detail = item.get("updated_at") or item.get("error") or "No collection yet" | |
| rows.append('<div class="source-row"><div><strong>' + escape(title) + '</strong><small>' | |
| + escape(str(detail)) + '</small></div><span class="source-badge source-' | |
| + escape(status) + '">' + label + "</span></div>") | |
| return '<div class="source-panel">' + "".join(rows) + "</div>" | |
| def history_change(history: list[dict], key: str, days: int = 90) -> tuple[str, str]: | |
| points = [row for row in history if row.get(key) is not None and row.get("date")] | |
| if len(points) < 2: | |
| return "", "" | |
| latest = points[-1] | |
| latest_day = datetime.fromisoformat(latest["date"]) | |
| older = [row for row in points[:-1] if (latest_day - datetime.fromisoformat(row["date"])).days >= days] | |
| past = older[-1] if older else points[0] | |
| span = (latest_day - datetime.fromisoformat(past["date"])).days | |
| if span <= 0: | |
| return "", "" | |
| previous = past[key] | |
| if previous == 0: | |
| return ("New", f"past {span} days") if latest[key] else ("", "") | |
| label = "past 3 months" if 85 <= span <= 100 else f"past {span} days" | |
| return f"{(latest[key] - previous) / previous * 100:+.1f}%", label | |
| def pypi_change(history: list[dict], width: int, offset: int = 90) -> tuple[str, str]: | |
| """Compare equal-width daily download windows 90 days apart.""" | |
| if not history: | |
| return "", "" | |
| rows = {row["date"]: row["downloads"] for row in history} | |
| end = datetime.fromisoformat(history[-1]["date"]) | |
| current_days = [(end - timedelta(days=index)).date().isoformat() for index in range(width)] | |
| past_days = [(end - timedelta(days=offset + index)).date().isoformat() for index in range(width)] | |
| if any(day not in rows for day in current_days + past_days): | |
| return "", "" | |
| current, past = (sum(rows[day] for day in days) for days in (current_days, past_days)) | |
| if past <= 0: | |
| return ("New", "past 3 months") if current else ("", "") | |
| return f"{(current - past) / past * 100:+.1f}%", "past 3 months" | |
| def activity_change(activity: list[dict]) -> tuple[str, str]: | |
| if len(activity) < 2: | |
| return "", "" | |
| current = activity[-1]["active_contributors"] | |
| past = activity[-91]["active_contributors"] if len(activity) >= 91 else activity[0]["active_contributors"] | |
| if past <= 0: | |
| return ("New", "past 3 months") if current else ("", "") | |
| return f"{(current - past) / past * 100:+.1f}%", "past 3 months" | |
| def table(records: list[dict], columns: list[str]) -> pd.DataFrame: | |
| return pd.DataFrame(records[:5]).reindex(columns=columns).fillna("") | |
| def contributor_table(records: list[dict], metric: str) -> pd.DataFrame: | |
| del metric # The supplied ranking determines row order; all reference columns remain visible. | |
| return pd.DataFrame(records).reindex(columns=["author", "total_downloads", "total_likes", "model_count"]).rename(columns={ | |
| "author": "Author", "total_downloads": "Total Downloads", "total_likes": "Total Likes", "model_count": "Model Count" | |
| }).fillna("") | |
| def notice(text: str) -> str: | |
| return '<div class="callout">' + escape(text) + "</div>" | |
| def build_app(snapshot: dict | None = None) -> gr.Blocks: | |
| snapshot = snapshot if snapshot is not None else load_snapshot() | |
| models, github, pypi, organizations = (data(snapshot, name) for name in ("models", "github", "pypi", "organizations")) | |
| history = snapshot.get("history", []) | |
| growth = models.get("growth_3m", {}) | |
| pypi_daily = pypi.get("daily_history", []) | |
| star_series = github.get("star_history") or history | |
| stars_delta, stars_window = history_change(star_series, "stars" if github.get("star_history") else "github_stars") | |
| contributor_activity = github.get("contributor_activity", []) | |
| if contributor_activity and "total_contributors" in contributor_activity[-1]: | |
| contributors_delta, contributors_window = history_change(contributor_activity, "total_contributors") | |
| else: | |
| contributors_delta, contributors_window = history_change(history, "github_contributors") | |
| active_delta, active_window = activity_change(contributor_activity) | |
| issues_delta, issues_window = history_change(history, "open_issues") | |
| prs_delta, prs_window = history_change(history, "open_prs") | |
| pypi_30_delta, pypi_30_window = pypi_change(pypi_daily, 30) | |
| pypi_7_delta, pypi_7_window = pypi_change(pypi_daily, 7) | |
| pypi_day_delta, pypi_day_window = pypi_change(pypi_daily, 1) | |
| cohort_window = "models published in the past 3 months" | |
| theme = gr.themes.Default( | |
| primary_hue=gr.themes.colors.orange, secondary_hue=gr.themes.colors.indigo, | |
| neutral_hue=gr.themes.colors.slate, | |
| font=[gr.themes.Font("Helvetica Neue"), gr.themes.Font("Helvetica"), gr.themes.Font("Arial")], | |
| ) | |
| with gr.Blocks(css=CSS, theme=theme, title="Diffusers Project Pulse") as demo: | |
| gr.HTML(header(snapshot)) | |
| if not snapshot: | |
| gr.HTML(notice("No generated snapshot was found. Run python -m scripts.refresh_snapshot, then restart the app.")) | |
| with gr.Tabs(): | |
| with gr.Tab("Overview"): | |
| gr.Markdown("## Ecosystem at a glance") | |
| gr.HTML(card_row( | |
| metric_card("Hub Models", models.get("count"), growth.get("models", ""), "Diffusers-tagged Hub repositories", cohort_window), | |
| metric_card("Hub Downloads", models.get("downloads_30d_sum"), growth.get("downloads", ""), "30-day downloads attributed to models published in the past 3 months", cohort_window), | |
| metric_card("Publishers", models.get("publisher_count"), growth.get("publishers", ""), "Distinct publishing accounts", cohort_window), | |
| metric_card("PyPI · 30 days", pypi.get("last_month"), pypi_30_delta, "Mirror-excluded downloads compared with the same window three months ago", pypi_30_window), | |
| )) | |
| with gr.Row(equal_height=True): | |
| with gr.Column(scale=2): | |
| gr.Plot(value=area_chart(models.get("growth", []), "models", "Published model growth · last 12 months"), show_label=False) | |
| with gr.Column(scale=1): | |
| gr.Plot(value=task_mix_chart(models.get("task_counts", {})), show_label=False) | |
| if models.get("truncated"): | |
| gr.HTML(notice("Hub collection reached its configured limit; model totals and rankings are partial.")) | |
| with gr.Accordion("Data source health", open=False): | |
| gr.HTML(source_health(snapshot)) | |
| gr.HTML('<div class="dashboard-footer"><p>Data is collected every six hours and shared by every visitor. ' | |
| 'Publication histories are precomputed; counter-only trends grow with scheduled snapshots.</p>' | |
| '<p>ℹ️ <em>Figures may be cached, estimated, or subject to API limitations, and may not reflect real-time values.</em></p></div>') | |
| with gr.Tab("Models"): | |
| gr.HTML(card_row( | |
| metric_card("Total Models", models.get("count"), growth.get("models", ""), "Diffusers-tagged model repositories", cohort_window), | |
| metric_card("Unique Publishers", models.get("publisher_count"), growth.get("publishers", ""), "Distinct Hub publishing accounts", cohort_window), | |
| metric_card("Total Downloads", models.get("downloads_30d_sum"), growth.get("downloads", ""), "Sum of last-30-day Hub model downloads", cohort_window), | |
| metric_card("Total Likes", models.get("likes_sum"), growth.get("likes", ""), "Likes on matching models", cohort_window), | |
| )) | |
| gr.Markdown("### 🏆 Top Models") | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("##### By Downloads 📥") | |
| gr.Dataframe(value=table(models.get("top_downloads", []), ["id", "author", "downloads", "likes", "last_modified"]), interactive=False, wrap=True) | |
| with gr.Column(): | |
| gr.Markdown("##### By Likes 👍") | |
| gr.Dataframe(value=table(models.get("top_likes", []), ["id", "author", "likes", "downloads", "last_modified"]), interactive=False, wrap=True) | |
| gr.Markdown("### 👥 Top Contributors") | |
| contributors = models.get("contributors", {}) | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("##### By Total Downloads 📥") | |
| gr.Dataframe(value=contributor_table(contributors.get("downloads", []), "total_downloads"), interactive=False, wrap=True) | |
| with gr.Column(): | |
| gr.Markdown("##### By Total Likes 👍") | |
| gr.Dataframe(value=contributor_table(contributors.get("likes", []), "total_likes"), interactive=False, wrap=True) | |
| gr.Markdown("##### By Model Count 📊") | |
| gr.Dataframe(value=contributor_table(contributors.get("count", []), "model_count"), interactive=False, wrap=True) | |
| gr.Markdown("### 📈 Growth Trends") | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Plot(value=area_chart(models.get("growth", []), "models", "Cumulative models"), show_label=False) | |
| gr.Plot(value=area_chart(models.get("growth", []), "downloads", "Downloads from published models", "#9085e9"), show_label=False) | |
| with gr.Column(): | |
| gr.Plot(value=area_chart(models.get("growth", []), "publishers", "Cumulative publishers", "#1baf7a"), show_label=False) | |
| gr.Plot(value=area_chart(models.get("growth", []), "likes", "Likes on published models", "#c98500"), show_label=False) | |
| gr.HTML('<div class="disclaimer">ℹ️ Publication charts group models by creation date; downloads and likes show current counters attributed to those models.</div>') | |
| with gr.Tab("GitHub & Community"): | |
| gr.Markdown("### 🚀 Project Momentum") | |
| gr.HTML(card_row( | |
| metric_card("GitHub Stars", github.get("stars"), stars_delta, "Compared with historical star counts", stars_window), | |
| metric_card("PyPI Downloads", pypi.get("last_month"), pypi_30_delta, "Last 30 days compared with the same window three months earlier", pypi_30_window), | |
| )) | |
| with gr.Row(): | |
| gr.Plot(value=star_history_chart(github.get("star_history", []), history), show_label=False) | |
| gr.Plot(value=pypi_chart(pypi_daily), show_label=False) | |
| if not github.get("star_history"): | |
| gr.HTML(notice("Historical star counts are temporarily unavailable; the chart grows from saved snapshots.")) | |
| gr.Markdown("### 🤝 Community Health") | |
| activity = contributor_activity | |
| gr.HTML(card_row( | |
| metric_card("Contributors", github.get("contributors"), contributors_delta, | |
| "Distinct commit authors when full history is available; otherwise GitHub-linked contributor accounts", contributors_window), | |
| metric_card("Active Contributors · 30d", activity[-1]["active_contributors"] if activity else None, | |
| active_delta, "Distinct commit authors in the rolling 30-day window", active_window), | |
| )) | |
| with gr.Row(): | |
| gr.Plot(value=contributor_growth_chart(activity, history), show_label=False) | |
| gr.Plot(value=top_contributors_chart(github.get("top_contributors", [])), show_label=False) | |
| gr.HTML(notice("Contributor growth counts distinct commit authors. The top-contributors ranking uses GitHub-linked accounts and may have a different total.")) | |
| gr.Markdown("### 🛠️ Development Activity") | |
| gr.HTML(card_row( | |
| metric_card("Open Issues", github.get("open_issues"), issues_delta, "Open GitHub issues", issues_window), | |
| metric_card("Open PRs", github.get("open_prs"), prs_delta, "Open GitHub pull requests", prs_window), | |
| )) | |
| gr.Plot(value=multiple_history_chart(history, [("open_issues", "Open Issues"), ("open_prs", "Open PRs")], "Open Issues & PRs"), show_label=False) | |
| gr.Markdown("### 📦 Package Adoption") | |
| gr.HTML(card_row( | |
| metric_card("Last 30 Days", pypi.get("last_month"), pypi_30_delta, "Mirror-excluded PyPI downloads", pypi_30_window), | |
| metric_card("Last 7 Days", pypi.get("last_week"), pypi_7_delta, "Mirror-excluded PyPI downloads", pypi_7_window), | |
| metric_card("Yesterday", pypi.get("last_day"), pypi_day_delta, "Most recent PyPI daily observation", pypi_day_window), | |
| )) | |
| with gr.Tab("Organizations"): | |
| gr.Markdown("## 🏭 Who is building on Diffusers") | |
| org_growth = organizations.get("growth_3m", {}) | |
| by_kind = organizations.get("by_kind", {}) | |
| gr.HTML(card_row( | |
| metric_card("Active Organizations", organizations.get("active"), org_growth.get("active", ""), | |
| "Curated organizations with Diffusers-tagged models", cohort_window), | |
| metric_card("Companies", by_kind.get("company"), org_growth.get("company", ""), | |
| "Commercial organizations in the curated list", cohort_window), | |
| metric_card("Labs & Universities", by_kind.get("lab", 0) + by_kind.get("university", 0) | |
| if organizations else None, org_growth.get("academic", ""), | |
| "Research institutes and universities in the curated list", cohort_window), | |
| metric_card("Organization Downloads", organizations.get("downloads"), org_growth.get("downloads", ""), | |
| "Current Hub downloads on models published by curated organizations", cohort_window), | |
| )) | |
| gr.Plot(value=organization_adoption_chart(organizations.get("adoption", [])), show_label=False) | |
| with gr.Row(): | |
| gr.Plot(value=top_organizations_chart(organizations.get("organizations", [])), show_label=False) | |
| gr.Plot(value=country_bubble_chart(organizations.get("countries", {})), show_label=False) | |
| gr.Markdown("### Directory") | |
| cards = "".join( | |
| '<div class="company-card"><span class="org-badge org-' + escape(org["kind"]) + '">' | |
| + escape({"company": "Company", "lab": "Research lab", "university": "University"}[org["kind"]]) | |
| + '</span><a href="https://huggingface.co/' + escape(org["account"], quote=True) | |
| + '" target="_blank" rel="noopener noreferrer">' + escape(org["name"]) + "</a>" | |
| + '<div class="company-stats">' + f'{org["model_count"]:,} models · {org["total_downloads"]:,} downloads' | |
| + '<br><span class="org-country">' + escape(org["country"]) + "</span></div></div>" | |
| for org in organizations.get("organizations", []) | |
| ) | |
| gr.HTML('<div class="company-grid">' + cards + "</div>" if cards else notice("No curated organization data is available yet.")) | |
| gr.HTML('<div class="curation-note">Organizations are curated by hand; membership was last reviewed on ' | |
| + escape(organizations.get("curated_at", "unknown")) | |
| + '. Counts refresh with the dashboard snapshot.</div>') | |
| return demo | |