Update app.R
Browse files
app.R
CHANGED
|
@@ -73,61 +73,114 @@ download_private_parquet <- function(repo_id, filename) {
|
|
| 73 |
# Main URL for direct file download
|
| 74 |
url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
temp_file <- tempfile(fileext = ".parquet")
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
response <-
|
|
|
|
| 81 |
url,
|
| 82 |
add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))),
|
| 83 |
-
write_disk(temp_file, overwrite = TRUE)
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
if (status_code(response) == 200) {
|
| 87 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
tryCatch({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
data <- read_parquet(temp_file)
|
| 90 |
file.remove(temp_file)
|
| 91 |
return(data)
|
| 92 |
}, error = function(e) {
|
| 93 |
-
#
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
write_disk(temp_file, overwrite = TRUE)
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
if (status_code(lfs_response) == 200) {
|
| 114 |
-
# Try to read the LFS file as parquet
|
| 115 |
-
tryCatch({
|
| 116 |
-
data <- read_parquet(temp_file)
|
| 117 |
-
file.remove(temp_file)
|
| 118 |
-
return(data)
|
| 119 |
-
}, error = function(e2) {
|
| 120 |
-
file.remove(temp_file)
|
| 121 |
-
stop(paste("Downloaded LFS content is not a valid parquet file:", e2$message))
|
| 122 |
-
})
|
| 123 |
-
} else {
|
| 124 |
-
file.remove(temp_file)
|
| 125 |
-
stop(paste("Failed to download LFS content. Status code:", status_code(lfs_response)))
|
| 126 |
-
}
|
| 127 |
-
} else {
|
| 128 |
-
file.remove(temp_file)
|
| 129 |
-
stop(paste("Downloaded content is not a valid parquet file:", e$message))
|
| 130 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
})
|
| 132 |
} else {
|
| 133 |
file.remove(temp_file)
|
|
|
|
| 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)
|