Update app.R
Browse files
app.R
CHANGED
|
@@ -70,171 +70,57 @@ download_private_parquet <- function(repo_id, filename) {
|
|
| 70 |
library(httr)
|
| 71 |
library(arrow)
|
| 72 |
|
| 73 |
-
# Main URL for direct file download
|
| 74 |
-
url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
|
| 75 |
-
|
| 76 |
-
# Download with curl for more control and debugging
|
| 77 |
-
temp_file <- tempfile(fileext = ".parquet")
|
| 78 |
-
|
| 79 |
-
# Use httr's RETRY for more reliable downloads
|
| 80 |
-
response <- RETRY(
|
| 81 |
-
"GET",
|
| 82 |
-
url,
|
| 83 |
-
add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))),
|
| 84 |
-
write_disk(temp_file, overwrite = TRUE),
|
| 85 |
-
times = 3
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
if (status_code(response) == 200) {
|
| 89 |
-
# Check file size
|
| 90 |
-
file_info <- file.info(temp_file)
|
| 91 |
-
|
| 92 |
-
if (file_info$size == 0) {
|
| 93 |
-
file.remove(temp_file)
|
| 94 |
-
stop("Downloaded file is empty")
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
# Check if this looks like an LFS pointer (small file)
|
| 98 |
-
if (file_info$size < 1000) { # LFS pointers are typically small
|
| 99 |
-
tryCatch({
|
| 100 |
-
# Read the first line to see if it's an LFS pointer
|
| 101 |
-
content_text <- readLines(temp_file, n = 1, warn = FALSE)
|
| 102 |
-
|
| 103 |
-
if (grepl("^version https://git-lfs.github.com/spec/", content_text)) {
|
| 104 |
-
# It's an LFS pointer - read all lines
|
| 105 |
-
all_lines <- readLines(temp_file, warn = FALSE)
|
| 106 |
-
|
| 107 |
-
# Extract oid
|
| 108 |
-
oid_line <- grep("oid sha256:", all_lines, value = TRUE)
|
| 109 |
-
if (length(oid_line) == 0) {
|
| 110 |
-
file.remove(temp_file)
|
| 111 |
-
stop("Could not find oid in LFS pointer")
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
oid <- gsub("oid sha256:", "", oid_line)
|
| 115 |
-
oid <- trimws(oid)
|
| 116 |
-
|
| 117 |
-
# Try the more reliable Hugging Face LFS URL format
|
| 118 |
-
lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename, "?download=true")
|
| 119 |
-
|
| 120 |
-
cat("Detected LFS pointer. Downloading from:", lfs_url, "\n")
|
| 121 |
-
|
| 122 |
-
# Download actual content
|
| 123 |
-
lfs_response <- RETRY(
|
| 124 |
-
"GET",
|
| 125 |
-
lfs_url,
|
| 126 |
-
add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))),
|
| 127 |
-
write_disk(temp_file, overwrite = TRUE),
|
| 128 |
-
times = 3
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
if (status_code(lfs_response) != 200) {
|
| 132 |
-
file.remove(temp_file)
|
| 133 |
-
stop(paste("Failed to download LFS content. Status code:", status_code(lfs_response)))
|
| 134 |
-
}
|
| 135 |
-
}
|
| 136 |
-
}, error = function(e) {
|
| 137 |
-
# Not an LFS pointer or error reading it
|
| 138 |
-
cat("Not an LFS pointer or error:", e$message, "\n")
|
| 139 |
-
})
|
| 140 |
-
}
|
| 141 |
-
|
| 142 |
-
# Try to read as parquet, with detailed error reporting
|
| 143 |
-
tryCatch({
|
| 144 |
-
file_info <- file.info(temp_file)
|
| 145 |
-
cat("File size before reading:", file_info$size, "bytes\n")
|
| 146 |
-
|
| 147 |
-
# Read first few bytes to check for parquet signature
|
| 148 |
-
con <- file(temp_file, "rb")
|
| 149 |
-
header <- readBin(con, "raw", n = 4)
|
| 150 |
-
close(con)
|
| 151 |
-
|
| 152 |
-
if (rawToChar(header) != "PAR1") {
|
| 153 |
-
cat("WARNING: File does not start with PAR1 magic bytes\n")
|
| 154 |
-
}
|
| 155 |
-
|
| 156 |
-
# Try to read as parquet
|
| 157 |
-
data <- read_parquet(temp_file)
|
| 158 |
-
file.remove(temp_file)
|
| 159 |
-
return(data)
|
| 160 |
-
}, error = function(e) {
|
| 161 |
-
# If read_parquet fails, try using httr to download the file directly to a specific location
|
| 162 |
-
# Sometimes that can help with file corruption issues
|
| 163 |
-
download_path <- file.path(getwd(), basename(filename))
|
| 164 |
-
|
| 165 |
-
cat("Failed to read as parquet. Downloading to:", download_path, "\n")
|
| 166 |
-
cat("Error was:", e$message, "\n")
|
| 167 |
-
|
| 168 |
-
download_response <- RETRY(
|
| 169 |
-
"GET",
|
| 170 |
-
url,
|
| 171 |
-
add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))),
|
| 172 |
-
write_disk(download_path, overwrite = TRUE),
|
| 173 |
-
times = 3
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
if (status_code(download_response) == 200) {
|
| 177 |
-
cat("Downloaded file to:", download_path, "\n")
|
| 178 |
-
cat("Please try reading this file directly with read_parquet(\"", download_path, "\")\n", sep = "")
|
| 179 |
-
}
|
| 180 |
-
|
| 181 |
-
file.remove(temp_file)
|
| 182 |
-
stop(paste("Could not read file as parquet:", e$message,
|
| 183 |
-
"\nFile downloaded to:", download_path))
|
| 184 |
-
})
|
| 185 |
-
} else {
|
| 186 |
-
file.remove(temp_file)
|
| 187 |
-
stop(paste("Failed to download dataset. Status code:", status_code(response)))
|
| 188 |
-
}
|
| 189 |
-
}
|
| 190 |
-
|
| 191 |
-
download_private_parquet <- function(repo_id, filename) {
|
| 192 |
url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
|
| 193 |
response <- GET(url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
|
| 194 |
|
| 195 |
if (status_code(response) == 200) {
|
| 196 |
-
# Get
|
| 197 |
-
|
| 198 |
|
| 199 |
-
# Check if this is an LFS pointer
|
| 200 |
-
|
|
|
|
|
|
|
| 201 |
# This is an LFS file - extract the oid (hash) from the pointer
|
|
|
|
| 202 |
oid_line <- grep("oid sha256:", strsplit(content_text, "\n")[[1]], value = TRUE)
|
| 203 |
oid <- gsub("oid sha256:", "", oid_line)
|
| 204 |
oid <- trimws(oid)
|
| 205 |
|
| 206 |
-
#
|
| 207 |
-
lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/
|
| 208 |
-
substr(oid, 1, 2), "/", substr(oid, 3, 4), "/", oid)
|
| 209 |
-
|
| 210 |
-
# Get the actual content from LFS storage
|
| 211 |
lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
|
| 212 |
|
| 213 |
if (status_code(lfs_response) == 200) {
|
| 214 |
-
|
| 215 |
} else {
|
| 216 |
-
#
|
| 217 |
-
lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/
|
|
|
|
| 218 |
lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
|
| 219 |
|
| 220 |
if (status_code(lfs_response) == 200) {
|
| 221 |
-
|
| 222 |
} else {
|
| 223 |
stop(paste("Failed to download LFS content. Status code:", status_code(lfs_response)))
|
| 224 |
}
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
-
#
|
| 229 |
-
|
|
|
|
|
|
|
| 230 |
tryCatch({
|
| 231 |
-
data <- read_parquet(
|
| 232 |
return(data)
|
| 233 |
}, error = function(e) {
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
| 238 |
})
|
| 239 |
} else {
|
| 240 |
stop(paste("Failed to download dataset. Status code:", status_code(response)))
|
|
|
|
| 70 |
library(httr)
|
| 71 |
library(arrow)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
|
| 74 |
response <- GET(url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
|
| 75 |
|
| 76 |
if (status_code(response) == 200) {
|
| 77 |
+
# Get raw content
|
| 78 |
+
content_raw <- content(response, "raw")
|
| 79 |
|
| 80 |
+
# Check if this is an LFS pointer by looking at the first few bytes
|
| 81 |
+
first_chunk <- rawToChar(head(content_raw, 100))
|
| 82 |
+
|
| 83 |
+
if (grepl("^version https://git-lfs.github.com/spec/", first_chunk)) {
|
| 84 |
# This is an LFS file - extract the oid (hash) from the pointer
|
| 85 |
+
content_text <- rawToChar(content_raw)
|
| 86 |
oid_line <- grep("oid sha256:", strsplit(content_text, "\n")[[1]], value = TRUE)
|
| 87 |
oid <- gsub("oid sha256:", "", oid_line)
|
| 88 |
oid <- trimws(oid)
|
| 89 |
|
| 90 |
+
# Try the alternative LFS URL format first (more reliable for Hugging Face)
|
| 91 |
+
lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/lfs/resolve/main/", filename, "?download=true")
|
|
|
|
|
|
|
|
|
|
| 92 |
lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
|
| 93 |
|
| 94 |
if (status_code(lfs_response) == 200) {
|
| 95 |
+
content_raw <- content(lfs_response, "raw")
|
| 96 |
} else {
|
| 97 |
+
# Try original LFS URL format as fallback
|
| 98 |
+
lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/.git/lfs/objects/",
|
| 99 |
+
substr(oid, 1, 2), "/", substr(oid, 3, 4), "/", oid)
|
| 100 |
lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
|
| 101 |
|
| 102 |
if (status_code(lfs_response) == 200) {
|
| 103 |
+
content_raw <- content(lfs_response, "raw")
|
| 104 |
} else {
|
| 105 |
stop(paste("Failed to download LFS content. Status code:", status_code(lfs_response)))
|
| 106 |
}
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
| 110 |
+
# Write to a temporary file and read with arrow
|
| 111 |
+
temp_file <- tempfile(fileext = ".parquet")
|
| 112 |
+
writeBin(content_raw, temp_file)
|
| 113 |
+
|
| 114 |
tryCatch({
|
| 115 |
+
data <- read_parquet(temp_file)
|
| 116 |
return(data)
|
| 117 |
}, error = function(e) {
|
| 118 |
+
stop(paste("Error parsing parquet file:", e$message))
|
| 119 |
+
}, finally {
|
| 120 |
+
# Clean up
|
| 121 |
+
if (file.exists(temp_file)) {
|
| 122 |
+
file.remove(temp_file)
|
| 123 |
+
}
|
| 124 |
})
|
| 125 |
} else {
|
| 126 |
stop(paste("Failed to download dataset. Status code:", status_code(response)))
|