TimStats commited on
Commit
4008409
·
verified ·
1 Parent(s): 9367fb6

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +47 -11
app.R CHANGED
@@ -13,19 +13,55 @@ download_private_csv <- function(repo_id, filename) {
13
  response <- GET(url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
14
 
15
  if (status_code(response) == 200) {
16
- content <- content(response, "text")
17
- con <- textConnection(content)
18
 
19
- # Try different read options
20
- data <- read.csv(con,
21
- header = TRUE,
22
- check.names = FALSE, # This prevents R from modifying column names
23
- fileEncoding = "UTF-8",
24
- stringsAsFactors = FALSE)
25
- close(con)
26
- return(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  } else {
28
- stop("Failed to download dataset")
29
  }
30
  }
31
 
 
13
  response <- GET(url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
14
 
15
  if (status_code(response) == 200) {
16
+ # Get content as text first to check if it's an LFS pointer
17
+ content_text <- content(response, "text", encoding = "UTF-8")
18
 
19
+ # Check if this is an LFS pointer (LFS files start with "version https://git-lfs.github.com/spec/")
20
+ if (grepl("^version https://git-lfs.github.com/spec/", content_text)) {
21
+ # This is an LFS file - extract the oid (hash) from the pointer
22
+ oid_line <- grep("oid sha256:", strsplit(content_text, "\n")[[1]], value = TRUE)
23
+ oid <- gsub("oid sha256:", "", oid_line)
24
+ oid <- trimws(oid)
25
+
26
+ # Construct the LFS content URL
27
+ lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/.git/lfs/objects/",
28
+ substr(oid, 1, 2), "/", substr(oid, 3, 4), "/", oid)
29
+
30
+ # Get the actual content from LFS storage
31
+ lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
32
+
33
+ if (status_code(lfs_response) == 200) {
34
+ content_text <- content(lfs_response, "text", encoding = "UTF-8")
35
+ } else {
36
+ # Alternative LFS URL format
37
+ lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/lfs/resolve/main/", filename, "?download=true")
38
+ lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
39
+
40
+ if (status_code(lfs_response) == 200) {
41
+ content_text <- content(lfs_response, "text", encoding = "UTF-8")
42
+ } else {
43
+ stop(paste("Failed to download LFS content. Status code:", status_code(lfs_response)))
44
+ }
45
+ }
46
+ }
47
+
48
+ # Process the content (whether it was LFS or regular)
49
+ con <- textConnection(content_text)
50
+ tryCatch({
51
+ data <- read.csv(con,
52
+ header = TRUE,
53
+ check.names = FALSE,
54
+ fileEncoding = "UTF-8",
55
+ stringsAsFactors = FALSE)
56
+ return(data)
57
+ }, error = function(e) {
58
+ close(con)
59
+ stop(paste("Error parsing CSV:", e$message))
60
+ }, finally = {
61
+ close(con)
62
+ })
63
  } else {
64
+ stop(paste("Failed to download dataset. Status code:", status_code(response)))
65
  }
66
  }
67