File size: 2,756 Bytes
af674e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce67f77
af674e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce67f77
af674e3
 
 
 
 
 
 
 
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
# A simple script that loops over all public models and get their libary_name
import gradio as gr
import pandas as pd
import numpy as np

from collections import Counter

from huggingface_hub import HfApi
from datasets import load_dataset

api = HfApi()
list_models = api.list_models()

def fetch_dataset_and_init():
    dataset = load_dataset("librarian-bots/model_cards_with_metadata", split="train")
    library_names = dataset["library_name"]

    string_counts = Counter(library_names)
    string_counts_series = pd.Series(string_counts)

    # Sort the series in descending order
    df = string_counts_series.sort_values(ascending=False).to_frame()
    df.columns = ["count"]
    df = df.reset_index()
    df_log = df.copy()
    df_log['count'] = np.log(df_log['count'])

    return df, df_log

df, df_log = fetch_dataset_and_init()

def get_current_nb_models():
    # We need this hack since `list_models` returns a generator..
    total_models = sum(1 for _ in list_models)
    diff_models = total_models - df["count"].sum()
    return str(diff_models)

plot_height = 512
plot_width = 1512
top_k = len(df)

def bar_plot_fn(display, top_k):
    if display == "simple":
        return gr.BarPlot(
            df[:top_k],
            x="index",
            y="count",
            tooltip=["index", "count"],
            height=plot_height,
            width=plot_width
        )
    elif display == "log":
        return gr.BarPlot(
            df_log[:top_k],
            x="index",
            y="count",
            tooltip=["index", "count"],
            height=plot_height,
            width=plot_width
        )


with gr.Blocks() as bar_plot:
    with gr.Column():
        with gr.Column():
            display = gr.Dropdown(
                choices=[
                    "simple",
                    "log",
                ],
                value="simple",
                label="Type of Bar Plot",
            )
            top_k = gr.Slider(
                label="Select top-K most used library_name",
                value=len(df),
                minimum=1,
                maximum=len(df),
                step=1,
            )
        with gr.Column():
            plot = gr.BarPlot()

        with gr.Row():
            fetch_button = gr.Button(value="Fetch current number of models without model cards (takes up to 1min to fetch everything)")
            text_box = gr.Textbox(value="", label="Number of models without model cards")

    top_k.change(bar_plot_fn, inputs=[display, top_k], outputs=plot)
    display.change(bar_plot_fn, inputs=[display, top_k], outputs=plot)
    fetch_button.click(get_current_nb_models, outputs=[text_box])
    bar_plot.load(fn=bar_plot_fn, inputs=[display, top_k], outputs=plot)

bar_plot.launch()