Skip to content

Commit

Permalink
Update to pins
Browse files Browse the repository at this point in the history
  • Loading branch information
akgold committed Aug 26, 2020
1 parent 4f319a4 commit b867982
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 98 deletions.
4 changes: 2 additions & 2 deletions 1_train_model.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://blogs.rstudio.com/tensorflow/posts/2018-01-29-dl-for-cancer-immunotherapy/

Sys.unsetenv("RETICULATE_PYTHON")
# Sys.unsetenv("RETICULATE_PYTHON")

suppressPackageStartupMessages({
library(tidyverse)
Expand Down Expand Up @@ -144,4 +144,4 @@ results %>%

# And this is the recommended way
model %>%
save_model_tf("saved_models")
save_model_tf("saved_model")
22 changes: 15 additions & 7 deletions 2_publish_model.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
library(rsconnect)
library(pins)
library(keras)

rsconnect::deployTFModel(
modelDir = "saved_models",
# server = "{server}", # <<- edit this line if necessary
# account = "{account}", # <<- edit this line if necessary
appTitle = "immunotherapy",
forceUpdate = TRUE
# Pin it on RStudio Connect
pins::board_register_rsconnect()
pins::pin(
"saved_model",
"peptide_model",
"Peptide Prediction Model",
"rsconnect",
zip = TRUE
)

mod_pinned <- pins::pin_get("peptide_model")
utils::unzip(mod_pinned, exdir = fs::path_dir(mod_pinned))

mod <- keras::load_model_tf(file.path(fs::path_dir(mod_pinned), "saved_model"))
File renamed without changes.
75 changes: 0 additions & 75 deletions plumber/3_consume_tf_api.R

This file was deleted.

41 changes: 27 additions & 14 deletions plumber/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,42 @@
library(plumber)
library(config)
library(reticulate)
library(PepTools)

source("3_consume_tf_api.R")
# Get pinned model from RStudio Connect
pins::board_register_rsconnect(
server = config::get("rsc_url"),
key = config::get("rsc_api_key")
)
mod_pinned <- pins::pin_get("peptide_model")
utils::unzip(mod_pinned[1], exdir = fs::path_dir(mod_pinned[1]))
mod <- keras::load_model_tf(file.path(fs::path_dir(mod_pinned[1]), "saved_model"))

#* @apiTitle Immunotherapy

#* Predict peptide class
#* @param peptide Character vector with a single peptide, eg. `"LLTDAQRIV"` or comma separated, e.g. `"LLTDAQRIV, LMAFYLYEV, VMSPITLPT, SLHLTNCFV, RQFTCMIAV"`
#* @get /predict
function(peptide, solo_url){
if (missing(solo_url) || is.null(solo_url)) {
solo_url <- config::get("solo_url_tensorflow") # TensorFlow API
}
function(peptide){
# Peptide classes for prediction
peptide_classes <- c("NB", "WB", "SB")

# split on commas and remove white space
peptide <- trimws(strsplit(peptide, ",")[[1]])

predict_peptide_class_fun(peptide = peptide, solo_url = solo_url)
# transform input into flattened array
x_val <-
peptide %>%
pep_encode() %>%
array_reshape(dim = c(nrow(.), 9*20))

# Get predictions from models
preds <- mod %>%
keras::predict_classes(x_val)

# Return original peptides with predictions
tibble::tibble(
peptide = peptide,
peptide_classes = peptide_classes[preds]
)
}



# peptide <- "LLTDAQRIV"

# source("3_consume_tf_api.R")
# solo_url <- config::get("solo_url_tensorflow")
# predict_peptide_class_fun(peptide = "LLTDAQRIV", solo_url = solo_url)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: immunotherapy_api
title: Immunotherapy API
username: alex.gold
account: alex.gold
server: colorado.rstudio.com
hostUrl: https://colorado.rstudio.com/rsc/__api__
appId: 7295
bundleId: 40050
url: https://colorado.rstudio.com/rsc/content/7295/
when: 1598468216.99496
asMultiple: FALSE
asStatic: FALSE
Binary file added saved_model/saved_model.pb
Binary file not shown.
Binary file not shown.
Binary file added saved_model/variables/variables.index
Binary file not shown.

0 comments on commit b867982

Please sign in to comment.