ranker / app.py
davanstrien's picture
davanstrien HF Staff
improve report
1c8f379
raw
history blame
7.64 kB
import gradio as gr
from huggingface_hub import list_spaces
from cachetools import TTLCache, cached
from toolz import groupby, valmap
# from diskcache import Cache
# import platform
# is_macos = platform.system() == "Darwin"
# if is_macos:
# cache = Cache("cache")
# def cached_decorator(func):
# return cache.memoize(typed=True, expire=1)(func)
# else:
# ttl_cache = TTLCache(maxsize=100, ttl=60 * 10)
# cached_decorator = cached(cache=ttl_cache)
# @cached_decorator
@cached(cache=TTLCache(maxsize=100, ttl=60 * 30))
def get_spaces():
return list(list_spaces(full=True))
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 please 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,
"value": my_dict[target_key],
"position": position + 1,
}
@cached(cache=TTLCache(maxsize=100, ttl=60 * 30))
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)
@cached(cache=TTLCache(maxsize=100, ttl=60 * 30))
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)
@cached(cache=TTLCache(maxsize=100, ttl=60 * 30))
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)
result = "## ⭐️ Space Likes Rankings ⭐️\n"
result += f"""Here are the rankings by likes for [`{space_or_org_id}`](https://huggingface.co/spaces/{space_or_org_id}) across all Spaces \n"""
result += f"""- You have {space_rank['value']:,} likes for this Space.\n"""
result += f"""- Your Space is ranked {space_rank['position']:,} out of {len(create_space_to_like_dict()):,} Spaces.\n"""
result += f"""- Space [{space_or_org_id}](https://huggingface.co/spaces/{space_or_org_id}) is ranked {space_rank['rank']:.2f}%\n"""
result += f"""- You have {space_rank['num_higher']:,} Spaces above and {space_rank['num_lower']:,} Spaces below in the raking of Space likes\n\n"""
result += """### ✨ Remember likes aren't everything!✨\n"""
result += """Some Spaces go very viral whilst other Spaces may be very useful for a smaller audience. If you think your Space is useful, please add it to this [thread](https://huggingface.co/spaces/librarian-bots/ranker/discussions/3) of awesome Spaces.
We'll look out for awesome Spaces added to this thread to promote more widely!"""
return result
if len(split) == 1:
org_rank = relative_rank_for_org(space_or_org_id, filter_zero=filter_zero)
result = "## ⭐️ Org/User Space Likes Rankings ⭐️\n"
result += "Here are the rankings for the org/user across all of their spaces \n"
result += f"""- You have {org_rank['value']:,} likes for this org/user.\n"""
result += f"""- Your org/user is ranked {org_rank['position']:,} out of {len(create_org_to_like_dict()):,} orgs/users.\n"""
result += f"""- You have {org_rank['num_higher']:,} orgs/users above and {org_rank['num_lower']:,} orgs/users below in the raking of Space likes \n\n"""
result += f"""- Organization or user [{space_or_org_id}](https://huggingface.co/{space_or_org_id}) is ranked in the top {org_rank['rank']:.2f}% \n\n"""
result += f"""You can find all your Spaces sorted by likes [here](https://huggingface.co/{space_or_org_id}?sort_spaces=likes#spaces)\n"""
result += """### ✨ Remember likes aren't everything!✨\n"""
result += """Some Spaces go very viral whilst other Spaces may be very useful for a smaller audience. If you think your Space is useful, please add it to this [thread](https://huggingface.co/spaces/librarian-bots/ranker/discussions/3) of awesome Spaces.
We'll look out for awesome Spaces added to this thread to promote more widely!"""
return result
def get_top_n_orgs_and_users(top_n=100):
orgs_to_likes = create_org_to_like_dict()
sorted_items = sorted(orgs_to_likes.items(), key=lambda item: item[1], reverse=True)
sorted_items = sorted_items[:top_n]
return sorted_items
def plot_top_n_orgs_and_users(top_n=100):
top_n = get_top_n_orgs_and_users(top_n)
return "".join(
f"\n{i+1}. [{org}](https://huggingface.co/{org}) with {likes:,} likes"
for i, (org, likes) in enumerate(top_n)
)
def get_top_n_spaces(top_n=100):
orgs_to_likes = create_space_to_like_dict()
sorted_items = sorted(orgs_to_likes.items(), key=lambda item: item[1], reverse=True)
sorted_items = sorted_items[:top_n]
return sorted_items
def plot_top_n_spaces(top_n=100):
top_n = get_top_n_spaces(top_n)
return "".join(
f"\n{i+1}. [{space}](https://huggingface.co/spaces/{space}) with"
f" {likes:,} likes"
for i, (space, likes) in enumerate(top_n)
)
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>"""
)
gr.Markdown(
"""## Rank Specific Spaces or Orgs
Provide this app with a Space ID or a Username/Organization name to rank by likes."""
)
with gr.Row():
space_id = gr.Textbox(
"librarian-bots", max_lines=1, label="Space or user/organization ID"
)
filter_zero = gr.Radio(
choices=["no", "yes"],
label="Filter out spaces with 0 likes in the ranking?",
value="yes",
)
run_btn = gr.Button("Show ranking for this Space org org/user!", label="Rank Space")
# gr.Markdown("### Results")
result = gr.Markdown()
run_btn.click(rank_space_and_org, inputs=[space_id, filter_zero], outputs=result)
gr.Markdown("## Leaderboard of Top 100 Spaces and Orgs/Users by Likes")
with gr.Row():
with gr.Accordion("Show rankings for Orgs and Users", open=False):
gr.Markdown("""## 🥇 Top 100 Orgs and Users by Likes 🥇""")
ranking_board = gr.Markdown(plot_top_n_orgs_and_users())
with gr.Accordion("Show rankings for Spaces", open=False):
gr.Markdown("""## 🏅 Top 100 Spaces by Likes 🏅""")
ranking_board = gr.Markdown(plot_top_n_spaces())
demo.queue(concurrency_count=4).launch()