File size: 3,392 Bytes
703dffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439d0c0
703dffd
 
 
 
263c06c
703dffd
263c06c
 
 
 
 
 
703dffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83d0ce6
fa34e34
703dffd
 
83d0ce6
fa34e34
703dffd
 
 
 
 
 
 
 
5225c83
703dffd
 
 
 
 
 
 
 
b54cb8f
703dffd
00785bd
703dffd
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import list_spaces
from cachetools import TTLCache, cached
from toolz import groupby, valmap


@cached(cache=TTLCache(maxsize=100, ttl=60 * 10))
def get_spaces():
    return list(iter(list_spaces(full=True, limit=None)))


get_spaces()  # to warm up the cache


def create_space_to_like_dict():
    spaces = get_spaces()
    return {space.id: space.likes for space in spaces}


def create_org_to_like_dict():
    spaces = get_spaces()
    grouped = groupby(lambda x: x.author, spaces)
    return valmap(lambda x: sum(s.likes for s in x), grouped)


def relative_rank(my_dict, target_key, filter_zero=False):
    if filter_zero:
        my_dict = {k: v for k, v in my_dict.items() if v != 0}

    if target_key not in my_dict:
        raise gr.Error(f"'{target_key}' not found lease check the ID and try again.")

    sorted_items = sorted(my_dict.items(), key=lambda item: item[1], reverse=True)

    position = [key for key, _ in sorted_items].index(target_key)
    num_lower = len(sorted_items) - position - 1

    num_higher = position
    return {
        "rank": (num_higher + 1) / len(my_dict) * 100,
        "num_higher": num_higher,
        "num_lower": num_lower,
    }


def relative_rank_for_space(space_id, filter_zero=False):
    space_to_like_dict = create_space_to_like_dict()
    return relative_rank(space_to_like_dict, space_id, filter_zero=filter_zero)


def relative_rank_for_org(org_id, filter_zero=False):
    org_to_like_dict = create_org_to_like_dict()
    return relative_rank(org_to_like_dict, org_id, filter_zero=filter_zero)


def rank_space(space_id):
    return relative_rank_for_space(space_id)


def rank_space_and_org(space_or_org_id, filter_zero):
    filter_zero = filter_zero == "yes"
    split = space_or_org_id.split("/")
    if len(split) == 2:
        space_rank = relative_rank_for_space(space_or_org_id, filter_zero=filter_zero)
        return f"""Space {space_or_org_id} is ranked {space_rank['rank']:.2f}%
        with {space_rank['num_higher']:,} Spaces above and {space_rank['num_lower']:,} Spaces below in the raking."""
    if len(split) == 1:
        org_rank = relative_rank_for_org(space_or_org_id, filter_zero=filter_zero)
        return f"""Organization or user {space_or_org_id} is ranked {org_rank['rank']:.2f}%
            with {org_rank['num_higher']:,} orgs/users above and {org_rank['num_lower']:,} orgs/users below in the raking."""


with gr.Blocks() as demo:
    gr.HTML("<h1 style='text-align: center;'> &#127942; HuggyRanker &#127942; </h1>")
    gr.HTML(
        """<p style='text-align: center;'>Rank a single Space or all of the Spaces created by an organization or user by likes</p>"""
    )
    gr.HTML(
        """<p style="text-align: center;"><i>Remember likes aren't everything!</i></p></div>"""
    )
    gr.Markdown(
        """## Rank Spaces
    Provide this app with a Space ID or a Username/Organization name to rank by likes."""
    )
    with gr.Row():
        space_id = gr.Textbox(max_lines=1, label="Space ID")
        filter_zero = gr.Radio(
            choices=["no", "yes"],
            label="Filter out spaces with 0 likes in the ranking?",
            value="yes",
        )
    run_btn = gr.Button("Rank Space!", label="Rank Space")
    result = gr.Markdown()
    run_btn.click(rank_space_and_org, inputs=[space_id, filter_zero], outputs=result)

demo.launch()