TimStats commited on
Commit
5892bee
·
verified ·
1 Parent(s): 43bd013

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +100 -47
app.R CHANGED
@@ -74,60 +74,113 @@ download_private_parquet <- function(repo_id, filename) {
74
  response <- GET(url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
75
 
76
  if (status_code(response) == 200) {
77
- # First check if it's an LFS pointer
78
  content_raw <- content(response, "raw")
79
- content_text <- rawToChar(content_raw)
80
 
81
- # Check if this is an LFS pointer
82
- if (grepl("^version https://git-lfs.github.com/spec/", content_text)) {
83
- # Extract the oid (hash) from the pointer
84
- oid_line <- grep("oid sha256:", strsplit(content_text, "\n")[[1]], value = TRUE)
85
- oid <- gsub("oid sha256:", "", oid_line)
86
- oid <- trimws(oid)
87
-
88
- # Construct the LFS content URL
89
- lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/.git/lfs/objects/",
90
- substr(oid, 1, 2), "/", substr(oid, 3, 4), "/", oid)
91
-
92
- # Get the actual content from LFS storage
93
- lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
94
 
95
- if (status_code(lfs_response) == 200) {
96
- content_raw <- content(lfs_response, "raw")
97
- } else {
98
- # Alternative LFS URL format
99
- lfs_url <- paste0("https://huggingface.co/datasets/", repo_id, "/lfs/resolve/main/", filename, "?download=true")
100
- lfs_response <- GET(lfs_url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
101
 
102
- if (status_code(lfs_response) == 200) {
103
- content_raw <- content(lfs_response, "raw")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  } else {
105
- stop(paste("Failed to download LFS content. Status code:", status_code(lfs_response)))
 
106
  }
107
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  }
109
-
110
- # Write the raw content to a temporary file
111
- temp_file <- tempfile(fileext = ".parquet")
112
- writeBin(content_raw, temp_file)
113
-
114
- # Read the parquet file using arrow
115
- tryCatch({
116
- data <- read_parquet(temp_file)
117
-
118
- # Clean up - remove the temporary file
119
- if (file.exists(temp_file)) {
120
- file.remove(temp_file)
121
- }
122
-
123
- return(data)
124
- }, error = function(e) {
125
- # Clean up in case of error
126
- if (file.exists(temp_file)) {
127
- file.remove(temp_file)
128
- }
129
- stop(paste("Error reading parquet file:", e$message))
130
- })
131
  } else {
132
  stop(paste("Failed to download dataset. Status code:", status_code(response)))
133
  }
 
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
  }