Skip to content

Commit

Permalink
fixed #8, changed loading method of conda environment
Browse files Browse the repository at this point in the history
  • Loading branch information
atomashevic committed Dec 11, 2023
1 parent 5eec5a3 commit e75566e
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 123 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export(image_scores)
export(nlp_scores)
export(punctuate)
export(setup_miniconda)
export(setup_modules)
export(simulate_video)
export(transformer_scores)
export(video_scores)
Expand Down
16 changes: 5 additions & 11 deletions R/image_scores.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@


image_scores <- function(image, classes, face_selection = "largest"){
if (!(reticulate::condaenv_exists("transforEmotion"))){
print("Creating and switching to transforEmotion virtual Python environment...")
Sys.sleep(1)
setup_miniconda()
} else
{
reticulate::use_condaenv("transforEmotion", required = FALSE)
if (!conda_check()){
stop("Python environment 'transforEmotion' is not available. Please run setup_miniconda() to install it.")

}
if (!reticulate::py_module_available("transformers") &!reticulate::py_module_available("face_recognition")){
print("Some Python libraries are not available in the transforEmotion conda environment. We are going to set them up. We need them for facial recognition and emotion detection in text, images and video. This may take a while, please be patient.")
Sys.sleep(1)
setup_modules()
else {
reticulate::use_condaenv("transforEmotion", required = FALSE)
}

source_python(system.file("python", "image.py", package = "transforEmotion"))
Expand Down
68 changes: 7 additions & 61 deletions R/setup_miniconda.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,6 @@ conda_check <- function(){
return (tE_env!=0)
}

#' Check if required Python libraries are installed in a Conda environment
#'
#' This function checks if a list of required Python libraries are installed in a specified Conda environment.
#'
#' @return A logical value indicating whether all the required Python libraries are installed.
#' reticulate::py_module_available("transformers")
check_python_libs <- function() {
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = '3')
python_libs <- c("transformers", "torch", "torchvision", "torchaudio", "tensorflow", "pytube", "pytz", "cv2")

# Extract the names of the libraries
lib_names <- sapply(strsplit(python_libs, " "), `[`, 1)
libs_installed <- logical(length(python_libs))

# Check if each Python library is installed
for (i in seq_along(python_libs)) {
python_lib <- python_libs[i]
if (!reticulate::py_module_available(python_lib)) {
print(paste("Python library", python_lib, "is not installed in Conda environment"))
libs_installed[i] <- FALSE
} else{
libs_installed[i] <- TRUE
}
}
return(all(libs_installed))
}

#' Install Miniconda and activate the transforEmotion environment
#'
#' @description Installs miniconda and activates the transforEmotion environment
Expand All @@ -58,58 +31,31 @@ check_python_libs <- function() {
setup_miniconda <- function()
{
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = '3')

# Install miniconda
path_to_miniconda <- try(
reticulate::install_miniconda(),
install_miniconda(),
silent = TRUE
)

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

# Give user the deets
if(any(class(path_to_miniconda) != "try-error")){
message("\nTo uninstall miniconda, use `reticulate::miniconda_uninstall()`")

}

# Create transformEmotion enviroment if it doesn't exist
te_ENV <- conda_check()

if (!te_ENV){
print("Creating 'transforEmotion' environment...")
path_to_env <- try(
reticulate::conda_create("transforEmotion"),
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")
if (!check_python_libs())
{
print("Installing missing Python libraries...")

print("Installing missing Python libraries...")
setup_modules()
}

} 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)`")
}

}

18 changes: 3 additions & 15 deletions R/setup_modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@
#'
#' @author Alexander P. Christensen <[email protected]>
#'
#' @export
#'
# Install modules
# Updated 13.04.2022
# Updated 11.12.2023

setup_modules <- function()
{
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = '3')
# Check if transforEmotion conda env is being used
if (!grepl("transforEmotion", reticulate::py_config()$python))
{
reticulate::use_condaenv("transforEmotion", required = TRUE)
}

# Install modules
message("\nInstalling modules for 'transforEmotion'...")
Expand All @@ -43,15 +37,9 @@ setup_modules <- function()
"transformers",
"pytube",
"pytz",
"pandas",
"opencv-python"
),
pip = TRUE
)
if (!check_python_libs())
{
stop("Not all Python libraries were installed correctly. Please try again.")
}
else {
message("All Python libraries were installed correctly.")
}
}
21 changes: 7 additions & 14 deletions R/video_scores.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,18 @@
#'
video_scores <- function(video, classes, nframes=100,
face_selection = "largest", cut = FALSE, start = 0, end = 60, uniform = FALSE, ffreq = 15, save_video = FALSE, save_frames = FALSE, save_dir = "temp/", video_name = "temp"){
################################################################
# TODO this piece of code needs to be a function, parametrized by the use of Python libraries: text, image, video
if (!conda_check()){
print("Creating and switching to transforEmotion virtual Python environment...")
Sys.sleep(1)
setup_miniconda()
} else
{
reticulate::use_condaenv("transforEmotion", required = FALSE)
if (!conda_check()){
stop("Python environment 'transforEmotion' is not available. Please run setup_miniconda() to install it.")

}
if (!check_python_libs()){
print("Some Python libraries are not available in the transforEmotion conda environment. We are going to set them up. We need them for facial recognition and emotion detection in text, images and video. This may take a while, please be patient.")
Sys.sleep(1)
setup_modules()
else {
reticulate::use_condaenv("transforEmotion", required = FALSE)
}

################################################################
reticulate::source_python(system.file("python", "image.py", package = "transforEmotion"))

reticulate::source_python(system.file("python", "video.py", package = "transforEmotion"))

if (!grepl("youtu", video)){
stop("You need to provide a YouTube video URL.")
}
Expand Down
8 changes: 3 additions & 5 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
msg <- styletext(styletext(paste("\ntransforEmotion (version ", packageVersion("transforEmotion"), ")\n", sep = ""), defaults = "underline"), defaults = "bold")
msg <- paste(msg,'\nFor help getting started, type browseVignettes("transforEmotion")\n')
msg <- paste(msg,"\nFor bugs and errors, submit an issue to <https://github.com/atomashevic/transforEmotion/issues>")
msg <- paste(msg,"\nBefore running any analysis, please run `transforEmotion::setup_miniconda()` to install the necessary Python modules.")
packageStartupMessage(msg)
Sys.unsetenv("RETICULATE_PYTHON")
tryCatch({
reticulate::use_condaenv("transforEmotion", required = TRUE)
}, error = function(e) {
message("Python transforEmotion conda environment not activate. \nPlease run `setup_miniconda()` to install and activate the the environment with all required libraries. \nIf this issue persists submit an issue to <https://github.com/atomashevic/transforEmotion/issues> \n ", e$message)
})
Sys.setenv(RETICULATE_PYTHON_ENV = "transforEmotion")
library(reticulate)
}
15 changes: 0 additions & 15 deletions man/check_python_libs.Rd

This file was deleted.

4 changes: 3 additions & 1 deletion temp.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ transforEmotion:::conda_check()

transforEmotion:::check_python_libs()

reticulate::import("pandas")
##### IMAGE TEST

url = "https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Donald_Trump_official_portrait.jpg/330px-Donald_Trump_official_portrait.jpg"
Expand All @@ -27,5 +28,6 @@ transforEmotion::image_scores(url, classes) # done

video = "https://www.youtube.com/watch?v=720O_yBLrTs&ab_channel=DonaldJTrump"

transforEmotion::video_scores(video, classes, nframes = 10)

Sys.setenv(RETICULATE_PYTHON_ENV = "transforEmotion")
library(reticulate)

0 comments on commit e75566e

Please sign in to comment.