Update app.R
Browse files
app.R
CHANGED
|
@@ -189,78 +189,55 @@ download_private_parquet <- function(repo_id, filename) {
|
|
| 189 |
}
|
| 190 |
|
| 191 |
download_private_parquet <- function(repo_id, filename) {
|
| 192 |
-
library(httr)
|
| 193 |
-
library(arrow)
|
| 194 |
-
|
| 195 |
-
# Main URL for direct file download
|
| 196 |
url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
|
| 197 |
-
|
| 198 |
-
cat("Downloading from URL:", url, "\n")
|
| 199 |
-
|
| 200 |
-
# Download the file content directly into memory
|
| 201 |
-
response <- RETRY(
|
| 202 |
-
"GET",
|
| 203 |
-
url,
|
| 204 |
-
add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))),
|
| 205 |
-
times = 3
|
| 206 |
-
)
|
| 207 |
|
| 208 |
if (status_code(response) == 200) {
|
| 209 |
-
# Get
|
| 210 |
-
|
| 211 |
-
cat("Content downloaded, size:", length(content_raw), "bytes\n")
|
| 212 |
-
|
| 213 |
-
# Write to a temporary file in a directory we should have access to
|
| 214 |
-
temp_dir <- tempdir()
|
| 215 |
-
temp_file <- file.path(temp_dir, basename(filename))
|
| 216 |
-
|
| 217 |
-
cat("Writing to temporary file:", temp_file, "\n")
|
| 218 |
-
writeBin(content_raw, temp_file)
|
| 219 |
|
| 220 |
-
#
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
#
|
| 226 |
-
|
|
|
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
}, error = function(e) {
|
| 231 |
-
cat("Error reading parquet file:", e$message, "\n")
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
#
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
# Read from the buffer
|
| 240 |
-
data <- arrow::read_parquet(buffer)
|
| 241 |
-
|
| 242 |
-
unlink(temp_file)
|
| 243 |
-
cat("Successfully read parquet using buffer method\n")
|
| 244 |
-
return(data)
|
| 245 |
-
}, error = function(e2) {
|
| 246 |
-
cat("Alternative method failed:", e2$message, "\n")
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
})
|
| 262 |
} else {
|
| 263 |
-
stop(paste("Failed to download
|
| 264 |
}
|
| 265 |
}
|
| 266 |
|
|
|
|
| 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 content as text first to check if it's an LFS pointer
|
| 197 |
+
content_text <- content(response, "text", encoding = "UTF-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
# Check if this is an LFS pointer (LFS files start with "version https://git-lfs.github.com/spec/")
|
| 200 |
+
if (grepl("^version https://git-lfs.github.com/spec/", content_text)) {
|
| 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 |
+
# Construct the LFS content URL
|
| 207 |
+
lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/.git/lfs/objects/",
|
| 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 |
+
content_text <- content(lfs_response, "text", encoding = "UTF-8")
|
| 215 |
+
} else {
|
| 216 |
+
# Alternative LFS URL format
|
| 217 |
+
lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/lfs/resolve/main/", filename, "?download=true")
|
| 218 |
+
lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
+
if (status_code(lfs_response) == 200) {
|
| 221 |
+
content_text <- content(lfs_response, "text", encoding = "UTF-8")
|
| 222 |
+
} else {
|
| 223 |
+
stop(paste("Failed to download LFS content. Status code:", status_code(lfs_response)))
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
# Process the content (whether it was LFS or regular)
|
| 229 |
+
con <- textConnection(content_text)
|
| 230 |
+
tryCatch({
|
| 231 |
+
data <- read_parquet(con)
|
| 232 |
+
return(data)
|
| 233 |
+
}, error = function(e) {
|
| 234 |
+
close(con)
|
| 235 |
+
stop(paste("Error parsing CSV:", e$message))
|
| 236 |
+
}, finally = {
|
| 237 |
+
close(con)
|
| 238 |
})
|
| 239 |
} else {
|
| 240 |
+
stop(paste("Failed to download dataset. Status code:", status_code(response)))
|
| 241 |
}
|
| 242 |
}
|
| 243 |
|