Skip to content

Commit

Permalink
docs: Enhance README and improve module import handling
Browse files Browse the repository at this point in the history
- Updated README.md to clarify GPU installation prompts and requirements, emphasizing that users can still access package functionalities without a GPU, albeit with reduced performance.
- Added detailed instructions for GPU support, including necessary hardware and software prerequisites.
- Improved error handling in R scripts (image_scores.R, video_scores.R, sentence_similarity.R, transformer_scores.R, rag.R) to attempt module imports and fallback to setup if modules are missing, enhancing user experience and robustness.
- Adjusted messages to provide clearer guidance during module setup and installation processes.

These changes aim to improve user understanding of GPU support and streamline the setup process for the transforEmotion package.
  • Loading branch information
atomashevic committed Jan 7, 2025
1 parent 709abef commit 605a626
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 51 deletions.
21 changes: 16 additions & 5 deletions R/image_scores.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,25 @@
#' @export

image_scores <- function(image, classes, face_selection = "largest", model = "oai-base") {
if (!conda_check()) {
stop("Python environment 'transforEmotion' is not available. Please run setup_miniconda() to install it.")
} else {

# Suppress TensorFlow messages
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = "2")

# Try to import required Python module
module_import <- try({
reticulate::use_condaenv("transforEmotion", required = FALSE)
image_module <- reticulate::source_python(system.file("python", "image.py", package = "transforEmotion"))
image_module
}, silent = TRUE)

# If import fails, try setting up modules
if(inherits(module_import, "try-error")) {
message("Required Python modules not found. Setting up modules...")
setup_modules()
reticulate::use_condaenv("transforEmotion", required = FALSE)
image_module <- reticulate::source_python(system.file("python", "image.py", package = "transforEmotion"))
}

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

# check if image has image file extension
if (!grepl("\\.(jpg|jpeg|png|bmp)$", image)) {
stop("Image file name must have an image file extension: jpg, jpeg, png, bmp")
Expand Down
41 changes: 24 additions & 17 deletions R/rag.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,27 +161,34 @@ rag <- function(
}else{device <- match.arg(device)}

# Run setup for modules
setup_modules()
# setup_modules()

# Check for llama_index in environment
if(!exists("llama_index", envir = as.environment(envir))){

# Import 'llama-index'
message("Importing llama-index module...")

# Determine if 'llama-index-legacy' exists
if("llama-index-legacy" %in% reticulate::py_list_packages()$package){

# {llama-index} >= 0.10.5
llama_index <- reticulate::import("llama_index.legacy")

}else{

# {llama-index} < 0.10.5
llama_index <- reticulate::import("llama_index")


# Try to import llama-index
llama_index <- try(
if("llama-index-legacy" %in% reticulate::py_list_packages()$package) {
# {llama-index} >= 0.10.5
reticulate::import("llama_index.legacy")
} else {
# {llama-index} < 0.10.5
reticulate::import("llama_index")
}, silent = TRUE
)

# If import fails, try setting up modules
if(inherits(llama_index, "try-error")) {
message("Required Python modules not found. Setting up modules...")
setup_modules()

# Try import again
llama_index <- if("llama-index-legacy" %in% reticulate::py_list_packages()$package) {
reticulate::import("llama_index.legacy")
} else {
reticulate::import("llama_index")
}
}

}

# Check for service context
Expand Down
20 changes: 13 additions & 7 deletions R/sentence_similarity.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,19 @@ sentence_similarity <- function(
if(exists(transformer, envir = as.environment(envir))){
classifier <- get(transformer, envir = as.environment(envir))
}else{

# Run setup for modules
setup_modules()

# Import 'sentence-transformers' module
message("Importing sentence-transformers module...")
sentence_transformers <- reticulate::import("sentence_transformers")

# Try to import sentence-transformers
sentence_transformers <- try(
reticulate::import("sentence_transformers"),
silent = TRUE
)

# If import fails, try setting up modules
if(inherits(sentence_transformers, "try-error")) {
message("Required Python modules not found. Setting up modules...")
setup_modules()
sentence_transformers <- reticulate::import("sentence_transformers")
}

# Check for custom transformer
if(transformer %in% c("all_minilm_l6")){
Expand Down
29 changes: 27 additions & 2 deletions R/setup_gpu_modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,32 @@ setup_gpu_modules <- function() {

# Check for Linux and add llama-cpp-python if applicable
if (system.check()$OS == "linux") {
modules <- c(modules, "llama-cpp-python")
# Set environment variables for llama-cpp-python build
Sys.setenv(
LDFLAGS = "-lstdc++fs",
CMAKE_ARGS = "-DLLAMA_CUBLAS=on" # Enable CUDA support
)

# Try installing pre-built wheel first, then fallback to source
tryCatch({
reticulate::conda_install(
envname = "transforEmotion",
packages = "llama-cpp-python",
pip_options = c("--upgrade", "--quiet", "--prefer-binary"),
pip = TRUE
)
}, error = function(e) {
message("Pre-built wheel not available, attempting to build from source...")
reticulate::conda_install(
envname = "transforEmotion",
packages = "llama-cpp-python",
pip_options = c("--upgrade", "--quiet"),
pip = TRUE
)
})

# Remove llama-cpp-python from main modules list since we handled it separately
modules <- modules[modules != "llama-cpp-python"]
}

# Determine whether any modules need to be installed
Expand All @@ -52,7 +77,7 @@ setup_gpu_modules <- function() {
# Only proceed if there are modules to install
if (length(missing_modules) > 0) {
# Set pip options for quiet installation
pip_options <- c("--upgrade", "--quiet")
pip_options <- c("--upgrade", "--quiet", "--prefer-binary")

# Install modules silently
suppressMessages(
Expand Down
4 changes: 2 additions & 2 deletions R/setup_modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ setup_modules <- function() {

if (has_gpu) {
# Prompt user for GPU installation
message("\nNVIDIA GPU detected. Do you want to install GPU modules? (yes/no)")
message("\nNVIDIA GPU detected. Do you want to install GPU modules? ([Y]es/[N]o)")
user_response <- tolower(readline())
use_gpu <- user_response %in% c("yes", "y")
use_gpu <- user_response %in% c("yes", "y", "Yes", "Y")
}

# Set necessary modules with their versions
Expand Down
26 changes: 18 additions & 8 deletions R/transformer_scores.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,24 @@ transformer_scores <- function(
if(exists(transformer, envir = as.environment(envir))){
classifier <- get(transformer, envir = as.environment(envir))
}else{

# Run setup for modules
setup_modules()

# Import 'transformers' and 'torch' module
message("Importing transformers and torch modules...")
transformers <- reticulate::import("transformers")
torch <- reticulate::import("torch")

# Try to import required modules
modules_import <- try({
transformers <- reticulate::import("transformers")
torch <- reticulate::import("torch")
list(transformers = transformers, torch = torch)
}, silent = TRUE)

# If import fails, try setting up modules
if(inherits(modules_import, "try-error")) {
message("Required Python modules not found. Setting up modules...")
setup_modules()
transformers <- reticulate::import("transformers")
torch <- reticulate::import("torch")
} else {
transformers <- modules_import$transformers
torch <- modules_import$torch
}

# Check for custom transformer
if(transformer %in% c(
Expand Down
27 changes: 18 additions & 9 deletions R/video_scores.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,27 @@ video_scores <- function(video, classes, nframes = 100, face_selection = "larges
start = 0, end = -1, uniform = FALSE, ffreq = 15,
save_video = FALSE, save_frames = FALSE, save_dir = "temp/",
video_name = "temp", model = "oai-base") {
if (!conda_check()){
stop("Python environment 'transforEmotion' is not available. Please run setup_miniconda() to install it.")

}
else {

# Suppress TensorFlow messages
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = "2")

# Try to import required Python modules
modules_import <- try({
reticulate::use_condaenv("transforEmotion", required = FALSE)
image_module <- reticulate::source_python(system.file("python", "image.py", package = "transforEmotion"))
video_module <- reticulate::source_python(system.file("python", "video.py", package = "transforEmotion"))
list(image = image_module, video = video_module)
}, silent = TRUE)

# If import fails, try setting up modules
if(inherits(modules_import, "try-error")) {
message("Required Python modules not found. Setting up modules...")
setup_modules()
reticulate::use_condaenv("transforEmotion", required = FALSE)
image_module <- reticulate::source_python(system.file("python", "image.py", package = "transforEmotion"))
video_module <- reticulate::source_python(system.file("python", "video.py", package = "transforEmotion"))
}

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

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

# check if classes is a character vector
if(!is.character(classes)){
stop("Classes must be a character vector.")
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ After loading package **for the first time**, you need to setup the Python virtu
setup_miniconda()
```

You will be prompted to install GPU libraries. If you have an NVIDIA GPU, select "[Y]es" to install GPU libraries. If you don't have an NVIDIA GPU, select "[N]o" to proceed with CPU-only installation.

You will gain access to all functionalities of the package even without GPU, but be aware that some functions will be significantly slower.

If you have doubts whether you should install GPU libraries, see [GPU Support](#gpu-support) section below.

> [!WARNING]
> If you using [radian](https://github.com/randy3k/radian) console in VSCode or in a terminal emulator, you won't be able to set up the transforEmotion package. Radian is written in Python and (in most cases) already runs in your default Python environment. This prevents transforEmotion package from setting up the new virtual environment and installing the correct versions of necessary Python packages. Switch to default R console and everything should work fine.
Expand Down Expand Up @@ -220,7 +226,20 @@ The `image_scores` and `video_scores` functions support different models. The av
- `eva-8B`: "BAAI/EVA-CLIP-8B-448" - A very large model that requires significant HDD space and RAM.
- `jina-v2`: "jinaai/jina-clip-v2" - Another large model with high accuracy but requires more resources.

> **Note:** The larger models like `eva-8B` and `jina-v2` may take a lot of HDD space and need a lot of RAM to run efficiently. Choose the model based on your requirements for speed, accuracy, and available system resources.
> **Note:** The larger models like `eva-8B` and `jina-v2` may take a lot of HDD space and need a lot of RAM to run efficiently. Choose the model based on your requirements for speed, accuracy, and available system resources. We recommend using 'oai-base' or 'oai-large' for most applications.
## GPU Support

When running the `setup_miniconda()` function, you will be prompted to install GPU libraries. If you select "[Y]es" when prompted to install GPU libraries, make sure you have:

1. An NVIDIA GPU (GTX 1060 or newer)
2. CUDA Toolkit 11.7+ installed
3. Updated NVIDIA drivers
4. GCC/G++ version 9 or newer (Linux only)

Without these requirements, the GPU installation will likely fail. If you're unsure, select "no" to proceed with CPU-only installation.

If GPU installation fails, you can try running the `setup_modules()` function and selection "[N]o" when prompted to install GPU libraries.

## References

Expand Down
1 change: 1 addition & 0 deletions setup_gpu_modules.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 605a626

Please sign in to comment.