-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhuggingface.R
165 lines (140 loc) · 4.22 KB
/
huggingface.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
library(data.table)
library(rvest)
library(stringr)
library(jsonlite)
set_working_directory <- function() {
cwd <- getSrcDirectory(function(x){x})
if (length(cwd) == 0) {
fp <- str_match(commandArgs(), "--file=(.*)")[,2]
cwd <- dirname(fp[!is.na(fp)])
}
setwd(cwd)
cwd
}
dir_create_csv <- function(cwd) {
dir.create(file.path(cwd, "csv"), showWarnings = FALSE, recursive = TRUE)
}
# implement extract2 from magrittr
extract2 <- .Primitive("[[")
# extract json from script elements
json_from_html <- function(url) {
html <- read_html(url, options = "HUGE")
json <- html |>
html_elements("script") |>
html_text() |>
extract2(2) |>
str_replace("^window.gradio_config = ", "") |>
str_replace(";$", "")
len <- str_length(json)
stopifnot(len > 1e4)
fromJSON(json)
}
find_column_index <- function(json, column_name) {
headers <- json$components$props$headers
has_column_name <- function(header){
any(str_detect(header, column_name))
}
which(vapply(headers, has_column_name, logical(1)))[1]
}
dt_from_json_index <- function(json, idx) {
data <- json$components$props$value[[idx]]$data
as.data.table(data)
}
model_name_and_url <- function(model) {
html <- read_html(model)
a <- html |>
html_element("a")
model <- a |>
html_text()
url <- a |>
html_attr("href")
list(model = model, url = url)
}
add_model_url_columns <- function(dt) {
dt[, c("model", "url") := model_name_and_url(model), by = .I]
}
is_column_class <- function(x, asclass) {
if(is.character(x)) {
suppressWarnings(all(!is.na(asclass(x))))
} else {
FALSE
}
}
set_column_class <- function(dt, asclass) {
cols <- names(dt)[vapply(dt, is_column_class, logical(1), asclass)]
dt[, (cols) := lapply(.SD, asclass), .SDcols = cols]
}
set_dt_classes <- function(dt) {
set_column_class(dt, as.numeric)
set_column_class(dt, as.logical)
}
dt_from_json <- function(json, fn, column) {
idx <- find_column_index(json, column)
headers <- fn(json, idx)
dt <- dt_from_json_index(json, idx)
setnames(dt, headers)
add_model_url_columns(dt)
set_dt_classes(dt)
}
dt_from_json_v2 <- function(json) {
dt <- as.data.table(json)
headers <- str_replace_all(colnames(dt), "\\.", "_")
setnames(dt, headers)
}
normalize_json_headers_hg <- function(json, idx) {
headers <- json$components$props$value[[idx]]$headers
headers[str_detect(headers, "Average ⬆️")] <- "Average"
headers[str_detect(headers, "Hub ❤️")] <- "Hub Hearts"
headers <- str_replace_all(headers, " ", "_")
headers <- str_replace_all(headers, "\\W", "")
tolower(headers)
}
normalize_json_headers_lm <- function(json, idx) {
headers <- json$components$props$value[[idx]]$headers
headers <- str_trim(str_replace_all(headers, "\\W+", " "))
headers[str_detect(headers, "Rank UB")] <- "Rank"
headers[str_detect(headers, "95 CI")] <- "95 Pct CI"
headers <- str_replace_all(headers, " ", "_")
tolower(headers)
}
dt_from_html_hg <- function(url) {
json <- json_from_html(url)
dt_from_json(json, normalize_json_headers_hg, "^Hub ❤️")
}
dt_from_api_hg_v2 <- function(url) {
json <- fromJSON(url)
dt_from_json_v2(json)
}
dt_from_html_lm <- function(url) {
json <- json_from_html(url)
dt_from_json(json, normalize_json_headers_lm, "^Knowledge Cutoff")
}
dt_merge_tables <- function(dt1, dt2) {
dt1[, key := tolower(str_split_i(model_name, "/", 2))]
dt2[, key := tolower(model)]
merge(dt1, dt2, by = "key")
}
# read data.table from csv or calculate it
dt_if_missing <- function(file, fn, ...) {
csv <- file.path("csv", file)
if(file.exists(csv)) {
dt <- fread(csv)
} else {
dt <- do.call(fn, list(...))
fwrite(dt, csv)
dt
}
}
cwd <- set_working_directory()
dir_create_csv(cwd)
url <- "https://open-llm-leaderboard-old-open-llm-leaderboard.hf.space/"
file <- "huggingface_v1.csv"
hg1 <- dt_if_missing(file, dt_from_html_hg, url)
url <- "https://lmarena-ai-chatbot-arena-leaderboard.hf.space/"
file <- "lmsys.csv"
lmsys <- dt_if_missing(file, dt_from_html_lm, url)
url <- "https://open-llm-leaderboard-open-llm-leaderboard.hf.space/api/leaderboard/formatted"
file <- "huggingface_v2.csv"
hg2 <- dt_if_missing(file, dt_from_api_hg_v2, url)
file <- "merged.csv"
merged <- dt_if_missing(file, dt_merge_tables, hg2, lmsys)