File size: 3,085 Bytes
0384ea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
__all__ = ['block', 'make_clickable_repo', 'get_submissions']

import gradio as gr
import pandas as pd
from huggingface_hub import HfApi

def make_clickable_repo(name, repo_type):
    if repo_type == "spaces":
        link = "https://huggingface.co/" + "spaces/" + name
    elif repo_type == "models":
        link = "https://huggingface.co/"  + name
    else:
        link = "https://huggingface.co/" + "datasets/" + name
    return f'<a target="_blank" href="{link}">{name.split("/")[-1]}</a>'

def get_repo_ids(repo_type):
    api = HfApi()
    if repo_type == "spaces":
        repos = api.list_spaces(author="somosnlp")
        repos = [s for s in repos if s.id not in ["somosnlp", "somosnlp/likes_leaderboard"]]
    elif repo_type == "models":
        repos = api.list_models(author="somosnlp", full=True)
    else:
        repos = api.list_datasets(author="somosnlp")
    return repos

def get_submissions(repo_type):
    submissions = get_repo_ids(repo_type)
    leaderboard = []

    for submission in submissions:
        leaderboard.append(
            (
                make_clickable_repo(submission.id, repo_type),
                submission.likes,
            )
        )

    df = pd.DataFrame(data=leaderboard, columns=["Repo", "Likes"])
    df.sort_values(by=["Likes"], ascending=False, inplace=True)
    df.insert(0, "Rank", list(range(1, len(df) + 1)))
    return df

block = gr.Blocks()

with block:
    gr.Markdown(
        """# Somos NLP ❤️ Leaderboard
    """
    )
    with gr.Tabs():
        with gr.TabItem("Spaces (ML apps)"):
            with gr.Row():
                spaces_data = gr.components.Dataframe(
                    type="pandas", datatype=["number", "markdown", "number"]
                )
            with gr.Row():
                data_run = gr.Button("Refresh")
                data_run.click(
                    get_submissions, inputs=gr.Variable("spaces"), outputs=spaces_data
                )
        with gr.TabItem("Models"):
            with gr.Row():
                models_data = gr.components.Dataframe(
                    type="pandas", datatype=["number", "markdown", "number"]
                )
            with gr.Row():
                data_run = gr.Button("Refresh")
                data_run.click(
                    get_submissions, inputs=gr.Variable("models"), outputs=models_data
                )
        with gr.TabItem("Datasets"):
            with gr.Row():
                datasets_data = gr.components.Dataframe(
                    type="pandas", datatype=["number", "markdown", "number"]
                )
            with gr.Row():
                data_run = gr.Button("Refresh")
                data_run.click(
                    get_submissions, inputs=gr.Variable("datasets"), outputs=datasets_data
                )


    block.load(get_submissions, inputs=gr.Variable("spaces"), outputs=spaces_data)
    block.load(get_submissions, inputs=gr.Variable("models"), outputs=models_data)
    block.load(get_submissions, inputs=gr.Variable("datasets"), outputs=datasets_data)


block.launch(debug=True)