OwenStOnge commited on
Commit
f606e01
·
verified ·
1 Parent(s): c28379a

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +20 -47
app.R CHANGED
@@ -1999,56 +1999,29 @@ observeEvent(input$upload_hf_btn, {
1999
  arrow::write_parquet(combined, tmp)
2000
 
2001
  result <- tryCatch({
2002
- # Save parquet as CSV instead to avoid LFS issues
2003
- tmp_csv <- tempfile(fileext = ".csv")
2004
- write.csv(combined, tmp_csv, row.names = FALSE)
2005
-
2006
- file_bytes <- readBin(tmp_csv, "raw", file.info(tmp_csv)$size)
2007
- file_b64 <- base64enc::base64encode(file_bytes)
2008
-
2009
- csv_filename <- gsub("\\.parquet$", ".csv", filename)
2010
-
2011
- # Use the git create/update file API
2012
- body <- jsonlite::toJSON(list(
2013
- message = paste("Update", csv_filename),
2014
- content = file_b64
2015
- ), auto_unbox = TRUE)
2016
-
2017
- resp <- httr::PUT(
2018
- paste0("https://huggingface.co/api/datasets/", repo_id, "/blob/main/", csv_filename),
2019
- httr::add_headers(
2020
- Authorization = paste("Bearer", hf_token),
2021
- `Content-Type` = "application/json"
2022
- ),
2023
- body = body,
2024
- encode = "raw"
2025
- )
2026
-
2027
- resp_text <- httr::content(resp, as = "text", encoding = "UTF-8")
2028
-
2029
- if (httr::status_code(resp) %in% c(200, 201)) {
2030
  paste0("Done! ", nrow(combined), " total rows in dataset.")
2031
  } else {
2032
- # Try alternate endpoint
2033
- resp2 <- httr::PUT(
2034
- paste0("https://huggingface.co/api/datasets/", repo_id, "/content/main/", csv_filename),
2035
- httr::add_headers(
2036
- Authorization = paste("Bearer", hf_token),
2037
- `Content-Type` = "application/json"
2038
- ),
2039
- body = body,
2040
- encode = "raw"
2041
- )
2042
-
2043
- resp_text2 <- httr::content(resp2, as = "text", encoding = "UTF-8")
2044
-
2045
- if (httr::status_code(resp2) %in% c(200, 201)) {
2046
- paste0("Done! ", nrow(combined), " total rows in dataset.")
2047
- } else {
2048
- paste0("Upload failed: ", httr::status_code(resp2), " - ", resp_text2)
2049
- }
2050
  }
2051
-
2052
  }, error = function(e) {
2053
  paste("Upload error:", e$message)
2054
  })
 
1999
  arrow::write_parquet(combined, tmp)
2000
 
2001
  result <- tryCatch({
2002
+ header_file <- tempfile(fileext = ".json")
2003
+ cat(sprintf(
2004
+ '{"summary":"Update %s","operations":[{"key":"file0","path_in_repo":"%s"}]}',
2005
+ filename, filename
2006
+ ), file = header_file)
2007
+
2008
+ resp <- system2("/usr/bin/curl", args = c(
2009
+ "-s",
2010
+ "-X", "POST",
2011
+ "-H", paste0("Authorization: Bearer ", hf_token),
2012
+ "-F", paste0("header=@", header_file, ";type=application/json"),
2013
+ "-F", paste0("file0=@", tmp, ";type=application/octet-stream"),
2014
+ paste0("https://huggingface.co/api/datasets/", repo_id, "/commit/main")
2015
+ ), stdout = TRUE, stderr = TRUE)
2016
+
2017
+ file.remove(header_file)
2018
+ resp_text <- paste(resp, collapse = "\n")
2019
+
2020
+ if (grepl("commitOid", resp_text)) {
 
 
 
 
 
 
 
 
 
2021
  paste0("Done! ", nrow(combined), " total rows in dataset.")
2022
  } else {
2023
+ paste0("Upload response: ", resp_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2024
  }
 
2025
  }, error = function(e) {
2026
  paste("Upload error:", e$message)
2027
  })