TimStats commited on
Commit
3212dea
·
verified ·
1 Parent(s): 5892bee

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +47 -98
app.R CHANGED
@@ -70,118 +70,67 @@ download_private_parquet <- function(repo_id, filename) {
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
- content_raw <- content(response, "raw")
78
-
79
- # Check first few bytes to see if it's a parquet file
80
- # Parquet files start with "PAR1"
81
- if (length(content_raw) >= 4 &&
82
- rawToChar(content_raw[1:4]) == "PAR1") {
83
- # It's a parquet file, process it directly
84
- temp_file <- tempfile(fileext = ".parquet")
85
- writeBin(content_raw, temp_file)
86
 
87
- tryCatch({
88
- data <- read_parquet(temp_file)
 
 
 
 
89
 
90
- # Clean up
91
- if (file.exists(temp_file)) {
92
- file.remove(temp_file)
93
- }
94
 
95
- return(data)
96
- }, error = function(e) {
97
- if (file.exists(temp_file)) {
98
- file.remove(temp_file)
99
- }
100
- stop(paste("Error reading parquet file:", e$message))
101
- })
102
- } else {
103
- # It might be an LFS pointer, try to safely read it as text
104
- tryCatch({
105
- # Try to convert only the start of the content to check for LFS pointer
106
- # This avoids issues with embedded nulls
107
- first_100_chars <- rawToChar(content_raw[1:min(100, length(content_raw))])
108
 
109
- if (grepl("^version https://git-lfs.github.com/spec/", first_100_chars)) {
110
- # Convert the whole content to text, assuming it's small and valid text
111
- content_text <- rawToChar(content_raw)
112
-
113
- # Extract the oid (hash) from the pointer
114
- oid_line <- grep("oid sha256:", strsplit(content_text, "\n")[[1]], value = TRUE)
115
- oid <- gsub("oid sha256:", "", oid_line)
116
- oid <- trimws(oid)
117
-
118
- # Construct the LFS content URL
119
- lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/.git/lfs/objects/",
120
- substr(oid, 1, 2), "/", substr(oid, 3, 4), "/", oid)
121
-
122
- # Get the actual content from LFS storage
123
- lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
124
-
125
- if (status_code(lfs_response) == 200) {
126
- content_raw <- content(lfs_response, "raw")
127
- } else {
128
- # Alternative LFS URL format
129
- lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/lfs/resolve/main/", filename, "?download=true")
130
- lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
131
-
132
- if (status_code(lfs_response) == 200) {
133
- content_raw <- content(lfs_response, "raw")
134
- } else {
135
- stop(paste("Failed to download LFS content. Status code:", status_code(lfs_response)))
136
- }
137
- }
138
-
139
- # Write to temp file and read with arrow
140
- temp_file <- tempfile(fileext = ".parquet")
141
- writeBin(content_raw, temp_file)
142
-
143
  tryCatch({
144
  data <- read_parquet(temp_file)
145
-
146
- # Clean up
147
- if (file.exists(temp_file)) {
148
- file.remove(temp_file)
149
- }
150
-
151
  return(data)
152
- }, error = function(e) {
153
- if (file.exists(temp_file)) {
154
- file.remove(temp_file)
155
- }
156
- stop(paste("Error reading parquet file:", e$message))
157
  })
158
  } else {
159
- # Not an LFS pointer, but also not a valid parquet file
160
- stop("Downloaded content is neither a valid parquet file nor an LFS pointer")
161
  }
162
- }, error = function(e) {
163
- # If we can't even read the first few bytes as text, assume it's a binary parquet file
164
- temp_file <- tempfile(fileext = ".parquet")
165
- writeBin(content_raw, temp_file)
166
-
167
- tryCatch({
168
- data <- read_parquet(temp_file)
169
-
170
- # Clean up
171
- if (file.exists(temp_file)) {
172
- file.remove(temp_file)
173
- }
174
-
175
- return(data)
176
- }, error = function(e2) {
177
- if (file.exists(temp_file)) {
178
- file.remove(temp_file)
179
- }
180
- stop(paste("Error reading content as parquet:", e2$message))
181
- })
182
- })
183
- }
184
  } else {
 
185
  stop(paste("Failed to download dataset. Status code:", status_code(response)))
186
  }
187
  }
 
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
+ # Create a temporary file to save the download
77
+ temp_file <- tempfile(fileext = ".parquet")
78
+
79
+ # Try direct download first
80
+ response <- GET(
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
+ # Try to read the downloaded file as parquet
88
+ tryCatch({
89
+ data <- read_parquet(temp_file)
90
+ file.remove(temp_file)
91
+ return(data)
92
+ }, error = function(e) {
93
+ # Check if this might be an LFS pointer
94
+ content_text <- readLines(temp_file, warn = FALSE)
 
95
 
96
+ # Check if this is an LFS pointer
97
+ if (any(grepl("^version https://git-lfs.github.com/spec/", content_text))) {
98
+ # Extract the oid (hash) from the pointer
99
+ oid_line <- grep("oid sha256:", content_text, value = TRUE)
100
+ oid <- gsub("oid sha256:", "", oid_line)
101
+ oid <- trimws(oid)
102
 
103
+ # Alternative LFS URL format (this is more reliable for Hugging Face)
104
+ lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/lfs/resolve/main/", filename, "?download=true")
 
 
105
 
106
+ # Download LFS content to the temp file
107
+ lfs_response <- GET(
108
+ lfs_url,
109
+ add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))),
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)
134
  stop(paste("Failed to download dataset. Status code:", status_code(response)))
135
  }
136
  }