cfahlgren1 HF staff commited on
Commit
ecba77a
1 Parent(s): cf32e4e

use api instead of duckdb on dataset

Browse files
Files changed (3) hide show
  1. bun.lockb +0 -0
  2. package.json +0 -1
  3. src/pages/index.tsx +69 -106
bun.lockb CHANGED
Binary files a/bun.lockb and b/bun.lockb differ
 
package.json CHANGED
@@ -9,7 +9,6 @@
9
  "lint": "next lint"
10
  },
11
  "dependencies": {
12
- "@duckdb/duckdb-wasm": "^1.28.1-dev106.0",
13
  "@emotion/react": "^11.13.0",
14
  "@emotion/styled": "^11.13.0",
15
  "@mui/material": "^5.16.6",
 
9
  "lint": "next lint"
10
  },
11
  "dependencies": {
 
12
  "@emotion/react": "^11.13.0",
13
  "@emotion/styled": "^11.13.0",
14
  "@mui/material": "^5.16.6",
src/pages/index.tsx CHANGED
@@ -2,145 +2,107 @@
2
 
3
  import { Inter } from "next/font/google";
4
  import ActivityCalendar from "react-activity-calendar";
5
- import * as duckdb from "@duckdb/duckdb-wasm"
6
  import { useState, useEffect } from "react";
7
  import { Tooltip as MuiTooltip } from '@mui/material';
8
 
9
  const inter = Inter({ subsets: ["latin"] });
10
 
11
- interface CustomActivity {
 
 
 
 
 
12
  date: string;
13
  count: number;
14
  level: number;
15
- details: Array<{ provider: string; count: number }>;
16
  }
17
 
18
- type ProviderKey = "mistralai" | "meta-llama" | "openai" | "anthropic" | "google";
19
-
20
  export default function Home() {
21
- const [calendarData, setCalendarData] = useState<Record<ProviderKey, CustomActivity[]>>({} as Record<ProviderKey, CustomActivity[]>);
22
  const [isLoading, setIsLoading] = useState(true);
23
 
24
- const PROVIDERS_MAP: Record<ProviderKey, { name: string; color: string }> = {
25
- "mistralai": {"name": "Mistral AI", "color": "#ff7000"},
26
- "meta-llama": {"name": "Meta", "color": "#0668E1"},
27
- "openai": {"name": "OpenAI", "color": "#10A37F"},
28
- "anthropic": {"name": "Anthropic", "color": "#cc785c"},
29
- "google": {"name": "Google", "color": "#4285F4"},
30
  }
31
 
32
- const getModelData = async (conn: duckdb.AsyncDuckDBConnection) => {
33
- const result = await conn.query(`
34
- SELECT
35
- STRFTIME(DATE_TRUNC('day', CAST(createdAt AS DATE)), '%Y-%m-%d') AS date,
36
- CASE
37
- WHEN provider IN ('meta-llama', 'facebook') THEN 'meta-llama'
38
- ELSE provider
39
- END AS provider,
40
- COUNT(*) AS count
41
- FROM (
42
- SELECT *, SPLIT_PART(id, '/', 1) AS provider
43
- FROM models
44
- WHERE CAST(createdAt AS DATE) >= DATE_TRUNC('year', CURRENT_DATE)
45
- ) subquery
46
- WHERE provider IN (${Object.keys(PROVIDERS_MAP).map(p => `'${p}'`).join(', ')}, 'facebook')
47
- GROUP BY 1, 2
48
- ORDER BY date
49
- `);
50
- return result.toArray().map((row: any) => ({
51
- date: row.date,
52
- provider: row.provider,
53
- count: Number(row.count)
54
- }));
55
- }
56
-
57
- const generateCalendarData = (modelData: any[]) => {
58
- const data: Record<ProviderKey, CustomActivity[]> = Object.keys(PROVIDERS_MAP).reduce((acc, provider) => {
59
- acc[provider as ProviderKey] = [];
60
- return acc;
61
- }, {} as Record<ProviderKey, CustomActivity[]>);
62
 
63
  const today = new Date();
64
  const startOfYear = new Date(today.getFullYear(), 0, 1);
65
 
 
66
  for (let d = new Date(startOfYear); d <= today; d.setDate(d.getDate() + 1)) {
67
  const dateString = d.toISOString().split('T')[0];
68
 
69
- Object.keys(PROVIDERS_MAP).forEach((provider) => {
70
- const dayData = modelData.filter(item => item.date === dateString && item.provider === provider);
71
- const count = dayData.reduce((sum, item) => sum + item.count, 0);
 
 
72
 
73
- data[provider as ProviderKey].push({
74
- date: dateString,
75
- count,
76
- level: 0,
77
- details: dayData,
78
- });
79
  });
80
  }
81
 
82
- const avgCounts: Record<ProviderKey, number> = Object.fromEntries(
83
- Object.keys(PROVIDERS_MAP).map(provider => [
 
84
  provider,
85
- data[provider as ProviderKey].reduce((sum, day) => sum + day.count, 0) / data[provider as ProviderKey].length || 0
86
  ])
87
- ) as Record<ProviderKey, number>;
88
 
 
89
  Object.entries(data).forEach(([provider, days]) => {
90
- const avgCount = avgCounts[provider as ProviderKey];
91
  days.forEach(day => {
92
- day.level = day.count === 0 ? 0 :
93
- day.count <= avgCount * 0.5 ? 1 :
94
- day.count <= avgCount ? 2 :
95
- day.count <= avgCount * 1.5 ? 3 : 4;
 
96
  });
97
  });
98
 
99
  return data;
100
  }
101
 
102
- const initDB = async () => {
103
- const CDN_BASE = `https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@next`
104
-
105
- const JSDELIVR_BUNDLES = {
106
- mvp: {
107
- mainModule: `${CDN_BASE}/dist/duckdb-mvp.wasm`,
108
- mainWorker: `${CDN_BASE}/dist/duckdb-browser-mvp.worker.js`,
109
- },
110
- eh: {
111
- mainModule: `${CDN_BASE}/dist/duckdb-eh.wasm`,
112
- mainWorker: `${CDN_BASE}/dist/duckdb-browser-eh.worker.js`,
113
- },
 
 
 
 
 
 
 
 
 
 
 
114
  }
115
-
116
- const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES)
117
- const worker_url = URL.createObjectURL(
118
- new Blob([`importScripts("${bundle.mainWorker}");`], {
119
- type: "text/javascript",
120
- })
121
- )
122
-
123
- const worker = new Worker(worker_url)
124
- const logger = new duckdb.ConsoleLogger()
125
- const db = new duckdb.AsyncDuckDB(logger, worker)
126
- await db.instantiate(bundle.mainModule)
127
-
128
- const connection = await db.connect()
129
-
130
- await connection.query(`
131
- CREATE VIEW models AS SELECT * FROM read_parquet('https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/refs%2Fconvert%2Fparquet/models/train/0000.parquet?download=true');
132
- `);
133
-
134
- const modelData = await getModelData(connection);
135
- const calendarData = generateCalendarData(modelData);
136
- setCalendarData(calendarData);
137
- setIsLoading(false);
138
-
139
- await connection.close();
140
  }
141
 
142
  useEffect(() => {
143
- initDB();
144
  }, []);
145
 
146
  return (
@@ -154,21 +116,22 @@ export default function Home() {
154
  <>
155
  {Object.entries(PROVIDERS_MAP)
156
  .sort(([keyA], [keyB]) =>
157
- calendarData[keyB as ProviderKey].reduce((sum, day) => sum + day.count, 0) -
158
- calendarData[keyA as ProviderKey].reduce((sum, day) => sum + day.count, 0)
159
  )
160
- .map(([key, value]) => (
161
- <div key={key} className="mb-8">
162
- <h2 className="text-2xl font-bold mb-2">{value.name}</h2>
163
  <ActivityCalendar
164
- data={calendarData[key as ProviderKey]}
165
  theme={{
166
- dark: ['#161b22', value.color],
167
  }}
 
168
  colorScheme="dark"
169
  renderBlock={(block, activity) => (
170
  <MuiTooltip
171
- title={`${activity.count} activities on ${activity.date}`}
172
  >
173
  {block}
174
  </MuiTooltip>
 
2
 
3
  import { Inter } from "next/font/google";
4
  import ActivityCalendar from "react-activity-calendar";
 
5
  import { useState, useEffect } from "react";
6
  import { Tooltip as MuiTooltip } from '@mui/material';
7
 
8
  const inter = Inter({ subsets: ["latin"] });
9
 
10
+ interface ModelData {
11
+ createdAt: string;
12
+ id: string;
13
+ }
14
+
15
+ interface Activity {
16
  date: string;
17
  count: number;
18
  level: number;
 
19
  }
20
 
 
 
21
  export default function Home() {
22
+ const [calendarData, setCalendarData] = useState<Record<string, Activity[]>>({});
23
  const [isLoading, setIsLoading] = useState(true);
24
 
25
+ const PROVIDERS_MAP: Record<string, { color: string; authors: string[] }> = {
26
+ "Mistral AI": { color: "#ff7000", authors: ["mistralai"] },
27
+ "Meta": { color: "#0668E1", authors: ["facebook", "meta-llama"] },
28
+ "OpenAI": { color: "#10A37F", authors: ["openai"] },
29
+ "Anthropic": { color: "#cc785c", authors: ["anthropic"] },
30
+ "Google": { color: "#4285F4", authors: ["google"] },
31
  }
32
 
33
+ const generateCalendarData = (modelData: ModelData[]) => {
34
+ const data: Record<string, Activity[]> = Object.fromEntries(
35
+ Object.keys(PROVIDERS_MAP).map(provider => [provider, []])
36
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  const today = new Date();
39
  const startOfYear = new Date(today.getFullYear(), 0, 1);
40
 
41
+ // generate daily data for each provider
42
  for (let d = new Date(startOfYear); d <= today; d.setDate(d.getDate() + 1)) {
43
  const dateString = d.toISOString().split('T')[0];
44
 
45
+ Object.entries(PROVIDERS_MAP).forEach(([provider, { authors }]) => {
46
+ const count = modelData.filter(item =>
47
+ item.createdAt.startsWith(dateString) &&
48
+ authors.some(author => item.id.startsWith(author))
49
+ ).length;
50
 
51
+ data[provider].push({ date: dateString, count, level: 0 });
 
 
 
 
 
52
  });
53
  }
54
 
55
+ // calculate average counts for each provider
56
+ const avgCounts = Object.fromEntries(
57
+ Object.entries(data).map(([provider, days]) => [
58
  provider,
59
+ days.reduce((sum, day) => sum + day.count, 0) / days.length || 0
60
  ])
61
+ );
62
 
63
+ // assign levels based on count relative to average
64
  Object.entries(data).forEach(([provider, days]) => {
65
+ const avgCount = avgCounts[provider];
66
  days.forEach(day => {
67
+ day.level =
68
+ day.count === 0 ? 0 :
69
+ day.count <= avgCount * 0.5 ? 1 :
70
+ day.count <= avgCount ? 2 :
71
+ day.count <= avgCount * 1.5 ? 3 : 4;
72
  });
73
  });
74
 
75
  return data;
76
  }
77
 
78
+ const initData = async () => {
79
+ try {
80
+ const allAuthors = Object.values(PROVIDERS_MAP).flatMap(({ authors }) => authors);
81
+ const uniqueAuthors = Array.from(new Set(allAuthors));
82
+
83
+ const allModelData = await Promise.all(
84
+ uniqueAuthors.map(async (author) => {
85
+ const response = await fetch(`https://huggingface.co/api/models?author=${author}&sort=createdAt&direction=-1`);
86
+ const data = await response.json();
87
+ return data.map((item: any) => ({
88
+ createdAt: item.createdAt,
89
+ id: item.id,
90
+ }));
91
+ })
92
+ );
93
+
94
+ const flatModelData = allModelData.flat();
95
+ const calendarData = generateCalendarData(flatModelData);
96
+ setCalendarData(calendarData);
97
+ } catch (error) {
98
+ console.error("Error fetching data:", error);
99
+ } finally {
100
+ setIsLoading(false);
101
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  }
103
 
104
  useEffect(() => {
105
+ initData();
106
  }, []);
107
 
108
  return (
 
116
  <>
117
  {Object.entries(PROVIDERS_MAP)
118
  .sort(([keyA], [keyB]) =>
119
+ calendarData[keyB].reduce((sum, day) => sum + day.count, 0) -
120
+ calendarData[keyA].reduce((sum, day) => sum + day.count, 0)
121
  )
122
+ .map(([providerName, { color }]) => (
123
+ <div key={providerName} className="mb-8">
124
+ <h2 className="text-2xl font-bold mb-2">{providerName}</h2>
125
  <ActivityCalendar
126
+ data={calendarData[providerName]}
127
  theme={{
128
+ dark: ['#161b22', color],
129
  }}
130
+ hideTotalCount
131
  colorScheme="dark"
132
  renderBlock={(block, activity) => (
133
  <MuiTooltip
134
+ title={`${activity.count} models created on ${activity.date}`}
135
  >
136
  {block}
137
  </MuiTooltip>