File size: 21,377 Bytes
2811ff1
6054b77
ffc8ed8
 
6054b77
35ffa10
2811ff1
b233a23
4ad4863
 
 
 
 
 
 
 
 
 
 
 
b233a23
 
 
9eeefaa
 
b233a23
 
cede300
 
b233a23
6054b77
35ffa10
 
 
 
 
 
b233a23
cede300
 
 
 
 
 
 
 
 
 
 
4ad4863
 
 
3cceb68
4ad4863
35ffa10
4ad4863
b233a23
855952e
4ad4863
b233a23
 
 
 
 
 
 
 
 
 
 
4ad4863
 
b233a23
855952e
4ad4863
b233a23
35ffa10
b233a23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ad4863
 
 
b233a23
2811ff1
4ad4863
b233a23
 
 
 
 
 
ffc8ed8
 
4ad4863
 
ffc8ed8
 
 
 
 
 
4ad4863
 
 
 
 
 
b233a23
4ad4863
b233a23
2811ff1
4ad4863
35ffa10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe024d4
35ffa10
 
 
 
4ad4863
35ffa10
4ad4863
35ffa10
4ad4863
 
 
 
35ffa10
4ad4863
35ffa10
fe024d4
4ad4863
fe024d4
 
4ad4863
35ffa10
cede300
35ffa10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cede300
 
35ffa10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cede300
 
 
 
35ffa10
 
 
b233a23
 
cede300
 
 
b233a23
4ad4863
 
 
 
 
35ffa10
4ad4863
 
 
35ffa10
 
a8701cd
 
 
 
 
 
4ad4863
 
a8701cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ad4863
 
 
a8701cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ad4863
 
b233a23
2811ff1
cede300
f136ea6
3cceb68
f136ea6
 
65d3762
f136ea6
 
 
 
65d3762
 
f136ea6
 
 
35ffa10
f136ea6
4ad4863
35ffa10
4ad4863
35ffa10
4ad4863
cede300
 
 
 
 
 
 
4ad4863
b233a23
 
 
 
 
 
 
 
cede300
b233a23
 
 
cede300
b233a23
 
 
 
 
 
 
 
 
4ad4863
b233a23
 
 
 
cede300
b233a23
 
 
 
 
 
35ffa10
 
 
 
 
 
 
cede300
 
3cceb68
 
 
b233a23
 
 
 
 
 
35ffa10
b233a23
 
 
f136ea6
35ffa10
cede300
 
 
 
 
 
b233a23
4ad4863
f136ea6
b233a23
 
 
cede300
b233a23
 
 
cede300
b233a23
 
f136ea6
b233a23
 
cede300
b233a23
4ad4863
b233a23
ffc8ed8
cede300
fe024d4
 
 
f136ea6
b233a23
 
 
 
 
 
 
cede300
 
 
f136ea6
 
cede300
f136ea6
 
 
 
 
cede300
f136ea6
 
 
 
 
 
 
 
 
35ffa10
fb3bf5f
 
 
 
cede300
fb3bf5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35ffa10
f136ea6
 
 
 
cede300
 
 
 
 
 
 
 
 
 
 
 
 
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
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 (already filtered by time from DuckDB query)
        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)
    """

    # Group by and get top N
    top = (
        filtered_df.groupby(group_col)[["total_downloads", "percent_of_total"]]
        .sum()
        .nlargest(top_n, columns="total_downloads")
        .reset_index()
        .rename(
            columns={
                group_col: "Name",
                "total_downloads": "Total Value",
                "percent_of_total": "% of total",
            }
        )
    )

    # Create a downloadable version of the leaderboard
    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
    meta_cols = meta_cols_map.get(group_col, [])

    # Collect all metadata per top n for each category (country, author, model)
    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].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"))
            # Add downloads chip for country (only once)

        # 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
        for d in meta.get("total_downloads", []):
            formatted_downloads = format_large_number(d)
            chips.append(("⬇️", formatted_downloads, "downloads"))

        return chips

    # Function to create downloadable dataframe metadata
    def build_download_metadata(nm):
        meta = download_map.get(nm, {})
        download_info = {}

        for col in meta_cols:
            if col not in meta or not meta[col]:
                continue

            vals = meta.get(col, [])
            if vals:
                download_info[col] = ", ".join(str(v) for v in vals if pd.notna(v))
            else:
                download_info[col] = ""

        return download_info

    # Apply metadata builder to top dataframe
    top["Metadata"] = top["Name"].astype(object).apply(build_metadata)

    # Capitalize "user" back to "User" for display
    top["Name"] = top["Name"].replace("user", "User")

    # Build download dataframe with metadata
    download_info_list = [build_download_metadata(nm) for nm in download_top["Name"]]
    download_info_df = pd.DataFrame(download_info_list)
    download_top = pd.concat([download_top, download_info_df], axis=1)

    return top[["Name", "Metadata", "% of total"]], 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 top N entries with minimal data transfer

    Args:
        con: DuckDB connection object
        group_col: Column to group by
        top_n: Number of top entries
        time_filter: Optional tuple of (start_timestamp, end_timestamp)

    Returns:
        Pandas DataFrame with only the rows needed for top N
    """
    # Build time filter clause
    time_clause = ""
    if time_filter:
        start = pd.to_datetime(time_filter[0], unit="s")
        end = pd.to_datetime(time_filter[1], unit="s")
        time_clause = f"WHERE time >= '{start}' AND time <= '{end}'"

    # If grouping by country, group by the transformed country column
    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

    # When grouping by derived_author, lookup the country where derived_author = author
    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') THEN 'International/Online'
                    ELSE org_country_single
                END AS org_country_single,
                author,
                derived_author,
                merged_country_groups_single,
                merged_modality,
                downloads,
                model
            FROM {view}
            {time_clause}
        ),
        
        -- Create a lookup table for derived_author -> country
        author_country_lookup AS (
            SELECT DISTINCT
                author,
                FIRST_VALUE(org_country_single) OVER (PARTITION BY author ORDER BY downloads DESC) AS author_country
            FROM base_data
            WHERE author IS NOT NULL
        ),

        total_downloads_cte AS (
            SELECT SUM(downloads) AS total_downloads_all
            FROM base_data
        ),

        top_items AS (
            SELECT 
                b.group_key AS name,
                SUM(b.downloads) AS total_downloads,
                ROUND(SUM(b.downloads) * 100.0 / t.total_downloads_all, 2) AS percent_of_total,
                COALESCE(acl.author_country, ANY_VALUE(b.org_country_single)) AS org_country_single,
                ANY_VALUE(b.author) AS author,
                ANY_VALUE(b.derived_author) AS derived_author,
                ANY_VALUE(b.merged_country_groups_single) AS merged_country_groups_single,
                ANY_VALUE(b.merged_modality) AS merged_modality,
                ANY_VALUE(b.model) AS model
            FROM base_data b
            CROSS JOIN total_downloads_cte t
            LEFT JOIN author_country_lookup acl ON b.group_key = acl.author
            GROUP BY b.group_key, acl.author_country, t.total_downloads_all
        )

        SELECT *
        FROM top_items
        ORDER BY total_downloads DESC
        LIMIT {top_n};
        """
    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') THEN 'International/Online'
                    ELSE org_country_single
                END AS org_country_single,
                author,
                derived_author,
                merged_country_groups_single,
                merged_modality,
                downloads,
                model
            FROM {view}
            {time_clause}
        ),

        total_downloads_cte AS (
            SELECT SUM(downloads) AS total_downloads_all
            FROM base_data
        ),

        top_items AS (
            SELECT 
                b.group_key AS name,
                SUM(b.downloads) AS total_downloads,
                ROUND(SUM(b.downloads) * 100.0 / t.total_downloads_all, 2) AS percent_of_total,
                ANY_VALUE(b.org_country_single) AS org_country_single,
                ANY_VALUE(b.author) AS author,
                ANY_VALUE(b.derived_author) AS derived_author,
                ANY_VALUE(b.merged_country_groups_single) AS merged_country_groups_single,
                ANY_VALUE(b.merged_modality) AS merged_modality,
                ANY_VALUE(b.model) AS model
            FROM base_data b
            CROSS JOIN total_downloads_cte t
            GROUP BY b.group_key, t.total_downloads_all
        )

        SELECT *
        FROM top_items
        ORDER BY total_downloads DESC
        LIMIT {top_n};
        """

    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))