File size: 3,644 Bytes
6499a7c
 
 
 
 
 
 
 
 
 
 
393e4b3
6499a7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
393e4b3
 
6499a7c
 
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
98
99
100
101
102
103
104
105
from bokeh.models import ColumnDataSource, FactorRange, HoverTool
from bokeh.plotting import figure
from bokeh.transform import dodge
from bokeh.io import output_notebook, show
from bokeh.palettes import Category20

from huggingface_hub import HfApi
from huggingface_hub import ModelSearchArguments, DatasetSearchArguments
from huggingface_hub import ModelFilter

import pandas as pd
import gradio as gr

api = HfApi()
filt = ModelFilter(library = "diffusers",)
diffusers_models = api.list_models(filter=filt, sort='downloads', direction=-1)
#len(diffusers_models)

diffusers_dict = {}
downloads, authors, modelids, likes = [], [], [], []
print(len(diffusers_models))
for data in diffusers_models:
  #print(data.downloads, data.author, data.modelId, data.likes)
  downloads.append(data.downloads)
  authors.append(data.author) 
  modelids.append(data.modelId) 
  likes.append(data.likes)

diffusers_dict['modelid'] = modelids
diffusers_dict['author'] = authors
diffusers_dict['download'] = downloads
diffusers_dict['likes'] = likes

diffusers_df = pd.DataFrame.from_dict(diffusers_dict)
diffusers_df = diffusers_df[(diffusers_df['download'] != 0) & (diffusers_df['likes'] != 0) ]
grouped = diffusers_df.groupby('author').sum().sort_values(by='download', ascending=False)

#getting data ready for bokeh plots
data_bokeh = grouped.sort_values('download', ascending=False).head(15)
data_bokeh.reset_index(inplace=True)
data_bokeh

#y - axis 1
authors = data_bokeh['author']

#x-axis
downloads = data_bokeh['download']

#y - axis 2
likes = data_bokeh['likes']

# create sample data
data = {'authors': authors, 
        'downloads': downloads, 
        'likes': likes} 

source = ColumnDataSource(data=data)

def display_df():
    df = grouped
    return df

def bokehplots():
    # set up figure
    p = figure(x_range=FactorRange(*authors), plot_height=350, plot_width=600, title='Downloads and Likes by Author')
    p.vbar(x=dodge('authors',-0.2, range=p.x_range), top='downloads', width=0.4, source=source,
          color=Category20[3][0], legend_label='Downloads')
    p.vbar(x=dodge('authors',0.2, range=p.x_range), top='likes', width=0.4, source=source,
          color=Category20[3][1], legend_label='Likes')
    p.xaxis.major_label_orientation = 45

    # set up y-axis for downloads
    p.yaxis.axis_label = 'Downloads'
    p.yaxis.axis_label_text_color = Category20[3][0]
    p.yaxis.major_label_text_color = Category20[3][0]

    # set up y-axis for likes
    p.extra_y_ranges = {'likes': Range1d(start=0, end=max(likes)+500)}
    p.add_layout(LinearAxis(y_range_name='likes', axis_label='Likes',
                            axis_label_text_color=Category20[3][1], major_label_text_color=Category20[3][1]), 'right')
    p.vbar(x=dodge('authors', 0.4, range=p.x_range), top='likes', width=0.5, source=source,
          color=Category20[3][1], legend_label='Likes', y_range_name='likes')

    # Create a HoverTool object and specify the information to display in the tooltip
    hover = HoverTool(tooltips=[('authors', '@authors'), ('downloads', '@downloads'), ('likes', '@likes')])
    # Add the HoverTool to the plot
    p.add_tools(hover)

    # remove grid lines
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_color = None

    # set legend location
    p.legend.location = 'top_right'

    return p


with gr.Blocks() as demo: 
    plot = gr.Plot()
    out_dataframe = gr.Dataframe(wrap=True, max_rows=10, overflow_row_behaviour= "paginate", datatype = ["str", "number", "number"], interactive=False)
    demo.load(bokehplots, outputs=[plot])
    demo.load(fn=display_df, outputs=[out_dataframe])  

demo.launch(debug=True)