TimStats commited on
Commit
9c243c2
·
verified ·
1 Parent(s): 8a11c47

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +39 -62
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 the raw binary content
210
- content_raw <- content(response, "raw")
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
- # Try to read the parquet file
221
- tryCatch({
222
- cat("Reading parquet file...\n")
223
- data <- read_parquet(temp_file)
 
 
224
 
225
- # Clean up
226
- unlink(temp_file)
 
227
 
228
- cat("Successfully read parquet file\n")
229
- return(data)
230
- }, error = function(e) {
231
- cat("Error reading parquet file:", e$message, "\n")
232
 
233
- # Try an alternative approach using the arrow C++ library directly
234
- cat("Trying alternative method...\n")
235
- tryCatch({
236
- # Create a buffer from raw content
237
- buffer <- arrow::py_buffer(content_raw)
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
- # Try with different options
249
- tryCatch({
250
- cat("Trying with options...\n")
251
- data <- read_parquet(temp_file, mmap = FALSE)
252
-
253
- unlink(temp_file)
254
- cat("Successfully read parquet with mmap=FALSE\n")
255
- return(data)
256
- }, error = function(e3) {
257
- cat("All methods failed to read the parquet file\n")
258
- stop(paste("Failed to read parquet file:", e3$message))
259
- })
260
- })
 
 
 
 
 
261
  })
262
  } else {
263
- stop(paste("Failed to download file. Status code:", status_code(response)))
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