ysharma HF Staff commited on
Commit
6499a7c
·
1 Parent(s): 01909f5

create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from bokeh.models import ColumnDataSource, FactorRange, HoverTool
2
+ from bokeh.plotting import figure
3
+ from bokeh.transform import dodge
4
+ from bokeh.io import output_notebook, show
5
+ from bokeh.palettes import Category20
6
+
7
+ from huggingface_hub import HfApi
8
+ from huggingface_hub import ModelSearchArguments, DatasetSearchArguments
9
+ from huggingface_hub import ModelFilter
10
+
11
+ import pandas as pd
12
+
13
+ api = HfApi()
14
+ filt = ModelFilter(library = "diffusers",)
15
+ diffusers_models = api.list_models(filter=filt, sort='downloads', direction=-1)
16
+ #len(diffusers_models)
17
+
18
+ diffusers_dict = {}
19
+ downloads, authors, modelids, likes = [], [], [], []
20
+ print(len(diffusers_models))
21
+ for data in diffusers_models:
22
+ #print(data.downloads, data.author, data.modelId, data.likes)
23
+ downloads.append(data.downloads)
24
+ authors.append(data.author)
25
+ modelids.append(data.modelId)
26
+ likes.append(data.likes)
27
+
28
+ diffusers_dict['modelid'] = modelids
29
+ diffusers_dict['author'] = authors
30
+ diffusers_dict['download'] = downloads
31
+ diffusers_dict['likes'] = likes
32
+
33
+ diffusers_df = pd.DataFrame.from_dict(diffusers_dict)
34
+ diffusers_df = diffusers_df[(diffusers_df['download'] != 0) & (diffusers_df['likes'] != 0) ]
35
+ grouped = diffusers_df.groupby('author').sum().sort_values(by='download', ascending=False)
36
+
37
+ #getting data ready for bokeh plots
38
+ data_bokeh = grouped.sort_values('download', ascending=False).head(15)
39
+ data_bokeh.reset_index(inplace=True)
40
+ data_bokeh
41
+
42
+ #y - axis 1
43
+ authors = data_bokeh['author']
44
+
45
+ #x-axis
46
+ downloads = data_bokeh['download']
47
+
48
+ #y - axis 2
49
+ likes = data_bokeh['likes']
50
+
51
+ # create sample data
52
+ data = {'authors': authors,
53
+ 'downloads': downloads,
54
+ 'likes': likes}
55
+
56
+ source = ColumnDataSource(data=data)
57
+
58
+ def display_df():
59
+ df = grouped
60
+ return df
61
+
62
+ def bokehplots():
63
+ # set up figure
64
+ p = figure(x_range=FactorRange(*authors), plot_height=350, plot_width=600, title='Downloads and Likes by Author')
65
+ p.vbar(x=dodge('authors',-0.2, range=p.x_range), top='downloads', width=0.4, source=source,
66
+ color=Category20[3][0], legend_label='Downloads')
67
+ p.vbar(x=dodge('authors',0.2, range=p.x_range), top='likes', width=0.4, source=source,
68
+ color=Category20[3][1], legend_label='Likes')
69
+ p.xaxis.major_label_orientation = 45
70
+
71
+ # set up y-axis for downloads
72
+ p.yaxis.axis_label = 'Downloads'
73
+ p.yaxis.axis_label_text_color = Category20[3][0]
74
+ p.yaxis.major_label_text_color = Category20[3][0]
75
+
76
+ # set up y-axis for likes
77
+ p.extra_y_ranges = {'likes': Range1d(start=0, end=max(likes)+500)}
78
+ p.add_layout(LinearAxis(y_range_name='likes', axis_label='Likes',
79
+ axis_label_text_color=Category20[3][1], major_label_text_color=Category20[3][1]), 'right')
80
+ p.vbar(x=dodge('authors', 0.4, range=p.x_range), top='likes', width=0.5, source=source,
81
+ color=Category20[3][1], legend_label='Likes', y_range_name='likes')
82
+
83
+ # Create a HoverTool object and specify the information to display in the tooltip
84
+ hover = HoverTool(tooltips=[('authors', '@authors'), ('downloads', '@downloads'), ('likes', '@likes')])
85
+ # Add the HoverTool to the plot
86
+ p.add_tools(hover)
87
+
88
+ # remove grid lines
89
+ p.xgrid.grid_line_color = None
90
+ p.ygrid.grid_line_color = None
91
+
92
+ # set legend location
93
+ p.legend.location = 'top_right'
94
+
95
+ return p
96
+
97
+
98
+ with gr.Blocks() as demo:
99
+ plot = gr.Plot()
100
+ out_dataframe = gr.Dataframe(wrap=True, max_rows=10, overflow_row_behaviour= "paginate", datatype = ["str", "number", "number"], interactive=False)
101
+ demo.load(bokehplots, inputs=[], outputs=[plot])
102
+ demo.load(fn=display_df, outputs=out_dataframe)
103
+
104
+ demo.launch(debug=True)