Skip to content

Commit

Permalink
fix: use rlang to check and install suggested pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaburg1 committed Oct 11, 2024
1 parent 0a47388 commit 3227b0d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 66 deletions.
8 changes: 4 additions & 4 deletions R/NLP_operations.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#'
tokenize_text <- function(text) {

check_and_install_dependencies("text2vec")
rlang::check_installed("text2vec")

# Convert text to lowercase, remove special characters and extra spaces, and
# tokenize
Expand Down Expand Up @@ -50,7 +50,7 @@ generate_glove_model <- function(
overwrite = FALSE
) {

check_and_install_dependencies("parallel")
rlang::check_installed("parallel")

# Create a hash of the data and the model parameters
data_hash <- rlang::hash(list(
Expand Down Expand Up @@ -177,7 +177,7 @@ compute_text_sim <- function(x_text, y_texts, embedding_matrix) {
#
# if (method == "word2vec") {
#
# check_and_install_dependencies("word2vec")
# rlang::check_installed("word2vec")
#
# # Word2vec returns NA if a segment is a single word with a punctuation
# # mark at the end, don't know why
Expand All @@ -191,7 +191,7 @@ compute_text_sim <- function(x_text, y_texts, embedding_matrix) {
# word2vec::doc2vec(model, x_text)) |>
# as.vector()
# } else {
# check_and_install_dependencies("stringdist")
# rlang::check_installed("stringdist")
#
# y_probes$similarity <- stringdist::stringsim(
# x_text, y_probes$text, method = "cosine")
Expand Down
9 changes: 2 additions & 7 deletions R/speech_to_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ split_audio <- function(
) {

# Check if the av package is installed and ask to install it if not
check_and_install_dependencies("av")
rlang::check_installed("av")

# Calculate segment length in seconds
segment_length_sec <- segment_duration * 60
Expand Down Expand Up @@ -278,7 +278,7 @@ use_whisper_ctranslate2_stt <- function(
n_threads = NULL
) {

check_and_install_dependencies("parallel")
rlang::check_installed("parallel")

if (is.null(n_threads)) {
n_threads <- parallel::detectCores()
Expand Down Expand Up @@ -315,11 +315,6 @@ use_whisper_ctranslate2_stt <- function(

jsonlite::read_json(output_file_path)

# In console, synchronous version, no logging
# p <- processx::process$run(
# "whisper-ctranslate2",
# args = args,
# echo_cmd = T, stdout = "|")
}

#' Use Azure Whisper Model for Speech-to-Text
Expand Down
37 changes: 0 additions & 37 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,3 @@
#' Check and install dependencies
#'
#' Checks if a list of packages are installed and installs them if not. Trigger
#' an error if the user chooses not to install a package.
#'
#' @param deps A character vector of package names.
#'
#' @return Nothing.
#'
check_and_install_dependencies <- function(deps) {
for (dep in deps) {
stop_message <- paste0(dep, " is required but was not installed.")
# Check if the package is installed
is_installed <- requireNamespace(dep, quietly = TRUE)

if (!is_installed) {
# If not, ask the user if they want to install it
if (interactive()) {
# Only in interactive sessions, otherwise just stop
do_install <- utils::menu(
c("Yes", "No"),
title = paste0(dep, " is not installed. Install it now?")) == 1

if(do_install) {
try({
utils::install.packages(dep)
# After successful installation, recheck if the package is now installed
is_installed <- requireNamespace(dep, quietly = FALSE)
})
}
}
}

# Stop if the package is not installed
if (!is_installed) stop(stop_message)
}
}

#' Check if transcript segments are silent
#'
Expand Down
18 changes: 0 additions & 18 deletions man/check_and_install_dependencies.Rd

This file was deleted.

0 comments on commit 3227b0d

Please # to comment.