lvwerra HF staff commited on
Commit
7c7fb6e
·
1 Parent(s): 0179f4f
Files changed (2) hide show
  1. app.py +96 -0
  2. html_template.html +24 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+ import pandas as pd
5
+ import json
6
+ from datasets import load_dataset
7
+ import requests
8
+ from huggingface_hub import list_datasets, list_models, list_spaces
9
+ from collections import Counter
10
+ import numpy as np
11
+
12
+
13
+ def compute_ranking(df, column, method="sum", keep="last"):
14
+ df_rank = df.groupby("author").aggregate({column: method})[[column]]
15
+ df_rank = df_rank.sort_values(by=column)
16
+ df_rank.reset_index(drop=True, inplace=True)
17
+ df_rank["top_perc"] = df_rank.apply(lambda x: f"{100 * (1-(x.name/len(df_rank))):.2f}", axis=1)
18
+ df_rank = df_rank.drop_duplicates(subset=column, keep=keep)
19
+ df_rank = df_rank.rename({column: "value"}, axis='columns')
20
+ return df_rank
21
+
22
+ class NpEncoder(json.JSONEncoder):
23
+ def default(self, obj):
24
+ if isinstance(obj, np.integer):
25
+ return int(obj)
26
+ if isinstance(obj, np.floating):
27
+ return float(obj)
28
+ if isinstance(obj, np.ndarray):
29
+ return obj.tolist()
30
+ return super(NpEncoder, self).default(obj)
31
+
32
+ ds = load_dataset("open-source-metrics/model-repos-stats", split="train")
33
+ df = ds.to_pandas()
34
+
35
+ df_ranks = {}
36
+ df_ranks["likes"] = compute_ranking(df, "likes")
37
+ df_ranks["downloads"] = compute_ranking(df, "downloads_30d")
38
+ df_ranks["repos"] = compute_ranking(df, "repo_id", method="count")
39
+
40
+ with open("./html_template.html", "r") as f:
41
+ template = f.read()
42
+
43
+ def create_user_summary(user_name):
44
+ summary = {}
45
+
46
+ df_user = df.loc[df["author"]==user_name]
47
+
48
+ r = requests.get(f'https://huggingface.co/api/users/{user_name}/likes')
49
+ user_datasets = list_datasets(author="lvwerra")
50
+ user_spaces = list_spaces(author="lvwerra")
51
+
52
+ summary["likes_user_total"] = df_user["likes"].sum()
53
+ summary["likes_user_given"] = len(r.json())
54
+ summary["likes_user_top"] = df_ranks["likes"][df_ranks["likes"]["value"]>=summary["likes_user_total"]].iloc[0]["top_perc"]
55
+ summary["likes_repo_most"] = df_user.sort_values(by="likes", ascending=False).iloc[0]["repo_id"]
56
+ summary["likes_repo_most_n"] = df_user.sort_values(by="likes", ascending=False).iloc[0]["likes"]
57
+
58
+ summary["downloads_user_total"] = df_user["downloads_30d"].sum()
59
+ summary["downloads_user_top"] = df_ranks["downloads"][df_ranks["downloads"]["value"]>=summary["downloads_user_total"]].iloc[0]["top_perc"]
60
+ summary["downlods_repo_most"] = df_user.sort_values(by="downloads_30d", ascending=False).iloc[0]["repo_id"]
61
+ summary["downlods_repo_most_n"] = df_user.sort_values(by="downloads_30d", ascending=False).iloc[0]["downloads_30d"]
62
+
63
+ summary["repos_model_total"] = len(df_user)
64
+ summary["repos_model_top"] = df_ranks["repos"][df_ranks["repos"]["value"]>=summary["repos_model_total"]].iloc[0]["top_perc"]
65
+ summary["repos_model_fav_type"] = Counter(df_user["model_type"].dropna()).most_common(1)[0][0]
66
+
67
+ summary["repos_datasets_total"] = len(list(iter(user_datasets)))
68
+ summary["repos_spaces_total"] = len(list(iter(user_spaces)))
69
+ summary["repos_spaces_fav_sdk"] = Counter([info.sdk for info in user_spaces]).most_common(1)[0][0]
70
+
71
+ return dict_to_html(summary)
72
+
73
+
74
+ def dict_to_html(summary):
75
+
76
+ report = template
77
+
78
+ for key in summary:
79
+ report = report.replace("{{" + key + "}}", str(summary[key]))
80
+ return report
81
+
82
+
83
+ demo = gr.Blocks(
84
+ css=".gradio-container {background-color: #000000}"
85
+ )
86
+ with demo:
87
+ with gr.Row():
88
+ gr.HTML(value="""<p style="text-align: center; color: rgb(255, 210, 31); font-family: 'Consolas', monospace; font-size: 24px;"> <b>Enter your HF user name:</b></p>""")
89
+ with gr.Row():
90
+ username = gr.Textbox(lines=1, max_lines=1, label="User name")
91
+ with gr.Row():
92
+ run = gr.Button()
93
+ with gr.Row():
94
+ output = gr.HTML(label="Generated code")
95
+ event = run.click(create_user_summary, [username], output)
96
+ demo.launch()
html_template.html ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <br>
2
+ <br>
3
+ <p style="text-align: center; color: rgb(255, 210, 31); font-family: 'Consolas', monospace; font-size: 48px;"> <b>Your Personal <img src="https://huggingface.co/front/assets/huggingface_logo.svg" alt="image" style="vertical-align: middle; display: inline-block; height: 72px;"> Report</b></p>
4
+ <br>
5
+ <br>
6
+ <br>
7
+ <p style="color: rgb(255, 210, 31);font-family: 'Consolas', monospace; font-size: 36px;">♥︎ Likes received: {{likes_user_total}} (top {{likes_user_top}}%)</p>
8
+ <br>
9
+ <p style="color: rgb(255, 210, 31);font-family: 'Consolas', monospace; font-size: 16px;">Your model that got most likes ({{likes_repo_most_n}}): {{likes_repo_most}}</p>
10
+ <br>
11
+ <br>
12
+ <p style="color: rgb(255, 210, 31);font-family: 'Consolas', monospace; font-size: 36px;">⤓ Total downloads: {{downloads_user_total}} (top {{downloads_user_top}} %)</p>
13
+ <br>
14
+ <p style="color: rgb(255, 210, 31);font-family: 'Consolas', monospace; font-size: 16px;">Your model that was downloaded most ({{downlods_repo_most_n}}): {{downlods_repo_most}}</p>
15
+ <br>
16
+ <br>
17
+ <p style="color: rgb(255, 210, 31);font-family: 'Consolas', monospace; font-size: 36px;">⚅ Number of models: {{repos_model_total}} (top {{repos_model_top}} %)</p>
18
+ <br>
19
+ <p style="color: rgb(255, 210, 31);font-family: 'Consolas', monospace; font-size: 16px;">Your favourite model type is: {{repos_model_fav_type}}</p>
20
+ <br>
21
+ <br>
22
+ <p style="color: rgb(255, 210, 31);font-family: 'Consolas', monospace; font-size: 36px;">⌂ You also have {{repos_datasets_total}} datasets and {{repos_spaces_total}} spaces!</p>
23
+ <br>
24
+ <p style="color: rgb(255, 210, 31);font-family: 'Consolas', monospace; font-size: 16px;">Your favourite spaces SDK is {{repos_spaces_fav_sdk}}.</p>