OwenStOnge commited on
Commit
75dbc5b
·
verified ·
1 Parent(s): 3b30050

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +42 -22
app.R CHANGED
@@ -1999,36 +1999,56 @@ observeEvent(input$upload_hf_btn, {
1999
  arrow::write_parquet(combined, tmp)
2000
 
2001
  result <- tryCatch({
2002
- header_file <- tempfile(fileext = ".json")
2003
- writeLines(sprintf(
2004
- '{"summary":"Update %s","operations":[{"key":"file0","path_in_repo":"%s"}]}',
2005
- filename, filename
2006
- ), header_file)
2007
-
2008
- h <- curl::new_handle()
2009
- curl::handle_setheaders(h,
2010
- "Authorization" = paste("Bearer", hf_token)
2011
- )
2012
 
2013
- curl::handle_setform(h,
2014
- header = curl::form_file(header_file, type = "application/json"),
2015
- file0 = curl::form_file(tmp, type = "application/octet-stream")
2016
- )
2017
 
2018
- resp <- curl::curl_fetch_memory(
2019
- paste0("https://huggingface.co/api/datasets/", repo_id, "/commit/main"),
2020
- handle = h
2021
- )
 
 
 
2022
 
2023
- file.remove(header_file)
 
 
 
 
 
 
 
 
2024
 
2025
- resp_text <- rawToChar(resp$content)
2026
 
2027
- if (resp$status_code %in% c(200, 201)) {
2028
  paste0("Done! ", nrow(combined), " total rows in dataset.")
2029
  } else {
2030
- paste0("Upload failed: ", resp$status_code, " - ", resp_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2031
  }
 
2032
  }, error = function(e) {
2033
  paste("Upload error:", e$message)
2034
  })
 
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
  })