Spaces:
Runtime error
Runtime error
| import pandas as pd | |
| from dash import html, dcc | |
| from dash_iconify import DashIconify | |
| import dash_mantine_components as dmc | |
| import base64 | |
| import countryflag | |
| button_style = { | |
| "display": "inline-block", | |
| "marginBottom": "10px", | |
| "marginRight": "15px", | |
| "marginTop": "30px", | |
| "padding": "6px 16px", | |
| "backgroundColor": "#082030", | |
| "color": "white", | |
| "borderRadius": "6px", | |
| "textDecoration": "none", | |
| "fontWeight": "bold", | |
| "fontSize": "14px", | |
| } | |
| company_icon_map = { | |
| "google": "../assets/icons/google.png", | |
| "distilbert": "../assets/images/hf.svg", | |
| "sentence-transformers": "../assets/images/hf.svg", | |
| "facebook": "../assets/icons/meta.png", | |
| "openai": "../assets/icons/openai.png", | |
| "amazon": "../assets/icons/amazon.png", | |
| "microsoft": "../assets/icons/microsoft.png", | |
| } | |
| country_emoji_fallback = { | |
| "User": "👤", | |
| "Organization": "🏢", | |
| "Model": "📦", | |
| } | |
| meta_cols_map = { | |
| "org_country_single": ["org_country_single", "total_downloads"], | |
| "author": [ | |
| "org_country_single", | |
| "author", | |
| "total_downloads", | |
| ], | |
| "derived_author": [ | |
| "org_country_single", | |
| "derived_author", | |
| "total_downloads", | |
| ], | |
| "model": [ | |
| "org_country_single", | |
| "author", | |
| "derived_author", | |
| "merged_modality", | |
| "total_downloads", | |
| ], | |
| } | |
| # Chip renderer | |
| def chip(text, bg_color="#F0F0F0"): | |
| return html.Span( | |
| text, | |
| style={ | |
| "backgroundColor": bg_color, | |
| "padding": "4px 10px", | |
| "borderRadius": "12px", | |
| "margin": "2px", | |
| "display": "inline-flex", | |
| "alignItems": "center", | |
| "fontSize": "14px", | |
| }, | |
| ) | |
| # Progress bar for % of total | |
| def progress_bar(percent, bar_color="#AC482A"): | |
| return html.Div( | |
| style={ | |
| "position": "relative", | |
| "backgroundColor": "#E0E0E0", | |
| "borderRadius": "8px", | |
| "height": "20px", | |
| "width": "100%", | |
| "overflow": "hidden", | |
| }, | |
| children=[ | |
| html.Div( | |
| style={ | |
| "backgroundColor": bar_color, | |
| "width": f"{percent}%", | |
| "height": "100%", | |
| "borderRadius": "8px", | |
| "transition": "width 0.5s", | |
| } | |
| ), | |
| html.Div( | |
| f"{percent:.1f}%", | |
| style={ | |
| "position": "absolute", | |
| "top": 0, | |
| "left": "50%", | |
| "transform": "translateX(-50%)", | |
| "color": "black", | |
| "fontWeight": "bold", | |
| "fontSize": "12px", | |
| "lineHeight": "20px", | |
| "textAlign": "center", | |
| }, | |
| ), | |
| ], | |
| ) | |
| # Helper to convert DataFrame to CSV and encode for download | |
| def df_to_download_link(df, filename): | |
| csv_string = df.to_csv(index=False) | |
| b64 = base64.b64encode(csv_string.encode()).decode() | |
| return html.Div( | |
| html.A( | |
| children=dmc.ActionIcon( | |
| DashIconify(icon="mdi:download", width=24), | |
| size="lg", | |
| color="#082030", | |
| ), | |
| id=f"download-{filename}", | |
| download=f"{filename}.csv", | |
| href=f"data:text/csv;base64,{b64}", | |
| target="_blank", | |
| title="Download CSV", | |
| style={ | |
| "padding": "6px 12px", | |
| "display": "inline-flex", | |
| "alignItems": "center", | |
| "justifyContent": "center", | |
| }, | |
| ), | |
| style={"textAlign": "right"}, | |
| ) | |
| # Helper to get popover content for each metadata type | |
| def get_metadata_popover_content(icon, name, meta_type): | |
| popover_texts = { | |
| "country": f"Country: {name}", | |
| "author": f"Author/Organization: {name}", | |
| "downloads": f"Total downloads: {name}", | |
| "modality": f"Modality: {name}", | |
| } | |
| return popover_texts.get(meta_type, name) | |
| # Chip renderer with hovercard | |
| def chip_with_hovercard(text, bg_color="#F0F0F0", meta_type=None, icon=None): | |
| hovercard_content = get_metadata_popover_content(icon, text, meta_type) | |
| return dmc.HoverCard( | |
| width="auto", | |
| shadow="md", | |
| position="top", | |
| children=[ | |
| dmc.HoverCardTarget( | |
| html.Span( | |
| text, | |
| style={ | |
| "backgroundColor": bg_color, | |
| "padding": "4px 10px", | |
| "borderRadius": "12px", | |
| "margin": "2px", | |
| "display": "inline-flex", | |
| "alignItems": "center", | |
| "fontSize": "14px", | |
| "cursor": "pointer", | |
| "transition": "background-color 0.15s", | |
| }, | |
| # Add a class for hover effect | |
| className="chip-hover-darken" | |
| ) | |
| ), | |
| dmc.HoverCardDropdown(dmc.Text(hovercard_content, size="sm")), | |
| ], | |
| ) | |
| # Render multiple chips in one row, each with popover | |
| def render_chips(metadata_list, chip_color): | |
| chips = [] | |
| for icon, name, meta_type in metadata_list: | |
| if isinstance(icon, str) and icon.endswith((".png", ".jpg", ".jpeg", ".svg")): | |
| chips.append( | |
| dmc.HoverCard( | |
| width=220, | |
| shadow="md", | |
| position="top", | |
| children=[ | |
| dmc.HoverCardTarget( | |
| html.Span( | |
| [ | |
| html.Img( | |
| src=icon, | |
| style={"height": "18px", "marginRight": "6px"}, | |
| ), | |
| name, | |
| ], | |
| style={ | |
| "backgroundColor": chip_color, | |
| "padding": "4px 10px", | |
| "borderRadius": "12px", | |
| "margin": "2px", | |
| "display": "inline-flex", | |
| "alignItems": "left", | |
| "fontSize": "14px", | |
| "cursor": "pointer", | |
| }, | |
| ) | |
| ), | |
| dmc.HoverCardDropdown( | |
| dmc.Text( | |
| get_metadata_popover_content(icon, name, meta_type), | |
| size="sm", | |
| ) | |
| ), | |
| ], | |
| ) | |
| ) | |
| else: | |
| chips.append( | |
| chip_with_hovercard(f"{icon} {name}", chip_color, meta_type, icon) | |
| ) | |
| return html.Div( | |
| chips, style={"display": "flex", "flexWrap": "wrap", "justifyContent": "left"} | |
| ) | |
| def render_table_content( | |
| df, download_df, chip_color, bar_color="#AC482A", filename="data" | |
| ): | |
| return html.Div( | |
| [ | |
| # Add download button above the table | |
| df_to_download_link(download_df, filename), | |
| # Wrap the table in a horizontal scroll container so the table can be wide | |
| html.Div( | |
| # scroll wrapper | |
| html.Table( | |
| [ | |
| html.Thead( | |
| html.Tr( | |
| [ | |
| html.Th( | |
| "Rank", | |
| className="rank-col", | |
| style={ | |
| "backgroundColor": "#F0F0F0", | |
| "textAlign": "left", | |
| }, | |
| ), | |
| html.Th( | |
| "Name", | |
| className="name-col", | |
| style={ | |
| "backgroundColor": "#F0F0F0", | |
| "textAlign": "left", | |
| }, | |
| ), | |
| html.Th( | |
| "Metadata", | |
| className="metadata-col", | |
| style={ | |
| "backgroundColor": "#F0F0F0", | |
| "textAlign": "left", | |
| "marginRight": "10px", | |
| }, | |
| ), | |
| html.Th( | |
| "% of Total", | |
| className="percent-col", | |
| style={ | |
| "backgroundColor": "#F0F0F0", | |
| "textAlign": "left", | |
| }, | |
| ), | |
| ] | |
| ) | |
| ), | |
| html.Tbody( | |
| [ | |
| html.Tr( | |
| [ | |
| html.Td(idx + 1, style={"textAlign": "center"}), | |
| html.Td(row["Name"], className="name-cell", style={"textAlign": "left"}), | |
| html.Td(render_chips(row["Metadata"], chip_color), className="metadata-cell", style={"textAlign": "left", "whiteSpace": "normal", "wordBreak": "break-word"}), | |
| html.Td( | |
| progress_bar(row["% of total"], bar_color), | |
| className="percent-cell", | |
| style={"textAlign": "center", "minWidth": "180px", "padding": "8px"}, | |
| ), | |
| ] | |
| ) | |
| for idx, row in df.iterrows() | |
| ] | |
| ), | |
| ], | |
| # allow the table to be wider than its container (minWidth prevents squish) | |
| style={"borderCollapse": "collapse", "width": "100%", "minWidth": "980px", "tableLayout": "auto"}, | |
| className="leaderboard-table", | |
| ), | |
| className="leaderboard-scroll-wrapper", | |
| style={"overflowX": "auto", "-webkit-overflow-scrolling": "touch", "width": "100%"}, | |
| ), | |
| ] | |
| ) | |
| # Function to get top N leaderboard (now accepts pandas DataFrame from DuckDB query) | |
| def get_top_n_leaderboard(filtered_df, group_col, top_n=10, derived_author_toggle=True): | |
| """ | |
| Get top N entries for a leaderboard | |
| Args: | |
| filtered_df: Pandas DataFrame of model-level rows. Must contain: | |
| - group_col (the grouping key) | |
| - total_downloads (per-model downloads for the requested window) | |
| - plus metadata columns: org_country_single, author, derived_author, merged_country_groups_single, merged_modality, model | |
| group_col: Column to group by | |
| top_n: Number of top entries to return | |
| derived_author_toggle: If True, attribute to model uploader (derived_author); if False, attribute to original model creator (author) | |
| Returns: | |
| tuple: (display_df, download_df) | |
| display_df: DataFrame with columns ["Name","Metadata","% of total"] for rendering | |
| download_df: DataFrame suitable for CSV download with numeric totals and metadata columns | |
| """ | |
| if filtered_df is None or filtered_df.empty: | |
| return pd.DataFrame(), pd.DataFrame() | |
| # Ensure numeric total_downloads | |
| if "total_downloads" not in filtered_df.columns: | |
| # fallback if older code still returned 'downloads' (unlikely) | |
| if "downloads" in filtered_df.columns: | |
| filtered_df["total_downloads"] = filtered_df["downloads"] | |
| else: | |
| filtered_df["total_downloads"] = 0 | |
| # Compute overall total across all models in this filtered set | |
| total_all = filtered_df["total_downloads"].sum() | |
| if total_all == 0: | |
| return pd.DataFrame(), pd.DataFrame() | |
| # Sum per group (group_col) to get group totals | |
| grouped = ( | |
| filtered_df.groupby(group_col)["total_downloads"] | |
| .sum() | |
| .reset_index() | |
| .rename(columns={group_col: "Name", "total_downloads": "Total Value"}) | |
| ) | |
| # Pick top N groups by summed downloads | |
| top = grouped.nlargest(top_n, columns="Total Value").reset_index(drop=True) | |
| # Compute percent of total for display (rounded) | |
| top["% of total"] = top["Total Value"].apply(lambda v: round(v * 100.0 / total_all, 2)) | |
| # Build download version (numeric) | |
| download_top = top.copy() | |
| download_top["Total Value"] = download_top["Total Value"].astype(int) | |
| download_top["% of total"] = download_top["% of total"].round(2) | |
| # All relevant metadata columns for the grouping | |
| meta_cols = meta_cols_map.get(group_col, []) | |
| # Collect metadata per group by inspecting the underlying model-level rows | |
| meta_map = {} | |
| download_map = {} | |
| for name in top["Name"]: | |
| name_data = filtered_df[filtered_df[group_col] == name] | |
| meta_map[name] = {} | |
| download_map[name] = {} | |
| for col in meta_cols: | |
| if col in name_data.columns: | |
| unique_vals = name_data[col].dropna().unique() | |
| meta_map[name][col] = list(unique_vals) | |
| download_map[name][col] = list(unique_vals) | |
| # Function to build metadata chips | |
| def build_metadata(nm): | |
| meta = meta_map.get(nm, {}) | |
| chips = [] | |
| # Countries | |
| for c in meta.get("org_country_single", []): | |
| if c == "United States of America": | |
| c = "USA" | |
| if c == "user": | |
| c = "User" | |
| try: | |
| flag_emoji = countryflag.getflag(c) | |
| if not flag_emoji or flag_emoji == c: | |
| flag_emoji = country_emoji_fallback.get(c, "🌍") | |
| except Exception: | |
| flag_emoji = country_emoji_fallback.get(c, "🌍") | |
| chips.append((flag_emoji, c, "country")) | |
| # Author - use derived_author_toggle to determine which column | |
| author_key = "derived_author" if derived_author_toggle else "author" | |
| for a in meta.get(author_key, []): | |
| icon = company_icon_map.get(a, "") | |
| if icon == "": | |
| if meta.get("merged_country_groups_single", ["User"])[0] != "User": | |
| icon = "🏢" | |
| else: | |
| icon = "👤" | |
| chips.append((icon, a, "author")) | |
| # Modality | |
| for m in meta.get("merged_modality", []): | |
| if pd.notna(m): | |
| chips.append(("", m, "modality")) | |
| # Total downloads (aggregate numeric value for this group) | |
| # Use the summed value from top (we can retrieve it) | |
| # but we also include any per-model totals if desired - keep simple: use group total | |
| group_total = int(top.loc[top["Name"] == nm, "Total Value"].iloc[0]) if nm in top["Name"].values else None | |
| if group_total is not None: | |
| formatted_downloads = format_large_number(group_total) | |
| chips.append(("⬇️", formatted_downloads, "downloads")) | |
| return chips | |
| # Attach Metadata column for display DataFrame | |
| display_df = top.rename(columns={"Total Value": "total_downloads"}) | |
| display_df["Metadata"] = display_df["Name"].astype(object).apply(build_metadata) | |
| # Format display_df columns for render_table_content | |
| display_df_formatted = display_df.rename(columns={"% of total": "% of total"}) | |
| # Keep only necessary columns in expected order | |
| display_for_render = display_df_formatted[["Name", "Metadata", "% of total"]] | |
| # Build download dataframe with metadata for CSV | |
| download_info_list = [] | |
| for nm in download_top["Name"]: | |
| info = {} | |
| meta = download_map.get(nm, {}) | |
| for col in meta_cols: | |
| if col in meta and meta[col]: | |
| info[col] = ", ".join(str(v) for v in meta[col] if pd.notna(v)) | |
| else: | |
| info[col] = "" | |
| # attach totals | |
| info["Total Value"] = int(download_top.loc[download_top["Name"] == nm, "Total Value"].iloc[0]) | |
| info["% of total"] = float(download_top.loc[download_top["Name"] == nm, "% of total"].iloc[0]) | |
| download_info_list.append(info) | |
| download_info_df = pd.DataFrame(download_info_list) | |
| download_top = pd.concat([download_top.reset_index(drop=True), download_info_df.reset_index(drop=True)], axis=1) | |
| return display_for_render, download_top | |
| def get_top_n_from_duckdb( | |
| con, group_col, top_n=10, time_filter=None, view="all_downloads" | |
| ): | |
| """ | |
| Query DuckDB directly to get model-level rows with per-model total_downloads (delta or full) | |
| Returns rows similar to _get_filtered_top_n_from_duckdb in app.py. | |
| """ | |
| # Compute date window | |
| if time_filter and len(time_filter) == 2: | |
| start = pd.to_datetime(time_filter[0], unit="s") | |
| end = pd.to_datetime(time_filter[1], unit="s") | |
| else: | |
| start = pd.to_datetime("1970-01-01") | |
| # We cannot access end_dt here; rely on time_filter for end in typical use. | |
| end = pd.Timestamp.now() | |
| start_str = str(start) | |
| end_str = str(end) | |
| # If grouping by country, transform some country values | |
| if group_col == "org_country_single": | |
| group_expr = """CASE | |
| WHEN org_country_single IN ('HF', 'United States of America') THEN 'United States of America' | |
| WHEN org_country_single IN ('International', 'Online', 'Online?') THEN 'International/Online' | |
| ELSE org_country_single | |
| END""" | |
| else: | |
| group_expr = group_col | |
| # Derived author special-case | |
| if group_col == "derived_author": | |
| query = f""" | |
| WITH base_data AS ( | |
| SELECT | |
| {group_expr} AS group_key, | |
| CASE | |
| WHEN org_country_single IN ('HF', 'United States of America') THEN 'United States of America' | |
| WHEN org_country_single IN ('International', 'Online', 'Online?') THEN 'International/Online' | |
| ELSE org_country_single | |
| END AS org_country_single, | |
| author, | |
| derived_author, | |
| merged_country_groups_single, | |
| merged_modality, | |
| model, | |
| time, | |
| downloadsAllTime | |
| FROM {view} | |
| ), | |
| author_country_lookup AS ( | |
| SELECT DISTINCT | |
| author, | |
| FIRST_VALUE(org_country_single) OVER (PARTITION BY author ORDER BY downloadsAllTime DESC) AS author_country | |
| FROM base_data | |
| WHERE author IS NOT NULL | |
| ), | |
| model_metrics AS ( | |
| SELECT | |
| model, | |
| group_key, | |
| ANY_VALUE(org_country_single) AS org_country_single, | |
| ANY_VALUE(author) AS author, | |
| ANY_VALUE(derived_author) AS derived_author, | |
| ANY_VALUE(merged_country_groups_single) AS merged_country_groups_single, | |
| ANY_VALUE(merged_modality) AS merged_modality, | |
| COALESCE(MAX(CASE WHEN time <= '{end_str}' THEN downloadsAllTime END), 0) | |
| - COALESCE(MAX(CASE WHEN time < '{start_str}' THEN downloadsAllTime END), 0) | |
| AS total_downloads | |
| FROM base_data | |
| GROUP BY model, group_key | |
| ), | |
| total_downloads_cte AS ( | |
| SELECT SUM(total_downloads) AS total_downloads_all FROM model_metrics | |
| ) | |
| SELECT | |
| mm.model, | |
| mm.group_key, | |
| COALESCE(acl.author_country, mm.org_country_single) AS org_country_single, | |
| mm.author, | |
| mm.derived_author, | |
| mm.merged_country_groups_single, | |
| mm.merged_modality, | |
| mm.total_downloads, | |
| CASE WHEN td.total_downloads_all = 0 THEN 0 ELSE ROUND(mm.total_downloads * 100.0 / td.total_downloads_all, 2) END AS percent_of_total | |
| FROM model_metrics mm | |
| LEFT JOIN author_country_lookup acl ON mm.group_key = acl.author | |
| CROSS JOIN total_downloads_cte td | |
| WHERE mm.total_downloads > 0 | |
| ORDER BY mm.total_downloads DESC | |
| LIMIT {top_n * 10}; | |
| """ | |
| else: | |
| query = f""" | |
| WITH base_data AS ( | |
| SELECT | |
| {group_expr} AS group_key, | |
| CASE | |
| WHEN org_country_single IN ('HF', 'United States of America') THEN 'United States of America' | |
| WHEN org_country_single IN ('International', 'Online', 'Online?') THEN 'International/Online' | |
| ELSE org_country_single | |
| END AS org_country_single, | |
| author, | |
| derived_author, | |
| merged_country_groups_single, | |
| merged_modality, | |
| model, | |
| time, | |
| downloadsAllTime | |
| FROM {view} | |
| ), | |
| model_metrics AS ( | |
| SELECT | |
| model, | |
| group_key, | |
| ANY_VALUE(org_country_single) AS org_country_single, | |
| ANY_VALUE(author) AS author, | |
| ANY_VALUE(derived_author) AS derived_author, | |
| ANY_VALUE(merged_country_groups_single) AS merged_country_groups_single, | |
| ANY_VALUE(merged_modality) AS merged_modality, | |
| COALESCE(MAX(CASE WHEN time <= '{end_str}' THEN downloadsAllTime END), 0) | |
| - COALESCE(MAX(CASE WHEN time < '{start_str}' THEN downloadsAllTime END), 0) | |
| AS total_downloads | |
| FROM base_data | |
| GROUP BY model, group_key | |
| ), | |
| total_downloads_cte AS ( | |
| SELECT SUM(total_downloads) AS total_downloads_all FROM model_metrics | |
| ) | |
| SELECT | |
| mm.model, | |
| mm.group_key, | |
| mm.org_country_single, | |
| mm.author, | |
| mm.derived_author, | |
| mm.merged_country_groups_single, | |
| mm.merged_modality, | |
| mm.total_downloads, | |
| CASE WHEN td.total_downloads_all = 0 THEN 0 ELSE ROUND(mm.total_downloads * 100.0 / td.total_downloads_all, 2) END AS percent_of_total | |
| FROM model_metrics mm | |
| CROSS JOIN total_downloads_cte td | |
| WHERE mm.total_downloads > 0 | |
| ORDER BY mm.total_downloads DESC | |
| LIMIT {top_n * 10}; | |
| """ | |
| try: | |
| return con.execute(query).fetchdf() | |
| except Exception as e: | |
| print(f"Error querying DuckDB: {e}") | |
| return pd.DataFrame() | |
| def format_large_number(n): | |
| """Shorten large numbers, e.g. 5,000,000 -> '5 million'.""" | |
| if n >= 1_000_000_000: | |
| return f"{n / 1_000_000_000:.1f} billion" | |
| elif n >= 1_000_000: | |
| return f"{n / 1_000_000:.1f} million" | |
| elif n >= 1_000: | |
| return f"{n / 1_000:.1f}k" | |
| else: | |
| return str(int(n)) | |