File size: 1,716 Bytes
6c46ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import pandas as pd
from huggingface_hub.hf_api import SpaceInfo

class BlocksSpace:
    def __init__(self, url, name):
        self.url = url
        self.name = name

    def fetch_spaces(self):
        r = requests.get(self.url)
        d = r.json()
        spaces = [SpaceInfo(**x) for x in d]
        blocks_spaces = {}
        for i in range(0,len(spaces)):
            if spaces[i].id.split('/')[0] == self.name and hasattr(spaces[i], 'likes') and spaces[i].id != f'{self.name}/Leaderboard' and spaces[i].id != f'{self.name}/README':
                blocks_spaces[spaces[i].id]=spaces[i].likes
        return blocks_spaces

    def get_spaces_dataframe(self):
        blocks_spaces = self.fetch_spaces()
        df = pd.DataFrame(
            [{"Spaces_Name": Spaces, "likes": likes} for Spaces,likes in blocks_spaces.items()])
        df = df.sort_values(by=['likes'],ascending=False)
        return df

block = gr.Blocks()
my_blocks_space = BlocksSpace("https://huggingface.co/api/spaces", "Gradio-Blocks")

with block:    
    gr.Markdown("""Leaderboard for the most popular Blocks Event Spaces. To learn more and join, see Blocks Party Event""")
    with gr.Tabs():
        with gr.TabItem("Blocks Party Leaderboard"):
            with gr.Row():
                data = gr.outputs.Dataframe(type="pandas")
            with gr.Row():
                data_run = gr.Button("Refresh")
                data_run.click(my_blocks_space.get_spaces_dataframe, inputs=None, outputs=data)
    # running the function on page load in addition to when the button is clicked
    block.load(my_blocks_space.get_spaces_dataframe, inputs=None, outputs=data)               

block.launch()