Commit
·
80aa351
1
Parent(s):
a6338be
Upload 3 files
Browse files- data.parquet +3 -0
- main.py +150 -0
- requirements.txt +7 -0
data.parquet
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:32f51e54683c1fa3390bc4e318e1008d686844bb451b82c3c1a91787e2b986d9
|
3 |
+
size 3765676
|
main.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import contextlib
|
2 |
+
import gradio as gr
|
3 |
+
import polars as pl
|
4 |
+
from functools import lru_cache
|
5 |
+
from cytoolz import concat, frequencies, topk
|
6 |
+
from datasets import load_dataset
|
7 |
+
from ast import literal_eval
|
8 |
+
from typing import Union, List, Optional
|
9 |
+
import numpy as np
|
10 |
+
from itertools import combinations
|
11 |
+
from toolz import unique
|
12 |
+
import pandas as pd
|
13 |
+
|
14 |
+
pd.options.plotting.backend = "plotly"
|
15 |
+
|
16 |
+
|
17 |
+
def download_dataset():
|
18 |
+
return load_dataset("open-source-metrics/model-repos-stats", split="train")
|
19 |
+
|
20 |
+
|
21 |
+
def _clean_tags(tags: Optional[Union[str, List[str]]]):
|
22 |
+
try:
|
23 |
+
tags = literal_eval(tags)
|
24 |
+
if isinstance(tags, str):
|
25 |
+
return [tags]
|
26 |
+
if isinstance(tags, list):
|
27 |
+
return [tag for tag in tags if isinstance(tag, str)]
|
28 |
+
else:
|
29 |
+
return []
|
30 |
+
except (ValueError, SyntaxError):
|
31 |
+
return []
|
32 |
+
|
33 |
+
|
34 |
+
def prep_dataset():
|
35 |
+
ds = download_dataset()
|
36 |
+
df = ds.to_pandas()
|
37 |
+
df['languages'] = df['languages'].apply(_clean_tags)
|
38 |
+
df['datasets'] = df['datasets'].apply(_clean_tags)
|
39 |
+
df['tags'] = df['tags'].apply(_clean_tags)
|
40 |
+
df = df.drop(columns=['Unnamed: 0'])
|
41 |
+
df.to_parquet("data.parquet")
|
42 |
+
return df
|
43 |
+
|
44 |
+
|
45 |
+
def load_data():
|
46 |
+
return pd.read_parquet("data.parquet")
|
47 |
+
|
48 |
+
|
49 |
+
def filter_df_by_library(filter='transformers'):
|
50 |
+
df = load_data()
|
51 |
+
return df[df['library'] == filter] if filter else df
|
52 |
+
|
53 |
+
|
54 |
+
@lru_cache()
|
55 |
+
def get_library_choices(min_freq: int = 50):
|
56 |
+
df = load_data()
|
57 |
+
library_counts = df.library.value_counts()
|
58 |
+
return library_counts[library_counts > min_freq].index.to_list()
|
59 |
+
|
60 |
+
|
61 |
+
@lru_cache()
|
62 |
+
def get_all_tags():
|
63 |
+
df = load_data()
|
64 |
+
tags = df['tags'].to_list()
|
65 |
+
return list(concat(tags))
|
66 |
+
|
67 |
+
@lru_cache()
|
68 |
+
def get_case_sensitive_duplicate_tags():
|
69 |
+
tags = get_all_tags()
|
70 |
+
unique_tags = unique(tags)
|
71 |
+
return [
|
72 |
+
tag_combo
|
73 |
+
for tag_combo in combinations(unique_tags, 2)
|
74 |
+
if tag_combo[0].lower() == tag_combo[1].lower()
|
75 |
+
]
|
76 |
+
|
77 |
+
|
78 |
+
def display_case_sensitive_duplicate_tags():
|
79 |
+
return pd.DataFrame(get_case_sensitive_duplicate_tags())
|
80 |
+
|
81 |
+
def tag_frequency(case_sensitive=True):
|
82 |
+
tags = get_all_tags()
|
83 |
+
if not case_sensitive:
|
84 |
+
tags = (tag.lower() for tag in tags)
|
85 |
+
tags_frequencies = dict(frequencies(tags))
|
86 |
+
df = pd.DataFrame.from_dict(tags_frequencies, orient='index', columns=['Count']).sort_values(
|
87 |
+
by='Count', ascending=False)
|
88 |
+
return df
|
89 |
+
|
90 |
+
def plot_frequency(filter):
|
91 |
+
df = filter_df_by_library(filter)
|
92 |
+
tags = concat(df['tags'])
|
93 |
+
tags = dict(frequencies(tags))
|
94 |
+
df = pd.DataFrame.from_dict(tags, orient='index', columns=['Count']).sort_values(
|
95 |
+
by='Count', ascending=False)
|
96 |
+
return df
|
97 |
+
|
98 |
+
|
99 |
+
def has_model_card_by_library(top_n):
|
100 |
+
df = load_data()
|
101 |
+
if top_n:
|
102 |
+
top_libs = df.library.value_counts().head(int(top_n)).index.to_list()
|
103 |
+
# min_thresh = df.library.value_counts()[:min_number].index.to_list()
|
104 |
+
df = df[df.library.isin(top_libs)]
|
105 |
+
return df.groupby('library')['has_text'].apply(lambda x: np.sum(x) / len(x)).sort_values().plot.barh()
|
106 |
+
|
107 |
+
|
108 |
+
def model_card_length_by_library(top_n):
|
109 |
+
df = load_data()
|
110 |
+
if top_n:
|
111 |
+
top_libs = df.library.value_counts().head(int(top_n)).index.to_list()
|
112 |
+
# min_thresh = df.library.value_counts()[:min_number].index.to_list()
|
113 |
+
df = df[df.library.isin(top_libs)]
|
114 |
+
return df.groupby('library')['text_length'].describe().round().reset_index()
|
115 |
+
# df = df.groupby('library')['text_length'].describe().round().reset_index()
|
116 |
+
# df['library'] = df.library.apply(lambda library: f"[{library}](https://huggingface.co/models?library={library})")
|
117 |
+
# return df.to_markdown()
|
118 |
+
|
119 |
+
|
120 |
+
df = load_data()
|
121 |
+
top_n = df.library.value_counts().shape[0]
|
122 |
+
|
123 |
+
with gr.Blocks() as demo:
|
124 |
+
gr.Markdown("# 🤗 Hub Metadata Explorer")
|
125 |
+
gr.Markdown("Some explanation")
|
126 |
+
with gr.Tab("Tags overview"):
|
127 |
+
gr.Markdown("Tags are one of the key...")
|
128 |
+
with gr.Row():
|
129 |
+
gr.Markdown("thsh")
|
130 |
+
with gr.Row():
|
131 |
+
case_sensitive = gr.Checkbox(False,label=)
|
132 |
+
gr.Plot(tag_frequency())
|
133 |
+
with gr.Row():
|
134 |
+
gr.Markdown(f"Number of tags which are case sensitive {len(get_case_sensitive_duplicate_tags())}")
|
135 |
+
with gr.Accordion("View duplicate tags", open=False):
|
136 |
+
gr.Dataframe(display_case_sensitive_duplicate_tags())
|
137 |
+
with gr.Tab("Model Cards"):
|
138 |
+
gr.Markdown("""Model cards are a key component of metadata for a model. Model cards can include both
|
139 |
+
information created by a human i.e. outlining the goals behind the creation of the model and information
|
140 |
+
created by a training framework. This automatically generated information can contain information about
|
141 |
+
number of epochs, learning rate, weight decay etc. """)
|
142 |
+
min_lib_frequency = gr.Slider(minimum=1, maximum=top_n, value=10, label='filter by top n libraries')
|
143 |
+
with gr.Column():
|
144 |
+
plot = gr.Plot()
|
145 |
+
min_lib_frequency.change(has_model_card_by_library, [min_lib_frequency], plot, queue=False)
|
146 |
+
with gr.Column():
|
147 |
+
df = gr.Dataframe()
|
148 |
+
min_lib_frequency.change(model_card_length_by_library, [min_lib_frequency], df, queue=False)
|
149 |
+
|
150 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.15.0
|
2 |
+
pandas
|
3 |
+
polars
|
4 |
+
datasets
|
5 |
+
cytoolz
|
6 |
+
plotly
|
7 |
+
tabulate
|