jsulz HF staff commited on
Commit
1f81beb
1 Parent(s): cd50bb1

initial commit

Browse files
Files changed (2) hide show
  1. app.py +242 -0
  2. requirements.txt +62 -0
app.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from plotly import graph_objects as go
4
+
5
+
6
+ def process_dataset():
7
+ """
8
+ Process the dataset and perform the following operations:
9
+ 1. Read the file_counts_and_sizes, repo_by_size_df, unique_files_df, and file_extensions data from parquet files.
10
+ 2. Convert the total size to petabytes and format it to two decimal places.
11
+ 3. Capitalize the 'type' column in the file_counts_and_sizes dataframe.
12
+ 4. Rename the columns in the file_counts_and_sizes dataframe.
13
+ 5. Sort the file_counts_and_sizes dataframe by total size in descending order.
14
+ 6. Drop rows with missing values in the 'extension' column of the file_extensions dataframe.
15
+ 7. Return the repo_by_size_df, unique_files_df, file_counts_and_sizes, and file_extensions dataframes.
16
+ """
17
+
18
+ file_counts_and_sizes = pd.read_parquet(
19
+ "hf://datasets/jsulz/lfs-anon/file_counts_and_sizes.parquet"
20
+ )
21
+ repo_by_size_df = pd.read_parquet(
22
+ "hf://datasets/jsulz/lfs-anon/repo_by_size.parquet"
23
+ )
24
+ unique_files_df = pd.read_parquet(
25
+ "hf://datasets/jsulz/lfs-anon/repo_by_size_file_dedupe.parquet"
26
+ )
27
+ file_extensions = pd.read_parquet(
28
+ "hf://datasets/jsulz/lfs-anon/file_extensions.parquet"
29
+ )
30
+
31
+ # Convert the total size to petabytes and format to two decimal places
32
+ file_counts_and_sizes = format_dataframe_size_column(
33
+ file_counts_and_sizes, "total_size"
34
+ )
35
+
36
+ file_counts_and_sizes["type"] = file_counts_and_sizes["type"].str.capitalize()
37
+ # update the column name to 'total size (PB)'
38
+ file_counts_and_sizes = file_counts_and_sizes.rename(
39
+ columns={
40
+ "type": "Repository Type",
41
+ "num_files": "Number of Files",
42
+ "total_size": "Total Size (PBs)",
43
+ }
44
+ )
45
+ # sort the dataframe by total size in descending order
46
+ file_counts_and_sizes = file_counts_and_sizes.sort_values(
47
+ by="Total Size (PBs)", ascending=False
48
+ )
49
+
50
+ # drop nas from the extension column
51
+ file_extensions = file_extensions.dropna(subset=["extension"])
52
+
53
+ return repo_by_size_df, unique_files_df, file_counts_and_sizes, file_extensions
54
+
55
+
56
+ def format_dataframe_size_column(_df, column_name):
57
+ """
58
+ Format the size to petabytes and return the formatted size.
59
+ """
60
+ _df[column_name] = _df[column_name] / 1e15
61
+ _df[column_name] = _df[column_name].map("{:.2f}".format)
62
+ return _df
63
+
64
+
65
+ def cumulative_growth_plot_analysis(df, df_compressed):
66
+ """
67
+ Calculates the cumulative growth of models, spaces, and datasets over time and generates a plot and dataframe from the analysis.
68
+
69
+ Args:
70
+ df (DataFrame): The input dataframe containing the data.
71
+ df_compressed (DataFrame): The input dataframe containing the compressed data.
72
+
73
+ Returns:
74
+ tuple: A tuple containing two elements:
75
+ - fig (Figure): The Plotly figure showing the cumulative growth of models, spaces, and datasets over time.
76
+ - last_10_months (DataFrame): The last 10 months of data showing the month-to-month growth in petabytes.
77
+
78
+ Raises:
79
+ None
80
+ """
81
+ # Convert year and month into a datetime column
82
+ df["date"] = pd.to_datetime(df[["year", "month"]].assign(day=1))
83
+ df_compressed["date"] = pd.to_datetime(
84
+ df_compressed[["year", "month"]].assign(day=1)
85
+ )
86
+
87
+ # Sort by date to ensure correct cumulative sum
88
+ df = df.sort_values(by="date")
89
+ df_compressed = df_compressed.sort_values(by="date")
90
+
91
+ # Pivot the dataframe to get the totalsize for each type
92
+ pivot_df = df.pivot_table(
93
+ index="date", columns="type", values="totalsize", aggfunc="sum"
94
+ ).fillna(0)
95
+ pivot_df_compressed = df_compressed.pivot_table(
96
+ index="date", columns="type", values="totalsize", aggfunc="sum"
97
+ ).fillna(0)
98
+
99
+ # Calculate cumulative sum for each type
100
+ cumulative_df = pivot_df.cumsum()
101
+ cumulative_df_compressed = pivot_df_compressed.cumsum()
102
+
103
+ last_10_months = cumulative_df.tail(10).copy()
104
+ last_10_months["total"] = last_10_months.sum(axis=1)
105
+ last_10_months["total_change"] = last_10_months["total"].diff()
106
+ last_10_months = format_dataframe_size_column(last_10_months, "total_change")
107
+ last_10_months["date"] = cumulative_df.tail(10).index
108
+ # drop the dataset, model, and space
109
+ last_10_months = last_10_months.drop(columns=["model", "space", "dataset"])
110
+ # pretiffy the date column to not have 00:00:00
111
+ last_10_months["date"] = last_10_months["date"].dt.strftime("%Y-%m")
112
+ # drop the first row
113
+ last_10_months = last_10_months.drop(last_10_months.index[0])
114
+ # order the columns date, total, total_change
115
+ last_10_months = last_10_months[["date", "total_change"]]
116
+ # rename the columns
117
+ last_10_months = last_10_months.rename(
118
+ columns={"date": "Date", "total_change": "Month-to-Month Growth (PBs)"}
119
+ )
120
+
121
+ # Create a Plotly figure
122
+ fig = go.Figure()
123
+
124
+ # Define a color map for each type
125
+ color_map = {"model": "blue", "space": "green", "dataset": "red"}
126
+
127
+ # Add a scatter trace for each type
128
+ for column in cumulative_df.columns:
129
+ fig.add_trace(
130
+ go.Scatter(
131
+ x=cumulative_df.index,
132
+ y=cumulative_df[column] / 1e15, # Convert to petabytes
133
+ mode="lines",
134
+ name=column.capitalize(),
135
+ line=dict(color=color_map.get(column, "black")), # Use color map
136
+ )
137
+ )
138
+
139
+ # Add a scatter trace for each type
140
+ for column in cumulative_df_compressed.columns:
141
+ fig.add_trace(
142
+ go.Scatter(
143
+ x=cumulative_df_compressed.index,
144
+ y=cumulative_df_compressed[column] / 1e15, # Convert to petabytes
145
+ mode="lines",
146
+ name=column.capitalize() + " (Compressed)",
147
+ line=dict(color=color_map.get(column, "black"), dash="dash"),
148
+ )
149
+ )
150
+
151
+ # Update layout
152
+ fig.update_layout(
153
+ title="Cumulative Growth of Models, Spaces, and Datasets Over Time",
154
+ xaxis_title="Date",
155
+ yaxis_title="Cumulative Size (PBs)",
156
+ legend_title="Type",
157
+ yaxis=dict(tickformat=".2f"), # Format y-axis labels to 2 decimal places
158
+ )
159
+ return fig, last_10_months
160
+
161
+
162
+ def plot_total_sum(by_type_arr):
163
+ # Sort the array by size in decreasing order
164
+ by_type_arr = sorted(by_type_arr, key=lambda x: x[1], reverse=True)
165
+
166
+ # Create a Plotly figure
167
+ fig = go.Figure()
168
+
169
+ # Add a bar trace for each type
170
+ for type, size in by_type_arr:
171
+ fig.add_trace(
172
+ go.Bar(
173
+ x=[type],
174
+ y=[size / 1e15], # Convert to petabytes
175
+ name=type.capitalize(),
176
+ )
177
+ )
178
+
179
+ # Update layout
180
+ fig.update_layout(
181
+ title="Top 20 File Extensions by Total Size",
182
+ xaxis_title="File Extension",
183
+ yaxis_title="Total Size (PBs)",
184
+ yaxis=dict(tickformat=".2f"), # Format y-axis labels to 2 decimal places
185
+ )
186
+ return fig
187
+
188
+
189
+ # Create a gradio blocks interface and launch a demo
190
+ with gr.Blocks() as demo:
191
+ df, file_df, by_type, by_extension = process_dataset()
192
+
193
+ # Add a heading
194
+ gr.Markdown("# Git LFS Analysis Across the Hub")
195
+ with gr.Row():
196
+ # scale so that
197
+ # group the data by month and year and compute a cumulative sum of the total_size column
198
+ fig, last_10_months = cumulative_growth_plot_analysis(df, file_df)
199
+ with gr.Column(scale=1):
200
+ gr.Markdown("# Repository Growth")
201
+ gr.Markdown(
202
+ "The cumulative growth of models, spaces, and datasets over time can be seen in the adjacent chart. Beside that is a view of the total change, month to month, of LFS files stored on the hub over 2024. We're averaging nearly 2.3 PBs uploaded to LFS per month!"
203
+ )
204
+ gr.Dataframe(last_10_months, height=250)
205
+ with gr.Column(scale=3):
206
+ gr.Plot(fig)
207
+ with gr.Row():
208
+ with gr.Column(scale=1):
209
+ gr.Markdown(
210
+ "This table shows the total number of files and cumulative size of those files across all repositories on the Hub. These numbers might be hard to grok, so let's try to put them in context. The last [Common Crawl](https://commoncrawl.org/) download was [451 TBs](https://github.com/commoncrawl/cc-crawl-statistics/blob/master/stats/crawler/CC-MAIN-2024-38.json#L31). The Spaces repositories alone outpaces that. Meanwhile, between Datasets and Model repos, the Hub stores 64 Common Crawls."
211
+ )
212
+ with gr.Column(scale=3):
213
+ gr.Dataframe(by_type)
214
+
215
+ # Add a heading
216
+ gr.Markdown("## File Extension Analysis")
217
+ gr.Markdown(
218
+ "Breaking this down by file extension, some interesting trends emerge. [Safetensors](https://huggingface.co/docs/safetensors/en/index) are quickly becoming the defacto standard on the hub, accounting for over 7PBs (25%) of LFS storage. The top 20 file extensions seen here and in the table below account for 82% of all LFS storage on the hub."
219
+ )
220
+ # Get the top 10 file extnesions by size
221
+ by_extension_size = by_extension.sort_values(by="size", ascending=False).head(22)
222
+ # get the top 10 file extensions by count
223
+ # by_extension_count = by_extension.sort_values(by="count", ascending=False).head(20)
224
+
225
+ # make a pie chart of the by_extension_size dataframe
226
+ gr.Plot(plot_total_sum(by_extension_size[["extension", "size"]].values))
227
+ # drop the unnamed: 0 column
228
+ by_extension_size = by_extension_size.drop(columns=["Unnamed: 0"])
229
+ # format the size column
230
+ by_extension_size = format_dataframe_size_column(by_extension_size, "size")
231
+ # Rename the other columns
232
+ by_extension_size = by_extension_size.rename(
233
+ columns={
234
+ "extension": "File Extension",
235
+ "count": "Number of Files",
236
+ "size": "Total Size (PBs)",
237
+ }
238
+ )
239
+ gr.Dataframe(by_extension_size)
240
+
241
+
242
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ annotated-types==0.7.0
3
+ anyio==4.6.0
4
+ certifi==2024.8.30
5
+ charset-normalizer==3.3.2
6
+ click==8.1.7
7
+ colorama==0.4.6
8
+ contourpy==1.3.0
9
+ cycler==0.12.1
10
+ duckdb==1.1.0
11
+ fastapi==0.115.0
12
+ ffmpy==0.4.0
13
+ filelock==3.16.1
14
+ fonttools==4.54.0
15
+ fsspec==2024.9.0
16
+ gradio-client==1.3.0
17
+ gradio==4.44.0
18
+ h11==0.14.0
19
+ httpcore==1.0.5
20
+ httpx==0.27.2
21
+ huggingface-hub==0.25.1
22
+ idna==3.10
23
+ importlib-resources==6.4.5
24
+ jinja2==3.1.4
25
+ kiwisolver==1.4.7
26
+ markdown-it-py==3.0.0
27
+ markupsafe==2.1.5
28
+ matplotlib==3.9.2
29
+ mdurl==0.1.2
30
+ numpy==2.1.1
31
+ orjson==3.10.7
32
+ packaging==24.1
33
+ pandas==2.2.3
34
+ pillow==10.4.0
35
+ plotly==5.24.1
36
+ pyarrow==17.0.0
37
+ pydantic-core==2.23.4
38
+ pydantic==2.9.2
39
+ pydub==0.25.1
40
+ pygments==2.18.0
41
+ pyparsing==3.1.4
42
+ python-dateutil==2.9.0.post0
43
+ python-multipart==0.0.10
44
+ pytz==2024.2
45
+ pyyaml==6.0.2
46
+ requests==2.32.3
47
+ rich==13.8.1
48
+ ruff==0.6.7
49
+ semantic-version==2.10.0
50
+ shellingham==1.5.4
51
+ six==1.16.0
52
+ sniffio==1.3.1
53
+ starlette==0.38.6
54
+ tenacity==9.0.0
55
+ tomlkit==0.12.0
56
+ tqdm==4.66.5
57
+ typer==0.12.5
58
+ typing-extensions==4.12.2
59
+ tzdata==2024.2
60
+ urllib3==2.2.3
61
+ uvicorn==0.30.6
62
+ websockets==12.0