File size: 2,309 Bytes
16b4fc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import list_models, list_datasets, list_spaces
import pandas as pd

def get_user_stats():
    users = {}
    for k, fn in zip(['model', 'dataset', 'space'], [list_models, list_datasets, list_spaces]):
        for repo in fn(full=True):
            if repo.author is None:
                continue

            if repo.author not in users:
                users[repo.author] = {
                    x: 0 for x in [
                        'model_likes',
                        'num_models',
                        'dataset_likes',
                        'num_datasets',
                        'space_likes',
                        'num_spaces',
                        'total_likes',
                        'total_repos'
                    ]
                }
            users[repo.author][f"{k}_likes"] += repo.likes
            users[repo.author][f"num_{k}s"] += 1
    
    for username, user_stats in users.items():
        users[username]['total_likes'] += sum([v for k, v in user_stats.items() if "likes" in k])
        users[username]['total_repos'] += sum([v for k, v in user_stats.items() if "num_" in k])

    for k, v in users.items():
        users[k] = dict(users[k])

    return users

def make_clickable_user(user_id):
    link = "https://huggingface.co/" + user_id
    return f'<a  target="_blank" href="{link}">{user_id}</a>'


def get_user_stats_df(limit=100):
    users = get_user_stats()
    df = pd.DataFrame([{'username': make_clickable_user(k), **v} for k, v in users.items()])
    df.sort_values(by=["total_likes"], ascending=False, inplace=True)
    df.insert(0, "rank", list(range(1, len(df) + 1)))
    if limit:
        df = df.head(limit)
    return df


with gr.Blocks() as demo:

    with gr.Row():
        data = gr.components.Dataframe(
            type="pandas",
            datatype=["number", "markdown", "number", "number", "number", "number", "number", "number", "number", "number"],
        )
    # block.load(get_submissions, inputs=gr.Variable("nature"), outputs=nature_data)
    with gr.Row():
        btn = gr.Button("Refresh")
        btn.click(
            get_user_stats_df, inputs=gr.Variable("stats"), outputs=data
        )

    demo.load(get_user_stats_df, inputs=gr.Variable("stats"), outputs=data)
demo.launch()