Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -26,6 +26,8 @@ team_meta <- tryCatch({
|
|
| 26 |
NULL
|
| 27 |
})
|
| 28 |
|
|
|
|
|
|
|
| 29 |
# -------------------- GLOBAL CSS --------------------
|
| 30 |
app_css <- "
|
| 31 |
body { background-color: #f5f5f5; font-family: 'Segoe UI', Arial, sans-serif; }
|
|
@@ -160,8 +162,32 @@ app_css <- "
|
|
| 160 |
"
|
| 161 |
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
|
|
|
|
|
|
| 165 |
|
| 166 |
process_dataset <- function(df) {
|
| 167 |
if ("Batter" %in% names(df)) {
|
|
@@ -3360,13 +3386,22 @@ standardize_stuffplus_to_league <- function(data, league_comparison_data) {
|
|
| 3360 |
league_comparison_data[[col]] <- as.character(league_comparison_data[[col]])
|
| 3361 |
}
|
| 3362 |
}
|
| 3363 |
-
|
| 3364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3365 |
|
| 3366 |
data <- data %>%
|
| 3367 |
mutate(data_ind = 1)
|
| 3368 |
-
|
| 3369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3370 |
|
| 3371 |
league_comparison_data <- league_comparison_data %>%
|
| 3372 |
mutate(data_ind = 0)
|
|
@@ -3598,7 +3633,9 @@ create_advanced_pitch_characteristics <- function(data, player_name) {
|
|
| 3598 |
} else {
|
| 3599 |
message("WARNING: No reference data available. Using local standardization.")
|
| 3600 |
pitcher_data$raw_stuff <- tryCatch({
|
| 3601 |
-
|
|
|
|
|
|
|
| 3602 |
}, error = function(e) {
|
| 3603 |
message("Stuff+ prediction error: ", e$message)
|
| 3604 |
rep(NA_real_, nrow(pitcher_data))
|
|
|
|
| 26 |
NULL
|
| 27 |
})
|
| 28 |
|
| 29 |
+
|
| 30 |
+
|
| 31 |
# -------------------- GLOBAL CSS --------------------
|
| 32 |
app_css <- "
|
| 33 |
body { background-color: #f5f5f5; font-family: 'Segoe UI', Arial, sans-serif; }
|
|
|
|
| 162 |
"
|
| 163 |
|
| 164 |
|
| 165 |
+
download_private_rds <- function(repo_id, filename) {
|
| 166 |
+
url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
|
| 167 |
+
|
| 168 |
+
api_key <- Sys.getenv("HF_Token")
|
| 169 |
+
|
| 170 |
+
if (api_key == "") {
|
| 171 |
+
stop("API key is not set.")
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
response <- GET(url, add_headers(Authorization = paste("Bearer", api_key)))
|
| 175 |
+
|
| 176 |
+
if (status_code(response) == 200) {
|
| 177 |
+
temp_file <- tempfile(fileext = ".rds")
|
| 178 |
+
|
| 179 |
+
writeBin(content(response, "raw"), temp_file)
|
| 180 |
+
|
| 181 |
+
data <- readRDS(temp_file)
|
| 182 |
+
|
| 183 |
+
return(data)
|
| 184 |
+
} else {
|
| 185 |
+
stop(paste("Failed to download dataset. Status code:", status_code(response)))
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
|
| 189 |
+
stuffplus_recipe <- download_private_rds("CoastalBaseball/PitcherAppFiles", "stuffplus_recipe.rds")
|
| 190 |
+
stuffplus_model <- xgb.load("stuffplus_xgb.json")
|
| 191 |
|
| 192 |
process_dataset <- function(df) {
|
| 193 |
if ("Batter" %in% names(df)) {
|
|
|
|
| 3386 |
league_comparison_data[[col]] <- as.character(league_comparison_data[[col]])
|
| 3387 |
}
|
| 3388 |
}
|
| 3389 |
+
|
| 3390 |
+
df_processed <- bake(stuffplus_recipe, new_data = data)
|
| 3391 |
+
|
| 3392 |
+
df_matrix <- as.matrix(df_processed)
|
| 3393 |
+
|
| 3394 |
+
data$raw_stuff <- predict(stuffplus_model, df_matrix)
|
| 3395 |
|
| 3396 |
data <- data %>%
|
| 3397 |
mutate(data_ind = 1)
|
| 3398 |
+
|
| 3399 |
+
df_processed <- bake(stuffplus_recipe, new_data = league_comparison_data)
|
| 3400 |
+
|
| 3401 |
+
df_matrix <- as.matrix(df_processed)
|
| 3402 |
+
|
| 3403 |
+
league_comparison_data$raw_stuff <- predict(stuffplus_model, df_matrix)
|
| 3404 |
+
|
| 3405 |
|
| 3406 |
league_comparison_data <- league_comparison_data %>%
|
| 3407 |
mutate(data_ind = 0)
|
|
|
|
| 3633 |
} else {
|
| 3634 |
message("WARNING: No reference data available. Using local standardization.")
|
| 3635 |
pitcher_data$raw_stuff <- tryCatch({
|
| 3636 |
+
df_processed <- bake(stuffplus_recipe, new_data = pitcher_data)
|
| 3637 |
+
df_matrix <- as.matrix(df_processed)
|
| 3638 |
+
pitcher_data$raw_stuff <- predict(stuffplus_model, df_matrix)
|
| 3639 |
}, error = function(e) {
|
| 3640 |
message("Stuff+ prediction error: ", e$message)
|
| 3641 |
rep(NA_real_, nrow(pitcher_data))
|