Skip to content

Commit

Permalink
Merge pull request #6 from atomashevic/atomashevic/issue5
Browse files Browse the repository at this point in the history
Atomashevic/issue5 Package now uses a package-specific miniconda environment
  • Loading branch information
atomashevic authored Nov 22, 2023
2 parents 440d22d + 4e46871 commit 2c512fc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
50 changes: 42 additions & 8 deletions R/setup_miniconda.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#' Install Miniconda
#' Install Miniconda and activate the transforEmotion environment
#'
#' @description Installs miniconda
#' @description Installs miniconda and activates the transforEmotion environment
#'
#' @details Installs miniconda using \code{\link[reticulate]{install_miniconda}}
#' @details Installs miniconda using \code{\link[reticulate]{install_miniconda}} and activates the transforEmotion environment using \code{\link[reticulate]{use_condaenv}}. If the transforEmotion environment does not exist, it will be created using \code{\link[reticulate]{conda_create}}.
#'
#' @author Alexander P. Christensen <[email protected]>
#' Aleksandar Tomasevic <[email protected]>
#'
#' @export
#'
# Install miniconda
# Updated 13.04.2022
# Updated 15.11.2023
setup_miniconda <- function()
{

Expand All @@ -22,9 +23,42 @@ setup_miniconda <- function()
# Check for try-error
if(any(class(path_to_miniconda) != "try-error")){

# Give user the deets
message("\nTo uninstall miniconda, use `reticulate::miniconda_uninstall()`")
# Give user the deets
message("\nTo uninstall miniconda, use `reticulate::miniconda_uninstall()`")

}

}

# Create transformEmotion enviroment if it doesn't exist

if (!(reticulate::condaenv_exists("transforEmotion"))){
path_to_env <- try(
reticulate::conda_create("transforEmotion"),
silent = TRUE
)

# Check for try-error
if(any(class(path_to_env) != "try-error")){

# Give user the deets
message("\nNew Python virtual environment created. To remove it, use: \n `reticulate::conda_remove(\"transforEmotion\")`")
}

}

# Activate the environment

Sys.unsetenv("RETICULATE_PYTHON")
reticulate::use_condaenv("transforEmotion", required = TRUE)

# Check if the enviroment is activated

if (grepl("transforEmotion", reticulate::py_config()$python)){
message("\ntransforEmotion Python virtual environment activated")
} else {
# throw an error if the environment is not activated
print("Your active Python environment is:")
print(reticulate::py_config()$python)
stop("Please activate the transforEmotion Python environment instead: `reticulate::use_condaenv(\"transforEmotion\", required = TRUE)`")
}

}
2 changes: 1 addition & 1 deletion R/setup_modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ setup_modules <- function()
Sys.sleep(1) # one second pause before the console explodes with text

# Actually install the modules
reticulate::conda_install(
reticulate::conda_install("transforEmotion",
packages = c(
"torch", "torchvision",
"torchaudio", "tensorflow",
Expand Down
4 changes: 2 additions & 2 deletions R/transformer_scores.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ transformer_scores <- function(
# Run setup for miniconda
setup_miniconda()

# Check if 'transformers' module is available
if(!reticulate::py_module_available("transformers")){
# Check if 'torch' and 'transformers' module are available
if(!(reticulate::py_module_available("transformers") & reticulate::py_module_available("torch"))){

# Run setup for modules
setup_modules()
Expand Down
7 changes: 4 additions & 3 deletions man/setup_miniconda.Rd

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

0 comments on commit 2c512fc

Please sign in to comment.