Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deployTFModel and writeManifest support TensorFlow #1070

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* `deployApp()` and `writeManifest()` receive the default value for the
`image` argument from the `RSCONNECT_IMAGE` environment variable. (#1063)

* `deployTF()` can deploy a TensorFlow model to Posit Connect. Requires Posit
Connect 2024.05.0 or higher.

# rsconnect 1.2.2

* Use internally computed SHA1 sums and PKI signing when SHA1 is disabled
Expand Down
2 changes: 1 addition & 1 deletion R/appDependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ appDependencies <- function(appDir = getwd(),
}

needsR <- function(appMetadata) {
if (appMetadata$appMode == "static") {
if (appMetadata$appMode %in% c("static", "tensorflow-saved-model")) {
return(FALSE)
}

Expand Down
26 changes: 20 additions & 6 deletions R/appMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ appMetadata <- function(appDir,
appMode <- "shiny"
} else {
# Inference only uses top-level files
rootFiles <- appFiles[dirname(appFiles) == "."]
appMode <- inferAppMode(
file.path(appDir, rootFiles),
appDir,
appFiles,
usesQuarto = quarto,
isShinyappsServer = isShinyappsServer
)
Expand Down Expand Up @@ -98,10 +98,18 @@ checkAppLayout <- function(appDir, appPrimaryDoc = NULL) {
# can be run/served.
}

# infer the mode of the application from files in the root dir
inferAppMode <- function(absoluteRootFiles,
usesQuarto = NA,
isShinyappsServer = FALSE) {
# Infer the mode of the application from included files. Most content types
# only consider files at the directory root. TensorFlow saved models may be
# anywhere in the hierarchy.
inferAppMode <- function(
appDir,
appFiles,
usesQuarto = NA,
isShinyappsServer = FALSE) {

rootFiles <- appFiles[dirname(appFiles) == "."]
absoluteRootFiles <- file.path(appDir, rootFiles)
absoluteAppFiles <- file.path(appDir, appFiles)
Comment on lines +110 to +112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird that TensorFlow files can be anywhere in the hierarchy! I guess for those, you may be using R in any working dir and are just pointing to a TF file? Maybe not weird, just different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not "anywhere", but it's not at the root. For example, this is a "model" directory with an accompanying manifest.json at the root.

model
model/1
model/1/variables
model/1/variables/variables.data-00000-of-00001
model/1/variables/variables.index
model/1/saved_model.pb
model/manifest.json


matchingNames <- function(paths, pattern) {
idx <- grepl(pattern, basename(paths), ignore.case = TRUE, perl = TRUE)
Expand Down Expand Up @@ -190,6 +198,12 @@ inferAppMode <- function(absoluteRootFiles,
return("quarto-static")
}

# TensorFlow model files are lower in the hierarchy, not at the root.
modelFiles <- matchingNames(absoluteAppFiles, "^(saved_model.pb|saved_model.pbtxt)$")
if (length(modelFiles) > 0) {
return("tensorflow-saved-model")
}

# no renderable content
"static"
}
Expand Down
17 changes: 4 additions & 13 deletions R/deployTFModel.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
#' Deploy a TensorFlow saved model
#'
#' This function is defunct. Posit Connect no longer supports hosting of
#' TensorFlow Model APIs. A TensorFlow model can be deployed as a
#' [Plumber API](https://tensorflow.rstudio.com/guides/deploy/plumber.html),
#' [Shiny application](https://tensorflow.rstudio.com/guides/deploy/shiny), or
#' other supported content type.
#' Deploys a directory containing a TensorFlow saved model.
#'
#' @param modelDir Path to the saved model directory. Unused.
#' @param ... Unused.
#' #' @inheritParams deployApp
#'
#' @family Deployment functions
#' @export
deployTFModel <- function(modelDir, ...) {
lifecycle::deprecate_stop(
when = "1.0.0",
what = "deployTFModel()",
details = "Posit Connect no longer supports hosting of TensorFlow Model APIs."
)
deployTFModel <- function(...) {
deployApp(appMode = "tensorflow-saved-model", ...)
}
16 changes: 5 additions & 11 deletions man/deployTFModel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test-writeManifest.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@ test_that("environment.image is not set when image is not provided", {
expect_null(manifest$environment)
})

test_that("TensorFlow models are identified", {
skip_on_cran()

app_dir <- local_temp_app(list(
"1/saved_model.pb" = "fake-saved-model"
))
manifest <- makeManifest(app_dir)
expect_equal(manifest$metadata$appmode, "tensorflow-saved-model")
expect_null(manifest$packages)
expect_named(manifest$files, c("1/saved_model.pb"))
})

test_that("environment.image is set when image is provided", {
skip_on_cran()

Expand Down
Loading